Skip to content

Commit

Permalink
Fix formatting issues with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
krrish-sehgal committed Nov 5, 2024
1 parent 4e654b5 commit 122e701
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 191 deletions.
14 changes: 8 additions & 6 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@
IssueView,
JoinCompany,
ListHunts,
MaliciousIpListView,
OngoingHunts,
PreviousHunts,
ProjectBadgeView,
ProjectDetailView,
ProjectListView,
ReportIpView,
ScoreboardView,
SpecificIssuesView,
SpecificMonthLeaderboardView,
Expand All @@ -76,9 +78,6 @@
UserDeleteView,
UserProfileDetailsView,
UserProfileDetailView,
ReportIpView,
MaliciousIpListView,

)
from website.views import ( # TODO AutoLabel,
SaveBiddingData,
Expand Down Expand Up @@ -460,8 +459,7 @@
path("project/<slug:slug>/", ProjectDetailView.as_view(), name="project_view"),
path("projects/<slug:slug>/badge/", ProjectBadgeView.as_view(), name="project-badge"),
path("report-ip/", ReportIpView.as_view(), name="report_ip"),
path("malicious-ips/", MaliciousIpListView.as_view(), name='malicious_ips_list'),

path("malicious-ips/", MaliciousIpListView.as_view(), name="malicious_ips_list"),
re_path(
r"^api/v1/createissues/$",
csrf_exempt(IssueCreate.as_view()),
Expand Down Expand Up @@ -573,7 +571,11 @@
path("api/timelogsreport/", website.views.TimeLogListAPIView, name="timelogsreport"),
path("time-logs/", website.views.TimeLogListView, name="time_logs"),
path("sizzle-daily-log/", website.views.sizzle_daily_log, name="sizzle_daily_log"),
path("user-sizzle-report/<str:username>/", website.views.user_sizzle_report, name="user_sizzle_report"),
path(
"user-sizzle-report/<str:username>/",
website.views.user_sizzle_report,
name="user_sizzle_report",
),
]

if settings.DEBUG:
Expand Down
86 changes: 50 additions & 36 deletions website/class_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
from decimal import Decimal
from urllib.parse import urlparse


from django.urls import reverse_lazy
from django.views.generic import FormView

import requests
import six
import stripe
Expand Down Expand Up @@ -47,7 +43,7 @@
from django.utils.decorators import method_decorator
from django.utils.timezone import now
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import DetailView, ListView, TemplateView, View
from django.views.generic import DetailView, FormView, ListView, TemplateView, View
from django.views.generic.edit import CreateView
from PIL import Image, ImageDraw, ImageFont
from rest_framework.authtoken.models import Token
Expand All @@ -57,7 +53,14 @@
from user_agents import parse

from blt import settings
from website.forms import CaptchaForm, GitHubURLForm, HuntForm, UserDeleteForm, UserProfileForm , IpReportForm
from website.forms import (
CaptchaForm,
GitHubURLForm,
HuntForm,
IpReportForm,
UserDeleteForm,
UserProfileForm,
)
from website.models import (
IP,
BaconToken,
Expand All @@ -72,6 +75,7 @@
Hunt,
HuntPrize,
InviteFriend,
IpReport,
Issue,
IssueScreenshot,
Monitor,
Expand All @@ -87,7 +91,6 @@
UserProfile,
Wallet,
Winner,
IpReport,
)
from website.utils import (
get_client_ip,
Expand Down Expand Up @@ -1201,6 +1204,7 @@ def get_context_data(self, **kwargs):

return context


class UploadCreate(View):
template_name = "index.html"

Expand Down Expand Up @@ -2214,7 +2218,7 @@ def get_context_data(self, **kwargs):


class ReportIpView(FormView):
template_name = 'report_ip.html'
template_name = "report_ip.html"
form_class = IpReportForm
captcha = CaptchaForm()

Expand All @@ -2223,64 +2227,74 @@ def post(self, request, *args, **kwargs):
captcha_form = CaptchaForm(request.POST)
if not captcha_form.is_valid():
messages.error(request, "Invalid CAPTCHA. Please try again.")
return render(request, self.template_name, {
"form": self.get_form(),
"captcha_form": captcha_form,
})

return render(
request,
self.template_name,
{
"form": self.get_form(),
"captcha_form": captcha_form,
},
)

# Process form and duplicate IP check
form = self.get_form()
if form.is_valid():
ip_address = form.cleaned_data.get("ip_address")
ip_type = form.cleaned_data.get("ip_type")
if IpReport.objects.filter(ip_address=ip_address, ip_type=ip_type).exists():
messages.error(request, "This IP address has already been reported.")
return render(request, self.template_name, {
"form": form,
"captcha_form": captcha_form,
})

return render(
request,
self.template_name,
{
"form": form,
"captcha_form": captcha_form,
},
)

return self.form_valid(form)
else:
return self.form_invalid(form)

@atomic
def form_valid(self, form):
# Check daily report limit per IP
reporter_ip = get_client_ip(self.request)
limit = 50 if self.request.user.is_authenticated else 30
today = now().date()
recent_reports_count = IpReport.objects.filter(
reporter_ip_address=reporter_ip,
created=today
reporter_ip_address=reporter_ip, created=today
).count()

if recent_reports_count >= limit:
messages.error(self.request, "You have reached the daily limit for IP reports.")
return render(self.request, self.template_name, {
"form": self.get_form(),
"captcha_form": CaptchaForm(),
})

return render(
self.request,
self.template_name,
{
"form": self.get_form(),
"captcha_form": CaptchaForm(),
},
)

form.instance.reporter_ip_address = reporter_ip
form.instance.user = self.request.user if self.request.user.is_authenticated else None
form.save()
messages.success(self.request, "IP report successfully submitted.")


return redirect("malicious_ips_list")
return redirect("malicious_ips_list")

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['captcha_form'] = CaptchaForm()
context = super().get_context_data(**kwargs)
context["captcha_form"] = CaptchaForm()
return context


class MaliciousIpListView(ListView):
model = IpReport
template_name = 'malicious_ips_list.html'
context_object_name = 'malicious_ips'
paginate_by = 10
template_name = "malicious_ips_list.html"
context_object_name = "malicious_ips"
paginate_by = 10

def get_queryset(self):
return IpReport.objects.all()
return IpReport.objects.all()
7 changes: 4 additions & 3 deletions website/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django import forms
from mdeditor.fields import MDTextFormField

from .models import Bid, Monitor, UserProfile ,IpReport
from .models import Bid, IpReport, Monitor, UserProfile


class UserProfileForm(forms.ModelForm):
Expand Down Expand Up @@ -66,8 +66,9 @@ class Meta:
class IpReportForm(forms.ModelForm):
class Meta:
model = IpReport
fields = ['ip_address', 'ip_type', 'description', 'malicious_activity_title']

fields = ["ip_address", "ip_type", "description", "malicious_activity_title"]


class BidForm(forms.ModelForm):
class Meta:
model = Bid
Expand Down
5 changes: 1 addition & 4 deletions website/migrations/0142_ipreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

dependencies = [
("website", "0141_project_project_visit_count_project_repo_visit_count"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
Expand All @@ -29,9 +28,7 @@ class Migration(migrations.Migration):
("ip_address", models.GenericIPAddressField()),
(
"ip_type",
models.CharField(
choices=[("ipv4", "IPv4"), ("ipv6", "IPv6")], max_length=10
),
models.CharField(choices=[("ipv4", "IPv4"), ("ipv6", "IPv6")], max_length=10),
),
("description", models.TextField()),
("created", models.DateTimeField(auto_now_add=True)),
Expand Down
14 changes: 6 additions & 8 deletions website/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
import os
import uuid
import ipaddress

from decimal import Decimal
from urllib.parse import urlparse

Expand Down Expand Up @@ -879,17 +877,17 @@ def __str__(self):

class IpReport(models.Model):
IP_TYPE_CHOICES = [
('ipv4', 'IPv4'),
('ipv6', 'IPv6'),
("ipv4", "IPv4"),
("ipv6", "IPv6"),
]

user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
malicious_activity_title = models.CharField(max_length=255)
ip_address = models.GenericIPAddressField()
malicious_activity_title = models.CharField(max_length=255)
ip_address = models.GenericIPAddressField()
ip_type = models.CharField(max_length=10, choices=IP_TYPE_CHOICES)
description = models.TextField()
created = models.DateTimeField(auto_now_add=True)
reporter_ip_address = models.GenericIPAddressField(null=True, blank=True)

def __str__(self):
return f"{self.ip_address} ({self.ip_type}) - {self.malicious_activity_title}"
return f"{self.ip_address} ({self.ip_type}) - {self.malicious_activity_title}"
38 changes: 17 additions & 21 deletions website/templates/malicious_ips_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,46 +84,43 @@
font-size: 1.2em;
}
</style>

<h1>Reported Malicious IPs</h1>
<div class="report-container">
{% for ip in malicious_ips %}
<div class="report-card" onclick="toggleExpand(this)">
<div class="ip-header">
<span class="ip-address">{{ ip.ip_address }}</span>
<span class="expand-icon"></span>
</div>
<div class="user-info">
<i class="fas fa-user"></i> {{ ip.user.username|default:"Anonymous" }} - {{ ip.created|date:"M d, Y" }}
</div>
<div class="description-content">
<p class="reason">Reason: {{ ip.malicious_activity_title}}</p>
<p>{{ ip.description|default:"No additional details provided." }}</p>
<div class="report-card" onclick="toggleExpand(this)">
<div class="ip-header">
<span class="ip-address">{{ ip.ip_address }}</span>
<span class="expand-icon"></span>
</div>
<div class="user-info">
<i class="fas fa-user"></i> {{ ip.user.username|default:"Anonymous" }} - {{ ip.created|date:"M d, Y" }}
</div>
<div class="description-content">
<p class="reason">Reason: {{ ip.malicious_activity_title }}</p>
<p>{{ ip.description|default:"No additional details provided." }}</p>
</div>
</div>
</div>
{% empty %}
<p>No malicious IPs reported.</p>
<p>No malicious IPs reported.</p>
{% endfor %}
</div>

<div class="col-md-12 text-center mt-4">
<div class="pagination">
{% if is_paginated %}
{% if page_obj.has_previous %}
<a href="?page=1" class="btn btn-default">First</a>
<a href="?page={{ page_obj.previous_page_number }}" class="btn btn-default">Previous</a>
<a href="?page={{ page_obj.previous_page_number }}"
class="btn btn-default">Previous</a>
{% endif %}

<span class="current">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>

{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}" class="btn btn-default">Next</a>
<a href="?page={{ page_obj.paginator.num_pages }}" class="btn btn-default">Last</a>
<a href="?page={{ page_obj.paginator.num_pages }}"
class="btn btn-default">Last</a>
{% endif %}
{% endif %}
</div>
</div>

<div class="bottom-right">
<a href="https://github.com/OWASP-BLT/BLT/blob/main/website/templates/new_home.html">
<i class="fab fa-github"></i>
Expand All @@ -132,7 +129,6 @@ <h1>Reported Malicious IPs</h1>
<i class="fab fa-figma"></i>
</a>
</div>

<script>
function toggleExpand(card) {
const descriptionContent = card.querySelector(".description-content");
Expand Down
Loading

0 comments on commit 122e701

Please sign in to comment.