Skip to content
Browse files

Refactored UserVote crud and added tests.

  • Loading branch information...
1 parent f33da51 commit 672a9ebdcaeb76378196a87c261d0783f443f98c @ahume committed Dec 3, 2012
Showing with 79 additions and 67 deletions.
  1. +68 −58 app/views/votes.py
  2. +11 −9 test/test.py
View
126 app/views/votes.py
@@ -11,52 +11,6 @@
from models import User, MP, UserVote, Question, MPVote
import utils
-class UserVoteHandler(webapp.RequestHandler, utils.JsonAPIResponse):
- def get(self, username, question_key):
-
- response = {}
-
- #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())
- #except:
- # response['error'] = 'Cannot find username'
- # self.returnJSON(404, response)
- # return
-
- self.returnJSON(200, response)
-
-class MPVoteListHandler(webapp.RequestHandler, utils.QueryFilter, utils.JsonAPIResponse):
- def get(self, slug):
-
- response = {}
- try:
- mp = MP.all().filter('slug =', slug)[0]
- response['mp'] = utils.mp_to_dict(mp)
- except:
- self.returnJSON(404, response)
- return
-
- self.query = MPVote.all().filter('mp_slug =', slug)
- self.filterQueryOnParam('question')
- self.filterQueryOnParam('selection')
-
- response['votes'] = []
- for vote in self.query:
- d = db.to_dict(vote)
- d['question'] = utils.question_to_dict(vote.parent())
- del d['mp_party']
- del d['mp_constituency']
- del d['mp_slug']
- del d['mp_name']
- response['votes'].append(d)
- response['total'] = len(response['votes'])
-
- self.returnJSON(200, response)
-
-
-
class UserVoteListHandler(webapp.RequestHandler, utils.QueryFilter, utils.JsonAPIResponse):
def get(self, username):
response = {}
@@ -83,21 +37,12 @@ def get(self, username):
self.returnJSON(200, response)
def post(self, username):
- response = self.update(username)
+ question_key = self.request.get('question')
+ response = self.update(username, question_key)
if response is not None:
self.returnJSON(201, response)
-
- def put(self, username):
- response = self.update(username)
- if response is not None:
- self.returnJSON(200, response)
-
-
-
- def update(self, username):
-
- question_key = self.request.get('question')
+ def update(self, username, question_key):
response = {}
@@ -141,3 +86,68 @@ def update(self, username):
response['user'] = utils.user_to_dict(user)
return response
+class UserVoteHandler(UserVoteListHandler, utils.JsonAPIResponse):
+ def get(self, username, question_key):
+
+ response = {}
+
+ 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())
+ except:
+ response['error'] = 'Cannot find username'
+ self.returnJSON(404, response)
+ return
+
+ self.returnJSON(200, response)
+
+ def put(self, username, question_key):
+ response = self.update(username, question_key)
+ if response is not None:
+ self.returnJSON(200, response)
+
+
+ def delete(self, username, question_key):
+
+ response = {}
+
+ try:
+ vote = UserVote.all().filter('user_username =', username).filter('question =', question_key)[0]
+ vote.delete()
+ response['status'] = 200
+ except:
+ response['error'] = 'Cannot find username'
+ self.returnJSON(404, response)
+ return
+
+ self.returnJSON(200, response)
+
+
+class MPVoteListHandler(webapp.RequestHandler, utils.QueryFilter, utils.JsonAPIResponse):
+ def get(self, slug):
+
+ response = {}
+ try:
+ mp = MP.all().filter('slug =', slug)[0]
+ response['mp'] = utils.mp_to_dict(mp)
+ except:
+ self.returnJSON(404, response)
+ return
+
+ self.query = MPVote.all().filter('mp_slug =', slug)
+ self.filterQueryOnParam('question')
+ self.filterQueryOnParam('selection')
+
+ response['votes'] = []
+ for vote in self.query:
+ d = db.to_dict(vote)
+ d['question'] = utils.question_to_dict(vote.parent())
+ del d['mp_party']
+ del d['mp_constituency']
+ del d['mp_slug']
+ del d['mp_name']
+ response['votes'].append(d)
+ response['total'] = len(response['votes'])
+
+ self.returnJSON(200, response)
View
20 test/test.py
@@ -256,28 +256,30 @@ def test_unanswered_list(self):
class TestUserVoteList(PolitmusAPITest):
- def test_list(self):
+ def test_uservote_crud(self):
data = self.get_data('/users/andyhume/votes')
-
self.assertEqual(data['status'], 200)
self.assertEqual(len(data['votes']), 0)
- data = self.post_data('/users/andyhume/votes', {'selection': 'aye', 'question_id': 'ahBkZXZ-cG9saXRtdXMtYXBpcg4LEghRdWVzdGlvbhgTDA'})
+ data = self.get_data('/users/andyhume/unanswered-questions')
+ question_key = data['questions'][0]['key']
+ data = self.post_data('/users/andyhume/votes', {'selection': 'aye', 'question': question_key})
self.assertEqual(data['status'], 201)
+ self.assertEqual(data['vote']['selection'], 'aye')
data = self.get_data('/users/andyhume/votes')
self.assertEqual(len(data['votes']), 1)
-
- def test_create_vote(self):
-
- data = self.post_data('/users/andyhume/votes', {'selection': 'aye', 'question_id': 'ahBkZXZ-cG9saXRtdXMtYXBpcg4LEghRdWVzdGlvbhgTDA'})
-
+ data = self.put_data('/users/andyhume/votes/%s' % question_key, {'selection': 'no'})
self.assertEqual(data['status'], 200)
- self.assertEqual(len(data['votes']), 1)
+ self.assertEqual(data['vote']['selection'], 'no')
+ data = self.delete_data('/users/andyhume/votes/%s' % question_key)
+ self.assertEqual(data['status'], 200)
+ data = self.get_data('/users/andyhume/votes')
+ self.assertEqual(len(data['votes']), 0)
class TestQuestionList(PolitmusAPITest):

0 comments on commit 672a9eb

Please sign in to comment.
Something went wrong with that request. Please try again.