diff --git a/civicfix/core/models.py b/civicfix/core/models.py index 050a1c6..6e3ba7c 100644 --- a/civicfix/core/models.py +++ b/civicfix/core/models.py @@ -56,4 +56,23 @@ class Issue(models.Model): updated_at = models.DateTimeField(auto_now=True) def __str__(self): - return self.title \ No newline at end of file + return self.title + + def vote_count(self): + return self.votes.count() + + def has_user_voted(self, user): + if user.is_authenticated: + return self.votes.filter(user=user).exists() + return False + +class Vote(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name='votes') + created_at = models.DateTimeField(auto_now_add=True) + + class Meta: + unique_together = ('user', 'issue') # Prevent duplicate votes per user + + def __str__(self): + return f"{self.user.username} voted on {self.issue.title}"