From 5ea6e75d0ac940b3e4863805b79c8e0079ba3077 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Tue, 19 Nov 2019 04:20:23 +0100 Subject: [PATCH] Fix bug causing automatic attendance to not be updated when user first signs up then off. --- .../migrations/0003_auto_20191119_0412.py | 18 ++++++++++++++++++ drakul/raids/models.py | 15 ++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 drakul/raids/migrations/0003_auto_20191119_0412.py diff --git a/drakul/raids/migrations/0003_auto_20191119_0412.py b/drakul/raids/migrations/0003_auto_20191119_0412.py new file mode 100644 index 0000000..a4b2133 --- /dev/null +++ b/drakul/raids/migrations/0003_auto_20191119_0412.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.6 on 2019-11-19 04:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('raids', '0002_auto_20191119_0310'), + ] + + operations = [ + migrations.AlterField( + model_name='raidresponse', + name='status', + field=models.PositiveSmallIntegerField(choices=[(1, 'Signed Off'), (2, 'Signed Up'), (3, 'Stand By'), (4, 'Confirmed')]), + ), + ] diff --git a/drakul/raids/models.py b/drakul/raids/models.py index 5869c7e..a363c18 100755 --- a/drakul/raids/models.py +++ b/drakul/raids/models.py @@ -70,8 +70,7 @@ class RaidResponse(models.Model): (SIGNED_UP, "Signed Up"), ] status = models.PositiveSmallIntegerField( - choices=STATUS_CHOICES, - default=SIGNED_UP + choices=STATUS_CHOICES ) note = models.CharField( @@ -92,6 +91,10 @@ class RaidResponse(models.Model): models.UniqueConstraint(fields=["raid", "character"], name="unique_character_raid_signup") ] + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._original_status = self.status + def clean(self): # Make sure sign-offs are role-agnostic, but all other responses are not if self.status == RaidResponse.SIGNED_OFF: @@ -99,14 +102,16 @@ class RaidResponse(models.Model): elif self.role is None: raise ValidationError({"role": "This field is required."}) - def save(self, *args, **kwargs): - # Set attendance if it hasn't been set manually - if self.attendance is None: + # Set attendance to one of the default values if status was changed + if self.status != self._original_status: if self.status >= RaidResponse.SIGNED_UP: self.attendance = settings.DEFAULT_ATTENDANCE_ATTENDING # 1.0 by default else: self.attendance = settings.DEFAULT_ATTENDANCE_NOT_ATTENDING # 0.0 by default + + def save(self, *args, **kwargs): super().save(*args, **kwargs) + self._original_status = self.status def __str__(self): return super().__str__() # TODO?