Skip to content

Commit

Permalink
Merge pull request #602 from srahulbadami/fix-576
Browse files Browse the repository at this point in the history
Improved View Page's Logic - Fix 576
  • Loading branch information
souravbadami authored Mar 17, 2018
2 parents 1e4e816 + 137f817 commit f325038
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
29 changes: 29 additions & 0 deletions website/migrations/0045_auto_20180314_2032.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
16 changes: 15 additions & 1 deletion website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

35 changes: 30 additions & 5 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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("/")
Expand Down Expand Up @@ -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

0 comments on commit f325038

Please sign in to comment.