-
Notifications
You must be signed in to change notification settings - Fork 0
/
trash.py
180 lines (168 loc) · 7.87 KB
/
trash.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
'''
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/.
'''
from google.appengine.ext import db
import json
class DeletedSketch(db.Model):
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)
lowerFileName = db.StringProperty(default="")
overwrite = False
def to_dict(self):
d = dict([(p, unicode(getattr(self, p))) for p in self.properties()])
d["id"] = self.key().id()
return d
class Trash(db.Model):
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)
lowerFileName = db.StringProperty(default="")
overwrite = False
def to_dict(self):
d = dict([(p, unicode(getattr(self, p))) for p in self.properties()])
d["id"] = self.key().id()
return d
@staticmethod
def move_to_trash(sketch):
entity = Trash(sketchId = sketch.sketchId,
version = sketch.version,
changeDescription = sketch.changeDescription,
fileName = sketch.fileName,
owner = sketch.owner,
fileData = sketch.fileData,
thumbnailData = sketch.thumbnailData,
original_sketch = sketch.original_sketch,
original_version = sketch.original_version,
created = sketch.created,
modified = sketch.modified,
appver = sketch.appver,
isLatest = sketch.isLatest,
lowerFileName = sketch.lowerFileName)
verify = entity.put()
if(verify):
sketch.delete()
return True
else:
return False
@staticmethod
def delete_permenantly(sketch):
entity = DeletedSketch(sketchId = sketch.sketchId,
version = sketch.version,
changeDescription = sketch.changeDescription,
fileName = sketch.fileName,
owner = sketch.owner,
fileData = sketch.fileData,
thumbnailData = sketch.thumbnailData,
original_sketch = sketch.original_sketch,
original_version = sketch.original_version,
created = sketch.created,
modified = sketch.modified,
appver = sketch.appver,
isLatest = sketch.isLatest,
lowerFileName = sketch.lowerFileName)
verify = entity.put()
if(verify):
sketch.delete()
return True
else:
return False
@staticmethod
def restore_sketch(sketch):
entity = Sketch(sketchId = sketch.sketchId,
version = sketch.version,
changeDescription = sketch.changeDescription,
fileName = sketch.fileName,
owner = sketch.owner,
fileData = sketch.fileData,
thumbnailData = sketch.thumbnailData,
original_sketch = sketch.original_sketch,
original_version = sketch.original_version,
created = sketch.created,
modified = sketch.modified,
appver = sketch.appver,
isLatest = sketch.isLatest,
lowerFileName = sketch.lowerFileName)
verify = entity.put()
if(verify):
sketch.delete()
return True
else:
return False
@staticmethod
def get_entities_by_id(data,userid=""):
utc = UTC()
jsonData = json.loads(data)
criteria = long(jsonData['id'])
sortby = jsonData['sort']
theQuery = Trash.all().filter('owner',long(criteria)).filter('isLatest',True).order(sortby)
objects = theQuery.fetch(limit=None)
entities = []
for object in objects:
user_name = User.get_name(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': object.fileName,
'appver': object.appver,
'p_view': 1,
'p_edit': True,
'p_comment': True,
'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':'view_trash_sketches',
'en_type': 'Trash',
'entities': entities}
return result
@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 = Trash.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
from rpx import UTC, User
from sketches import VersionCount, Sketch
from comments_likes import Comment, Like
from crypto import Crypto