-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
249 lines (167 loc) · 6.9 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from google.appengine.ext import ndb
from google.appengine.api import users
from google.appengine.ext import blobstore
from myuser import MyUser
from directory import Directory
from file import File
import logging
import re
# Get user from this page
def get_user():
return users.get_current_user()
# get user from data
def get_my_user():
user = get_user()
if user:
my_user_key = ndb.Key(MyUser, user.user_id())
return my_user_key.get()
def is_user_logged_in():
return True if get_user() else False
# returns true if for this user a myuser object already exists in the datastore
def user_exists():
return True if get_my_user() else False
def add_new_user(user):
my_user = MyUser(id=user.user_id())
add_root_directory(my_user)
# set current path on first login to root directory
my_user.current_directory = ndb.Key(Directory, my_user.key.id() + '/')
my_user.put()
def get_directories_in_current_path():
return get_current_directory_object().directories
def get_files_in_current_path():
return get_current_directory_object().files
def get_file_object(file_name):
my_user = get_my_user()
parent_directory_object = get_current_directory_object()
file_path = get_path(file_name, parent_directory_object)
file_id = my_user.key.id() + file_path
file_key = ndb.Key(File, file_id)
return file_key.get()
def get_path(name, parent_directory_object):
if is_in_root_directory():
return parent_directory_object.path + name
else:
return parent_directory_object.path + '/' + name
# returns true if current directory is root directory
def is_in_root_directory():
current_directory = get_current_directory_object()
return True if current_directory.parent_directory is None else False
def is_directory_empty(directory):
return not directory.files and not directory.directories
# returns key of current directory
def get_current_directory_key():
my_user = get_my_user()
return my_user.current_directory
# returns key of current directory
def get_current_directory_object():
return get_current_directory_key().get()
# returns key of parent directory
def get_parent_directory_key():
current_directory = get_current_directory_key()
return current_directory.get().parent_directory
def add_root_directory(my_user):
directory_id = my_user.key.id() + '/'
directory = Directory(id=directory_id)
directory.parent_directory = None
directory.name = 'root'
directory.path = '/'
directory.put()
my_user.root_directory = directory.key
my_user.put()
def add_directory(name, parent_directory_key):
my_user = get_my_user()
parent_directory_object = parent_directory_key.get()
path = get_path(name, parent_directory_object)
directory_id = my_user.key.id() + path
directory = Directory(id=directory_id)
# check if directory already exists in this path
if exists(directory.key, parent_directory_object.directories):
# Add key to parent directory
parent_directory_object.directories.append(directory.key)
parent_directory_object.put()
# Set all attributes of the directory and save it to datastore
directory.parent_directory = parent_directory_key
directory.name = name
directory.path = path
directory.put()
def add_file(upload, file_name):
my_user = get_my_user()
current_directory_object = get_current_directory_object()
file_id = my_user.key.id() + get_path(file_name, current_directory_object)
file_key = ndb.Key(File, file_id)
if exists(file_key, current_directory_object.files):
file_object = File(id=file_id)
file_object.name = file_name
file_object.blob = upload.key()
file_object.put()
current_directory_object.files.append(file_key)
current_directory_object.put()
else:
# Delete uploaded file from the blobstore
blobstore.delete(upload.key())
logging.debug("A file with this name already exists in this directory!")
def delete_directory(directory_name):
my_user = get_my_user()
# current directory is the parent directory of the one that will be deleted
parent_directory_object = get_current_directory_object()
directory_id = my_user.key.id() + get_path(directory_name, parent_directory_object)
directory_key = ndb.Key(Directory, directory_id)
directory_object = directory_key.get()
if is_directory_empty(directory_object):
# Delete reference to this object from parent_directory
parent_directory_object.directories.remove(directory_key)
parent_directory_object.put()
# Delete directory object from datastore
directory_key.delete()
def delete_file(file_name):
my_user = get_my_user()
parent_directory_object = get_current_directory_object()
file_path = get_path(file_name, parent_directory_object)
file_id = my_user.key.id() + file_path
file_key = ndb.Key(File, file_id)
# Delete file key from directory
parent_directory_object.files.remove(file_key)
parent_directory_object.put()
# Delete actual file from blobstore
blobstore.delete(file_key.get().blob)
# Delete file object
file_key.delete()
def navigate_up():
my_user = get_my_user()
if not is_in_root_directory():
parent_directory_key = get_parent_directory_key()
my_user.current_directory = parent_directory_key
my_user.put()
def navigate_home():
my_user = get_my_user()
my_user.current_directory = ndb.Key(Directory, my_user.key.id() + '/')
my_user.put()
def navigate_to_directory(directory_name):
my_user = get_my_user()
parent_directory_object = get_current_directory_object()
directory_id = my_user.key.id() + get_path(directory_name, parent_directory_object)
directory_key = ndb.Key(Directory, directory_id)
my_user.current_directory = directory_key
my_user.put()
# checks if a key is in a list of keys, if so returns true
def exists(key, key_list):
return key not in key_list
# Remove all '/' and ';' from the directory name and leading whitespaces
def prepare_directory_name(directory_name):
return re.sub(r'[/;]', '', directory_name).lstrip()
# returns a given list in alphabetical order, sorted by attribute name
# only works with a keyproperty-list
# The keys in this list have to be from an object that has a 'name' property of type strinproperty
# In this case: used to sort keylist of files and directories
def sort_list(list_input):
return sorted(list_input, key=lambda element: element.get().name.lower())
# extracts all the names from a list of directory/ file keys
def get_names_from_list(elements):
names = list()
for element in elements:
names.append(element.get().name)
return names
def get_login_url(main_page):
return users.create_login_url(main_page.request.uri)
def get_logout_url(main_page):
return users.create_logout_url(main_page.request.uri)