Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding domain.hackerdojo.com query for nice names in logs and comments.

  • Loading branch information...
commit 8b8be74e4bfaea1cc68838a11da8631bd4d95df3 1 parent 0038f10
@DFectuoso DFectuoso authored
Showing with 57 additions and 7 deletions.
  1. +2 −2 app.yaml
  2. +52 −2 main.py
  3. +3 −3 templates/main.html
View
4 app.yaml
@@ -1,4 +1,4 @@
-application: hackerdojo-log
+application: hackerdojo-log-dfect
version: 1
runtime: python
api_version: 1
@@ -14,4 +14,4 @@ handlers:
static_dir: static
- url: .*
script: main.py
- login: required
+ login: required
View
54 main.py
@@ -1,11 +1,55 @@
+import logging
+
from google.appengine.ext import webapp, db
from google.appengine.api import urlfetch, memcache, users
from google.appengine.ext.webapp import util, template
+from google.appengine.api.labs import taskqueue
+from django.utils import simplejson
+
+# 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
+
+# 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)
+ memcache.set('/users/%s:fullname' % username, "%s %s" % (user['first_name'], user['last_name']), month_ttl)
+#Data Models:
class Update(db.Model):
user = db.UserProperty(auto_current_user_add=True)
body = db.StringProperty(required=True, multiline=True)
created = db.DateTimeProperty(auto_now_add=True)
+
+ def userFullname(self):
+ return fullname(username(self.user))
class Comment(db.Model):
user = db.UserProperty(auto_current_user_add=True)
@@ -13,6 +57,11 @@ class Comment(db.Model):
created = db.DateTimeProperty(auto_now_add=True)
update = db.ReferenceProperty(Update)
+ def userFullname(self):
+ return fullname(username(self.user))
+
+
+# Handlers:
class CommentHandler(webapp.RequestHandler):
def post(self, update_id):
update = Update.get_by_id(int(update_id))
@@ -38,11 +87,12 @@ def post(self):
update.put()
self.redirect('/')
-
def main():
application = webapp.WSGIApplication([
('/', MainHandler),
- ('/comment/(.+)', CommentHandler)], debug=True)
+ ('/comment/(.+)', CommentHandler),
+ ('/worker/user', UserWorker),
+ ], debug=True)
util.run_wsgi_app(application)
View
6 templates/main.html
@@ -9,7 +9,7 @@
{% for update in updates %}
<p>{{update.body}} |
{{update.created|timesince}} ago |
- {{update.user.nickname}} |
+ {{update.userFullname}} |
<a href="javascript:void()"
onclick="toggleComment({{update.key.id}})"
id="comment-link-{{update.key.id}}">Comment</a>
@@ -22,7 +22,7 @@
</form></p>
{% for comment in update.comment_set %}
<p style="margin-left: 20px;">{{comment.body}} | {{comment.created|timesince}} ago |
- {{comment.user.nickname}}</p>
+ {{comment.userFullname}}</p>
{% endfor %}
{% endfor %}
@@ -38,4 +38,4 @@
}
}
</script>
-{% endblock %}
+{% endblock %}
Please sign in to comment.
Something went wrong with that request. Please try again.