Fix bug causing automatic attendance to not be updated when user first signs up then off.

This commit is contained in:
Casper V. Kristensen 2019-11-19 04:20:23 +01:00
parent 0ac3bda6b7
commit 5ea6e75d0a
Signed by: caspervk
GPG key ID: 289CA03790535054
2 changed files with 28 additions and 5 deletions

View file

@ -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')]),
),
]

View file

@ -70,8 +70,7 @@ class RaidResponse(models.Model):
(SIGNED_UP, "Signed Up"), (SIGNED_UP, "Signed Up"),
] ]
status = models.PositiveSmallIntegerField( status = models.PositiveSmallIntegerField(
choices=STATUS_CHOICES, choices=STATUS_CHOICES
default=SIGNED_UP
) )
note = models.CharField( note = models.CharField(
@ -92,6 +91,10 @@ class RaidResponse(models.Model):
models.UniqueConstraint(fields=["raid", "character"], name="unique_character_raid_signup") 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): def clean(self):
# Make sure sign-offs are role-agnostic, but all other responses are not # Make sure sign-offs are role-agnostic, but all other responses are not
if self.status == RaidResponse.SIGNED_OFF: if self.status == RaidResponse.SIGNED_OFF:
@ -99,14 +102,16 @@ class RaidResponse(models.Model):
elif self.role is None: elif self.role is None:
raise ValidationError({"role": "This field is required."}) raise ValidationError({"role": "This field is required."})
def save(self, *args, **kwargs): # Set attendance to one of the default values if status was changed
# Set attendance if it hasn't been set manually if self.status != self._original_status:
if self.attendance is None:
if self.status >= RaidResponse.SIGNED_UP: if self.status >= RaidResponse.SIGNED_UP:
self.attendance = settings.DEFAULT_ATTENDANCE_ATTENDING # 1.0 by default self.attendance = settings.DEFAULT_ATTENDANCE_ATTENDING # 1.0 by default
else: else:
self.attendance = settings.DEFAULT_ATTENDANCE_NOT_ATTENDING # 0.0 by default self.attendance = settings.DEFAULT_ATTENDANCE_NOT_ATTENDING # 0.0 by default
def save(self, *args, **kwargs):
super().save(*args, **kwargs) super().save(*args, **kwargs)
self._original_status = self.status
def __str__(self): def __str__(self):
return super().__str__() # TODO? return super().__str__() # TODO?