def test_vote_get(self): r = self._req() l = Link.objects.create(url='http://google.com') views.vote(r, Link, l.id, 1) resp = views.vote(r, Link, l.id, None) self.assertEqual(resp.status_code, 200) assert json.loads(resp.content.decode('utf8'))['num_votes'] == 1
def like(request, content_type, id, vote, template_name='likes/inclusion_tags/likes.html', can_vote_test=can_vote_test): # Crawlers will follow the like link if anonymous liking is enabled. They # typically do not have referrer set. if 'HTTP_REFERER' not in request.META: return HttpResponseNotFound() url_friendly_content_type = content_type content_type = content_type.replace("-", ".") if request.is_ajax(): return views.vote( request, content_type=content_type, object_id=id, vote=int(vote), template_name=template_name, can_vote_test=can_vote_test, extra_context={ 'likes_enabled': True, 'can_vote': False, "content_type": url_friendly_content_type } ) else: # Redirect to referer but append unique number(determined # from global vote count) to end of URL to bypass local cache. redirect_url = '%s?v=%s' % (request.META['HTTP_REFERER'], \ Vote.objects.count() + 1) return views.vote( request, content_type=content_type, object_id=id, vote=int(vote), redirect_url=redirect_url, can_vote_test=can_vote_test )
def annotation_vote(request, annotation_id, vote_score): vote_score = int(vote_score) if vote_score == 2: vote_score = -1 vote(request, Annotation, int(annotation_id), vote_score) annotation = Annotation.objects.get(id=annotation_id) return HttpResponse(json.dumps(annotation.to_dict()), content_type="application/json")
def like(request, content_type, id, vote): url_friendly_content_type = content_type content_type = content_type.replace("-", ".") if request.is_ajax(): return views.vote(request, content_type=content_type, object_id=id, vote=vote, template_name='likes/inclusion_tags/likes.html', can_vote_test=can_vote, extra_context={'can_vote': False, 'vote_status': 'voted', "content_type": url_friendly_content_type}) else: redirect_url = request.META['HTTP_REFERER'] return views.vote(request, content_type=content_type, object_id=id, vote=vote, redirect_url=redirect_url, can_vote_test=can_vote)
def post(self, request, *args, **kwargs): self.object = self.get_object() object_id = self.kwargs.get("enemy_id") content_type = ContentType.objects.get_for_model(self.model) print(only_one_vote(request, content_type, object_id, vote=+1)) if only_one_vote(request, content_type, object_id, vote=+1): vote(request, content_type, object_id, vote=+1) return redirect(self.get_success_url())
def save(self): poll_option = self.cleaned_data['poll_option'] content_type = ContentType.objects.get(app_label='poll', model='polloption') vote(self.request, content_type, poll_option, 1) poll_voted.send(sender=self.__class__, poll_option=poll_option, request=self.request)
def post_vote_reset(request, post_id): p = get_object_or_404(Post, pk=post_id) if p.post_meshblock is not None: vote(request, Meshblock, p.post_meshblock.id, 0) return vote(request, Post, post_id, 0, redirect_url=request.META.get('HTTP_REFERER'))
def test_string_content_type(self): r = self._req() l = Link.objects.create(url='https://google.com') views.vote(r, 'tests.Link', l.id, 1) assert Link.objects.get().vote_total == 1 # Test with custom manager name other_link = AnotherLink.objects.create(url='https://google.com') views.vote(r, 'tests.AnotherLink', other_link.id, 1) assert AnotherLink.ballot_custom_manager.get().vote_total == 1
def test_can_vote_test(self): r = self._req() l = Link.objects.create(url='https://google.com') def can_vote_test(request, content_type, object_id, vote): return True views.vote(r, Link, 1, 1, can_vote_test=can_vote_test) def never(request, content_type, object_id, vote): return False forbidden = views.vote(r, Link, 1, 1, can_vote_test=never) self.assertEquals(forbidden.status_code, 403)
def test_can_vote_test(self): r = self._req() l = Link.objects.create(url='http://google.com') def can_vote_test(request, content_type, object_id, vote): return True views.vote(r, Link, 1, 1, can_vote_test=can_vote_test) def never(request, content_type, object_id, vote): return False forbidden = views.vote(r, Link, 1, 1, can_vote_test=never) self.assertEquals(forbidden.status_code, 403)
def post_vote_up(request, post_id): # get object for purpose of meshblock voting p = get_object_or_404(Post, pk=post_id) if p.post_meshblock is not None: vote(request, Meshblock, p.post_meshblock.id, +1) return vote(request, Post, post_id, +1, redirect_url=request.META.get('HTTP_REFERER'))
def test_string_content_type(self): r = self._req() l = Link.objects.create(url='http://google.com') views.vote(r, 'tests.Link', l.id, 1) assert Link.objects.get().vote_total == 1 # Test with custom manager name other_link = AnotherLink.objects.create(url='http://google.com') views.vote(r, 'tests.AnotherLink', other_link.id, 1) assert AnotherLink.ballot_custom_manager.get().vote_total == 1
def like(request, content_type, id, vote): # Crawlers will follow the like link if anonymous liking is enabled. They # typically do not have referrer set. if 'HTTP_REFERER' not in request.META: return HttpResponseNotFound() url_friendly_content_type = content_type app, modelname = content_type.split('-') #ip_address = request.secretballot_token content_type = ContentType.objects.get(app_label=app, model__iexact=modelname) if request.is_ajax(): likes_template = 'likes_%s.html' % modelname.lower() try: template.loader.get_template(likes_template) except template.TemplateDoesNotExist: likes_template = 'likes.html' response = views.vote( request, content_type=content_type, object_id=id, vote=vote, template_name=likes_template, can_vote_test=can_vote_test, extra_context={ 'likes_enabled': True, 'can_vote': False, "content_type": url_friendly_content_type, } ) else: # Redirect to referer but append unique number(determined # from global vote count) to end of URL to bypass local cache. redirect_url = '%s?v=%s' % (request.META['HTTP_REFERER'], random.randint(0, 10)) response = views.vote( request, content_type=content_type, object_id=id, vote=vote, redirect_url=redirect_url, can_vote_test=can_vote_test, #ip_address=ip_address ) signals.object_liked.send(sender=content_type.model_class(), instance=content_type.get_object_for_this_type(id=id), request=request) return response
def like(request, content_type, id): """ Handles likes on a model object. """ # check the content_type if isinstance(content_type, ContentType): pass elif isinstance(content_type, ModelBase): content_type = ContentType.objects.get_for_model(content_type) elif isinstance(content_type, basestring) and '-' in content_type: app, modelname = content_type.split('-') content_type = ContentType.objects.get(app_label=app, model__iexact=modelname) else: content_type = ContentType.objects.get(app_label='be_local_server', model__iexact=content_type) if request.method == 'POST': response = views.vote( request, content_type = content_type, object_id = id, vote = '+1', mimetype='application/json' ) # JSON formatting response.content = response.content.replace("'","\"") return response if request.method == 'DELETE': response = views.vote( request, content_type = content_type, object_id = id, vote=None, mimetype='application/json' ) # JSON formatting response.content = response.content.replace("'","\"") return response if request.method == 'GET': vote = content_type.model_class().objects.from_request(request).get(pk=id).user_vote if (vote): body = '{"is_liked": true}' return HttpResponse(body, status=status.HTTP_200_OK, content_type='application/json') else: body = '{"is_liked": false}' return HttpResponse(body, status=status.HTTP_404_NOT_FOUND, content_type='application/json')
def test_vote_template(self): r = self._req() l = Link.objects.create(url='http://google.com') resp = views.vote(r, Link, l.id, 1, template_name='vote.html') self.assertEqual(resp.status_code, 200) self.assertIn(b'voted', resp.content) self.assertIn(b'total_upvotes=1', resp.content)
def like(request, content_type, id, vote): # Crawlers will follow the like link if anonymous liking is enabled. They # typically do not have referrer set. if 'HTTP_REFERER' not in request.META: return HttpResponseNotFound() url_friendly_content_type = content_type app, modelname = content_type.split('-') content_type = ContentType.objects.get(app_label=app, model__iexact=modelname) if request.is_ajax(): likes_template = 'likes/inclusion_tags/likes_%s.html' % modelname.lower() try: template.loader.get_template(likes_template) except template.TemplateDoesNotExist: likes_template = 'likes/inclusion_tags/likes.html' response = views.vote( request, content_type=content_type, object_id=id, vote=vote, template_name=likes_template, can_vote_test=can_vote_test, extra_context={ 'likes_enabled': True, 'can_vote': False, "content_type": url_friendly_content_type, } ) else: # Redirect to referer but append unique number(determined # from global vote count) to end of URL to bypass local cache. redirect_url = '%s?v=%s' % (request.META['HTTP_REFERER'], random.randint(0, 10)) response = views.vote( request, content_type=content_type, object_id=id, vote=vote, redirect_url=redirect_url, can_vote_test=can_vote_test ) signals.object_liked.send(sender=content_type.model_class(), instance=content_type.get_object_for_this_type(id=id), request=request) return response
def test_vote_delete(self): r = self._req() l = Link.objects.create(url='http://google.com') views.vote(r, Link, l.id, 1) views.vote(r, Link, l.id, 0) # delete assert Link.objects.get().vote_total == 0 # Test with custom manager other_link = AnotherLink.objects.create(url='http://google.com') views.vote(r, AnotherLink, other_link.id, 1) views.vote(r, AnotherLink, other_link.id, 0) # update assert AnotherLink.ballot_custom_manager.get().vote_total == 0
def test_vote_delete(self): r = self._req() l = Link.objects.create(url='https://google.com') views.vote(r, Link, l.id, 1) views.vote(r, Link, l.id, 0) # delete assert Link.objects.get().vote_total == 0 # Test with custom manager other_link = AnotherLink.objects.create(url='https://google.com') views.vote(r, AnotherLink, other_link.id, 1) views.vote(r, AnotherLink, other_link.id, 0) # update assert AnotherLink.ballot_custom_manager.get().vote_total == 0
def like(request, content_type, id, vote): # Crawlers will follow the like link if anonymous liking is enabled. They # typically do not have referrer set. if 'HTTP_REFERER' not in request.META: return HttpResponseNotFound() url_friendly_content_type = content_type app, modelname = content_type.split('-') content_type = ContentType.objects.get(app_label=app, model__iexact=modelname) if request.is_ajax(): response = views.vote( request, content_type=content_type, object_id=id, vote=vote, template_name='likes/inclusion_tags/likes.html', can_vote_test=can_vote_test, extra_context={ 'likes_enabled': True, 'can_vote': False, "content_type": url_friendly_content_type } ) else: # Redirect to referer but append unique number(determined # from global vote count) to end of URL to bypass local cache. redirect_url = '%s?v=%s' % (request.META['HTTP_REFERER'], \ Vote.objects.count() + 1) response = views.vote( request, content_type=content_type, object_id=id, vote=vote, redirect_url=redirect_url, can_vote_test=can_vote_test ) signals.object_liked.send(sender=content_type.model_class(), instance=content_type.get_object_for_this_type(id=id), request=request) return response
def vote_ajax(request): html_return = '' if 'value' in request.GET and 'type' in request.GET and request.GET[ 'type'] == 'question': try: message = VotaInteligenteMessage.objects.get( message_ptr_id=request.GET['value']) return vote(request, VotaInteligenteMessage, request.GET['value'], 1) except ObjectDoesNotExist: return HttpResponse('veuillez votez correctement') elif 'value' in request.GET and 'type' in request.GET and request.GET[ 'type'] == 'reponse': try: message = VotaInteligenteAnswer.objects.get( id=request.GET['value']) return vote(request, VotaInteligenteAnswer, request.GET['value'], 1) except ObjectDoesNotExist: return HttpResponse('veuillez votez correctement') else: return HttpResponse('veuillez votez correctement')
def like(request, content_type, id, vote): if "HTTP_REFERER" not in request.META: return HttpResponseNotFound() url_friendly_content_type = content_type app, modelname = content_type.split("-") content_type = ContentType.objects.get(app_label= app, model__iexact=modelname) if request.is_ajax(): cclikes_template = "cclikes/inclusion_tags/cclikes_%s.html" try: template.loader.get_template(cclikes_template) except template.TemplateDoesNotExist: cclikes_template = "cclikes/inclusion_tags/cclikes.html" response = views.Vote( request, content_type=content_type, object_id=id, vote=vote, template_name=cclikes_template, can_vote_test=can_vote_test, extra_context={ "likes_enabled": True, "can_vote": False, "content_type": url_friendly_content_type, } ) else: redirect_url = "%s?v=%s" % (request.META["HTTP_REFERER"], random.randint(0, 10)) response = views.vote( request, content_type=content_type, object_id=id, vote=vote, redirect_url=redirect_url, can_vote_test=can_vote_test ) signals.object_liked.send(sender=content_type.model_class(), instance=content_type.get_object_for_this_type(id=id), request=request) return response
def test_content_type_content_type(self): r = self._req() l = Link.objects.create(url='http://google.com') ctype = ContentType.objects.get(model='link') views.vote(r, ctype, l.id, 1) assert Link.objects.get().vote_total == 1
def test_string_content_type(self): r = self._req() l = Link.objects.create(url='http://google.com') views.vote(r, 'tests.Link', l.id, 1) assert Link.objects.get().vote_total == 1
def test_report_comment_abuse(self): # Prepare context. context = Context() request = RequestFactory().get('/') request.user = AnonymousUser() request.META['HTTP_USER_AGENT'] = 'testing_agent' request.secretballot_token = SecretBallotUserIpUseragentMiddleware().\ generate_token(request) comment = Comment.objects.create( content_type_id=1, site_id=1, comment="abuse report testing comment" ) context['request'] = request context['comment'] = comment # Without having actioned anything on the comment the # Report Abuse action should be rendered. out = Template("{% load moderator_inclusion_tags %}" "{% report_comment_abuse comment %}").render(context) self.failUnless('Report Abuse' in out) # Like a comment. views.vote( request, content_type='.'.join((comment._meta.app_label, comment._meta.module_name)), object_id=comment.id, vote=1, redirect_url='/', can_vote_test=can_vote_test ) # Reset previous like and test it applied. Vote.objects.all().delete() # Without having actioned anything on the comment the # Report Abuse action should be rendered. out = Template("{% load moderator_inclusion_tags %}" "{% report_comment_abuse comment %}").render(context) self.failUnless('Report Abuse' in out) # Dislike/report an abuse comment. views.vote( request, content_type='.'.join((comment._meta.app_label, comment._meta.module_name)), object_id=comment.id, vote=-1, redirect_url='/', can_vote_test=can_vote_test ) self.assertEqual(Vote.objects.all().count(), 1) #repeat votes should not count views.vote( request, content_type='.'.join((comment._meta.app_label, comment._meta.module_name)), object_id=comment.id, vote=-1, redirect_url='/', can_vote_test=can_vote_test ) self.assertEqual(Vote.objects.all().count(), 1)
def test_vote_delete(self): r = self._req() l = Link.objects.create(url='http://google.com') views.vote(r, Link, l.id, 1) views.vote(r, Link, l.id, 0) # delete assert Link.objects.get().vote_total == 0
def comment_vote_reset(request, comment_id): return vote(request, Comment, comment_id, 0, redirect_url=request.META.get('HTTP_REFERER'))
def test_vote_default_json(self): r = self._req() l = Link.objects.create(url='http://google.com') resp = views.vote(r, Link, l.id, 1) self.assertEqual(resp.status_code, 200) assert json.loads(resp.content.decode('utf8'))['num_votes'] == 1
def test_vote_redirect(self): r = self._req() l = Link.objects.create(url='http://google.com') resp = views.vote(r, Link, l.id, 1, redirect_url='/thanks/') self.assertEqual(resp.status_code, 302) self.assertEqual(resp.url, '/thanks/')