def vote_add(request, system_name): """ Add a blank vote and redirect to its edit page """ vs = get_object_or_404(VotingSystem, machine_name=system_name) # raise an error if the user trying to access is not an admin if not vs.isAdmin(request.user.profile): raise PermissionDenied v = Vote() s = Status() s.save() now = str(int(time.time())) v.name = "Untitled Vote 1" v.machine_name = "new_" + now v.system = vs v.status = s v.creator = request.user v.min_votes = 0 v.max_votes = 0 v.save() return redirect('votes:edit', system_name=system_name, vote_name=v.machine_name)
def post(self, request, **kwargs): action = kwargs['action'] resource_id = kwargs['resource_id'] resource = Resource.objects.filter(id=resource_id).first() user_id = self.request.user.id vote = Vote.objects.filter( resource_id=resource_id, user_id=user_id).first() vote_mapping = { 'like': True, 'unlike': False, } # Create a vote object if the user has not voted yet if vote is None: vote = Vote() vote.resource = resource vote.user = self.request.user if vote.vote is None or vote.vote is not vote_mapping[action]: # If user has not voted yet or is changing his vote set vote to # current vote vote.vote = vote_mapping[action] status = action vote.save() else: vote.delete() status = "novote" response_dict = { "upvotes": len(resource.upvotes()), "downvotes": len(resource.downvotes()), "status": status, } response_json = json.dumps(response_dict) return HttpResponse(response_json, content_type="application/json")
def create_vote(sender, **kwargs): print "Post Save triggered" created = kwargs.get('created') if created: print "Instance Created" instance = kwargs.get('instance') vote = Vote(positive=0, negative=0, composition=instance) vote.save()
def test_for_downvote(self): resource = self.create_resources() vote = Vote() vote.user = self.user vote.resource = resource vote.vote = False vote.save() self.assertEqual(len(resource.downvotes()), 1)
def issueVote(request, id): try: issue = Issue.objects.get(pk=id) except Issue.DoesNotExist: messages.add_message(request, messages.ERROR, 'Issue #%s not found'%(id)) return redirect(reverse('index')) voted = request.user.vote_set.filter(issue=id) query = request.GET if 'vote' in query and not voted and query['vote'] in ('Yae','Nay'): vote = Vote(vote=(query['vote']=='Yae'), issue=issue, user=request.user) vote.save() messages.add_message(request, messages.INFO, 'Your vote has been saved') return redirect(reverse('issue_view',kwargs={'id':id}))
def create(self, validated_data): """ Custom create method. Support nested multiple vote choices creation. """ assert "choices" in validated_data choices_data = validated_data.pop("choices") vote = Vote(**validated_data) vote.save() self.create_choices(vote, choices_data) return vote
def do_vote(user, content, vote_request): try: content_type = ContentType.objects.get_for_model(content) old_vote = Vote.objects.get(user=user, object_id=content.id, content_type=content_type) vote_was_up = old_vote.isVoteUp old_vote.delete() new_vote = False except: new_vote = True vote = Vote(user=user, content=content) if new_vote: if vote_request == "up": vote.isVoteUp = True vote.save() return 1 elif vote_request == "down": vote.isVoteUp = False vote.save() return -1 elif not new_vote: if vote_request == "up": if vote_was_up: return 0 else: vote.isVoteUp = True vote.save() return 1 elif vote_request == "down": if not vote_was_up: return 0 else: vote.isVoteUp = False vote.save() return -1 else: raise Exception("Unknown vote request " + str(vote_request))
def create_vote(sender, **kwargs): created = kwargs.get('created') if created: instance = kwargs.get('instance') vote = Vote(positive=0, negative=0, composition=instance) vote.save()
def test_can_not_save_empty_vote_name(self): vote_ = Vote(vote_name='') with self.assertRaises(ValidationError): vote_.save() vote_.full_clean()
class VoteTests(UserSetUp, TestCase): """Vote model tests.""" def setUp(self): super(VoteTests, self).setUp() self.exhibition = Exhibition.objects.create( title="New Exhibition", description="description goes here", author=self.user ) self.artwork = Artwork.objects.create(title="New Artwork", code="// code goes here", author=self.user) self.submission = Submission.objects.create( exhibition=self.exhibition, artwork=self.artwork, submitted_by=self.user ) self.vote = Vote(submission=self.submission, status=Vote.THUMBS_UP, voted_by=self.user) def test_str(self): self.assertEquals(str(self.vote), "New Exhibition :: New Artwork :: Thumbs Up") def test_str_unset(self): unset_vote = Vote() self.assertEquals(str(unset_vote), "unset :: none") def test_thumbs_up_score(self): self.assertEqual(self.submission.score, 0) # Creating a thumbs up vote increments submission.score self.vote.save() self.submission = Submission.objects.get(id=self.submission.id) self.assertEqual(self.submission.score, 1) # Editing a thumbs up vote does not change submission.score self.vote.voted_by = self.staff_user self.vote.save() self.submission = Submission.objects.get(id=self.submission.id) self.assertEqual(self.submission.score, 1) # Deleting a thumbs up vote decrements submission.score self.vote.delete() self.submission = Submission.objects.get(id=self.submission.id) self.assertEqual(self.submission.score, 0) def test_save_unique(self): # one vote per submission per user self.assertEqual(self.vote.save(), None) vote2 = Vote(submission=self.submission, voted_by=self.user, status=Vote.THUMBS_UP) self.assertRaisesRegexp(IntegrityError, "columns submission_id, voted_by_id are not unique", vote2.save) def test_can_delete(self): self.vote.save() # public cannot delete a vote self.assertFalse(self.vote.can_delete()) # voter can delete own vote self.assertTrue(self.vote.can_delete(user=self.user)) # other users cannot delete another user's vote self.assertFalse(self.vote.can_delete(user=self.staff_user)) self.assertFalse(self.vote.can_delete(user=self.super_user)) def test_can_delete_queryset(self): exhibition1 = Exhibition.objects.create( title="Student Exhibition", description="description goes here", author=self.user ) submission1 = Submission.objects.create(exhibition=exhibition1, artwork=self.artwork, submitted_by=self.user) exhibition2 = Exhibition.objects.create( title="Staff Exhibition", description="description goes here", author=self.user ) submission2 = Submission.objects.create( exhibition=exhibition2, artwork=self.artwork, submitted_by=self.staff_user ) student_vote1 = Vote.objects.create(submission=submission1, status=Vote.THUMBS_UP, voted_by=self.user) staff_vote1 = Vote.objects.create(submission=submission1, status=Vote.THUMBS_UP, voted_by=self.staff_user) super_vote1 = Vote.objects.create(submission=submission1, status=Vote.THUMBS_UP, voted_by=self.super_user) student_vote2 = Vote.objects.create(submission=submission2, status=Vote.THUMBS_UP, voted_by=self.user) staff_vote2 = Vote.objects.create(submission=submission2, status=Vote.THUMBS_UP, voted_by=self.staff_user) super_vote2 = Vote.objects.create(submission=submission2, status=Vote.THUMBS_UP, voted_by=self.super_user) # public can't delete any votes public_qs = Vote.can_delete_queryset() self.assertEquals(0, len(public_qs.all())) # students can delete own votes student_qs = Vote.can_delete_queryset(user=self.user) self.assertEquals(2, len(student_qs.all())) self.assertTrue(student_qs.all()[0].can_delete(user=self.user)) self.assertTrue(student_qs.all()[1].can_delete(user=self.user)) student_sub1_qs = Vote.can_delete_queryset(user=self.user, submission=submission1) self.assertEquals(1, len(student_sub1_qs.all())) self.assertEquals(student_vote1.id, student_sub1_qs.all()[0].id) self.assertTrue(student_sub1_qs.all()[0].can_delete(user=self.user)) student_sub2_qs = Vote.can_delete_queryset(user=self.user, submission=submission2) self.assertEquals(1, len(student_sub2_qs.all())) self.assertEquals(student_vote2.id, student_sub2_qs.all()[0].id) self.assertTrue(student_sub2_qs.all()[0].can_delete(user=self.user)) student_subs_qs = Vote.can_delete_queryset(user=self.user, submission=[submission1.id, submission2.id]) self.assertEquals(2, len(student_subs_qs.all())) self.assertEquals(student_vote1.id, student_subs_qs.all()[0].id) self.assertEquals(student_vote2.id, student_subs_qs.all()[1].id) self.assertTrue(student_subs_qs.all()[0].can_delete(user=self.user)) self.assertTrue(student_subs_qs.all()[1].can_delete(user=self.user)) student_exh1_qs = Vote.can_delete_queryset(user=self.user, exhibition=exhibition1) self.assertEquals(1, len(student_exh1_qs.all())) self.assertEquals(student_vote1.id, student_exh1_qs.all()[0].id) self.assertTrue(student_qs.all()[0].can_delete(user=self.user)) student_exh2_qs = Vote.can_delete_queryset(user=self.user, exhibition=exhibition2) self.assertEquals(1, len(student_exh2_qs.all())) self.assertEquals(student_vote2.id, student_exh2_qs.all()[0].id) self.assertTrue(student_exh2_qs.all()[0].can_delete(user=self.user)) # staff can delete own votes staff_qs = Vote.can_delete_queryset(user=self.staff_user) self.assertEquals(2, len(staff_qs.all())) self.assertTrue(staff_qs.all()[0].can_delete(user=self.staff_user)) self.assertTrue(staff_qs.all()[1].can_delete(user=self.staff_user)) staff_sub1_qs = Vote.can_delete_queryset(user=self.staff_user, submission=submission1) self.assertEquals(1, len(staff_sub1_qs.all())) self.assertEquals(staff_vote1.id, staff_sub1_qs.all()[0].id) self.assertTrue(staff_sub1_qs.all()[0].can_delete(user=self.staff_user)) staff_sub2_qs = Vote.can_delete_queryset(user=self.staff_user, submission=submission2) self.assertEquals(1, len(staff_sub2_qs.all())) self.assertEquals(staff_vote2.id, staff_sub2_qs.all()[0].id) self.assertTrue(staff_sub2_qs.all()[0].can_delete(user=self.staff_user)) staff_subs_qs = Vote.can_delete_queryset(user=self.staff_user, submission=[submission1.id, submission2.id]) self.assertEquals(2, len(staff_subs_qs.all())) self.assertEquals(staff_vote1.id, staff_subs_qs.all()[0].id) self.assertEquals(staff_vote2.id, staff_subs_qs.all()[1].id) self.assertTrue(staff_subs_qs.all()[0].can_delete(user=self.staff_user)) self.assertTrue(staff_subs_qs.all()[1].can_delete(user=self.staff_user)) staff_exh1_qs = Vote.can_delete_queryset(user=self.staff_user, exhibition=exhibition1) self.assertEquals(1, len(staff_exh1_qs.all())) self.assertEquals(staff_vote1.id, staff_exh1_qs.all()[0].id) self.assertTrue(staff_exh1_qs.all()[0].can_delete(user=self.staff_user)) staff_exh2_qs = Vote.can_delete_queryset(user=self.staff_user, exhibition=exhibition2) self.assertEquals(1, len(staff_exh2_qs.all())) self.assertEquals(staff_vote2.id, staff_exh2_qs.all()[0].id) self.assertTrue(staff_exh2_qs.all()[0].can_delete(user=self.staff_user)) # superusers can delete own votes super_qs = Vote.can_delete_queryset(user=self.super_user) self.assertEquals(2, len(super_qs.all())) self.assertTrue(super_qs.all()[0].can_delete(user=self.super_user)) self.assertTrue(super_qs.all()[1].can_delete(user=self.super_user)) super_sub1_qs = Vote.can_delete_queryset(user=self.super_user, submission=submission1) self.assertEquals(1, len(super_sub1_qs.all())) self.assertEquals(super_vote1.id, super_sub1_qs.all()[0].id) self.assertTrue(super_sub1_qs.all()[0].can_delete(user=self.super_user)) super_sub2_qs = Vote.can_delete_queryset(user=self.super_user, submission=submission2) self.assertEquals(1, len(super_sub2_qs.all())) self.assertEquals(super_vote2.id, super_sub2_qs.all()[0].id) self.assertTrue(super_sub2_qs.all()[0].can_delete(user=self.super_user)) super_subs_qs = Vote.can_delete_queryset(user=self.super_user, submission=[submission1.id, submission2.id]) self.assertEquals(2, len(super_subs_qs.all())) self.assertEquals(super_vote1.id, super_subs_qs.all()[0].id) self.assertEquals(super_vote2.id, super_subs_qs.all()[1].id) self.assertTrue(super_subs_qs.all()[0].can_delete(user=self.super_user)) self.assertTrue(super_subs_qs.all()[1].can_delete(user=self.super_user)) super_exh1_qs = Vote.can_delete_queryset(user=self.super_user, exhibition=exhibition1) self.assertEquals(1, len(super_exh1_qs.all())) self.assertEquals(super_vote1.id, super_exh1_qs.all()[0].id) self.assertTrue(super_exh1_qs.all()[0].can_delete(user=self.super_user)) super_exh2_qs = Vote.can_delete_queryset(user=self.super_user, exhibition=exhibition2) self.assertEquals(1, len(super_exh2_qs.all())) self.assertEquals(super_vote2.id, super_exh2_qs.all()[0].id) self.assertTrue(super_exh2_qs.all()[0].can_delete(user=self.super_user))
class VoteTests(UserSetUp, TestCase): """Vote model tests.""" def setUp(self): super(VoteTests, self).setUp() self.exhibition = Exhibition.objects.create( title='New Exhibition', description='description goes here', author=self.user) self.artwork = Artwork.objects.create(title='New Artwork', code='// code goes here', author=self.user) self.submission = Submission.objects.create(exhibition=self.exhibition, artwork=self.artwork, submitted_by=self.user) self.vote = Vote(submission=self.submission, status=Vote.THUMBS_UP, voted_by=self.user) def test_str(self): self.assertEquals(str(self.vote), 'New Exhibition :: New Artwork :: Thumbs Up') def test_str_unset(self): unset_vote = Vote() self.assertEquals(str(unset_vote), 'unset :: none') def test_thumbs_up_score(self): self.assertEqual(self.submission.score, 0) # Creating a thumbs up vote increments submission.score self.vote.save() self.submission = Submission.objects.get(id=self.submission.id) self.assertEqual(self.submission.score, 1) # Editing a thumbs up vote does not change submission.score self.vote.voted_by = self.staff_user self.vote.save() self.submission = Submission.objects.get(id=self.submission.id) self.assertEqual(self.submission.score, 1) # Deleting a thumbs up vote decrements submission.score self.vote.delete() self.submission = Submission.objects.get(id=self.submission.id) self.assertEqual(self.submission.score, 0) def test_save_unique(self): # one vote per submission per user self.assertEqual(self.vote.save(), None) vote2 = Vote(submission=self.submission, voted_by=self.user, status=Vote.THUMBS_UP) self.assertRaisesRegexp( IntegrityError, 'columns submission_id, voted_by_id are not unique', vote2.save) def test_can_delete(self): self.vote.save() # public cannot delete a vote self.assertFalse(self.vote.can_delete()) # voter can delete own vote self.assertTrue(self.vote.can_delete(user=self.user)) # other users cannot delete another user's vote self.assertFalse(self.vote.can_delete(user=self.staff_user)) self.assertFalse(self.vote.can_delete(user=self.super_user)) def test_can_delete_queryset(self): exhibition1 = Exhibition.objects.create( title='Student Exhibition', description='description goes here', author=self.user) submission1 = Submission.objects.create(exhibition=exhibition1, artwork=self.artwork, submitted_by=self.user) exhibition2 = Exhibition.objects.create( title='Staff Exhibition', description='description goes here', author=self.user) submission2 = Submission.objects.create(exhibition=exhibition2, artwork=self.artwork, submitted_by=self.staff_user) student_vote1 = Vote.objects.create(submission=submission1, status=Vote.THUMBS_UP, voted_by=self.user) staff_vote1 = Vote.objects.create(submission=submission1, status=Vote.THUMBS_UP, voted_by=self.staff_user) super_vote1 = Vote.objects.create(submission=submission1, status=Vote.THUMBS_UP, voted_by=self.super_user) student_vote2 = Vote.objects.create(submission=submission2, status=Vote.THUMBS_UP, voted_by=self.user) staff_vote2 = Vote.objects.create(submission=submission2, status=Vote.THUMBS_UP, voted_by=self.staff_user) super_vote2 = Vote.objects.create(submission=submission2, status=Vote.THUMBS_UP, voted_by=self.super_user) # public can't delete any votes public_qs = Vote.can_delete_queryset() self.assertEquals(0, len(public_qs.all())) # students can delete own votes student_qs = Vote.can_delete_queryset(user=self.user) self.assertEquals(2, len(student_qs.all())) self.assertTrue(student_qs.all()[0].can_delete(user=self.user)) self.assertTrue(student_qs.all()[1].can_delete(user=self.user)) student_sub1_qs = Vote.can_delete_queryset(user=self.user, submission=submission1) self.assertEquals(1, len(student_sub1_qs.all())) self.assertEquals(student_vote1.id, student_sub1_qs.all()[0].id) self.assertTrue(student_sub1_qs.all()[0].can_delete(user=self.user)) student_sub2_qs = Vote.can_delete_queryset(user=self.user, submission=submission2) self.assertEquals(1, len(student_sub2_qs.all())) self.assertEquals(student_vote2.id, student_sub2_qs.all()[0].id) self.assertTrue(student_sub2_qs.all()[0].can_delete(user=self.user)) student_subs_qs = Vote.can_delete_queryset( user=self.user, submission=[submission1.id, submission2.id]) self.assertEquals(2, len(student_subs_qs.all())) self.assertEquals(student_vote1.id, student_subs_qs.all()[0].id) self.assertEquals(student_vote2.id, student_subs_qs.all()[1].id) self.assertTrue(student_subs_qs.all()[0].can_delete(user=self.user)) self.assertTrue(student_subs_qs.all()[1].can_delete(user=self.user)) student_exh1_qs = Vote.can_delete_queryset(user=self.user, exhibition=exhibition1) self.assertEquals(1, len(student_exh1_qs.all())) self.assertEquals(student_vote1.id, student_exh1_qs.all()[0].id) self.assertTrue(student_qs.all()[0].can_delete(user=self.user)) student_exh2_qs = Vote.can_delete_queryset(user=self.user, exhibition=exhibition2) self.assertEquals(1, len(student_exh2_qs.all())) self.assertEquals(student_vote2.id, student_exh2_qs.all()[0].id) self.assertTrue(student_exh2_qs.all()[0].can_delete(user=self.user)) # staff can delete own votes staff_qs = Vote.can_delete_queryset(user=self.staff_user) self.assertEquals(2, len(staff_qs.all())) self.assertTrue(staff_qs.all()[0].can_delete(user=self.staff_user)) self.assertTrue(staff_qs.all()[1].can_delete(user=self.staff_user)) staff_sub1_qs = Vote.can_delete_queryset(user=self.staff_user, submission=submission1) self.assertEquals(1, len(staff_sub1_qs.all())) self.assertEquals(staff_vote1.id, staff_sub1_qs.all()[0].id) self.assertTrue( staff_sub1_qs.all()[0].can_delete(user=self.staff_user)) staff_sub2_qs = Vote.can_delete_queryset(user=self.staff_user, submission=submission2) self.assertEquals(1, len(staff_sub2_qs.all())) self.assertEquals(staff_vote2.id, staff_sub2_qs.all()[0].id) self.assertTrue( staff_sub2_qs.all()[0].can_delete(user=self.staff_user)) staff_subs_qs = Vote.can_delete_queryset( user=self.staff_user, submission=[submission1.id, submission2.id]) self.assertEquals(2, len(staff_subs_qs.all())) self.assertEquals(staff_vote1.id, staff_subs_qs.all()[0].id) self.assertEquals(staff_vote2.id, staff_subs_qs.all()[1].id) self.assertTrue( staff_subs_qs.all()[0].can_delete(user=self.staff_user)) self.assertTrue( staff_subs_qs.all()[1].can_delete(user=self.staff_user)) staff_exh1_qs = Vote.can_delete_queryset(user=self.staff_user, exhibition=exhibition1) self.assertEquals(1, len(staff_exh1_qs.all())) self.assertEquals(staff_vote1.id, staff_exh1_qs.all()[0].id) self.assertTrue( staff_exh1_qs.all()[0].can_delete(user=self.staff_user)) staff_exh2_qs = Vote.can_delete_queryset(user=self.staff_user, exhibition=exhibition2) self.assertEquals(1, len(staff_exh2_qs.all())) self.assertEquals(staff_vote2.id, staff_exh2_qs.all()[0].id) self.assertTrue( staff_exh2_qs.all()[0].can_delete(user=self.staff_user)) # superusers can delete own votes super_qs = Vote.can_delete_queryset(user=self.super_user) self.assertEquals(2, len(super_qs.all())) self.assertTrue(super_qs.all()[0].can_delete(user=self.super_user)) self.assertTrue(super_qs.all()[1].can_delete(user=self.super_user)) super_sub1_qs = Vote.can_delete_queryset(user=self.super_user, submission=submission1) self.assertEquals(1, len(super_sub1_qs.all())) self.assertEquals(super_vote1.id, super_sub1_qs.all()[0].id) self.assertTrue( super_sub1_qs.all()[0].can_delete(user=self.super_user)) super_sub2_qs = Vote.can_delete_queryset(user=self.super_user, submission=submission2) self.assertEquals(1, len(super_sub2_qs.all())) self.assertEquals(super_vote2.id, super_sub2_qs.all()[0].id) self.assertTrue( super_sub2_qs.all()[0].can_delete(user=self.super_user)) super_subs_qs = Vote.can_delete_queryset( user=self.super_user, submission=[submission1.id, submission2.id]) self.assertEquals(2, len(super_subs_qs.all())) self.assertEquals(super_vote1.id, super_subs_qs.all()[0].id) self.assertEquals(super_vote2.id, super_subs_qs.all()[1].id) self.assertTrue( super_subs_qs.all()[0].can_delete(user=self.super_user)) self.assertTrue( super_subs_qs.all()[1].can_delete(user=self.super_user)) super_exh1_qs = Vote.can_delete_queryset(user=self.super_user, exhibition=exhibition1) self.assertEquals(1, len(super_exh1_qs.all())) self.assertEquals(super_vote1.id, super_exh1_qs.all()[0].id) self.assertTrue( super_exh1_qs.all()[0].can_delete(user=self.super_user)) super_exh2_qs = Vote.can_delete_queryset(user=self.super_user, exhibition=exhibition2) self.assertEquals(1, len(super_exh2_qs.all())) self.assertEquals(super_vote2.id, super_exh2_qs.all()[0].id) self.assertTrue( super_exh2_qs.all()[0].can_delete(user=self.super_user))