Skip to content

Commit

Permalink
Api to post from other apps
Browse files Browse the repository at this point in the history
  • Loading branch information
DFectuoso committed Aug 6, 2010
1 parent 408851d commit 0fb6a77
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app.yaml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ handlers:
upload: static/robots.txt upload: static/robots.txt
- url: /static - url: /static
static_dir: static static_dir: static
- url: /api
script: main.py
login: optional
- url: /notifications/notifyio/post - url: /notifications/notifyio/post
script: main.py script: main.py
login: admin login: admin
Expand Down
2 changes: 1 addition & 1 deletion dojo_name_api.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def fullname(username):
fullname = memcache.get('/users/%s:fullname' % username) fullname = memcache.get('/users/%s:fullname' % username)
if not fullname: if not fullname:
taskqueue.add(url='/worker/user', params={'username': username}) taskqueue.add(url='/worker/user', params={'username': username})
memcache.set('/users/%s:fullname' % username, username, 10) memcache.set('/users/%s:fullname' % username, username, 100)
return username return username
else: else:
return fullname return fullname
Expand Down
60 changes: 51 additions & 9 deletions main.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@


import dojo_name_api, keys, notify_io, logging, cgi import dojo_name_api, keys, notify_io, logging, cgi




#CONSTANTS# #CONSTANTS#
UPDATES_LIMIT = 100 UPDATES_LIMIT = 100
DOMAIN = "@gmail.com" DOMAIN = "@gmail.com"
SENDER_MAIL = "HD-Logs <santiago1717@gmail.com>" SENDER_MAIL = "HD-Logs <santiago1717@gmail.com>"
APP_NAMES = ["Kudos", "Signin","Events"]
APP_NAMES_NO_NOTIFY = ["Signin","Events"]


# Parsing the username ourselfs because the nickname on GAE does funky stuff with non @gmail account # Parsing the username ourselfs because the nickname on GAE does funky stuff with non @gmail account
def username(user): def username(user):
Expand All @@ -45,8 +45,15 @@ def sendEmailNotifications(update):
# Worket to handle the fullname queue request. # Worket to handle the fullname queue request.
class UserWorker(webapp.RequestHandler): class UserWorker(webapp.RequestHandler):
def post(self): def post(self):
username = self.request.get('username')
month_ttl = 3600*24*28 month_ttl = 3600*24*28
username = self.request.get('username')
try:
index = APP_NAMES.index(name)
except ValueError:
index = -1

