UUID, email fields and receiver for sending them, /edit API for modifying signup with ID and UUID

This commit is contained in:
Aarni Halinen
2020-06-22 23:09:20 +03:00
parent 027b9c370c
commit 26af46fa12
7 changed files with 154 additions and 20 deletions
+33 -5
View File
@@ -1,21 +1,23 @@
"""Webapp app models."""
from django.conf import settings
from django.db import models
from django.utils import timezone
# from datetime import timedelta
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from webapp.utils import month_from_now
from webapp.utils import month_from_now, send_signup_email
from django.utils.translation import ugettext_lazy as _
from auditlog.registry import auditlog
from phonenumber_field.modelfields import PhoneNumberField
from django.contrib.postgres.fields import JSONField
# import logging
from uuid import uuid4
import logging
from smtplib import SMTPAuthenticationError
VERBOSE_NAME = _('Webapp')
EMAIL_REGEX = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
class Tag(models.Model):
@@ -64,7 +66,7 @@ class Event(BaseFeed):
start_time = models.DateTimeField(default=timezone.now)
end_time = models.DateTimeField(default=timezone.now)
signupForm = models.ManyToManyField(
'SignupForm', blank=True)
'SignupForm', blank=True, related_name="event")
location = models.CharField(max_length=255, blank=True)
def __str__(self):
@@ -116,6 +118,13 @@ class SignupForm(models.Model):
properties[id] = {
"type": "string"
}
elif question_type == "email":
# Format is just a "FYI" field, so we also have pattern.
properties[id] = {
"type": "string",
"format": "email",
"pattern": EMAIL_REGEX
}
elif question_type == "radiobutton":
options = q["options"]
regexes = map(lambda x: f"^{x}$", options)
@@ -163,6 +172,10 @@ class Signup(models.Model):
answer = JSONField()
# Answer we use in signupForm signups field. Frontend uses first questions answer as this value.
list_name = models.CharField(_('Name'), max_length=255)
# If there is email in questions, we save it as own field
email = models.EmailField(blank=True, null=True)
# Random unique identifier. Used for signup editing by the user.
uuid = models.UUIDField(default=uuid4, editable=False)
def __str__(self):
return f"{self.signupForm}: {self.list_name} ({self.pk})"
@@ -172,6 +185,21 @@ class Signup(models.Model):
verbose_name_plural = _('Sign-ups')
@receiver(post_save, sender=Signup)
def email_on_singup(sender, instance, created, **kwargs):
"""Send email validation."""
if not settings.ENABLE_AUTOMATIC_EMAILS:
return
try:
if created and instance.email:
# TODO: Possible bug due to many-to-many relationship with events and forms.
subject = _(f"Olet ilmoittautunut tapahtumaan {instance.signupForm.event.first().title}")
send_signup_email(instance.email, subject, instance.id, instance.uuid)
except SMTPAuthenticationError:
logging.error('Failed to send email to signup')
class BaseRole(models.Model):
"""Base model for occupations/roles."""