Unverified Commit 477879f2 authored by Julien Palard's avatar Julien Palard
Browse files

Model to allow to vote for an answer.

parent 13dfd548
# Generated by Django 3.2.2 on 2021-10-04 11:45
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('website', '0019_auto_20210507_1828'),
]
operations = [
migrations.CreateModel(
name='Vote',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.IntegerField()),
],
),
migrations.AddField(
model_name='answer',
name='votes',
field=models.IntegerField(blank=True, default=0),
),
migrations.AddIndex(
model_name='answer',
index=models.Index(fields=['exercise', '-votes'], name='website_ans_exercis_d8ffb5_idx'),
),
migrations.AddField(
model_name='vote',
name='answer',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='website.answer'),
),
migrations.AddField(
model_name='vote',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
......@@ -4,7 +4,9 @@ from datetime import timedelta
from asgiref.sync import async_to_sync
from django.core.exceptions import ValidationError
from django.db import models, IntegrityError
from django.db.models import Count, Value, Q, Min
from django.db.models import Count, Value, Q, Min, Sum
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse
from django.utils.text import Truncator
from django.utils.timezone import now
......@@ -245,6 +247,9 @@ class Snippet(models.Model):
class Answer(models.Model):
class Meta:
indexes = [models.Index(fields=["exercise", "-votes"])]
exercise = models.ForeignKey(
Exercise, on_delete=models.CASCADE, related_name="answers"
)
......@@ -259,6 +264,7 @@ class Answer(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
corrected_at = models.DateTimeField(blank=True, null=True)
is_unhelpfull = models.BooleanField(default=False, blank=True)
votes = models.IntegerField(default=0, blank=True, null=False) # Sum of Vote.value
def short_correction_message(self):
return self.correction_message.strip().split("\n")[:1][:100]
......@@ -296,6 +302,20 @@ class Answer(models.Model):
self.save()
class Vote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
answer = models.ForeignKey(Answer, on_delete=models.CASCADE)
value = models.IntegerField(null=False) # Typically +1 or -1
@receiver(post_save, sender=Vote)
def update_vote_count(sender, instance, **kwargs):
instance.answer.votes = Vote.objects.filter(answer=instance.answer).aggregate(
Sum("value")
)["value__sum"]
instance.answer.save()
class TeamQuerySet(models.QuerySet):
def my_teams(self, user):
"""Get teams for which user is staff."""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment