-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketches.py
executable file
·1549 lines (1378 loc) · 59.8 KB
/
sketches.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Copyright 2015 Singapore Management University
This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was
not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
'''
"""Backend Module
@created: Goh Kian Wei (Brandon), Kevin Koh, Shannon Lim, Tony Tran, Samantha Wee, Wong Si Hui
@code_adapted_from: Chris Boesch, Daniel Tsou
"""
"""
Note to self: json.loads = json string to objects. json.dumps is object to json string.
"""
import datetime
import urllib
import os
import urllib
import urllib2
import logging
import webapp2
from google.appengine.api import memcache
from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from urlparse import parse_qsl
import json
from google.appengine.ext import deferred
"""
Attributes and functions for Sketch entity
"""
#Handles Sketch data, and saving and loading of sketches
class Sketch(db.Model):
#Use backend record id as the model id for simplicity
sketchId = db.IntegerProperty(required=True) #Sketch ID of Sketch. For common identification for multiple versions of the same sketch.
version = db.IntegerProperty(required=True) #Version of Sketch.
changeDescription = db.StringProperty() #Description of Sketch
fileName = db.StringProperty(required=True) #Sketch name
owner = db.IntegerProperty(required=True) #User who created the Sketch
fileData = db.TextProperty(required=True) #Content data of Sketch (XML string)
thumbnailData = db.TextProperty() #Encoded thumbnail data of Sketch
original_sketch = db.IntegerProperty(required=True) #SketchID of Sketch that this Sketch was created from (if applicable)
original_version = db.IntegerProperty(required=True) #Version of Sketch that this Sketch was created from (if applicable)
created = db.DateTimeProperty(auto_now_add=True) #The time that the model was created
modified = db.DateTimeProperty(auto_now=True)
appver = db.FloatProperty()
isLatest = db.BooleanProperty(default=True)
overwrite = False
lowerFileName = db.StringProperty(default="")
def to_dict(self):
d = dict([(p, unicode(getattr(self, p))) for p in self.properties()])
d["id"] = self.key().id()
return d
#Creates a new Sketch entity from solving discrepancy
@staticmethod
def addDiscrepancy(data, userid):
result = {}
jsonData = json.loads(data)
v_count = VersionCount.get_counter(long(jsonData['sketchId']))
try:
jsonData = json.loads(data)
theobject = None
versionCount = VersionCount.get_and_increment_counter(long(jsonData['sketchId']))
jsonData['version'] = str(versionCount)
versionCount_decrement = versionCount - 1
change = jsonData['changeDescription']
change = change[:255]
Sketch.update_latest_flag(jsonData['sketchId'])
entity = Sketch(sketchId=long(jsonData['sketchId']),
version=long(jsonData['version']),
changeDescription=change,
fileName=jsonData['fileName'],
owner=long(jsonData['owner_id']),
fileData=jsonData['fileData'],
thumbnailData=jsonData['thumbnailData'],
original_sketch=long(jsonData['originalSketch']),
original_version=long(versionCount_decrement),
appver=float(jsonData['appver']),
lowerFileName=jsonData['fileName'].lower())
verify = entity.put()
if (verify):
permissions_key = Permissions.add(long(jsonData['sketchId']),
bool(long(jsonData['p_view'])),
bool(jsonData['p_edit']),
bool(jsonData['p_comment']))
#Update group permissions when adding
group_count = 0
#try:
if jsonData['group_permissions']:
group_permissions = jsonData['group_permissions']
group_count = Sketch_Groups.add(long(jsonData['sketchId']),
group_permissions)
if (permissions_key != -1):
result = {'id': entity.key().id(),
'status': "success",
'method': "add",
'en_type': "Sketch",
'data': jsonData} #this would also check if the json submitted was valid
else:
#Rollback
rollback = Sketch.get_by_id(entity.key.id())
if rollback:
rollback.delete()
result = {'status': "error",
'message': "Save unsuccessful. Please try again."}
else:
result = {'status': "error",
'message': "Save unsuccessful. Please try again."}
except:
result = {'status': "error",
'message': "Save unsuccessful. Please try again."}
return result
#Creates a new Sketch entity
@staticmethod
def add(data, userid):
result = {}
#try:
#update ModelCount when adding
jsonData = json.loads(data)
if jsonData['fileName'].strip() != "":
#Check if sketch is original
check_original = False
#Check if sketch is a new version
check_new_version = False
#Check if sketch was made from latest version if not original
check_if_latest = True
#Check if the sketch is a new sketch
check_new_sketch=False
ModelCount.increment_counter('Sketch_count')
#For new sketches/sketches saved as new sketches
if jsonData['sketchId'] == "" or jsonData['sketchId'] == -1 or jsonData['sketchId'] == None:
#Assigns new SketchId
handmade_key = db.Key.from_path('Sketch', 1)
#Get new ID from DB
new_ids = db.allocate_ids(handmade_key, 1)
check_new_sketch=True
modelCount = new_ids[0]
ModelCount.increment_counter('Sketch')
logging.info("Model count : " + str(modelCount))
jsonData['sketchId'] = str(modelCount)
#Placeholder for current version of SketchId
versionCount = 0
modelCount=int(jsonData['sketchId'])
#If sketch is completely original (i.e. new sketch)
if jsonData['originalSketch'] == -1:
check_original = True
#Creates new version counter for the new Sketch.
versionCount = VersionCount.get_and_increment_counter(long(jsonData['sketchId']))
jsonData['originalVersion'] = versionCount
jsonData['originalSketch'] = str(modelCount)
#If sketch is an updated version of existing sketch (i.e. edit sketch)
elif jsonData['originalSketch'] == jsonData['sketchId']:
v_count = VersionCount.get_counter(long(jsonData['sketchId']))
versionCount = v_count.lastVersion
check_new_version = True
#Updated sketch was created from latest version (ALLOWED)
if long(jsonData['originalVersion']) == versionCount:
#Incrementing counter
versionCount = VersionCount.get_and_increment_counter(long(jsonData['sketchId']))
#Updated sketch was NOT created from latest version (HALT!)
else:
#If overwrite status is true, force old sketch to overwrite the new one
check_if_latest = False
thelatestobject = Sketch.all().filter('sketchId =', long(jsonData['sketchId'])).filter('version =', v_count.lastVersion).get()
thelatestobject_created = thelatestobject.created
Sketch.update_latest_flag(jsonData['sketchId'])
#If sketch is a new sketch created from an existing sketch (i.e. save as sketch)
else:
#Creates new version counter for the new Sketch.
versionCount = VersionCount.get_and_increment_counter(long(jsonData['sketchId']))
#If sketch was an updated sketch created from latest version, a sketch saved as a new sketch, or completely original.
if check_if_latest:
allow_permissions = False
if not check_new_version:
allow_permissions = True
elif long(userid) == long(jsonData['owner_id']):
allow_permissions = True
elif User.check_if_admin(long(userid)):
allow_permissions = True
#update AppVersionCount when adding
AppVersionCount.increment_counter(float(jsonData['appver']), check_original)
jsonData['version'] = str(versionCount)
file = jsonData['fileData']
thumbnail = jsonData['thumbnailData']
change = jsonData['changeDescription']
change = change[:255]
if(check_new_sketch):
new_key = db.Key.from_path('Sketch', modelCount)
entity = Sketch(key=new_key,
sketchId=long(jsonData['sketchId']),
version=long(jsonData['version']),
changeDescription=change,
fileName=jsonData['fileName'],
owner=long(jsonData['owner_id']),
fileData=file,
thumbnailData=thumbnail,
original_sketch=long(jsonData['originalSketch']),
original_version=long(jsonData['originalVersion']),
appver=float(jsonData['appver']),
lowerFileName=jsonData['fileName'].lower())
else:
entity = Sketch(sketchId=long(jsonData['sketchId']),
version=long(jsonData['version']),
changeDescription=change,
fileName=jsonData['fileName'],
owner=long(jsonData['owner_id']),
fileData=file,
thumbnailData=thumbnail,
original_sketch=long(jsonData['originalSketch']),
original_version=long(jsonData['originalVersion']),
appver=float(jsonData['appver']),
lowerFileName=jsonData['fileName'].lower())
verify = entity.put()
if (allow_permissions):
if (verify):
permissions_key = Permissions.add(long(jsonData['sketchId']),
bool(long(jsonData['p_view'])),
bool(jsonData['p_edit']),
bool(jsonData['p_comment']))
#Update group permissions when adding
group_count = 0
#try:
if "group_permissions" in jsonData and jsonData['group_permissions']:
group_permissions = jsonData['group_permissions']
group_count = Sketch_Groups.add(long(jsonData['sketchId']),
group_permissions)
#except:
# group_count = 0
if (permissions_key != -1):
if 'uniqueId' in jsonData:
result = {'id': entity.key().id(),
'status': "success",
'method': "add",
'en_type': "Sketch",
'sketchId':long(jsonData['sketchId']),
'uniqueId':jsonData['uniqueId'],
'data': jsonData}
else:
result = {'id': entity.key().id(),
'status': "success",
'method': "add",
'en_type': "Sketch",
'data': jsonData} #this would also check if the json submitted was valid
else:
#Rollback
rollback = Sketch.get_by_id(entity.key.id())
if rollback:
rollback.delete()
result = {'status': "error",
'message': "Save unsuccessful. Please try again."}
else:
result = {'status': "error",
'message': "Save unsuccessful. Please try again."}
else:
if 'uniqueId' in jsonData:
result = {'id': entity.key().id(),
'status': "success",
'method': "add",
'en_type': "Sketch",
'sketchId':long(jsonData['sketchId']),
'uniqueId':jsonData['uniqueId'],
'data': jsonData}
else:
result = {'id': entity.key().id(),
'status': "success",
'method': "add",
'en_type': "Sketch",
'data': jsonData} #If sketch was NOT created from latest version
else:
result = {'status': "errorDiscrepancy",
'message': "This sketch was recently updated to Version " + str(versionCount) + " at " + thelatestobject_created.strftime("%Y-%m-%d %H:%M") + ". You are trying to save from an older version.",
'submessage': " Would you like to overwrite the latest version?",
'version': long(versionCount - 1)}
else:
result = {'status': "error",
'message': "Save unsuccessful. Please try again."}
#except:
# result = {'status': "error",
# 'message': "Save unsuccessful. Please try again."}
return result
#Gets Sketch entities by search criteria
@staticmethod
def get_entities(data, userid=""):
utc = UTC()
#update ModelCount when adding
total_count = ModelCount.get_counter("Sketch_count")
jsonData = json.loads(data)
criteria = jsonData['criteria'].strip()
show = jsonData['show']
limit = long(jsonData['limit'])
offset = long(jsonData['offset'])
possible_names = []
possible_users = []
objects = []
if criteria:
possible_names = Sketch.get_matching_names(criteria)
possible_users = User.get_matching_ids(criteria)
sketch_id_users = set()
sketch_id_query = Sketch.all().filter('owner IN',possible_users[:29]).fetch(limit=None)
if sketch_id_query:
for obj in sketch_id_query:
sketch_id_users.add(obj.sketchId)
union = sketch_id_users | set(possible_names)
in_clause = list(union)
count = 0
remaining = len(in_clause)
#The IN filter can have maximum of 30 entries
MAX_COUNT_IN_CLAUSE = 30
while remaining > 0:
if remaining < MAX_COUNT_IN_CLAUSE:
objects = objects+Sketch.all().filter('sketchId IN', in_clause[count:count+remaining]).order('-modified').fetch(limit=None)
remaining = 0
count += remaining
else:
next = count + MAX_COUNT_IN_CLAUSE
objects = objects+Sketch.all().filter('sketchId IN', in_clause[count:next]).order('-modified').fetch(limit=None)
remaining -= MAX_COUNT_IN_CLAUSE
count += MAX_COUNT_IN_CLAUSE
entities = []
count = 0
next_offset = 0
if objects:
for object in objects[offset:]:
#Latest Version Filter
latest_check = True
if show == "latest":
versionCount = VersionCount.get_counter(long(object.sketchId))
if object.version < versionCount.lastVersion:
latest_check = False
#Check Permissions
permissions = Permissions.user_access_control(object.sketchId,userid)
user_name = User.get_name(object.owner)
p_view = 0
if latest_check and bool(permissions['p_view']):
p_view = 1
data = {'sketchId': object.sketchId,
'version': object.version,
'changeDescription': object.changeDescription,
'fileName': object.fileName,
'thumbnailData': object.thumbnailData,
'owner': user_name,
'owner_id': object.owner,
'originalSketch': object.original_sketch,
'originalVersion': object.original_version,
'originalName': Sketch.get_sketch_name(object.original_sketch,object.original_version),
'appver': object.appver,
'p_view': p_view,
'p_edit': bool(permissions['p_edit']),
'p_comment': bool(permissions['p_comment']),
'like': Like.get_entities_by_id(object.sketchId, 0)['count'],
'comment': Comment.get_entities_by_id(object.sketchId)['count']}
entity = {'id': object.key().id(),
'created': object.created.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': object.modified.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': data}
entities.append(entity)
count += 1
#if count >= limit:
# next = objects.index(object) + 1
# if next < len(objects):
# next_offset = objects.index(object) + 1
# break
result = {'method':'get_entities',
'en_type': 'Sketch',
'criteria': criteria,
'total_count': total_count,
'retrieve_type': show,
'retrieved_count': count,
'offset': offset,
'limit': limit,
'next_offset': next_offset,
'entities': entities}
return result
#Gets Sketch entities by User ID
@staticmethod
def get_entities_by_id(data,userid=""):
utc = UTC()
#update ModelCount when adding
#if model:
#theQuery = theQuery.filter('model', model)
jsonData = json.loads(data)
criteria = long(jsonData['id'])
show = jsonData['show']
limit = long(jsonData['limit'])
offset = long(jsonData['offset'])
sortby = jsonData['sort']
theQuery = Sketch.all().filter('owner',long(criteria)).filter('isLatest',True).order(sortby)
count = theQuery.count(limit=None)
objects = theQuery.fetch(limit=limit, offset=offset)
entities = []
next_offset = offset + limit
for object in objects:
#Check Permissions
permissions = Permissions.user_access_control(object.sketchId,userid)
user_name = User.get_name(object.owner)
p_view = 0
p_edit = False
p_comment = False
if bool(permissions['p_view']):
p_view = 1
p_edit = True
p_comment = True
data = {'sketchId': object.sketchId,
'version': object.version,
'changeDescription': object.changeDescription,
'fileName': object.fileName,
'thumbnailData': object.thumbnailData,
'owner': user_name,
'owner_id': object.owner,
'originalSketch': object.original_sketch,
'originalVersion': object.original_version,
'originalName': Sketch.get_sketch_name(object.original_sketch,object.original_version),
'appver': object.appver,
'p_view': p_view,
'p_edit': p_edit,
'p_comment': p_comment,
'like': Like.get_entities_by_id(object.sketchId, 0)['count'],
'comment': Comment.get_entities_by_id(object.sketchId)['count']}
entity = {'id': object.key().id(),
'created': object.created.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': object.modified.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': data}
entities.append(entity)
result = {'method':'get_entities_by_id',
'en_type': 'Sketch',
'count': count,
'offset': offset,
'limit': limit,
'next_offset': next_offset,
'entities': entities}
return result
#Test method by Ram for loading without thumbnails
@staticmethod
def get_entities_lite(criteria=""):
utc = UTC()
#update ModelCount when adding
param = dict(parse_qsl(criteria))
theResults = Sketch.all().filter('owner',long(param.get('user'))).order('-created').fetch(limit=None)
show = "latest"
entities = []
userid = long(param.get('user'))
for object in theResults:
#if userid == object.owner:
test = "works!"
#Latest Version Filter
latest_check = True
if show == "latest":
versionCount = VersionCount.get_counter(long(object.sketchId))
if object.version < versionCount.lastVersion:
latest_check = False
#Check Permissions
permissions = Permissions.user_access_control(object.sketchId,userid)
if bool(permissions['p_view']) and latest_check:
test = "works too!"
user_name = User.get_name(Crypto.decrypt(object.owner))
data = {'sketchId': object.sketchId,
'version': object.version,
'changeDescription': object.changeDescription,
'fileName': object.fileName,
'thumbnailData': object.thumbnailData,
'owner': user_name,
'owner_id': object.owner,
'originalSketch': object.original_sketch,
'originalVersion': object.original_version,
'originalName': Sketch.get_sketch_name(object.original_sketch,object.original_version),
'appver': object.appver,
'p_view': 1,
'p_edit': bool(permissions['p_edit']),
'p_comment': bool(permissions['p_comment']),
'like': Like.get_entities_by_id(object.sketchId, 0)['count'],
'comment': Comment.get_entities_by_id(object.sketchId)['count']}
entity = {'id': object.key().id(),
'created': object.created.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': object.modified.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': data}
if userid == object.owner:
entities.append(entity)
elif User.check_if_admin(userid):
entities.append(entity)
elif data['p_view'] == "Public":
entities.append(entity)
data = []
offset = int(param.get('offset'))
count = int(param.get('count'))
if len(entities) > offset:
if len(entities) > offset+count:
data = entities[offset:offset+count]
else:
data = entities[offset:]
modelCount = ModelCount.all().filter('en_type','Sketch').get()
if modelCount:
count = modelCount.count
result = {'count': len(data),
'entities': data}
return result
@staticmethod
def get_thumbnail(criteria=""):
#update ModelCount when adding
theResults = Sketch.all().filter('sketchId',long(criteria)).fetch(limit=None)
data=""
if theResults:
data = theResults[0].thumbnailData
return data
#Test method by Cam NEW
@staticmethod
def get_entities_by_criteria(criteria=""):
utc = UTC()
#update ModelCount when adding
theResults = Sketch.all().filter('owner',long(criteria)).order('lowerFileName').fetch(limit=None)
show = "latest"
entities = []
userid = long(criteria)
test = "hello " + criteria
for object in theResults:
#if userid == object.owner:
test = "works!"
#Latest Version Filter
latest_check = True
if show == "latest":
versionCount = VersionCount.get_counter(long(object.sketchId))
if object.version < versionCount.lastVersion:
latest_check = False
#Check Permissions
permissions = Permissions.user_access_control(object.sketchId,userid)
if bool(permissions['p_view']) and latest_check:
test = "works too!"
user_name = User.get_name(Crypto.decrypt(object.owner))
data = {'sketchId': object.sketchId,
'version': object.version,
'changeDescription': object.changeDescription,
'fileName': object.fileName,
'thumbnailData': object.thumbnailData,
'owner': Crypto.decrypt(user_name),
'owner_id': object.owner,
'originalSketch': object.original_sketch,
'originalVersion': object.original_version,
'originalName': Sketch.get_sketch_name(object.original_sketch,object.original_version),
'appver': object.appver,
'p_view': 1,
'p_edit': bool(permissions['p_edit']),
'p_comment': bool(permissions['p_comment']),
'like': Like.get_entities_by_id(object.sketchId, 0)['count'],
'comment': Comment.get_entities_by_id(object.sketchId)['count'],
'lowerFileName': object.lowerFileName}
entity = {'id': object.key().id(),
'created': object.created.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': object.modified.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': data}
if userid == object.owner:
entities.append(entity)
elif User.check_if_admin(userid):
entities.append(entity)
elif data['p_view'] == "Public":
entities.append(entity)
count = 0
modelCount = ModelCount.all().filter('en_type','Sketch').get()
if modelCount:
count = modelCount.count
result = {'method': test, #'get_entities_by_criteria_new',
'en_type': 'Sketch',
'count': count,
'entities': entities}
return result
#Gets a specific Sketch by SketchID and version
@staticmethod
def get_entity_by_versioning(data, purpose="View", userid=""):
utc = UTC()
versionmatch = True
latestversion = True
result = {'method':'get_entity_by_versioning',
'success':"no",
'id': 0,
'created': datetime.datetime.now().replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': datetime.datetime.now().replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': ""
}
try:
jsonData = json.loads(data)
sketchId = jsonData['id']
version = jsonData['version']
theobject = None
versionCount = VersionCount.get_counter(long(sketchId))
#Get latest version
if versionCount and long(version) == -1:
theobject = Sketch.all().filter('sketchId =', long(sketchId)).filter('version =', versionCount.lastVersion).get()
#Get specific version
elif long(version) != -1:
theobject = Sketch.all().filter('sketchId =', long(sketchId)).filter('version =', long(version)).get()
if theobject:
if long(version) != versionCount.lastVersion:
latestversion = False
else:
theobject = Sketch.all().filter('sketchId =', long(sketchId)).filter('version =', versionCount.lastVersion).get()
versionmatch = False
if theobject:
#Check Permissions
permissions = Permissions.user_access_control(theobject.sketchId,userid)
#Check access type (view/edit):
access = False
if purpose == "Edit":
access = bool(permissions['p_edit'])
else:
access = bool(permissions['p_view'])
if access:
user_name = User.get_name(theobject.owner)
data = {'sketchId': theobject.sketchId,
'version': theobject.version,
'changeDescription': theobject.changeDescription,
'fileName': theobject.fileName,
'owner': user_name,
'owner_id': theobject.owner,
'fileData': theobject.fileData,
'thumbnailData': theobject.thumbnailData,
'originalSketch': theobject.original_sketch,
'originalVersion': theobject.original_version,
'originalName': Sketch.get_sketch_name(theobject.original_sketch,theobject.original_version),
'appver': theobject.appver,
'p_view': 1,
'p_edit': bool(permissions['p_edit']),
'p_comment': bool(permissions['p_comment']),
'p_public': Permissions.check_permissions(theobject.sketchId),
'groups': Sketch_Groups.get_groups_for_sketch(theobject.sketchId),
'ismatching': versionmatch,
'islatest': latestversion}
result = {'method':'get_entity_by_versioning',
'en_type': 'Sketch',
'status':'success',
'id': theobject.key().id(),
'created': theobject.created.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': theobject.modified.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': data
}
else:
result = {'method':'get_entity_by_versioning',
'status':"Forbidden"}
else:
result = {'method':'get_entity_by_versioning',
'status':"Error"}
except (RuntimeError, ValueError):
result['data'] = ""
return result
#Gets a specific Sketch by SketchID and version
@staticmethod
def get_entity_by_versioning_mobile(sketchId="", version="", purpose="View", userid=""):
utc = UTC()
versionmatch = True
latestversion = True
result = {'method':'get_entity_by_versioning_mobile',
'success':"no",
'id': 0,
'created': datetime.datetime.now().replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': datetime.datetime.now().replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': ""
}
try:
query = Sketch.all()
query.filter('sketchId =', long(sketchId))
data = query.get()
#jsonData = json.loads(data)
#sketchId = jsonData['id']
#version = data.version #jsonData['version']
theobject = None
versionCount = VersionCount.get_counter(long(sketchId))
#Get latest version
if versionCount and long(version) == -1:
theobject = Sketch.all().filter('sketchId =', long(sketchId)).filter('version =', versionCount.lastVersion).get()
#Get specific version
elif long(version) != -1:
theobject = Sketch.all().filter('sketchId =', long(sketchId)).filter('version =', long(version)).get()
if theobject:
if long(version) != versionCount.lastVersion:
latestversion = False
else:
theobject = Sketch.all().filter('sketchId =', long(sketchId)).filter('version =', versionCount.lastVersion).get()
versionmatch = False
if theobject:
#Check Permissions
permissions = Permissions.user_access_control(theobject.sketchId,userid)
#Check access type (view/edit):
access = False
if purpose == "Edit":
access = bool(permissions['p_edit'])
else:
access = bool(permissions['p_view'])
if access:
user_name = User.get_name(theobject.owner)
data = {'sketchId': theobject.sketchId,
'version': theobject.version,
'changeDescription': theobject.changeDescription,
'fileName': theobject.fileName,
'owner': user_name,
'owner_id': theobject.owner,
'fileData': theobject.fileData,
'thumbnailData': theobject.thumbnailData,
'originalSketch': theobject.original_sketch,
'originalVersion': theobject.original_version,
'originalName': Sketch.get_sketch_name(theobject.original_sketch,theobject.original_version),
'appver': theobject.appver,
'p_view': 1,
'p_edit': bool(permissions['p_edit']),
'p_comment': bool(permissions['p_comment']),
'p_public': Permissions.check_permissions(theobject.sketchId),
'groups': Sketch_Groups.get_groups_for_sketch(theobject.sketchId),
'ismatching': versionmatch,
'islatest': latestversion}
result = {'method':'get_entity_by_versioning_mobile',
'en_type': 'Sketch',
'status':'success',
'id': theobject.key().id(),
'created': theobject.created.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': theobject.modified.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': data
}
else:
result = {'method':'get_entity_by_versioning_mobile',
'status':"Forbidden"}
else:
result = {'method':'get_entity_by_versioning_mobile',
'status':"Error"}
except (RuntimeError, ValueError):
result['data'] = ""
return result
#Gets Sketch entities by Group
@staticmethod
def get_entity_by_group(data,userid=""):
utc = UTC()
result = {}
jsonData = json.loads(data)
groupId = long(jsonData['id'])
limit = long(jsonData['limit'])
offset = long(jsonData['offset'])
group_sketches = Sketch_Groups.get_sketches_for_group(long(groupId))
entities = []
count = 0
next_offset = 0
for g_s in group_sketches[offset:]:
sketch_id = long(g_s['sketch_id'])
permissions = Permissions.user_access_control(sketch_id, userid)
if permissions['p_view']:
versionCount = VersionCount.get_counter(sketch_id)
theobject = Sketch.all().filter('sketchId',sketch_id).filter('version',long(versionCount.lastVersion)).get()
user_name = User.get_name(theobject.owner)
data = {'sketchId': theobject.sketchId,
'version': theobject.version,
'changeDescription': theobject.changeDescription,
'fileName': theobject.fileName,
'owner': user_name,
'owner_id': theobject.owner,
'thumbnailData': theobject.thumbnailData,
'originalSketch': theobject.original_sketch,
'originalVersion': theobject.original_version,
'originalName': Sketch.get_sketch_name(theobject.original_sketch,theobject.original_version),
'appver': theobject.appver,
'p_view': 1,
'p_edit': bool(permissions['p_edit']),
'p_comment': bool(permissions['p_comment']),
'g_edit': bool(g_s['edit']),
'g_comment': bool(g_s['comment']),
'like': Like.get_entities_by_id(theobject.sketchId, 0)['count'],
'comment': Comment.get_entities_by_id(theobject.sketchId)['count']}
entity = {'id': theobject.key().id(),
'created': theobject.created.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'modified': theobject.modified.replace(tzinfo=utc).strftime("%d %b %Y %H:%M:%S"),
'data': data}
entities.append(entity)
count += 1
if count >= limit:
next = group_sketches.index(g_s) + 1
if next < len(group_sketches):
next_offset = group_sketches.index(g_s) + 1
break
result = {'method':'get_entity_by_group',
'en_type': "Sketch",
'count': count,
'limit': limit,
'offset': offset,
'next_offset': next_offset,
'entities': entities}
return result
#Checks if a Sketch entity belongs to a particular User
@staticmethod
def check_if_owner(id = 0, user_id = 0):
is_owner = False
try:
if long(user_id) != 0:
versionCount = VersionCount.get_counter(id)
object = Sketch.all().filter('sketchId',id).filter('version',long(versionCount.lastVersion)).get()
if object:
if object.owner == long(user_id):
is_owner = True
except ValueError:
is_owner = False
return is_owner
#Gets the name of a Sketch entity
@staticmethod
def get_sketch_name(sketchId=-1,version=-1):
try:
object = Sketch.all().filter('sketchId =', long(sketchId)).filter('version =', long(version)).get()
if object:
return object.fileName
else:
return "N/A"
except:
return "N/A"
#Gets the names of Sketches that match the given criteria
@staticmethod
def get_matching_names(criteria=""):
theQuery = Sketch.all().filter("isLatest" , True)
objects = theQuery.run()
criteria_lowercase = criteria.lower()
entities = [x.sketchId for x in objects if criteria_lowercase in x.lowerFileName]
return entities
#Counts the number of Sketch entities that a particular User may #View/Edit/Comment
@staticmethod
def count_permitted(user_id=0, permission_type="View"):
query = Sketch.all()
objects = query.run()
count = 0
for object in objects:
permissions = Permissions.user_access_control(object.key().id(),user_id)
if permission_type == "View":
if bool(permissions['p_view']):
count = count + 1
if permission_type == "Edit":
if bool(permissions['p_edit']):
count = count + 1
if permission_type == "Comment":
if bool(permissions['p_comment']):
count = count + 1
return count
#Deletes a specific Sketch entity.
#You can't name it delete since db.Model already has a delete method
@staticmethod
def remove(sketch_id, version, user_id):
#Only the sketch owner or an admin may delete the sketch
can_delete = False
is_admin = User.check_if_admin(user_id)
is_owner = Sketch.check_if_owner(long(sketch_id), user_id)
if is_owner:
can_delete = True
elif is_admin:
can_delete = True
if can_delete:
entity = Sketch.all().filter('sketchId',sketch_id).filter('version',version).get()
if entity:
appver = entity.appver
fileName = entity.fileName
owner = entity.owner
check_original = False
if entity.sketchId == entity.original_sketch:
if entity.version == entity.original_version:
check_original = True
entity.delete()
ModelCount.decrement_counter('Sketch_count')
AppVersionCount.decrement_counter(appver, check_original)
Comment.delete_by_sketch(long(sketch_id))
Permissions.delete_by_sketch(long(sketch_id))
Sketch_Groups.delete_by_sketch(long(sketch_id))
Like.delete_by_sketch(long(sketch_id))
if is_admin and not is_owner:
Notification.add(owner, "ADMINDELETE", 0, fileName, 0)
result = {'method':'remove',
'id': sketch_id,
'status': 'success'
}
else:
result = {'method':'remove',
'id': sketch_id,
'status': 'error'}
else:
result = {'method':'remove',
'id': sketch_id,
'status': 'error'}
return result
#Edits a specific Sketch entity. Currently not used.
@staticmethod
def edit_entity(sketch_model_id, data):
jsonData = json.loads(data)
entity = Sketch.get_by_id(long(sketch_model_id))
if jsonData['sketchId']!='':
entity.sketchId=long(jsonData['sketchId'])
if jsonData['version']!='':
entity.version=long(jsonData['version'])
if jsonData['changeDescription']!='':
entity.changeDescription=jsonData['changeDescription']
if jsonData['fileName']!='':
entity.fileName=jsonData['fileName']