if index != -1:
memcache.set('/users/%s:fullname' % username, username, month_ttl)
user = dojo_name_api.dojo_name('/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) memcache.set('/users/%s:fullname' % username, "%s %s" % (user['first_name'], user['last_name']), month_ttl)


Expand Down Expand Up @@ -113,15 +120,47 @@ def get(self,cursor):
class CommentHandler(webapp.RequestHandler): class CommentHandler(webapp.RequestHandler):
def post(self, update_id): def post(self, update_id):
update = Update.get_by_id(int(update_id)) update = Update.get_by_id(int(update_id))
if update: body = sanitizeHtml(self.request.get('body'))
if update and len(body) > 0 and len(body) < 500:
image = 'http://0.gravatar.com/avatar/%s' % hashlib.md5(str(users.get_current_user()) + DOMAIN).hexdigest() image = 'http://0.gravatar.com/avatar/%s' % hashlib.md5(str(users.get_current_user()) + DOMAIN).hexdigest()
comment = Comment( comment = Comment(
body=sanitizeHtml(self.request.get('body')), body=body,
update=update, update=update,
image_url=image) image_url=image)
comment.put() comment.put()
self.redirect('/') self.redirect('/')


class ApiHandler(webapp.RequestHandler):
def post(self):
body = sanitizeHtml(self.request.get('body'))
name = sanitizeHtml(self.request.get('name'))
key = self.request.get('key')
if body == "" or name == "" or key == "":
self.response.out.write("body,name and key are required")
return
if key == keys.logs_key:
try:
index = APP_NAMES.index(name)
except ValueError:
index = -1

if index != -1:
user = users.User(name + DOMAIN)
update = Update(body=body,image_url="/static/dojo_icon.png",user=user)
update.put()
try:
index = APP_NAMES_NO_NOTIFY.index(name)
except ValueError:
index = -1
if index == -1:
sendNotifyIoNotifications(update)
sendEmailNotifications(update)
self.response.out.write("OK")
else:
self.response.out.write("Not a valid App")
else:
self.response.out.write("Invalid Key")

class MainHandler(webapp.RequestHandler): class MainHandler(webapp.RequestHandler):
def get(self): def get(self):
user = users.get_current_user() user = users.get_current_user()
Expand All @@ -136,10 +175,12 @@ def get(self):


def post(self): def post(self):
image = 'http://0.gravatar.com/avatar/%s' % hashlib.md5(str(users.get_current_user()) + DOMAIN).hexdigest() image = 'http://0.gravatar.com/avatar/%s' % hashlib.md5(str(users.get_current_user()) + DOMAIN).hexdigest()
update = Update(body=sanitizeHtml(self.request.get('body')),image_url=image) body = sanitizeHtml(self.request.get('body'))
sendEmailNotifications(update) if len(body) > 0 and len(body) < 500:
sendNotifyIoNotifications(update) update = Update(body=body,image_url=image)
update.put() sendEmailNotifications(update)
sendNotifyIoNotifications(update)
update.put()
self.redirect('/') self.redirect('/')


class EmailSendNotificationHandler(webapp.RequestHandler): class EmailSendNotificationHandler(webapp.RequestHandler):
Expand Down Expand Up @@ -179,6 +220,7 @@ def main():
('/', MainHandler), ('/', MainHandler),
('/updates/(.+)', UpdatesHandler), ('/updates/(.+)', UpdatesHandler),
('/comment/(.+)', CommentHandler), ('/comment/(.+)', CommentHandler),
('/api', ApiHandler),
('/notifications/email', EmailEnableNotificationHandler), ('/notifications/email', EmailEnableNotificationHandler),
('/notifications/email/send', EmailSendNotificationHandler), ('/notifications/email/send', EmailSendNotificationHandler),
('/notifications/notifyio', NotifyIoEnableNotificationHandler), ('/notifications/notifyio', NotifyIoEnableNotificationHandler),
Expand Down
2 changes: 2 additions & 0 deletions static/style.css
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ h4 { font-size: smaller; }
.update .avatar img {height:50px; width:50px; -webkit-border-radius: 2px;-moz-border-radius: 2px;border-radius: 2px;} .update .avatar img {height:50px; width:50px; -webkit-border-radius: 2px;-moz-border-radius: 2px;border-radius: 2px;}
.update .update-body {margin-left: 56px; min-height:48px; display:block;} .update .update-body {margin-left: 56px; min-height:48px; display:block;}
.update-meta {color:#999999; display:block; font-size:11px;} .update-meta {color:#999999; display:block; font-size:11px;}
.meta-right {text-align:right;color:#999999; display:block; font-size:11px;}
.meta-left {text-align:left;color:#999999; display:block; font-size:11px;}
.update .user-fullname {color:#D50025;} .update .user-fullname {color:#D50025;}
.comment .comment-fullname {color:#D50025;} .comment .comment-fullname {color:#D50025;}
.update-input { width:90%; height:50px} .update-input { width:90%; height:50px}
Expand Down
5 changes: 3 additions & 2 deletions templates/main.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


<h3>What are you doing?</h3> <h3>What are you doing?</h3>
<form method="post"> <form method="post">
<textarea name="body" class="update-input"></textarea> <textarea name="body" class="update-input" ></textarea>
<input type="submit" value="Update" /> <input type="submit" value="Update" /><span class="meta-right">Max 500 chars</span>
</form> </form>
<div id="updates"> <div id="updates">
{% for update in updates %} {% for update in updates %}
Expand Down Expand Up @@ -37,6 +37,7 @@ <h3>What are you doing?</h3>
<div class="comment-form-controls"> <div class="comment-form-controls">
<input type="submit" value="Comment" /> <input type="submit" value="Comment" />
<button type="button" class="comment-link-cancel" onclick="toggleComment({{update.key.id}})">Cancel</button> <button type="button" class="comment-link-cancel" onclick="toggleComment({{update.key.id}})">Cancel</button>
<span class="meta-left">max 500 chars</span>
</div> </div>
</form> </form>
</div> </div>
Expand Down

0 comments on commit 0fb6a77

Please sign in to comment.