-
Notifications
You must be signed in to change notification settings - Fork 4
/
news.py
90 lines (73 loc) · 2.88 KB
/
news.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
import webapp2
from google.appengine.api import mail
from google.appengine.ext import ndb
from google.appengine.api import users
import uuid
import datetime
from google.appengine.api import modules
sender_address = "[email protected]"
class Subscription(ndb.Model):
confirm_id = ndb.StringProperty()
create_date = ndb.DateTimeProperty(auto_now=True)
confirm_date = ndb.DateTimeProperty()
confirmed = ndb.BooleanProperty()
@classmethod
def get_by_confirm_id(cls, confirm_id):
return cls.query(Subscription.confirm_id == confirm_id).get()
@classmethod
def query_confirmed(cls):
return cls.query()#Subscription.confirmed == True)
class SubscribeHandler(webapp2.RequestHandler):
def get(self):
email_address = self.request.get("email_address")
if not mail.is_email_valid(email_address):
self.response.write('invalid email address')
self.response.status = 400
return
confirm_id = str(uuid.uuid4().get_hex().upper()[0:6])
confirmation_url = 'http://'+modules.get_hostname()+'/news/confirm?confirm_id='+confirm_id
subject = "Confirm your subscription"
body = """Thank you for subscribing to splunk-sizing.appspot.com!
Please confirm your email address by clicking on the link below:
%s
""" % confirmation_url
subscription = Subscription.get_or_insert(email_address)
if subscription.confirmed:
self.response.write('already confirmed')
self.response.status = 400
return
subscription.confirm_date = None
subscription.confirm_id = confirm_id
subscription.put()
mail.send_mail(sender_address, email_address, subject, body)
class ConfirmHandler(webapp2.RequestHandler):
def get(self):
confirm_id = self.request.get("confirm_id")
subscription = Subscription.get_by_confirm_id(confirm_id)
if subscription is None:
self.response.write('Unknown subscription')
return
subscription.confirm_date = datetime.datetime.utcnow()
subscription.confirmed = True
subscription.put()
self.response.write('Email address confirmed')
class ListHandler(webapp2.RequestHandler):
def get(self):
if users.get_current_user() is None:
login_url = users.create_login_url()
self.redirect(login_url)
return
if not users.is_current_user_admin():
self.response.write('Only admins can do this')
return
confirmed_subscriptions = Subscription.query_confirmed()
for subscription in confirmed_subscriptions:
self.response.write(subscription.key.string_id()+', ')
app = webapp2.WSGIApplication(
[
("/news/subscribe", SubscribeHandler),
("/news/confirm", ConfirmHandler),
("/news/list", ListHandler)
],
debug=True
)