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 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 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(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 get_context_data(self, **kwargs): context = super(ListArtworkView, self).get_context_data(**kwargs) user_model = get_user_model() author_id = self._get_author_id() try: author = user_model.objects.get(id=author_id) except user_model.DoesNotExist: author = None context['author'] = author context['shared'] = self._get_shared() # Fetch submissions for these artworks artwork_ids = [a.id for a in context['object_list']] submissions = Submission.objects.filter( artwork__id__in=artwork_ids).all() context['submissions'] = {s.artwork_id: s for s in submissions} # Fetch votes for these submissions submission_ids = [s.id for s in submissions] votes = Vote.can_delete_queryset(user=self.request.user, submission=submission_ids).all() context['votes'] = {v.submission_id: v for v in votes} # Store url for downloading code zip file url_name = self.request.resolver_match.url_name if not 'zip' in url_name: url_name = '%s-zip' % url_name kwargs = self.kwargs.copy() kwargs['shared'] = context['shared'] context['zip_file_url'] = reverse(url_name, kwargs=kwargs) return context
def create(self, validated_data): if not self.context['request']: raise ValueError('You should set context["reqeust"]!') questions = validated_data.pop('questions') password = validated_data.pop('password', None) tags = validated_data.pop('tags') creator: AbstractBaseUser = self.context['request'].user if (password and creator.is_authenticated) or ( not password and not creator.is_authenticated): raise ValidationError( 'You should specify password when not logged in. Otherwise, you should "NOT" specify password!' ) vote: Vote = Vote(**validated_data) vote.set_questions(questions) if password: vote.set_password(password) elif creator: vote.creator = creator vote.tags = ','.join(tags) vote.save() return vote
def can_vote(self, user=None): from votes.models import Vote if (user and user.is_authenticated() and self.exhibition.can_see(user) and not Vote.can_delete_queryset(user=user, submission=self)): return True return False
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 home(request): #Les 7 lignes a venir sont un peu degueu homes = Edito.objects.all() if homes: edito_title = homes[0].edito_title edito = homes[0].edito else: edito_title = '' edito = '' groups = list(MEP.view('meps/groups', group=True)) groups.sort(key=lambda group: group['value']['count'], reverse=True) countries = list(MEP.view('meps/countries', group=True)) countries.sort(key=lambda group: group['value']['count'], reverse=True) votes = Vote.view('votes/all', descending=True) context = { 'groups': groups, 'countries': countries, 'votes': votes, 'edito_title': edito_title, 'edito' : edito } return direct_to_template(request, 'home.html', context)
def get_context_data(self, **kwargs): context = super(ListArtworkView, self).get_context_data(**kwargs) user_model = get_user_model() author_id = self._get_author_id() try: author = user_model.objects.get(id=author_id) except user_model.DoesNotExist: author = None context['author'] = author context['shared'] = self._get_shared() # Fetch submissions for these artworks artwork_ids = [ a.id for a in context['object_list']] submissions = Submission.objects.filter(artwork__id__in=artwork_ids).all() context['submissions'] = { s.artwork_id:s for s in submissions } # Fetch votes for these submissions submission_ids = [ s.id for s in submissions ] votes = Vote.can_delete_queryset(user=self.request.user, submission=submission_ids).all() context['votes'] = { v.submission_id:v for v in votes } # Store url for downloading code zip file url_name = self.request.resolver_match.url_name if not 'zip' in url_name: url_name = '%s-zip' % url_name kwargs = self.kwargs.copy() kwargs['shared'] = context['shared'] context['zip_file_url'] = reverse(url_name, kwargs=kwargs) return context
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 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_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 get_context_data(self, **kwargs): context = super(ListSubmissionView, self).get_context_data(**kwargs) context['order'] = self._get_order_by() # Include in the current user's votes for this exhibition # as a dict of submission.id:vote exhibition = self._get_exhibition_id() votes = Vote.can_delete_queryset(user=self.request.user, exhibition=exhibition).all() context['votes'] = { v.submission_id:v for v in votes } # Store url for downloading code zip file if self.show_download: url_name = self.request.resolver_match.url_name if not 'zip' in url_name: url_name = '%s-zip' % url_name kwargs = self.kwargs.copy() context['zip_file_url'] = reverse(url_name, kwargs=kwargs) return context
def get_context_data(self, **kwargs): context = super(ListSubmissionView, self).get_context_data(**kwargs) context['order'] = self._get_order_by() # Include in the current user's votes for this exhibition # as a dict of submission.id:vote exhibition = self._get_exhibition_id() votes = Vote.can_delete_queryset(user=self.request.user, exhibition=exhibition).all() context['votes'] = {v.submission_id: v for v in votes} # Store url for downloading code zip file if self.show_download: url_name = self.request.resolver_match.url_name if not 'zip' in url_name: url_name = '%s-zip' % url_name kwargs = self.kwargs.copy() context['zip_file_url'] = reverse(url_name, kwargs=kwargs) return context
def get_context_data(self, **kwargs): context = super(ShowSubmissionView, self).get_context_data(**kwargs) submission = self.get_object() # Include share url pk = submission.id context['share_url'] = ShareView.reverse_share_url( 'submission-view', kwargs={'pk': pk}) # Include in the current user's votes for this submission # as a dict of submission.id:vote votes = Vote.can_delete_queryset(user=self.request.user, submission=pk).all() context['votes'] = { v.submission_id:v for v in votes } context['DISQUS_IDENTIFIER'] = submission.disqus_identifier context['DISQUS_TITLE'] = '%s' % submission context['DISQUS_URL'] = self.request.build_absolute_uri(submission.get_absolute_url()) return context
def get_context_data(self, **kwargs): context = super(ShowSubmissionView, self).get_context_data(**kwargs) submission = self.get_object() # Include share url pk = submission.id context['share_url'] = ShareView.reverse_share_url('submission-view', kwargs={'pk': pk}) # Include in the current user's votes for this submission # as a dict of submission.id:vote votes = Vote.can_delete_queryset(user=self.request.user, submission=pk).all() context['votes'] = {v.submission_id: v for v in votes} context['DISQUS_IDENTIFIER'] = submission.disqus_identifier context['DISQUS_TITLE'] = '%s' % submission context['DISQUS_URL'] = self.request.build_absolute_uri( submission.get_absolute_url()) return context
def notify_of_vote(created, **kwargs): vote = kwargs.get("instance") recipient = vote.content.user if created and vote.user != recipient: # Votes on proposals if vote.content_type == Proposal.get_content_type(): if vote.isVoteUp: type = "proposal_vote_up" else: type = "proposal_vote_down" # Votes on comments elif vote.content_type == Comment.get_content_type(): if vote.isVoteUp: type = "comment_vote_up" else: type = "comment_vote_down" Notification.objects.create(recipient=recipient, type=type, content_type=Vote.get_content_type(), object_id=vote.id)
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))
def votes_update(message): place_id = message.content.get('place_id') username = message.content.get('username') Vote.update(place_id, username)
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))
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 test_str_unset(self): unset_vote = Vote() self.assertEquals(str(unset_vote), 'unset :: none')
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 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))
def index(request): votes = Vote.view('votes/all') context = { 'votes': votes, } return direct_to_template(request, 'votes/index.html', context)
def detail(request, vote_name): votes = Vote.view('votes/by_name', key=vote_name) context = { 'vote': votes.first(), } return direct_to_template(request, 'votes/detail.html', context)
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))
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_update(self, place, username): assert not Vote.objects.exists() Vote.update(place.id, username) assert Vote.objects.exists()