-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathforms.py
More file actions
218 lines (197 loc) · 5.51 KB
/
Copy pathforms.py
File metadata and controls
218 lines (197 loc) · 5.51 KB
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
import app,models,utils
from google.appengine.api import users
class Form():
ok = False # form is valid or not
fields = {} # input data from html form
warn = [] # warning messages for every broken rule
data = {} # data returned to the caller {}
view = '' # template name used for the web form
url = '' # redirect url if form was processed
redirect = False # if form to be redirected
#---- FORM VALIDATION ----
def messages(request,fid,tid):
form = Form()
fields = request.getForm()
user = users.get_current_user()
uid = user.user_id()
# sanitize
author = utils.toStr(fields.get('author','Anonymous'),40)
email = fields.get('email')
url = fields.get('url')
content= utils.toTxt(fields.get('content',''),9000)
# validate
ok=True
warn=[]
if email or url or 'http' in content:
form.ok=False
form.redirect=True
form.url='%s/%s/%s'%(app.root,fid,tid)
return form
if not author:
warn.append('You need to identify yourself as the author')
ok=False
if author==author.upper():
warn.append('Author can not be all caps')
ok=False
if not content:
warn.append('You must enter some content')
ok=False
if content==content.upper():
warn.append('Content can not be all caps')
ok=False
# process
if ok:
form.ok = True
form.url = '%s/%s/%s'%(app.root,fid,tid)
data = {'topicid':tid,'forumid':fid,'userid':str(uid),'author':author,'content':content}
models.newMessage(data)
else:
form.ok = False
form.warn = warn
form.data = {
'forum' :models.getForum(fid),
'topic' :models.getTopic(tid),
'list' :models.getMessages(tid),
'warn' :warn,
'author' :author,
'content':content
}
return form
def newTopic(request,fid):
form = Form()
fields = request.getForm()
user = users.get_current_user()
uid = user.user_id()
# sanitize
title = utils.toStr(fields.get('title',''),80)
imp = utils.toInt(fields.get('importance','0'))
author = utils.toStr(fields.get('author',''),40)
email = fields.get('email')
url = fields.get('url')
content= utils.toTxt(fields.get('content',''),9000)
# validate
ok=True
warn=[]
if email or url:
form.ok=False
form.redirect=True
form.url='%s/%s'%(app.root,fid)
return form
if not title:
warn.append('Title can not be empty')
ok=False
if title==title.upper():
warn.append('Title can not be all caps')
ok=False
if not author:
warn.append('You need to identify as the author')
ok=False
if author==author.upper():
warn.append('Author can not be all caps')
ok=False
if not content:
warn.append('You must enter some content')
ok=False
if content==content.upper():
warn.append('Content can not be all caps')
ok=False
if ok:
dat1 = {'forumid':fid,'title':title,'userid':str(uid),'author':author,'importance':imp}
rec1 = models.newTopic(dat1)
tid = rec1.topicid
dat2 = {'topicid':tid,'forumid':fid,'userid':str(uid),'author':author,'content':content}
rec2 = models.newMessage(dat2)
form.ok = True
form.url = '%s/%s/%s'%(app.root,fid,tid)
else:
form.ok = False
form.warn = warn
form.data = {
'forum' :models.getForum(fid),
'warn' :warn,
'title' :title,
'author' :author,
'content':content
}
return form
def newForum(request):
form = Form()
fields = request.getForm()
# sanitize
title = utils.toStr(fields.get('title',''), 80)
desc = utils.toStr(fields.get('desc' ,''),120)
url = utils.idify(fields.get('url' ,''), 40)
order = utils.toInt(fields.get('order'))
# validate
ok=True
warn=[]
if not url:
warn.append('Forum needs a permalink')
ok=False
if not title:
warn.append('Title can not be empty')
ok=False
if title==title.upper():
warn.append('Title can not be all caps')
ok=False
if not desc:
warn.append('You must enter some description')
ok=False
if desc==desc.upper():
warn.append('Description can not be all caps')
ok=False
if ok:
if not order: order=models.getNextOrder()
data = {'url':url,'title':title,'description':desc,'order':order}
rec = models.newForum(data)
form.ok = True
form.url = '%s/admin/forum'%(app.root)
form.redirect = True
else:
form.ok = False
form.warn = warn
form.data = {
'forum' :models.getForum(fid),
'warn' :warn,
'title' :title,
'author' :author,
'content':content
}
return form
def myprofile(request):
form = Form()
fields = request.getForm()
user = users.get_current_user()
uid = user.user_id()
# sanitize
name = utils.toStr(fields.get('username',''),80)
nick = utils.toStr(fields.get('nickname',''),40)
email = utils.toStr(fields.get('email' ,''),80)
token = utils.toStr(fields.get('token' ,''),80)
userid= str(uid)
if not name: name = 'Anonymous'
# validate
ok=True
warn=[]
if userid != token :
warn.append('You need to login to change your profile')
ok=False
# TODO: check for duplicate nick
if not nick:
warn.append('Nickname can not be empty')
ok=False
if ok:
data = {'userid':uid,'nickname':nick,'username':name,'email':email}
rec = models.saveUser(data)
form.ok = True
form.url = '%s/myprofile'%(app.root)
form.redirect = True
else:
form.ok = False
form.warn = warn
form.data = {
'profile':models.getUser(uid),
'warn' :warn
}
return form
#---- END ----