rating and comment system complete
This commit is contained in:
@@ -113,6 +113,8 @@ class Project(models.Model):
|
||||
)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
rating_score = models.IntegerField(default=0)
|
||||
rating_count = models.PositiveIntegerField(default=0)
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
@@ -341,3 +343,39 @@ class ProjectDraft(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return f'Draft for {self.user.username}'
|
||||
|
||||
|
||||
class Rating(models.Model):
|
||||
"""One user's up/down vote on a project (Steam-style overall rating).
|
||||
|
||||
A user may hold a single vote per project; changing it edits this row.
|
||||
``rating_score``/``rating_count`` on Project mirror the aggregate."""
|
||||
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='ratings')
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='ratings',
|
||||
)
|
||||
value = models.BooleanField(default=True) # True=positive/up, False=negative/down
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=['project', 'user'], name='uniq_rating_project_user'),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f'{"+" if self.value else "-"}{self.user.username} on {self.project.slug}'
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
"""A forum-style comment on a project. Unlimited; body is Markdown."""
|
||||
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='comments')
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='comments',
|
||||
)
|
||||
body = models.TextField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.user.username} on {self.project.slug}: {self.body[:40]}'
|
||||
|
||||
Reference in New Issue
Block a user