forked from livid/v2ex-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.py
271 lines (255 loc) · 10.6 KB
/
notes.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
#!/usr/bin/env python
# coding=utf-8
import os
import re
import time
import datetime
import hashlib
import string
import random
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from v2ex.babel import Member
from v2ex.babel import Counter
from v2ex.babel import Section
from v2ex.babel import Node
from v2ex.babel import Topic
from v2ex.babel import Reply
from v2ex.babel import Note
from v2ex.babel import Site
from v2ex.babel import SYSTEM_VERSION
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
from v2ex.babel.l10n import *
from v2ex.babel.ext.cookies import Cookies
template.register_template_library('v2ex.templatetags.filters')
class NotesHomeHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
template_values['page_title'] = site.title + u' › 记事本'
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
q = db.GqlQuery("SELECT * FROM Note WHERE member = :1 ORDER BY last_modified DESC", member)
try:
notes_count = q.count()
except:
q = db.GqlQuery("SELECT * FROM Note WHERE member = :1 ORDER BY created DESC", member)
notes_count = q.count()
if (notes_count > 0):
template_values['notes'] = q
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'notes_home.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'notes_home.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
class NotesNewHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
template_values['page_title'] = site.title + u' › 新建记事'
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'notes_new.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
def post(self):
site = GetSite()
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
template_values['page_title'] = site.title + u' › 新建记事'
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
# Verification: content
note_content = self.request.get('content').strip()
note_content_length = len(note_content)
if note_content_length > 0:
note = Note()
q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'note.max')
if (q.count() == 1):
counter = q[0]
counter.value = counter.value + 1
else:
counter = Counter()
counter.name = 'note.max'
counter.value = 1
q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'note.total')
if (q2.count() == 1):
counter2 = q2[0]
counter2.value = counter2.value + 1
else:
counter2 = Counter()
counter2.name = 'note.total'
counter2.value = 1
note.num = counter.value
note.title = note_content.split("\n")[0][0:60].strip()
note.content = note_content
note.body = "\n".join(note_content.split("\n")[1:]).strip()
note.length = len(note_content)
note.member_num = member.num
note.member = member
note.put()
counter.put()
counter2.put()
self.redirect('/notes/' + str(note.num))
else:
template_values['note_content'] = note_content
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'notes_new.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
class NotesItemHandler(webapp.RequestHandler):
def get(self, num):
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
q = db.GqlQuery("SELECT * FROM Note WHERE num = :1", int(num))
if q.count() > 0:
note = q[0]
if note.member.num == member.num:
template_values['member'] = member
template_values['note'] = note
template_values['page_title'] = site.title + u' › 记事本 › ' + note.title
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'notes_item.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'notes_item.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/')
else:
self.redirect('/')
else:
self.redirect('/signin')
class NotesItemEraseHandler(webapp.RequestHandler):
def get(self, num):
site = GetSite()
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
template_values['page_title'] = site.title + u' › 记事本'
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
q = db.GqlQuery("SELECT * FROM Note WHERE num = :1", int(num))
if q.count() > 0:
note = q[0]
if note.member.num == member.num:
note.delete()
self.redirect('/notes')
else:
self.redirect('/notes')
else:
self.redirect('/notes')
else:
self.redirect('/signin')
class NotesItemEditHandler(webapp.RequestHandler):
def get(self, num):
site = GetSite()
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
template_values['page_title'] = site.title + u' › 记事本 › 编辑'
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
q = db.GqlQuery("SELECT * FROM Note WHERE num = :1", int(num))
if q.count() > 0:
note = q[0]
if note.member.num == member.num:
template_values['member'] = member
template_values['note'] = note
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'notes_edit.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/notes')
else:
self.redirect('/notes')
else:
self.redirect('/signin')
def post(self, num):
site = GetSite()
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
template_values['page_title'] = site.title + u' › 记事本'
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
# Verification: content
note_content = self.request.get('content').strip()
note_content_length = len(note_content)
if note_content_length > 0:
q = db.GqlQuery("SELECT * FROM Note WHERE num = :1", int(num))
if q.count() > 0:
note = q[0]
template_values['page_title'] = site.title + u' › 记事本 › 编辑'
if note.member.num == member.num:
note.title = note_content.split("\n")[0][0:60].strip()
note.content = note_content
note.body = "\n".join(note_content.split("\n")[1:]).strip()
note.length = len(note_content)
note.edits = note.edits + 1
note.put()
memcache.set('Note_' + str(note.num), note, 86400)
self.redirect('/notes/' + str(note.num))
else:
self.redirect('/notes')
else:
self.redirect('/notes')
else:
template_values['note_content'] = note_content
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'notes_new.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
def main():
application = webapp.WSGIApplication([
('/notes', NotesHomeHandler),
('/notes/new', NotesNewHandler),
('/notes/([0-9]+)', NotesItemHandler),
('/notes/([0-9]+)/erase', NotesItemEraseHandler),
('/notes/([0-9]+)/edit', NotesItemEditHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()