Only create no-responses if user did something that warrants it.

This commit is contained in:
Casper V. Kristensen 2020-01-07 00:32:22 +01:00
parent bb16c152c4
commit 94d90efad5
Signed by: caspervk
GPG key ID: 289CA03790535054
2 changed files with 16 additions and 2 deletions

View file

@ -28,8 +28,12 @@ def create_raid_no_responses(instance: Raid, **kwargs):
@receiver(post_save, sender=User)
def create_user_no_responses(instance: User, **kwargs):
# Delete all pre-existing no-responses for this user, in case date_joined or main as changed
def create_user_no_responses(instance: User, created: bool, **kwargs):
if (instance.original_main_id, instance.original_date_joined) == (instance.main_id, instance.date_joined) \
and not created:
return
# Delete all pre-existing no-responses for this user, in case date_joined or main was changed
RaidResponse.objects.filter(character__user=instance, status=RaidResponse.NO_RESPONSE).delete()
# Then create them (again)
RaidResponse.objects.bulk_create(

View file

@ -48,6 +48,14 @@ class User(AbstractUser):
class Meta:
ordering = ["rank", "username"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._set_originals()
def _set_originals(self):
self.original_main_id = self.main_id
self.original_date_joined = self.date_joined
def clean(self):
self.username = self.normalize_username(self.username)
if not hasattr(self, "rank"):
@ -75,6 +83,8 @@ class User(AbstractUser):
user = super().save(*args, **kwargs)
self.main.user = self
self.main.save()
self._set_originals()
return user