Permalink
Browse files

Added a history endpoint for displaying recent events from the comman…

…d line
  • Loading branch information...
lovett committed May 2, 2011
1 parent a15970d commit 9150528766e36391e551a5b1bd63d580717cc1f4
Showing with 85 additions and 0 deletions.
  1. +2 −0 appengine/app.yaml
  2. +68 −0 appengine/history.py
  3. +6 −0 appengine/index.yaml
  4. +9 −0 appengine/models.py
View
@@ -21,6 +21,8 @@ handlers:
script: mute.py
- url: /phonecall
script: phonecall.py
+- url: /history
+ script: history.py
- url: /revoke
script: revoke.py
login: required
View
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+
+from google.appengine.ext import webapp
+from google.appengine.ext.webapp.util import run_wsgi_app
+from models import ProfileManager, Announcement, Event
+from datetime import datetime, timedelta
+import calendar
+
+class HistoryHandler(webapp.RequestHandler):
+
+ def get(self):
+
+ required_parameters = ["token", "offset"]
+
+ for param in required_parameters:
+ param_value = self.request.get(param)
+
+ if not param_value:
+ self.response.set_status(400)
+ self.response.out.write("Missing parameter: %s" % param)
+ return
+
+ profile = ProfileManager.findByToken(self.request.get("token"))
+
+ if not profile:
+ self.response.set_status(400)
+ self.response.out.write("Invalid token")
+ return
+
+ if self.request.get("limit"):
+ limit = int(self.request.get("limit"))
+ else:
+ limit = 10
+
+ events = Event.find(profile, limit)
+
+ offset = self.request.get("offset")
+
+ if len(offset) == 5:
+ sign = offset[0:1]
+ hours = int(offset[1:3])
+ minutes = int(offset[3:5])
+
+ offset_seconds = hours * 3600 + minutes * 60
+ if sign == "-":
+ offset_seconds *= -1
+ else:
+ offset_seconds = 0
+
+ format = "%-16s %-10s %-s\n"
+ self.response.out.write(format % ("Date", "Type", "Message"))
+ self.response.out.write("------------------------------------------------------------------------\n")
+ for event in events:
+
+ event_date = event.received + timedelta(seconds=offset_seconds)
+ event_date = event_date.strftime("%b %d %I:%M %p")
+
+ self.response.out.write(format % (event_date, event.type, event.message.replace("\n", " ")))
+
+
+def main():
+ application = webapp.WSGIApplication([('/history', HistoryHandler)],
+ debug=True)
+ run_wsgi_app(application)
+
+
+if __name__ == '__main__':
+ main()
View
@@ -0,0 +1,6 @@
+indexes:
+- kind: Event
+ properties:
+ - name: owner
+ - name: received
+ direction: desc
View
@@ -96,6 +96,15 @@ class Event(db.Model):
received = db.DateTimeProperty(auto_now_add=True)
message = db.TextProperty()
+ @classmethod
+ def find(self, profile, limit=10):
+ if limit > 50:
+ limit = 10
+
+ q = Event.all()
+ q.filter("owner =", profile.owner.email())
+ q.order("-received")
+ return q.fetch(limit)
class Announcement():

0 comments on commit 9150528

Please sign in to comment.