Browse files

Making notify.io and mail work with task queues

  • Loading branch information...
1 parent a026404 commit 3aed5f74d40a9c6817fb5269c3c214a3597401bb @DFectuoso committed Aug 3, 2010
Showing with 47 additions and 44 deletions.
  1. +9 −0 app.yaml
  2. +38 −44 main.py
View
9 app.yaml
@@ -12,6 +12,15 @@ handlers:
upload: static/robots.txt
- url: /static
static_dir: static
+- url: /notifications/notifyio/post
+ script: main.py
+ login: admin
+- url: /worker/user
+ script: main.py
+ login: admin
+- url: /notifications/email/send
+ script: main.py
+ login: admin
- url: .*
script: main.py
login: required
View
82 main.py
@@ -11,66 +11,38 @@
from django.utils import simplejson
from django.template.defaultfilters import timesince
+import dojo_name_api, keys, notify_io, logging
+
#CONSTANTS#
UPDATES_LIMIT = 10
-NOTIFYIO_KEY = "l4y4eaxed6v5x2le7y"
+DOMAIN = "@gmail.com"
+SENDER_MAIL = "HD-Logs <santiago1717@gmail.com>"
# Parsing the username ourselfs because the nickname on GAE does funky stuff with non @gmail account
def username(user):
return user.nickname().split('@')[0] if user else None
-# Hacker Dojo Domain API helper with caching
-def dojo(path, cache_ttl=3600):
- base_url = 'http://domain.hackerdojo.com'
- resp = memcache.get(path)
- if not resp:
- resp = urlfetch.fetch(base_url + path, deadline=10)
- try:
- resp = simplejson.loads(resp.content)
- except Exception, e:
- resp = []
- cache_ttl = 10
- memcache.set(path, resp, cache_ttl)
- return resp
-
-# 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
-def fullname(username):
- fullname = memcache.get('/users/%s:fullname' % username)
- if not fullname:
- taskqueue.add(url='/worker/user', params={'username': username})
- memcache.set('/users/%s:fullname' % username, username, 10)
- return username
- else:
- return fullname
-
def str_to_bool(str):
if str == "true": return True
else: return False
-def sendNotifyIoNotifications(comment):
- profiles = Profile.all().filter('emailNotification =',True)
+def sendNotifyIoNotifications(update):
+ profiles = Profile.all().filter('notifyIoNotification =',True)
for profile in profiles:
- md5email = hashlib.md5(str(profile.user)).hexdigest()
- values = {'text': 'test', 'dsadsa':'dsadsa'}
- url = "http://api.notify.io/v1/notify/" + str(md5email) + "?api_key=" + NOTIFYIO_KEY
- logging.error("dsadsadsa %s", url)
- req = urllib2.Request(url,urllib.urlencode(values))
- urllib2.urlopen(req)
+ taskqueue.add(url='/notifications/notifyio/post', params={'email': profile.user, 'text':update.user_fullname() + ":" + update.body, 'title':"New HD-Log"})
def sendEmailNotifications(update):
- message = mail.EmailMessage(sender="HD-Logs <santiago1717@gmail.com>",subject="New Log")
- message.body = "Someone just posted on HD-Logs\n Log: " + update.body + "\n Wrote by:" + update.user_fullname()
+ body = "Someone just posted on HD-Logs\n Log: " + update.body + "\n Wrote by:" + update.user_fullname()
profiles = Profile.all().filter('emailNotification =',True)
for profile in profiles:
- message.to = str(profile.user)
- message.send()
+ taskqueue.add(url='/notifications/email/send', params={'to':profile.user,'body':body})
# Worket to handle the fullname queue request.
class UserWorker(webapp.RequestHandler):
def post(self):
username = self.request.get('username')
month_ttl = 3600*24*28
- user = dojo('/users/%s' % username, month_ttl)
+ user = dojo_name_api.dojo_name('/users/%s' % username, month_ttl)
memcache.set('/users/%s:fullname' % username, "%s %s" % (user['first_name'], user['last_name']), month_ttl)
#Data Models:
@@ -95,7 +67,7 @@ class Update(db.Model):
created = db.DateTimeProperty(auto_now_add=True)
def user_fullname(self):
- return fullname(username(self.user))
+ return dojo_name_api.fullname(username(self.user))
class Comment(db.Model):
user = db.UserProperty(auto_current_user_add=True)
@@ -104,7 +76,7 @@ class Comment(db.Model):
update = db.ReferenceProperty(Update)
def user_fullname(self):
- return fullname(username(self.user))
+ return dojo_name_api.fullname(username(self.user))
#Json representations
def comment_dict(comment):
@@ -158,13 +130,33 @@ def post(self):
update.put()
self.redirect('/')
-class EmailNotificationHandler(webapp.RequestHandler):
+class EmailSendNotificationHandler(webapp.RequestHandler):
+ def post(self):
+ sender = SENDER_MAIL
+ subject = "New HD-Log"
+ message = mail.EmailMessage(sender=sender,subject=subject)
+ message.body = self.request.get('body')
+ message.to = self.request.get('to') + DOMAIN
+ message.send()
+
+#this is used to turn off/on the email for a user
+class EmailEnableNotificationHandler(webapp.RequestHandler):
def post(self):
user = Profile.get_or_create()
user.emailNotification = str_to_bool(self.request.get('enable'))
user.put()
-class NotifyIoNotificationHandler(webapp.RequestHandler):
+#This is used by the taskqueue to actually send the notification
+class NotifyIoPostNotificationHandler(webapp.RequestHandler):
+ def post(self):
+ email = self.request.get('email') + DOMAIN
+ text = self.request.get('text')
+ title = self.request.get('title')
+ logging.info('Sending a Notify.io message to %s' % email)
+ notify_io.notify(email, text, title)
+
+# This is used to turn off/on the notifications for a user
+class NotifyIoEnableNotificationHandler(webapp.RequestHandler):
def post(self):
user = Profile.get_or_create()
user.notifyIoNotification = str_to_bool(self.request.get('enable'))
@@ -175,8 +167,10 @@ def main():
('/', MainHandler),
('/updates/(.+)', UpdatesHandler),
('/comment/(.+)', CommentHandler),
- ('/notifications/email', EmailNotificationHandler),
- ('/notifications/notifyio', NotifyIoNotificationHandler),
+ ('/notifications/email', EmailEnableNotificationHandler),
+ ('/notifications/email/send', EmailSendNotificationHandler),
+ ('/notifications/notifyio', NotifyIoEnableNotificationHandler),
+ ('/notifications/notifyio/post', NotifyIoPostNotificationHandler),
('/worker/user', UserWorker),
], debug=True)
util.run_wsgi_app(application)

0 comments on commit 3aed5f7

Please sign in to comment.