forked from aaronsw/watchdog
/
petition.py
329 lines (277 loc) · 12.8 KB
/
petition.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
from __future__ import with_statement
import web
from utils import forms, helpers, auth
from settings import db, render
import config
urls = (
'', 'redir',
'/', 'index',
'/new', 'new',
'/checkID', 'checkID',
'/share', 'share',
'/(.*)', 'petition'
)
render_plain = web.template.render('templates/') #without base, useful for sending mails
class redir:
def GET(self): raise web.seeother('/')
class checkID:
def POST(self):
"Return True if petition with id `pid` does not exist"
pid = web.input().pid
exists = bool(db.select('petition', where='id=$pid', vars=locals()))
return pid != 'new' and not(exists)
class index:
def GET(self):
petitions = db.select(['petition', 'signatory'],
what='petition.id, petition.title, count(signatory.user_id) as signature_count',
where='petition.id = signatory.petition_id',
group='petition.id, petition.title',
order='count(signatory.user_id) desc'
)
return render.petition_list(petitions)
def save_petition(p):
p.pid = p.pid.replace(' ', '_')
with db.transaction():
try:
owner = db.select('users', where='email=$p.email', vars=locals())[0]
except:
owner_id = db.insert('users', email=p.email, verified=True)
else:
if not owner.verified: db.update('users', where='email=$p.email', verified=True, vars=locals())
owner_id = owner.id
db.insert('petition', seqname=False, id=p.pid, title=p.ptitle, description=p.pdescription,
owner_id=owner_id)
#make the owner of the petition sign for it (??)
db.insert('signatory', seqname=False, user_id=owner_id, share_with='E', petition_id=p.pid)
def fill_user_details(form, fillings):
details = {}
if 'email' in fillings:
email = helpers.get_loggedin_email() or helpers.get_unverified_email()
if email: details['email'] = email
if email and 'name' in fillings:
name = db.select('users', what='name', where='email=$email', vars=locals())[0].name
if name: details['name'] = name
form.fill(**details)
if helpers.get_loggedin_email():
for i in form.inputs:
if i.name in details.keys():
i.attrs['readonly'] = 'true'
class new:
def GET(self):
pform = forms.petitionform()
fill_user_details(pform, 'email')
return render.petitionform(pform)
def POST(self):
p = web.input()
pform = forms.petitionform()
auth.assert_verified(p.email)
if pform.validates(p):
save_petition(p)
helpers.set_login_cookie(p.email)
msg = """Congratulations, you've created your petition.
Now sign and share it with all your friends."""
helpers.set_msg(msg)
return web.seeother('/%s' % p.pid)
else:
return render.petitionform(pform)
def askforpasswd(user_id):
useremail = helpers.get_loggedin_email()
#if the current user is the owner of the petition and has not set the password
r = db.select('users', where='id=$user_id AND email=$useremail AND password is NULL', vars=locals())
return bool(r)
def save_password(forminput):
password = auth.encrypt_password(forminput.password)
db.update('users', where='id=$forminput.user_id', password=password, vars=locals())
helpers.set_msg('Password stored')
def save_signature(forminput, pid):
try:
user = db.select('users', where='email=$forminput.email', vars=locals())[0]
except:
user_id = db.insert('users', name=forminput.name, email=forminput.email)
else:
user_id = user.id
if user.name != forminput.name:
db.update('users', where='id=$user_id', name=forminput.name, vars=locals())
user = web.storage(id=user_id, name=forminput.name, email=forminput.email)
signed = db.select('signatory', where='petition_id=$pid AND user_id=$user.id', vars=locals())
if not signed:
signature = dict(petition_id=pid, user_id=user_id,
share_with=forminput.share_with, comment=forminput.comment)
db.insert('signatory', seqname=False, **signature)
helpers.set_msg('Your signature has been taken for this petition.')
helpers.unverified_login(user.email)
return user
def sendmail_to_signatory(user, pid):
p = db.select('petition', where='id=$pid', vars=locals())[0]
p.url = 'http//watchdog.net/c/%s' % (pid)
token = auth.get_secret_token(user.email)
msg = render_plain.signatory_mailer(user, p, token)
#@@@ shouldn't this web.utf8 stuff taken care by in web.py?
web.sendmail(web.utf8(config.from_address), web.utf8(user.email), web.utf8(msg.subject.strip()), web.utf8(msg))
def is_author(email, pid):
if not email: return False
try:
user_id = db.select('users', where='email=$email', what='id', vars=locals())[0].id
owner_id = db.select('petition', where='id=$pid', what='owner_id', vars=locals())[0].owner_id
except:
return False
else:
return user_id == owner_id
class petition:
def GET(self, pid, signform=None, passwordform=None):
i = web.input()
options = ['signatories', 'unsign', 'edit', 'delete']
if i.get('m', None) in options:
handler = getattr(self, 'GET_'+i.m)
return handler(pid)
try:
p = db.select('petition', where='id=$pid', vars=locals())[0]
except:
raise web.notfound
p.signatory_count = db.query('select count(*) from signatory where petition_id=$pid',
vars=locals())[0].count
if not signform:
signform = forms.signform()
fill_user_details(signform, ['name', 'email'])
if askforpasswd(p.owner_id) and not passwordform: passwordform = forms.passwordform()
msg, msg_type = helpers.get_delete_msg()
return render.petition(p, signform, passwordform, msg)
def GET_edit(self, pid):
user_email = helpers.get_loggedin_email()
if is_author(user_email, pid):
p = db.select('petition', where='id=$pid', vars=locals())[0]
pform = forms.petitionform()
pform.fill(email=user_email, pid=p.id, ptitle=p.title, pdescription=p.description)
for i in pform.inputs:
if i.id in ['pid', 'email']: i.attrs['readonly'] = 'true'
title = "Edit petition"
return render.petitionform(pform, title, target='/c/%s?m=edit' % (pid))
else:
login_link = '<a href="/login">Login</a>'
helpers.set_msg('Only author of this petition can edit it. %s if you are.' % login_link, msg_type='error')
raise web.seeother('/%s' % pid)
def GET_unsign(self, pid):
i = web.input()
user = helpers.get_user_by_email(i.email)
if user:
signatory = db.select('signatory', where='petition_id=$pid and user_id=$user.id', vars=locals())
if not (user and signatory and auth.check_secret_token(i.email, i.token)):
msg = "Invalid token or there is no signature for this petition with this email."
msg_type = 'error'
else:
msg = render_plain.confirm_unsign(pid, user.id)
msg_type = ''
helpers.set_msg(msg, msg_type)
raise web.seeother('/%s' % pid)
def GET_signatories(self, pid):
user_email = helpers.get_loggedin_email()
ptitle = db.select('petition', what='title', where='id=$pid', vars=locals())[0].title
signs = db.select(['signatory', 'users'],
what='users.name, users.email, '
'signatory.share_with, signatory.comment',
where='petition_id=$pid AND user_id=users.id',
order='signtime desc',
vars=locals()).list()
return render.signature_list(pid, ptitle, signs, is_author(user_email, pid))
def GET_delete(self, pid):
user_email = helpers.get_loggedin_email()
if is_author(user_email, pid):
msg = render_plain.confirm_deletion(pid)
helpers.set_msg(msg)
else:
login_link = '<a href="/login">Login</a>'
helpers.set_msg('Only author of this petition can delete it. %s if you are.' % login_link, msg_type='error')
raise web.seeother('/%s' % pid)
def POST(self, pid):
i = web.input('m', _method='GET')
options = ['sign', 'unsign', 'edit', 'password', 'delete']
if i.m in options:
handler = getattr(self, 'POST_'+i.m)
return handler(pid)
else:
raise ValueError
def POST_password(self, pid):
form = forms.passwordform()
i = web.input()
if form.validates(i):
save_password(i)
raise web.seeother('/%s' % pid)
else:
return self.GET(pid, passwordform=form)
def POST_sign(self, pid):
form = forms.signform()
i = web.input()
if form.validates(i):
user = save_signature(i, pid)
sendmail_to_signatory(user, pid)
return web.seeother('/%s' % pid)
else:
return self.GET(pid, signform=form)
def POST_edit(self, pid):
i = web.input()
db.update('petition', where='id=$pid', title=i.ptitle, description=i.pdescription, vars=locals())
raise web.seeother('/%s' % pid)
def POST_unsign(self, pid):
i = web.input()
db.delete('signatory', where='petition_id=$pid and user_id=$i.user_id', vars=locals())
msg = 'Your signature has been removed for this petition.'
helpers.set_msg(msg)
raise web.seeother('/%s' % pid)
def POST_delete(self, pid):
with db.transaction():
title = db.select('petition', what='title', where='id=$pid', vars=locals())[0].title
db.delete('signatory', where='petition_id=$pid', vars=locals())
db.delete('petition', where='id=$pid', vars=locals())
helpers.set_msg('Petition "%s" deleted' % (title))
raise web.seeother('/')
def get_contacts(user_id):
contacts = db.select('contacts',
what='cname as name, cemail as email, provider',
where='user_id=$user_id',
vars=locals()).list()
#remove repeated emails due to multiple providers; prefer the one which has name
cdict = {}
for c in contacts:
if c.email not in cdict.keys():
cdict[c.email] = c
elif c.name:
cdict[c.email] = c
contacts = cdict.values()
for c in contacts:
c.name = c.name or c.email.split('@')[0]
contacts.sort(key=lambda x: x.name.lower())
return contacts
class share:
def GET(self, emailform=None, loadcontactsform=None):
i = web.input()
user_id = helpers.get_loggedin_userid()
contacts = get_contacts(user_id)
petition = db.select('petition', where='id=$i.pid', vars=locals())[0]
petition.url = 'http://watchdog.net/c/%s' %(i.pid)
if not emailform:
emailform = forms.emailform
subject='Share petition "%s"' % (petition.title)
msg = render_plain.share_petition_mail(petition)
emailform.fill(subject=subject, body=msg)
current_providers = set(c.provider.lower() for c in contacts)
all_providers = set(['google', 'yahoo'])
remaining_providers = all_providers.difference(current_providers)
remaining_providers = ' or '.join(p.title() for p in remaining_providers)
if remaining_providers and not loadcontactsform:
loadcontactsform = forms.loadcontactsform
return render.share_petition(petition, emailform,
contacts, remaining_providers, loadcontactsform)
def POST(self):
i = web.input()
emailform = forms.emailform()
if emailform.validates(i):
pid, msg, subject = i.pid, i.body, i.subject
emails = [e.strip() for e in i.emails.strip(', ').split(',')]
web.sendmail(config.from_address, emails, subject, msg)
helpers.set_msg('Thanks for sharing this petition with your friends!')
raise web.seeother('/%s' % (pid))
else:
return self.GET(emailform=emailform)
app = web.application(urls, globals())
if __name__ == '__main__':
app.run()