Skip to content

Commit

Permalink
Adding api for live messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Myers committed Jul 14, 2012
1 parent 5e4defe commit 2c88801
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
30 changes: 30 additions & 0 deletions july/api.py
Expand Up @@ -19,6 +19,8 @@
from july import settings
from july.people.forms import CommitForm, ProjectForm
from july.people.models import Commit, Project
from july.live.models import Message
from july.live.forms import MessageForm

EMAIL_MATCH = re.compile('<(.+?)>')

Expand Down Expand Up @@ -799,7 +801,34 @@ def get(self, metric):
'stats': stats,
'total': sum(stats),
})

class LiveHandler(API):

model = Message
form = MessageForm

def post(self):
"""Create a message from the api.
Example::
{
"username": "github",
"message": "Something interesting",
"picture_url": "http://avator.com/my_image.jpg",
"url": "http://github.com/defunkt/github/commit/k20lkjs920lskjlskjjd1",
"commit_hash": "k20lkjs920lskjlskjjd1",
"project": "http://github.com/defunkt/github"
},
"""
form = self.parse_form()
if not form.is_valid():
return self.respond_json(form.errors, status_code=400)

message = Message.create_message(**form.cleaned_data)

self.respond_json({'message': self.serialize(message)}, status_code=201)

###
### Setup the routes for the API
###
Expand All @@ -814,6 +843,7 @@ def get(self, metric):
webapp2.Route('/api/v1/stats/commits/<metric:.+>', StatsResource),
webapp2.Route('/api/v1/github', GithubHandler),
webapp2.Route('/api/v1/bitbucket', BitbucketHandler),
webapp2.Route('/api/v1/live', LiveHandler),
]

# The Main Application
Expand Down
24 changes: 24 additions & 0 deletions july/live/forms.py
@@ -0,0 +1,24 @@

from django import forms
from django.template.defaultfilters import escape

class MessageForm(forms.Form):

username = forms.CharField(required=True)
picture_url = forms.URLField(required=True)
message = forms.CharField(required=True)
commit_hash = forms.CharField()
url = forms.URLField()
project = forms.URLField()

def clean_username(self):
data = self.cleaned_data.get('username')
return escape(data)

def clean_message(self):
data = self.cleaned_data.get('message')
return escape(data)

def clean_commit_hash(self):
data = self.cleaned_data.get('commit_hash')
return escape(data)
5 changes: 3 additions & 2 deletions july/live/models.py
Expand Up @@ -22,8 +22,9 @@ def __unicode__(self):

@classmethod
def create_message(cls, username, picture_url, message, **kwargs):
message = cls(username=username, picture_url, message)
message = cls(username=username, picture_url=picture_url, message=message)
message.populate(**kwargs)
message.put()

deferred.defer(create_message, message.key.urlsafe())
deferred.defer(create_message, message.key.urlsafe())
return message
31 changes: 31 additions & 0 deletions tests/test_api.py
Expand Up @@ -13,6 +13,7 @@
from july.api import app, make_digest, utcdatetime
from july.people.models import Project, Location, Accumulator
from july import settings
from july.live.models import Message

class CommitApiTests(WebTestCase):

Expand Down Expand Up @@ -585,7 +586,37 @@ def test_global_counts(self):
u'total': 0,
u"metric": u"global",
})

class TestMessage(WebTestCase):

APPLICATION = app

DATA = {
"username": "github",
"message": "Something interesting",
"picture_url": "http://avator.com/my_image.jpg",
"url": "http://github.com/defunkt/github/commit/k20lkjs920lskjlskjjd1",
"commit_hash": "k20lkjs920lskjlskjjd1",
"project": "http://github.com/defunkt/github"}

def test_create_message(self):
data = json.dumps(self.DATA)
self.app.post('/api/v1/live', data)
count = Message.query().count(10)
self.assertEqual(count, 1)

def test_post_adds_points_to_global(self):
data = json.dumps(self.DATA)
self.app.post('/api/v1/live', data)
tasks = self.taskqueue_stub.GetTasks('default')
self.assertEqual(1, len(tasks))

# Run the task
for task in tasks:
deferred.run(base64.b64decode(task['body']))

#TODO: assert channels

if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
unittest.main()
Expand Down

0 comments on commit 2c88801

Please sign in to comment.