parent
15a63e0c75
commit
acca5da39d
12 changed files with 170 additions and 1 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue