Skip to content

Commit

Permalink
update store system
Browse files Browse the repository at this point in the history
  • Loading branch information
JisanAR03 committed Feb 17, 2024
1 parent 585e283 commit 8d41057
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
12 changes: 6 additions & 6 deletions blt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
17 changes: 0 additions & 17 deletions website/management/commands/create_cachetable.py

This file was deleted.

17 changes: 17 additions & 0 deletions website/migrations/0082_issue_reporter_ip_address.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
1 change: 1 addition & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
19 changes: 9 additions & 10 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -891,19 +893,16 @@ 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))

if tokenauth:
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)
Expand Down

0 comments on commit 8d41057

Please sign in to comment.