bug resolved
This commit is contained in:
+42
-14
@@ -1,4 +1,3 @@
|
|||||||
from cloudinary.models import CloudinaryField
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.core.validators import FileExtensionValidator
|
from django.core.validators import FileExtensionValidator
|
||||||
@@ -6,6 +5,7 @@ from django.conf import settings
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
#User Model
|
||||||
class User(AbstractUser):
|
class User(AbstractUser):
|
||||||
is_citizen = models.BooleanField(default=False)
|
is_citizen = models.BooleanField(default=False)
|
||||||
is_moderator = models.BooleanField(default=False)
|
is_moderator = models.BooleanField(default=False)
|
||||||
@@ -33,6 +33,9 @@ class User(AbstractUser):
|
|||||||
is_banned = models.BooleanField(default=False)
|
is_banned = models.BooleanField(default=False)
|
||||||
banned_until = models.DateTimeField(null=True, blank=True)
|
banned_until = models.DateTimeField(null=True, blank=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.username
|
||||||
|
|
||||||
def ban(self, days=7):
|
def ban(self, days=7):
|
||||||
"""Ban user for given number of days (default = 7)."""
|
"""Ban user for given number of days (default = 7)."""
|
||||||
self.is_banned = True
|
self.is_banned = True
|
||||||
@@ -45,16 +48,16 @@ class User(AbstractUser):
|
|||||||
self.banned_until = None
|
self.banned_until = None
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def is_currently_banned(self):
|
@property
|
||||||
"""Check if user is still banned (auto-unban if expired)."""
|
def currently_banned(self):
|
||||||
if self.is_banned and self.banned_until:
|
if self.is_banned and self.banned_until:
|
||||||
if timezone.now() >= self.banned_until:
|
if timezone.now() >= self.banned_until:
|
||||||
# Auto unban if ban expired
|
|
||||||
self.unban()
|
self.unban()
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
#Department Model
|
||||||
class Department(models.Model):
|
class Department(models.Model):
|
||||||
name = models.CharField(max_length=100, unique=True)
|
name = models.CharField(max_length=100, unique=True)
|
||||||
description = models.TextField(blank=True, null=True)
|
description = models.TextField(blank=True, null=True)
|
||||||
@@ -78,10 +81,17 @@ class Department(models.Model):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ["name"]
|
ordering = ["name"]
|
||||||
|
constraints = [
|
||||||
|
models.UniqueConstraint(
|
||||||
|
fields=["admin"],
|
||||||
|
name="unique_department_admin"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
#Issue Model
|
||||||
class Issue(models.Model):
|
class Issue(models.Model):
|
||||||
STATUS_REPORTED = 'reported'
|
STATUS_REPORTED = 'reported'
|
||||||
STATUS_ACKNOWLEDGED = 'acknowledged'
|
STATUS_ACKNOWLEDGED = 'acknowledged'
|
||||||
@@ -117,10 +127,11 @@ class Issue(models.Model):
|
|||||||
latitude = models.FloatField(null=True, blank=True)
|
latitude = models.FloatField(null=True, blank=True)
|
||||||
longitude = models.FloatField(null=True, blank=True)
|
longitude = models.FloatField(null=True, blank=True)
|
||||||
|
|
||||||
photo = CloudinaryField(
|
photo = models.ImageField(
|
||||||
'images',
|
upload_to="issues/",
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True,
|
null=True,
|
||||||
|
validators=[FileExtensionValidator(["jpg", "jpeg", "png", "webp"])]
|
||||||
)
|
)
|
||||||
|
|
||||||
status = models.CharField(
|
status = models.CharField(
|
||||||
@@ -133,39 +144,56 @@ class Issue(models.Model):
|
|||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ["-created_at"] # 🔹 latest issues first by default
|
ordering = ["-created_at"]
|
||||||
|
indexes = [
|
||||||
|
models.Index(fields=["status"]),
|
||||||
|
models.Index(fields=["created_at"]),
|
||||||
|
models.Index(fields=["department"]),
|
||||||
|
models.Index(fields=["reporter"]),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# 🔹 latest issues first by default
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.title} ({self.get_status_display()})"
|
return f"{self.title} ({self.get_status_display()})"
|
||||||
|
|
||||||
# 🔹 Helpers
|
# 🔹 Helpers
|
||||||
def vote_count(self):
|
def vote_count(self):
|
||||||
return self.votes.count() if hasattr(self, "votes") else 0
|
return self.votes.count()
|
||||||
|
|
||||||
|
|
||||||
def has_user_voted(self, user):
|
def has_user_voted(self, user):
|
||||||
if user.is_authenticated and hasattr(self, "votes"):
|
if user.is_authenticated:
|
||||||
return self.votes.filter(user=user).exists()
|
return self.votes.filter(user=user).exists()
|
||||||
return
|
return False
|
||||||
|
|
||||||
|
|
||||||
def assign_to_department(self, department):
|
def assign_to_department(self, department):
|
||||||
"""Assign issue to a department and auto-update status to acknowledged"""
|
if self.department_id == department.id:
|
||||||
|
return
|
||||||
self.department = department
|
self.department = department
|
||||||
self.status = self.STATUS_ACKNOWLEDGED
|
self.status = self.STATUS_ACKNOWLEDGED
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
#Vote Model
|
||||||
|
|
||||||
class Vote(models.Model):
|
class Vote(models.Model):
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name='votes')
|
issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name='votes')
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ('user', 'issue') # Prevent duplicate votes per user
|
constraints = [
|
||||||
|
models.UniqueConstraint(
|
||||||
|
fields=["user", "issue"],
|
||||||
|
name="unique_vote_per_user_issue"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.user.username} voted on {self.issue.title}"
|
return f"{self.user.username} voted on {self.issue.title}"
|
||||||
|
|
||||||
|
#Comment Model
|
||||||
class Comment(models.Model):
|
class Comment(models.Model):
|
||||||
issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name="comments")
|
issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name="comments")
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
|||||||
Reference in New Issue
Block a user