def test_anonymous_user_hit_not_counted(self): """ Test Multiple AnonymousUser Hit, not counted """ response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertTrue(response.hit_counted) self.assertEqual(response.hit_message, 'Hit counted: session key') response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertFalse(response.hit_counted) self.assertEqual(response.hit_message, 'Not counted: session key has active hit')
def test_registered_user_hit_not_counted(self): """ Test Multiple AnonymousUser Hit, not counted """ self.request_post.user = User.objects.create_user('john', '*****@*****.**', 'johnpassword') response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertTrue(response.hit_counted) self.assertEqual(response.hit_message, 'Hit counted: user authentication') response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertFalse(response.hit_counted) self.assertEqual(response.hit_message, 'Not counted: authenticated user has active hit')
def get_context_data(self, **kwargs): context = super(QuestionView, self).get_context_data(**kwargs) question = self.get_object() hit_count = HitCount.objects.get_for_object(question) HitCountMixin.hit_count(self.request, hit_count) if self.request.user.is_authenticated(): context['user_voted'] = not question.votes.exists(self.request.user) else: context['user_voted'] = False context['question'] = question context['answers'] = sorted(question.answer_set.all(), key=lambda answer: (answer.votes.count(), answer.create_at), reverse=True) return context
def module(request, moduleid): """ A particular module's contents """ context_dict = {} module = Module.objects.get(id=moduleid) context_dict['module_title'] = module.title context_dict['module_id'] = moduleid if request.user.is_authenticated(): user = request.user if Administrator.objects.filter(user=user).exists(): context_dict['is_admin'] = True context_dict['pages'] = module.page_set.all() # count user hits hit_count_obj = HitCount.objects.get_for_object(module) hit_count_response = HitCountMixin.hit_count(request, hit_count_obj) context_dict['visits'] = module.hit_count.hits # Pages query returns ordered tuple (no need to label each page) context_dict['module_pages'] = tuple(i for i in Page.objects.filter(module=module)) try: context_dict['user_progress_on_module'] = Takers.objects.get(user=request.user, module=module).progress except ObjectDoesNotExist: # if user isn't taking this module -> 0 progress context_dict['user_progress_on_module'] = 0 context_dict['user_modules'] = tuple(i.module for i in Takers.objects.filter(user=request.user)) context_dict['module_downloadable'] = tuple(i for i in ContentFile.objects.filter(module=module)) return render(request, 'lfs/content.html', context_dict)
def test_anonymous_user_hit(self): """ Test AnonymousUser Hit """ response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertTrue(response.hit_counted) self.assertEqual(response.hit_message, 'Hit counted: session key')
def article(request, slug): context = {} context['article'] = Article.objects.get(slug=slug) context['meta'] = get_object_or_404(Article, slug=slug).as_meta(request) hit_count = HitCount.objects.get_for_object(context['article']) hit_count_response = HitCountMixin.hit_count(request, hit_count) # noqa return TemplateResponse(request, "frontend/blog/post.html", context)
def test_anonymous_user_hit_counted_after_filter_active(self): """ Test Multiple AnonymousUser Hit, counted because of filter active """ # create a Hit ten days ago created = timezone.now() - timedelta(days=10) with mock.patch('django.utils.timezone.now') as mock_now: mock_now.return_value = created response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertTrue(response.hit_counted) self.assertEqual(response.hit_message, 'Hit counted: session key') # test a Hit today, within the filter time response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertTrue(response.hit_counted) self.assertEqual(response.hit_message, 'Hit counted: session key')
def test_blacklist_user_agent(self): """ Test black listed user agents. """ BlacklistUserAgent.objects.create(user_agent="my_clever_agent") response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertFalse(response.hit_counted) self.assertEqual(response.hit_message, 'Not counted: user agent has been blacklisted')
def test_blacklist_ip(self): """ Test black listed IPs. """ BlacklistIP.objects.create(ip="127.0.0.1") response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertFalse(response.hit_counted) self.assertEqual(response.hit_message, 'Not counted: user IP has been blacklisted')
def test_exclude_user_group(self): """ Exclude user by adding a group setting. """ self.request_post.user = User.objects.create_user('john', '*****@*****.**', 'johnpassword') group = Group.objects.create(name='Admin') group.user_set.add(self.request_post.user) response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertFalse(response.hit_counted) self.assertEqual(response.hit_message, 'Not counted: user excluded by group')
def test_registered_user_hit_counted_after_filter_active(self): """ Test Multiple AnonymousUser Hit, counted because of filter active """ self.request_post.user = User.objects.create_user('john', '*****@*****.**', 'johnpassword') # create a Hit ten days ago created = timezone.now() - timedelta(days=10) with mock.patch('django.utils.timezone.now') as mock_now: mock_now.return_value = created response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertTrue(response.hit_counted) self.assertEqual(response.hit_message, 'Hit counted: user authentication') # test a Hit today, within the filter time response = HitCountMixin.hit_count(self.request_post, self.hit_count) self.assertTrue(response.hit_counted) self.assertEqual(response.hit_message, 'Hit counted: user authentication')
def event(request, slug): """ Мероприятие :param request: :param slug: :return: """ context = {} context['event'] = Event.objects.get(slug=slug) date = abs(context['event'].end - context['event'].start) context['time'] = date.seconds / 60 hit_count = HitCount.objects.get_for_object(context['event']) hit_count_response = HitCountMixin.hit_count(request, hit_count) # noqa context['meta'] = get_object_or_404(Event, slug=slug).as_meta(request) context['datetime_now'] = timezone.now() return TemplateResponse(request, "frontend/event/event.html", context)
def render_to_response(self, context, **response_kwargs): obj = context.get('object') instance = self.get_object() try: # if num_views field exists, update it a = instance.num_views # crude test for existence except: pass else: hit_count = HitCount.objects.get_for_object(instance) hit_count_response = HitCountMixin.hit_count( self.request, hit_count) if(hit_count_response[0]): instance.num_views = F('num_views') + 1 instance.save() fields = utils.to_dict(obj) if 'tags' in fields: fields['tag_names'] = obj.get_tag_names() response = {'fields': fields, 'pk': obj.pk, 'model': str(obj.__class__)[8:-2]} return JsonResponse(response)
def UserProfileDetailView(request, pk): userProfile = models.User.objects.get(pk=pk).userprofile if request.GET.get('count_hit'): hit_count = HitCount.objects.get_for_object(userProfile) hit_count_response = HitCountMixin.hit_count(request, hit_count) if(hit_count_response[0]): userProfile.num_views = F('num_views') + 1 userProfile.save() userProfile.refresh_from_db() fields = utils.to_dict(userProfile) if(fields['department']): fields['dept_name'] = models.Dept.objects.get( id=fields['department']).name else: fields['dept_name'] = None fields['interest_names'] = userProfile.get_interest_names() fields['isModerator'] = models.Moderator.objects.filter(user__id=fields['user']).exists() profile_dict = {'model': "app.userprofile", 'pk': fields['id'], 'fields': fields} return JsonResponse(profile_dict)
def test_hits_per_ip_limit(self): """ Test `HITCOUNT_HITS_PER_IP_LIMIT` setting. Should allow multiple hits from the same IP until the limit is reached from that IP. """ responses = [] for x in range(3): # need a new session key each time. engine = import_module(settings.SESSION_ENGINE) store = engine.SessionStore() store.save() self.request_post.session = store responses.append(HitCountMixin.hit_count(self.request_post, self.hit_count)) self.assertTrue(responses[0].hit_counted) self.assertEqual(responses[0].hit_message, 'Hit counted: session key') self.assertTrue(responses[1].hit_counted) self.assertEqual(responses[1].hit_message, 'Hit counted: session key') self.assertFalse(responses[2].hit_counted) self.assertEqual(responses[2].hit_message, 'Not counted: hits per IP address limit reached') hit_count = HitCount.objects.get(pk=self.hit_count.pk) self.assertEqual(hit_count.hits, 2)
def life_detail(request, pk): life_post_from_db=Life_post.objects.get(pk=pk) life_hitcount=HitCount.objects.get_for_object(life_post_from_db) life_hitcount_response=HitCountMixin.hit_count(request, life_hitcount) return render(request, 'life/life_detail.html', {'life_post_from_db':life_post_from_db, 'life_hitcount_response':life_hitcount_response})
def humor_detail(request, pk): humor_post_from_db=Humor_post.objects.get(pk=pk) humor_hitcount=HitCount.objects.get_for_object(humor_post_from_db) humor_hitcount_response=HitCountMixin.hit_count(request, humor_hitcount) return render(request, 'humor/humor_detail.html', {'humor_post_from_db':humor_post_from_db, 'humor_hitcount_response':humor_hitcount_response})
def serve(self, request, *args, **kwargs): hit_count = HitCount.objects.get_for_object(self) ViewHitCountMixin.hit_count(request, hit_count) return super().serve(request, *args, **kwargs)
def post_detail(request, post_id): post = Post.objects.get(id=post_id) hit_counts = post.hit_count.hits hit_count = HitCount.objects.get_for_object(post) hit_count_response = HitCountMixin.hit_count(request, hit_count) return render(request, 'blog/post_detail.html', {'post': post})
def _countup_subtitle(self, request: HttpRequest, subtitle: Subtitle) -> None: hit_count = HitCount.objects.get_for_object(subtitle) hit_count_response = HitCountMixin.hit_count(request, hit_count) if not hit_count_response.hit_counted: logger.info('hit count failed: {}'.format(hit_count_response))
def redirect(request, linkpattern): link = get_object_or_404(Link, pattern=linkpattern) hit_count = HitCount.objects.get_for_object(link) hit_count_response = HitCountMixin.hit_count(request, hit_count) return HttpResponseRedirect(link.domain)
def create_hit_count(request, instance): hit_count = HitCount.objects.get_for_object(instance) hit_count_response = HitCountMixin.hit_count(request, hit_count)