forked from laander/kobstadenteaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·118 lines (96 loc) · 4.04 KB
/
main.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
import webapp2
from google.appengine.ext import ndb
from webapp2_extras import jinja2, json
import hashlib
import random
from google.appengine.api import mail
class EmailSignup(ndb.Model):
email = ndb.StringProperty()
confirmed = ndb.BooleanProperty(default=False)
date_added = ndb.DateTimeProperty(auto_now=True)
class ConfirmationKey(ndb.Model):
email = ndb.KeyProperty(kind=EmailSignup)
date_added = ndb.DateTimeProperty(auto_now=True)
class BaseHandler(webapp2.RequestHandler):
def render_response(self, filename, **kwargs):
kwargs.update({
#'current_user': self.user,
'current_url': self.request.url,
})
#kwargs.update(self.auth_config)
#if self.messages:
# kwargs['messages'] = self.messages
self.response.write(self.jinja2.render_template(filename, **kwargs))
@webapp2.cached_property
def jinja2(self):
# Returns a Jinja2 renderer cached in the app registry.
return jinja2.get_jinja2(app=self.app)
class IndexHandler(BaseHandler):
def get(self):
self.render_response("index.html", **{})
class CollectEmail(BaseHandler):
def _send_mail(self, email, key):
sender_address = "Kobstaden.dk <[email protected]>"
subject = "Tak for din interesse!"
body = self.jinja2.render_template("mail.txt", **{"key": key, "email": email})
body_html = self.jinja2.render_template("mail.html", **{"key": key, "email": email})
message = mail.EmailMessage(sender=sender_address, to=email, subject=subject, html=body_html, body=body)
message.send()
def post(self):
email = self.request.POST.get("email")
email = email.lower()
data = {
"email": email,
}
if mail.is_email_valid(email):
check_mail = EmailSignup.get_by_id(email)
if not check_mail:
signup = EmailSignup(id=email, email=email)
signup.put()
key = hashlib.sha224(str(random.getrandbits(256))).hexdigest()
conf = ConfirmationKey(id=key, email=signup.key)
conf.put()
self._send_mail(email, key)
data["message"] = "Success"
else:
data["message"] = "Email already signed up"
else:
data["message"] = "Invalid email"
self.response.write(json.encode(data))
class ConfirmEmail(webapp2.RequestHandler):
def get(self, key):
conf = ConfirmationKey.get_by_id(key)
if key:
signup = conf.email.get()
signup.confirmed = True
signup.put()
conf.delete()
self.redirect('/thankyou/')
self.redirect('/')
class RemoveEmail(webapp2.RequestHandler):
def get(self, email):
mail = EmailSignup.get_by_id(email)
mail.key.delete()
self.redirect('/removed/')
class GetMailList(BaseHandler):
def get(self):
mails = EmailSignup.query()
context = {
"emails": mails
}
self.render_response("mail_list.html", **context)
config = {}
config['webapp2_extras.jinja2'] = {
'environment_args': {'autoescape': True, 'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_', 'jinja2.ext.i18n']}
}
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler="main.IndexHandler", name='home'),
webapp2.Route(r'/signup/', handler="main.CollectEmail", name='signup'),
webapp2.Route(r'/thankyou/', handler="main.IndexHandler", name='signup'),
webapp2.Route(r'/removed/', handler="main.IndexHandler", name='signup'),
webapp2.Route(r'/confirm/<key>/', handler="main.ConfirmEmail", name='signup'),
webapp2.Route(r'/delete/<email>/', handler="main.RemoveEmail", name='signup'),
], debug=False, config=config)
app_admin = webapp2.WSGIApplication([
webapp2.Route(r'/admin/get_mails/', handler="main.GetMailList", name='home')
], debug=False, config=config)