From 8018706715c5d438b77cca36caf036c9abf9ad7d Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Sat, 28 Jan 2017 17:06:05 +0100 Subject: [PATCH] Check for shadowcalls on user registration, copy data if necessary --- contest/models.py | 4 +++- contest/signals.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 contest/signals.py diff --git a/contest/models.py b/contest/models.py index a279d9d..fcc4e1d 100644 --- a/contest/models.py +++ b/contest/models.py @@ -5,9 +5,10 @@ import datetime from django.db import models from django.contrib.auth.models import AbstractUser from django.core.validators import RegexValidator -from django.db.models import Q +from django.db.models import Q, signals from .validators import CallUsernameValidator +from .signals import checkForShadowCall class Contest(models.Model): name = models.CharField(max_length=20) @@ -63,6 +64,7 @@ class User(AbstractUser): def getCfmdRefCount(self): return len(set(map(lambda _x: _x["refStr"], self.qso_set.filter(ref__isnull=False).values("ref", "refStr")))) +signals.post_save.connect(checkForShadowCall, sender=User) class Band(models.Model): name = models.CharField(max_length=10) diff --git a/contest/signals.py b/contest/signals.py new file mode 100644 index 0000000..2de050a --- /dev/null +++ b/contest/signals.py @@ -0,0 +1,18 @@ + +def checkForShadowCall(sender, instance, created, raw, **kwargs): + """ Check for existing shadow call. If present copy it's data and delete it. """ + if created: + # to prevent circular imports we import ShadowCall here + from .models import ShadowCall + + try: + shadow = ShadowCall.objects.get(username=instance.username) + instance.ref = shadow.ref + instance.location = shadow.location + instance.opName = shadow.opName + instance.regTime = shadow.regTime + + instance.save() + shadow.delete() + except ShadowCall.DoesNotExist: + pass