Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ip table #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pytz = "^2024.1"
requests = "^2.32.3"
requests-oauthlib = "^1.3.1"
six = "^1.16.0"
tablib = "^3.2.0"
tablib = "^3.8.0"
ua-parser = "^1.0.0"
djangorestframework = "^3.15.2"
cffi = "^1.17.1"
Expand Down
80 changes: 21 additions & 59 deletions website/views/project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ipaddress
import json
import logging
import re
import socket
import time
Expand All @@ -20,7 +21,8 @@
from django.core.exceptions import ValidationError
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.core.validators import URLValidator
from django.db.models import F, Q, Sum
from django.db.models import Count, F, Q, Sum
from django.db.models.functions import TruncDate
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
Expand All @@ -45,7 +47,7 @@
)
from website.utils import admin_required

# logging.getLogger("matplotlib").setLevel(logging.ERROR)
logging.getLogger("matplotlib").setLevel(logging.ERROR)


def blt_tomato(request):
Expand Down Expand Up @@ -96,69 +98,30 @@ def distribute_bacon(request, contribution_id):


class ProjectBadgeView(APIView):
def get_client_ip(self, request):
# Check X-Forwarded-For header first
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
if x_forwarded_for:
# Return first IP in chain (real client IP)
ip = x_forwarded_for.split(",")[0].strip()
return ip

# Try X-Real-IP header next
x_real_ip = request.META.get("HTTP_X_REAL_IP")
if x_real_ip:
return x_real_ip

# Finally fall back to REMOTE_ADDR
remote_addr = request.META.get("REMOTE_ADDR")
return remote_addr

def get(self, request, slug):
# Get the project or return 404
# Retrieve the project or return 404
project = get_object_or_404(Project, slug=slug)

# Get today's date
today = now().date()

# Get the real client IP
user_ip = self.get_client_ip(request)

# Continue with existing code but use the new user_ip
visited_data = IP.objects.filter(
address=user_ip, path=request.path, created__date=today
).last()

if visited_data:
# If the creation date is today
if visited_data.created.date() == today:
# If the visit count is 1, update the project visit count
if visited_data.count == 1:
project.project_visit_count = F("project_visit_count") + 1
project.save()
else:
# If the creation date is not today, reset the creation date and count
visited_data.created = now()
visited_data.count = 1
visited_data.save()

# Increment the project visit count
project.project_visit_count = F("project_visit_count") + 1
project.save()
else:
# If no record exists, create a new one
IP.objects.create(address=user_ip, path=request.path, created=now(), count=1)

# Increment the project's visit count
project.project_visit_count = F("project_visit_count") + 1
project.save()
# Get unique visits, grouped by date
visit_counts = (
IP.objects.filter(path=request.path)
.annotate(date=TruncDate("created"))
.values("date")
.annotate(visit_count=Count("address"))
.order_by("date") # Order from oldest to newest
)

# Refresh project to get the latest visit count
project.refresh_from_db()
# Update project visit count
project.repo_visit_count += 1
project.save()

total_views = project.project_visit_count
# Extract dates and counts
dates = [entry["date"] for entry in visit_counts]
counts = [entry["visit_count"] for entry in visit_counts]
total_views = sum(counts) # Calculate total views

fig = plt.figure(figsize=(4, 1))
plt.bar(0, total_views, color="red", width=0.5)
plt.bar(dates, counts, width=0.5, color="red")

plt.title(
f"{total_views}",
Expand All @@ -185,7 +148,6 @@ def get(self, request, slug):
response["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
response["Pragma"] = "no-cache"
response["Expires"] = "0"

return response


Expand Down
Loading