Permalink
Browse files

Better util methods.

  • Loading branch information...
1 parent 8bcf565 commit 48037b556597166f27bb27dd0e5ff02b3ff10df8 @ahume committed Dec 3, 2012
Showing with 10 additions and 13 deletions.
  1. +7 −0 app/utils.py
  2. +3 −13 app/views/uservotes.py
View
@@ -87,6 +87,13 @@ def user_to_dict(user):
del u['mp_score']
return u
+def vote_to_dict(vote):
+ v = db.to_dict(vote)
+ v['question'] = question_to_dict(vote.parent())
+ del v['user_username']
+ del v['constituency']
+ return v
+
class QueryFilter(object):
def filterQueryOnParam(self, param):
View
@@ -1,7 +1,6 @@
import logging
from google.appengine.ext import webapp
-from google.appengine.ext import db
from models import User, UserVote, Question
import utils
@@ -22,11 +21,7 @@ def get(self, username):
response['votes'] = []
for vote in self.query:
- d = db.to_dict(vote)
- d['question'] = utils.question_to_dict(vote.parent())
- del d['user_username']
- del d['constituency']
- response['votes'].append(d)
+ response['votes'].append(utils.vote_to_dict(vote))
response['total'] = len(response['votes'])
self.returnJSON(200, response)
@@ -73,11 +68,7 @@ def update(self, username, question_key):
vote.selection = self.request.get('selection')
vote.put()
- response['vote'] = db.to_dict(vote)
- del response['vote']['user_username']
- del response['vote']['constituency']
-
-
+ response['vote'] = utils.vote_to_dict(vote)
response['user'] = utils.user_to_dict(user)
return response
@@ -88,8 +79,7 @@ def get(self, username, question_key):
try:
vote = UserVote.all().filter('user_username =', username).filter('question =', question_key)[0]
- response['vote'] = db.to_dict(vote)
- response['vote']['question'] = utils.question_to_dict(vote.parent())
+ response['vote'] = utils.vote_to_dict(vote)
except:
response['error'] = 'Cannot find username'
self.returnJSON(404, response)

0 comments on commit 48037b5

Please sign in to comment.