2
0
Fork 0

only resend registration sms every 15 minutes

This commit is contained in:
Andreas (@xAndy) Zimmermann 2022-04-20 19:18:48 +02:00
parent 15a1182fea
commit d1c0c9e161
4 changed files with 74 additions and 5 deletions

View File

@ -0,0 +1,22 @@
# Generated by Django 4.0.4 on 2022-04-20 17:01
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
("app", "0001_initial"),
]
operations = [
migrations.AddField(
model_name="logintoken",
name="sent_at",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
]

View File

@ -0,0 +1,22 @@
# Generated by Django 4.0.4 on 2022-04-20 17:06
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
("app", "0002_logintoken_sent_at"),
]
operations = [
migrations.AddField(
model_name="message",
name="created_at",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
]

View File

@ -41,9 +41,7 @@ class Helper(models.Model):
def send_confirmation(self): def send_confirmation(self):
(token, created) = LoginToken.objects.get_or_create(helper=self) (token, created) = LoginToken.objects.get_or_create(helper=self)
text = f"Hallo {self.name}, hier ist dein Registrierungslink zum Kontaktfestival http://short.url{token.get_absolute_url()}" token.send()
msg = Message(to=self, text=text)
msg.save()
return token return token
# current or next shift # current or next shift
@ -79,9 +77,10 @@ class Message(models.Model):
# remove limit and send long messages in multiple messages? # remove limit and send long messages in multiple messages?
text = models.CharField(max_length=160) text = models.CharField(max_length=160)
to = models.ForeignKey(Helper, on_delete=models.CASCADE) to = models.ForeignKey(Helper, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self): def __str__(self):
return f"{self.to.name}: {self.text}" return f"{self.to.name}({self.created_at}): {self.text}"
def gen_token(): def gen_token():
@ -93,6 +92,14 @@ class LoginToken(models.Model):
max_length=20, primary_key=True, default=gen_token, editable=False max_length=20, primary_key=True, default=gen_token, editable=False
) )
helper = models.ForeignKey(Helper, on_delete=models.CASCADE) helper = models.ForeignKey(Helper, on_delete=models.CASCADE)
sent_at = models.DateTimeField(auto_now_add=True)
def send(self):
text = f"Hallo {self.helper.name}, hier ist dein Registrierungslink zum Kontaktfestival http://short.url{self.get_absolute_url()}"
msg = Message(to=self.helper, text=text)
msg.save()
self.sent_at = timezone.now()
self.save()
def get_absolute_url(self): def get_absolute_url(self):
return reverse("token_login", kwargs={"token": self.id}) return reverse("token_login", kwargs={"token": self.id})

View File

@ -6,6 +6,7 @@ from django.db.models.fields import DateTimeField
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
import datetime
def index(request): def index(request):
@ -22,7 +23,7 @@ def index(request):
imp_shift = request.helper.important_shift() imp_shift = request.helper.important_shift()
print(imp_shift) print(imp_shift)
if imp_shift and imp_shift.is_running(): if imp_shift and imp_shift.is_running():
context["current_shift"] = imp_shift context["current_shift"] = imp_shift
free_shifts = ( free_shifts = (
@ -87,6 +88,23 @@ def register(request):
if not form.is_valid(): if not form.is_valid():
context["form"] = form context["form"] = form
return render(request, "register.html", context) return render(request, "register.html", context)
helper = Helper.objects.filter(pk=form.cleaned_data["phone"]).first()
if helper:
token = helper.logintoken_set.first()
# todo: resend login link after x minutes?
messages.add_message(
request,
messages.INFO,
f"Du hast einen Login-Link an deine Telefonnummer erhalten. Falls du die SMS nicht erhalten hast, probiere es in 15 Minuten noch ein mal.",
)
messages.add_message(
request,
messages.INFO,
f"DEBUG:{token.get_absolute_url()}",
)
if token.sent_at < (timezone.now() - datetime.timedelta(minutes=10)):
token.send()
return render(request, "register.html", context)
helper = Helper( helper = Helper(
name=form.cleaned_data["name"], phone=form.cleaned_data["phone"] name=form.cleaned_data["name"], phone=form.cleaned_data["phone"]