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)

View file

@ -88,6 +88,8 @@ JAZZMIN_SETTINGS = {
"costs.Costs": "fas fa-solid fa-money-bill",
"contact.Contact": "fas fa-solid fa-address-card",
"contact.ContactTag": "fas fa-solid fa-tags",
"sendemail.Emailadress": "fas fa-solid fa-envelope",
"sendemail.BirdEmail": "fas fa-solid fa-envelope",
},
# Icons that are used when one is not manually specified
# "default_icon_parents": "fas fa-chevron-circle-right",

View file

@ -86,6 +86,7 @@ INSTALLED_APPS = [
"contact",
"costs",
"export",
"sendemail",
]
MIDDLEWARE = [

View file

19
app/sendemail/admin.py Normal file
View file

@ -0,0 +1,19 @@
from django.contrib import admin
from .models import Emailadress, BirdEmail
@admin.register(Emailadress)
class EmailaddressAdmin(admin.ModelAdmin):
list_display = ["email_address", "created_at", "updated_at", "user"]
search_fields = ["email_address"]
list_filter = ["created_at", "updated_at", "user"]
list_per_page = 20
@admin.register(BirdEmail)
class BirdEmailAdmin(admin.ModelAdmin):
list_display = ["bird", "email"]
search_fields = ["bird", "email"]
list_filter = ["bird", "email"]
list_per_page = 20

8
app/sendemail/apps.py Normal file
View file

@ -0,0 +1,8 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _
class SendemailConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "sendemail"
verbose_name = _("Untere Naturschutzbehörde")

25
app/sendemail/message.py Normal file
View file

@ -0,0 +1,25 @@
# the message body for the email should have placeholders for the bird name, the date found and the diagnosis
def messagebody(
date,
bird,
place,
diagnosis,
) -> str:
text = f"""
Guten Tag,
am {date} wurde in der NABU Wildvogelhilfe ein Vogel der Art {bird} aufgenomen.
Der Fundort laut Finder*in war: {place}
Die Diagnose bei Fund lautet: {diagnosis}
Mit freundlichen Grüßen
NABU Wildvogelhilfe Jena
Untergliederung des
NABU Kreisverband Jena e.V.
Schillergässchen 5
07745 Jena
"""
return text

View file

@ -0,0 +1,44 @@
# Generated by Django 4.2.7 on 2024-02-05 23:09
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('bird', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Emailadress',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email_address', models.EmailField(max_length=254)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Benutzer')),
],
options={
'verbose_name': 'Emailadresse',
'verbose_name_plural': 'Emailadressen',
},
),
migrations.CreateModel(
name='BirdEmail',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bird', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bird.bird', verbose_name='Vogel')),
('email', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sendemail.emailadress', verbose_name='Emailadresse')),
],
options={
'verbose_name': 'Vogel-Email',
'verbose_name_plural': 'Vogel-Emails',
},
),
]

View file

35
app/sendemail/models.py Normal file
View file

@ -0,0 +1,35 @@
from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from bird.models import Bird
class Emailadress(models.Model):
email_address = models.EmailField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name=_("Benutzer")
)
def __str__(self):
return self.email_address
class Meta:
verbose_name = _("Emailadresse")
verbose_name_plural = _("Emailadressen")
class BirdEmail(models.Model):
bird = models.ForeignKey(Bird, on_delete=models.CASCADE, verbose_name=_("Vogel"))
email = models.ForeignKey(
Emailadress, on_delete=models.CASCADE, verbose_name=_("Emailadresse")
)
def __str__(self):
return f"{self.bird} - {self.email}"
class Meta:
verbose_name = _("Vogel-Email")
verbose_name_plural = _("Vogel-Emails")

3
app/sendemail/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
app/sendemail/views.py Normal file
View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.