/
category.py
308 lines (261 loc) · 12.3 KB
/
category.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
import jinja2
import os
import urllib
import webapp2
import rankdata
import item
import util
from google.appengine.ext import db
from google.appengine.api import users
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
add_page_path = 'category/add'
add_action_path = 'category/addaction'
edit_page_path = 'category/edit'
edit_action_path = 'category/editaction'
edit_category_path = 'category/editcategory'
edit_category_action_path = 'category/editcategoryaction'
delete_page_path = 'category/delete'
delete_action_path = 'category/deleteaction'
item_page_path = 'item/addeditdelete'
def get_category(author, name):
"""Returns category of the specified author, and name
:param author: the specified category author
:param name: the specified category name
:returns: category of the specified author and name
"""
category_key = db.Key.from_path('Category', '{author}/{category}'.format(author=author, category=name))
return db.get(category_key)
def get_category_by_key(key):
"""Returns category of specified key
:param key the specified key
:returns: category of the specified key
"""
category_key = db.Key.from_path('Category', key)
return db.get(category_key)
def get_categories_by_name(keyword):
"""Returns categories of specified keyword
Retrive all categories that its name equals keyword
:param keyword the specified keyword
:returns: categories of specified keyword
"""
category_query = rankdata.Category.all().filter('name = ', keyword)
return category_query.fetch(limit=None)
def get_categories(author=None, order='-create_time', item_number=0):
"""Returns categories of specified author, order and the number of
item in category is equals to or greater than the specified item
number
:param author the specified author. The default value is None,
if so, the all categories would be returned
:param order the specified order. The default value is ordering by
create time field in descending
:param item_number the specified item number. The default value is 0
:returns: categories of specified author, order and the number of
item in category is euqlas to or greater than the specified
item number
"""
category_query = rankdata.Category.all().filter("author = ", author).order(order) if author else rankdata.Category.all().order(order)
return [ category for category in category_query.run() if item.get_items(author=category.author, category_name=category.name, count_or_not=True) >= item_number ] if item_number else category_query.run()
def update_category(author, new_name, old_name):
"""Update category of specified author, old name, and new name
When update the category name, all items belonging to as well as
its all comments would be reserved automatically.
:param author the specified author
:param new_name the specified new category name
:param old_name the specified old category name
"""
new_category = rankdata.Category(key_name='{author}/{name}'.format(author=author, name=new_name), author=author, name=new_name)
new_category.put()
item.reserve_all_items(author=author, new_category_name=new_name, old_category_name=old_name)
old_category = get_category(author=author, name=old_name)
db.delete(old_category)
def delete_categories(author, names=[]):
"""Delete categories of specified author and name
When delete a category, all items belonging to as well as its all
comments would be delete automatically.
:param author the specified author
:param names the specified name list
"""
for name in names:
key = db.Key.from_path('Category', '{author}/{category}'.format(author=author, category=name))
item.delete_all_items(author, db.get(key).name)
db.delete(key)
def delete_all_categories(author):
"""Delete all categories of specified author
When delete a category, all items belonging to as well a its all
comments would be delete automatically
:param author the specified author
"""
category_query = rankdata.Category.all().filter("author = ", author)
for category in category_query.run():
item.delete_all_items(author, category.name)
db.delete(category)
class AddCategoryPage(webapp2.RequestHandler):
"""Construct add category HTML page
"""
def get(self):
"""Handle user request
The add category HTML page would list all categories that the
user already have.
"""
invalid_name = self.request.get('invalid_name')
user = users.get_current_user()
categories = get_categories(author=user)
url = users.create_logout_url(self.request.uri)
template_values = {
'categories': categories,
'url': url,
'user': user,
'invalid_name': invalid_name,
}
template = jinja_environment.get_template('{path}.html'.format(path=add_page_path))
self.response.out.write(template.render(template_values))
class AddCategoryAction(webapp2.RequestHandler):
"""Handle add category form submition
"""
def post(self):
"""Handle user request
The category name could not be empty, comtains '/' character as
well as already exist.
"""
name = self.request.get('category_name').strip()
if name == '':
self.redirect('/{path}?'.format(path=add_page_path) +
urllib.urlencode({'invalid_name': 'empty name'}))
return
elif '/' in name:
self.redirect('/{path}?'.format(path=add_page_path) +
urllib.urlencode({'invalid_name': name}))
return
user = users.get_current_user()
if get_category(author=user, name=name):
self.redirect('/{path}?'.format(path=add_page_path) +
urllib.urlencode({'invalid_name': name}))
else:
category = rankdata.Category(key_name='{author}/{name}'.format(author=user, name=name), author=user, name=name)
category.put()
self.redirect('/{path}?'.format(path=item_page_path) +
urllib.urlencode({'category_name': name, 'method': 'Add'}))
class EditCategoryPage(webapp2.RequestHandler):
"""Construct edit category HTML page
"""
def get(self):
"""Handle user request
The edit category HTML page would list all categories that user
already have.
"""
invalid_select = self.request.get('select_category')
user = users.get_current_user()
categories = get_categories(author=user)
url = users.create_logout_url(self.request.uri)
template_values = {
'categories': categories,
'url': url,
'user': user,
'invalid_select': invalid_select,
}
template = jinja_environment.get_template('{path}.html'.format(path=edit_page_path))
self.response.out.write(template.render(template_values))
class EditCategoryAction(webapp2.RequestHandler):
"""Handle edit category form submition and construct edit category
name HTML page
"""
def get(self):
"""Handler user request
The edit category name HTML page would list all items belonging to
the specified category.
"""
category_name = self.request.get('category_name')
if not category_name:
self.redirect('/{path}?'.format(path=edit_page_path) +
urllib.urlencode({'select_category': 'Nothing'}))
return
invalid_name = self.request.get('invalid_name')
user = users.get_current_user()
url = users.create_logout_url(self.request.uri)
items = item.get_items(author=user, category_name=category_name)
template_values = {
'category_name': category_name,
'items': items,
'url': url,
'user': user,
'invalid_name': invalid_name,
}
template = jinja_environment.get_template('{path}.html'.format(path=edit_category_path))
self.response.out.write(template.render(template_values))
class EditCategoryNameAction(webapp2.RequestHandler):
"""Handle edit category name form submition
"""
def post(self):
"""Handler user request
The new category name would not be empty, contains '/' character as
well as already exist. If the user does not change the category name,
it will do nothing. Otherwise, all items belonging to as well as its
all comments would be reserved automatically.
"""
old_category_name = self.request.get('category_name').strip()
new_category_name = self.request.get('new_category_name').strip()
if not cmp(old_category_name, new_category_name):
self.redirect('/{path}?'.format(path=edit_action_path) +
urllib.urlencode({'category_name': old_category_name}))
return
if new_category_name == '':
self.redirect('/{path}?'.format(path=edit_action_path) +
urllib.urlencode({'invalid_name': 'empty name', 'category_name': old_category_name}))
return
elif '/' in new_category_name:
self.redirect('/{path}?'.format(path=edit_action_path) +
urllib.urlencode({'invalid_name': new_category_name, 'category_name': old_category_name}))
return
user = users.get_current_user()
if get_category(author=user, name=new_category_name):
self.redirect('/{path}?'.format(path=edit_action_path) +
urllib.urlencode({'invalid_name': new_category_name, 'category_name': old_category_name}))
else:
update_category(author=user, new_name=new_category_name, old_name=old_category_name)
self.redirect('/{path}?'.format(path=edit_action_path) +
urllib.urlencode({'category_name': new_category_name}))
class DeleteCategoryPage(webapp2.RequestHandler):
"""Construct delete category HTML page
"""
def get(self):
"""Handle user request
The delete category HTML page would list all categories that user
already have as well as categories that user just deleted
"""
delete_name = self.request.get('delete_name')
delete_names = util.parse_string(delete_name) if delete_name else None
user = users.get_current_user()
categories = get_categories(author=user)
url = users.create_logout_url(self.request.uri)
template_value = {
'categories': categories,
'url': url,
'user': user,
'delete_names': delete_names,
}
template = jinja_environment.get_template('{path}.html'.format(path=delete_page_path))
self.response.out.write(template.render(template_value))
class DeleteCategoryAction(webapp2.RequestHandler):
"""Handle delete category form submition
"""
def post(self):
"""Handle user request
"""
command = self.request.get('delete')
user = users.get_current_user()
url = users.create_logout_url(self.request.uri)
if 'Delete' in command:
category_names = self.request.get_all('category_name')
delete_categories(user, category_names)
elif 'Clear' in command:
category_names = [entity.name for entity in get_categories(author=user)]
delete_all_categories(user)
self.redirect('/{path}?'.format(path=delete_page_path) + urllib.urlencode({'delete_name': [name for name in category_names]}))
app = webapp2.WSGIApplication([('/{path}'.format(path=add_page_path), AddCategoryPage),
('/{path}'.format(path=add_action_path), AddCategoryAction),
('/{path}'.format(path=edit_page_path), EditCategoryPage),
('/{path}'.format(path=edit_action_path), EditCategoryAction),
('/{path}'.format(path=edit_category_action_path), EditCategoryNameAction),
('/{path}'.format(path=delete_page_path), DeleteCategoryPage),
('/{path}'.format(path=delete_action_path), DeleteCategoryAction)],
debug=True)