-
Notifications
You must be signed in to change notification settings - Fork 0
/
confirm.py
94 lines (75 loc) · 3.35 KB
/
confirm.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime, os
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import util, template
from google.appengine.api import mail
from google.appengine.api.labs import taskqueue
from post import Post
import helper, config
class ConfirmationHandler(webapp.RequestHandler):
def handle_error(self, message):
self.redirect('/new?error_message=%s' % message)
def send_confirmation_request(self, post):
try:
message = mail.EmailMessage(sender="do-not-reply@neperczagtu.appspotmail.com",
to=self.request.get('email'),
subject='Apstiprināt zagtu velosipēdu!')
directory = os.path.dirname(__file__)
template_file = os.path.join(directory, 'views', 'confirmation.html')
message.html = template.render(template_file, {'post': post}, True)
message.check_initialized()
except mail.InvalidEmailError:
self.handle_error('Invalid email recipient.')
return
except mail.MissingRecipientsError:
self.handle_error('You must provide a recipient.')
return
except mail.MissingBodyError:
self.handle_error('You must provide a mail format.')
return
message.send()
def send_information(self, post):
try:
message = mail.EmailMessage(sender="do-not-reply@neperczagtu.appspotmail.com",
to=post.email,
subject='Ieraksts apstiprināts!')
directory = os.path.dirname(__file__)
template_file = os.path.join(directory, 'views', 'information.html')
message.html = template.render(template_file, {'post': post}, True)
message.check_initialized()
except mail.InvalidEmailError:
self.handle_error('Invalid email recipient.')
return
except mail.MissingRecipientsError:
self.handle_error('You must provide a recipient.')
return
except mail.MissingBodyError:
self.handle_error('You must provide a mail format.')
return
message.send()
def get(self):
try:
post = db.get(self.request.get('p'))
except:
self.redirect('/')
return
s = self.request.get('s')
if s:
if s == post.secret:
if not post.confirmed_at:
post.confirmed_at = datetime.datetime.today()
post.put()
# send info on how to edit post
self.send_information(post)
# update twitter
taskqueue.add(url='/post/twitter', params={'key': post.key()})
else:
logging.warning('Somebody tried to confirm post (key=%s) with incorrect secret (s=%s)', (post.key, s))
return self.redirect('/')
else:
self.send_confirmation_request(post)
self.response.out.write(helper.render('confirm', { 'email': self.request.get('email') }))
application = webapp.WSGIApplication([('/confirm', ConfirmationHandler)], debug=config.DEBUG)
if __name__ == '__main__':
util.run_wsgi_app(application)