Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Making notify.io and mail work with task queues

  • Loading branch information...
commit 3aed5f74d40a9c6817fb5269c3c214a3597401bb 1 parent a026404
Santiago authored

Showing 2 changed files with 47 additions and 44 deletions. Show diff stats Hide diff stats

  1. +9 0 app.yaml
  2. +38 44 main.py
9 app.yaml
@@ -12,6 +12,15 @@ handlers:
12 12 upload: static/robots.txt
13 13 - url: /static
14 14 static_dir: static
  15 +- url: /notifications/notifyio/post
  16 + script: main.py
  17 + login: admin
  18 +- url: /worker/user
  19 + script: main.py
  20 + login: admin
  21 +- url: /notifications/email/send
  22 + script: main.py
  23 + login: admin
15 24 - url: .*
16 25 script: main.py
17 26 login: required
82 main.py
@@ -11,66 +11,38 @@
11 11 from django.utils import simplejson
12 12 from django.template.defaultfilters import timesince
13 13
  14 +import dojo_name_api, keys, notify_io, logging
  15 +
14 16 #CONSTANTS#
15 17 UPDATES_LIMIT = 10
16   -NOTIFYIO_KEY = "l4y4eaxed6v5x2le7y"
  18 +DOMAIN = "@gmail.com"
  19 +SENDER_MAIL = "HD-Logs <santiago1717@gmail.com>"
17 20
18 21 # Parsing the username ourselfs because the nickname on GAE does funky stuff with non @gmail account
19 22 def username(user):
20 23 return user.nickname().split('@')[0] if user else None
21 24
22   -# Hacker Dojo Domain API helper with caching
23   -def dojo(path, cache_ttl=3600):
24   - base_url = 'http://domain.hackerdojo.com'
25   - resp = memcache.get(path)
26   - if not resp:
27   - resp = urlfetch.fetch(base_url + path, deadline=10)
28   - try:
29   - resp = simplejson.loads(resp.content)
30   - except Exception, e:
31   - resp = []
32   - cache_ttl = 10
33   - memcache.set(path, resp, cache_ttl)
34   - return resp
35   -
36   -# Return the name of a user from the memcache. If it was not set we set it and queue a query to the domain api
37   -def fullname(username):
38   - fullname = memcache.get('/users/%s:fullname' % username)
39   - if not fullname:
40   - taskqueue.add(url='/worker/user', params={'username': username})
41   - memcache.set('/users/%s:fullname' % username, username, 10)
42   - return username
43   - else:
44   - return fullname
45   -
46 25 def str_to_bool(str):
47 26 if str == "true": return True
48 27 else: return False
49 28
50   -def sendNotifyIoNotifications(comment):
51   - profiles = Profile.all().filter('emailNotification =',True)
  29 +def sendNotifyIoNotifications(update):
  30 + profiles = Profile.all().filter('notifyIoNotification =',True)
52 31 for profile in profiles:
53   - md5email = hashlib.md5(str(profile.user)).hexdigest()
54   - values = {'text': 'test', 'dsadsa':'dsadsa'}
55   - url = "http://api.notify.io/v1/notify/" + str(md5email) + "?api_key=" + NOTIFYIO_KEY
56   - logging.error("dsadsadsa %s", url)
57   - req = urllib2.Request(url,urllib.urlencode(values))
58   - urllib2.urlopen(req)
  32 + taskqueue.add(url='/notifications/notifyio/post', params={'email': profile.user, 'text':update.user_fullname() + ":" + update.body, 'title':"New HD-Log"})
59 33
60 34 def sendEmailNotifications(update):
61   - message = mail.EmailMessage(sender="HD-Logs <santiago1717@gmail.com>",subject="New Log")
62   - message.body = "Someone just posted on HD-Logs\n Log: " + update.body + "\n Wrote by:" + update.user_fullname()
  35 + body = "Someone just posted on HD-Logs\n Log: " + update.body + "\n Wrote by:" + update.user_fullname()
