Automatische Meldung an UNB

Fixes #26
This commit is contained in:
Gunther Weissenbaeck 2024-02-06 00:13:14 +01:00
parent 15a63e0c75
commit acca5da39d
12 changed files with 170 additions and 1 deletions

View file

@ -1,11 +1,20 @@
import environ
import names
from django.contrib.auth.decorators import login_required
from django.db.models import Q, Sum
from django.shortcuts import HttpResponse, redirect, render
from django.shortcuts import redirect, render, HttpResponse
from django.core.mail import send_mail, BadHeaderError
from smtplib import SMTPException
from .forms import BirdAddForm, BirdEditForm
from .models import Bird, FallenBird
from sendemail.models import BirdEmail
from sendemail.message import messagebody
env = environ.Env()
@login_required(login_url="account_login")
def bird_create(request):
@ -23,6 +32,26 @@ def bird_create(request):
fs.rescuer_id = rescuer_id
fs.save()
request.session["rescuer_id"] = None
# Send email to all related email addresses
email_addresses = BirdEmail.objects.filter(bird=fs.bird_id)
bird = Bird.objects.get(id=fs.bird_id)
try:
send_mail(
subject="Wildvogel gefunden!",
message=messagebody(
fs.date_found, bird, fs.place, fs.diagnostic_finding
),
from_email=env("DEFAULT_FROM_EMAIL"),
recipient_list=[
email.email.email_address for email in email_addresses
],
)
except BadHeaderError:
return HttpResponse("Invalid header found.")
except SMTPException as e:
print("There was an error sending an email: ", e)
return redirect("bird_all")
context = {"form": form}
return render(request, "bird/bird_create.html", context)