diff --git a/civicfix/core/__pycache__/models.cpython-313.pyc b/civicfix/core/__pycache__/models.cpython-313.pyc index ca9962d..9d95b66 100644 Binary files a/civicfix/core/__pycache__/models.cpython-313.pyc and b/civicfix/core/__pycache__/models.cpython-313.pyc differ diff --git a/civicfix/core/__pycache__/views.cpython-313.pyc b/civicfix/core/__pycache__/views.cpython-313.pyc index a59bdfc..1c0f9a9 100644 Binary files a/civicfix/core/__pycache__/views.cpython-313.pyc and b/civicfix/core/__pycache__/views.cpython-313.pyc differ diff --git a/civicfix/core/models.py b/civicfix/core/models.py index fe643a5..79b9590 100644 --- a/civicfix/core/models.py +++ b/civicfix/core/models.py @@ -10,7 +10,7 @@ class User(AbstractUser): is_moderator = models.BooleanField(default=False) is_resolver = models.BooleanField(default=False) phone = models.CharField(max_length=15, blank=True, null=True) - + groups = models.ManyToManyField( 'auth.Group', verbose_name='groups', @@ -27,25 +27,33 @@ class User(AbstractUser): related_name='core_user_permissions', related_query_name='core_user', ) + + # Ban-related fields is_banned = models.BooleanField(default=False) banned_until = models.DateTimeField(null=True, blank=True) - def ban(self, days=1): - """Ban user for given days (default 7 days).""" + def ban(self, days=7): + """Ban user for given number of days (default = 7).""" self.is_banned = True self.banned_until = timezone.now() + timedelta(days=days) self.save() def unban(self): + """Unban user immediately.""" self.is_banned = False self.banned_until = None self.save() def is_currently_banned(self): + """Check if user is still banned (auto-unban if expired).""" if self.is_banned and self.banned_until: - return timezone.now() < self.banned_until + if timezone.now() >= self.banned_until: + # Auto unban if ban expired + self.unban() + return False + return True return False - + class Department(models.Model): name = models.CharField(max_length=100, unique=True) description = models.TextField(blank=True, null=True) diff --git a/civicfix/core/views.py b/civicfix/core/views.py index eb10de2..224144c 100644 --- a/civicfix/core/views.py +++ b/civicfix/core/views.py @@ -6,6 +6,7 @@ from django.db import IntegrityError from django.db.models import Exists, OuterRef from django.http import JsonResponse from django.shortcuts import render, redirect, get_object_or_404 +from django.utils import timezone from django.views.decorators.http import require_POST from .models import Issue, User, Vote, Comment, Department from .forms import CitizenRegistrationForm, IssueForm, CommentForm @@ -117,7 +118,18 @@ def custom_login(request): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) + if user is not None: + # 🔹 Auto unban check + if hasattr(user, "is_currently_banned") and user.is_currently_banned(): + days_left = (user.banned_until - timezone.now()).days + messages.error( + request, + f"🚫 Your account is banned for {days_left} more days for reporting a fake issue." + ) + return redirect('login') + + # Normal login login(request, user) messages.success(request, f'Welcome back, {username}!') return redirect('home') @@ -127,6 +139,7 @@ def custom_login(request): messages.error(request, 'Invalid username or password.') else: form = AuthenticationForm() + return render(request, 'core/login.html', {'form': form}) @login_required @@ -314,15 +327,15 @@ def unban_user(request, user_id): messages.success(request, f"{citizen.username} has been unbanned.") return redirect('manage_users') -# core/views.py @login_required @user_passes_test(superadmin_check) def delete_fake_issue(request, issue_id): issue = get_object_or_404(Issue, id=issue_id) - reporter = issue.reporter + reporter = issue.reporter + reporter.ban(7) issue.delete() - messages.error(request, f"Issue by {reporter.username} was reported fake and deleted.") - return redirect('manage_issues') + messages.success(request, f"✅ Issue deleted and user {reporter.username} has been banned for 7 days.") + return redirect("manage_issues") def resolver_check(user):