63 36 profiles = Profile.all().filter('emailNotification =',True)
64 37 for profile in profiles:
65   - message.to = str(profile.user)
66   - message.send()
  38 + taskqueue.add(url='/notifications/email/send', params={'to':profile.user,'body':body})
67 39
68 40 # Worket to handle the fullname queue request.
69 41 class UserWorker(webapp.RequestHandler):
70 42 def post(self):
71 43 username = self.request.get('username')
72 44 month_ttl = 3600*24*28
73   - user = dojo('/users/%s' % username, month_ttl)
  45 + user = dojo_name_api.dojo_name('/users/%s' % username, month_ttl)
74 46 memcache.set('/users/%s:fullname' % username, "%s %s" % (user['first_name'], user['last_name']), month_ttl)
75 47
76 48 #Data Models:
@@ -95,7 +67,7 @@ class Update(db.Model):
95 67 created = db.DateTimeProperty(auto_now_add=True)
96 68
97 69 def user_fullname(self):
98   - return fullname(username(self.user))
  70 + return dojo_name_api.fullname(username(self.user))
99 71
100 72 class Comment(db.Model):
101 73 user = db.UserProperty(auto_current_user_add=True)
@@ -104,7 +76,7 @@ class Comment(db.Model):
104 76 update = db.ReferenceProperty(Update)
105 77
106 78 def user_fullname(self):
107   - return fullname(username(self.user))
  79 + return dojo_name_api.fullname(username(self.user))
108 80
109 81 #Json representations
110 82 def comment_dict(comment):
@@ -158,13 +130,33 @@ def post(self):
158 130 update.put()
159 131 self.redirect('/')
160 132
161   -class EmailNotificationHandler(webapp.RequestHandler):
  133 +class EmailSendNotificationHandler(webapp.RequestHandler):
  134 + def post(self):
  135 + sender = SENDER_MAIL
  136 + subject = "New HD-Log"
  137 + message = mail.EmailMessage(sender=sender,subject=subject)
  138 + message.body = self.request.get('body')
  139 + message.to = self.request.get('to') + DOMAIN
  140 + message.send()
  141 +
  142 +#this is used to turn off/on the email for a user
  143 +class EmailEnableNotificationHandler(webapp.RequestHandler):
162 144 def post(self):
163 145 user = Profile.get_or_create()
164 146 user.emailNotification = str_to_bool(self.request.get('enable'))
165 147 user.put()
166 148
167   -class NotifyIoNotificationHandler(webapp.RequestHandler):
  149 +#This is used by the taskqueue to actually send the notification
  150 +class NotifyIoPostNotificationHandler(webapp.RequestHandler):
  151 + def post(self):
  152 + email = self.request.get('email') + DOMAIN
  153 + text = self.request.get('text')
  154 + title = self.request.get('title')
  155 + logging.info('Sending a Notify.io message to %s' % email)
  156 + notify_io.notify(email, text, title)
  157 +
  158 +# This is used to turn off/on the notifications for a user
  159 +class NotifyIoEnableNotificationHandler(webapp.RequestHandler):
168 160 def post(self):
169 161 user = Profile.get_or_create()
170 162 user.notifyIoNotification = str_to_bool(self.request.get('enable'))
@@ -175,8 +167,10 @@ def main():
175 167 ('/', MainHandler),
176 168 ('/updates/(.+)', UpdatesHandler),
177 169 ('/comment/(.+)', CommentHandler),
178   - ('/notifications/email', EmailNotificationHandler),
179   - ('/notifications/notifyio', NotifyIoNotificationHandler),
  170 + ('/notifications/email', EmailEnableNotificationHandler),
  171 + ('/notifications/email/send', EmailSendNotificationHandler),
  172 + ('/notifications/notifyio', NotifyIoEnableNotificationHandler),
  173 + ('/notifications/notifyio/post', NotifyIoPostNotificationHandler),
180 174 ('/worker/user', UserWorker),
181 175 ], debug=True)
182 176 util.run_wsgi_app(application)

0 comments on commit 3aed5f7

Please sign in to comment.
Something went wrong with that request. Please try again.