From 88a90a34fd2623fbeeeca897a638d504149fdd95 Mon Sep 17 00:00:00 2001 From: srahulbadami Date: Thu, 15 Mar 2018 01:19:14 +0530 Subject: [PATCH 1/2] Improved View Page's Logic --- website/models.py | 16 +++++++++++++++- website/views.py | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/website/models.py b/website/models.py index 09333d8a3..4f0e6d994 100644 --- a/website/models.py +++ b/website/models.py @@ -262,7 +262,7 @@ class Points(models.Model): # @receiver(user_logged_in, dispatch_uid="some.unique.string.id.for.allauth.user_logged_in") # def user_logged_in_(request, user, **kwargs): # if not settings.TESTING: -# action.send(user, verb='logged in') +# action.send(user, verb='logged in') class InviteFriend(models.Model): @@ -320,3 +320,17 @@ def create_profile(sender, **kwargs): post_save.connect(create_profile, sender=User) + +class IP(models.Model): + address = models.CharField(max_length=25, null=True, blank=True) + user = models.CharField(max_length=25, null=True, blank=True) + issuenumber = models.IntegerField(null=True, blank=True) + def ipaddress(self): + return self.ipaddress + + def user_name(self): + return self.user + + def issue_number(self): + return self.issuenumber + \ No newline at end of file diff --git a/website/views.py b/website/views.py index b298f1092..7e7106b7a 100644 --- a/website/views.py +++ b/website/views.py @@ -41,7 +41,7 @@ from django.views.generic.edit import CreateView from user_agents import parse -from website.models import Issue, Points, Hunt, Domain, InviteFriend, UserProfile +from website.models import Issue, Points, Hunt, Domain, InviteFriend, UserProfile, IP from .forms import FormInviteFriend, UserProfileForm @@ -685,12 +685,29 @@ class IssueView(DetailView): model = Issue slug_field = "id" template_name = "issue.html" - def get(self, request, *args, **kwargs): + ipdetails = IP() + self.object = self.get_object() + ipdetails.user = self.request.user + ipdetails.address = get_client_ip(request) + ipdetails.issuenumber = self.object.id try: - self.object = self.get_object() - self.object.views = (self.object.views or 0) + 1 - self.object.save() + if self.request.user.is_authenticated(): + try: + objectget = IP.objects.get(user = self.request.user, issuenumber = self.object.id) + self.object.save() + except: + ipdetails.save() + self.object.views = (self.object.views or 0) + 1 + self.object.save() + else : + try: + objectget = IP.objects.get(address = get_client_ip(request), issuenumber = self.object.id) + self.object.save() + except: + ipdetails.save() + self.object.views = (self.object.views or 0) + 1 + self.object.save() except: messages.error(self.request, 'That issue was not found.') return redirect("/") @@ -985,3 +1002,11 @@ def unsave_issue(request, issue_pk): userprof = UserProfile.objects.get(user=request.user) userprof.issue_saved.remove(issue) return HttpResponse('OK') + +def get_client_ip(request): + x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') + if x_forwarded_for: + ip = x_forwarded_for.split(',')[0] + else: + ip = request.META.get('REMOTE_ADDR') + return ip \ No newline at end of file From 137f81735d3ae4d25ad6c2a1937177fdfc2ea379 Mon Sep 17 00:00:00 2001 From: srahulbadami Date: Thu, 15 Mar 2018 02:05:58 +0530 Subject: [PATCH 2/2] Migrations Updated --- website/migrations/0045_auto_20180314_2032.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 website/migrations/0045_auto_20180314_2032.py diff --git a/website/migrations/0045_auto_20180314_2032.py b/website/migrations/0045_auto_20180314_2032.py new file mode 100644 index 000000000..9b1b9961a --- /dev/null +++ b/website/migrations/0045_auto_20180314_2032.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-03-14 20:32 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('website', '0044_auto_20170907_1605'), + ] + + operations = [ + migrations.CreateModel( + name='IP', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('address', models.CharField(blank=True, max_length=25, null=True)), + ('user', models.CharField(blank=True, max_length=25, null=True)), + ('issuenumber', models.IntegerField(blank=True, null=True)), + ], + ), + migrations.AlterField( + model_name='userprofile', + name='issue_upvoted', + field=models.ManyToManyField(blank=True, related_name='upvoted', to='website.Issue'), + ), + ]