From 8d410574675bd28de640c9a254c088491494dcc6 Mon Sep 17 00:00:00 2001 From: JisanAR03 Date: Sun, 18 Feb 2024 01:37:33 +0600 Subject: [PATCH] update store system --- blt/settings.py | 12 ++++++------ .../management/commands/create_cachetable.py | 17 ----------------- .../0082_issue_reporter_ip_address.py | 17 +++++++++++++++++ website/models.py | 1 + website/views.py | 19 +++++++++---------- 5 files changed, 33 insertions(+), 33 deletions(-) delete mode 100644 website/management/commands/create_cachetable.py create mode 100644 website/migrations/0082_issue_reporter_ip_address.py diff --git a/blt/settings.py b/blt/settings.py index 5a8388bc0..f077d72ff 100644 --- a/blt/settings.py +++ b/blt/settings.py @@ -193,12 +193,12 @@ # SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' -CACHES = { - 'default': { - 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', - 'LOCATION': 'django_cache', - }, -} +# CACHES = { +# 'default': { +# 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', +# 'LOCATION': 'cache_table', +# } +# } REST_AUTH = { diff --git a/website/management/commands/create_cachetable.py b/website/management/commands/create_cachetable.py deleted file mode 100644 index c086412f8..000000000 --- a/website/management/commands/create_cachetable.py +++ /dev/null @@ -1,17 +0,0 @@ -from django.core.management.base import BaseCommand -from django.core.management import call_command -from django.db import connection - -class Command(BaseCommand): - help = 'Creates the cache table if it does not exist' - - def handle(self, *args, **options): - table_name = 'django_cache' - table_exists = table_name in connection.introspection.table_names() - - if table_exists: - self.stdout.write(self.style.SUCCESS(f"'{table_name}' table already exists.")) - else: - # Create the cache table - call_command('createcachetable') - self.stdout.write(self.style.SUCCESS(f"'{table_name}' table created.")) diff --git a/website/migrations/0082_issue_reporter_ip_address.py b/website/migrations/0082_issue_reporter_ip_address.py new file mode 100644 index 000000000..6f12c68b8 --- /dev/null +++ b/website/migrations/0082_issue_reporter_ip_address.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.8 on 2024-02-17 19:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("website", "0081_userprofile_issue_downvoted"), + ] + + operations = [ + migrations.AddField( + model_name="issue", + name="reporter_ip_address", + field=models.GenericIPAddressField(blank=True, null=True), + ), + ] diff --git a/website/models.py b/website/models.py index 8d10944a3..ea7a8be76 100644 --- a/website/models.py +++ b/website/models.py @@ -252,6 +252,7 @@ class Issue(models.Model): modified = models.DateTimeField(auto_now=True) is_hidden = models.BooleanField(default=False) rewarded = models.PositiveIntegerField(default=0) # money rewarded by the company + reporter_ip_address = models.GenericIPAddressField(null=True, blank=True) def __unicode__(self): diff --git a/website/views.py b/website/views.py index d5a9a4d91..8bee8bfb2 100644 --- a/website/views.py +++ b/website/views.py @@ -87,9 +87,7 @@ from django.core.validators import URLValidator from django.core.exceptions import ValidationError from django.http import HttpRequest -from django.core.cache import cache from django.utils.timezone import now -from django.http import HttpResponseForbidden def is_valid_https_url(url): validate = URLValidator(schemes=['https']) # Only allow HTTPS URLs @@ -730,14 +728,18 @@ def post(self, request, *args, **kwargs): return super().post(request, *args, **kwargs) def form_valid(self, form): - user_or_ip = self.request.user.get_username() if self.request.user.is_authenticated else get_client_ip(self.request) - limit = 50 if self.request.user.is_authenticated else 30 # Your set limits - cache_key = f"issue_create_{user_or_ip}_{now().date()}" - issue_count = cache.get(cache_key, 0) + reporter_ip = get_client_ip(self.request) + form.instance.reporter_ip_address = reporter_ip + + #implement rate limit + limit = 50 if self.request.user.is_authenticated else 30 + today = now().date() + recent_issues_count = Issue.objects.filter(reporter_ip_address=reporter_ip, created__date=today).count() - if issue_count >= limit: + if recent_issues_count >= limit: messages.error(self.request, "You have reached your issue creation limit for today.") return HttpResponseRedirect("/report/") + form.instance.reporter_ip_address = reporter_ip @atomic def create_issue(self,form): @@ -891,7 +893,6 @@ def create_issue(self,form): self.request.session["created"] = domain_exists self.request.session["domain"] = domain.id login_url = reverse("account_login") - cache.set(cache_key, issue_count + 1, 86400) # Increment the count messages.success(self.request, "Bug added!") return HttpResponseRedirect("{}?next={}".format(login_url, redirect_url)) @@ -899,11 +900,9 @@ def create_issue(self,form): self.process_issue( User.objects.get(id=token.user_id), obj, domain_exists, domain, True ) - cache.set(cache_key, issue_count + 1, 86400) # Increment the count return JsonResponse("Created", safe=False) else: self.process_issue(self.request.user, obj, domain_exists, domain) - cache.set(cache_key, issue_count + 1, 86400) # Increment the count return HttpResponseRedirect(redirect_url + "/") return create_issue(self,form)