Funkce na posílání e-mailu rozhodně nemá být uvnitř View
This commit is contained in:
parent
8d09fd5389
commit
04508206bb
2 changed files with 60 additions and 54 deletions
57
korektury/utils.py
Normal file
57
korektury/utils.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
from django.core.mail import EmailMessage
|
||||||
|
from django.http import HttpRequest
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from korektury.models import Komentar, Oprava
|
||||||
|
from personalni.models import Organizator
|
||||||
|
|
||||||
|
|
||||||
|
def send_email_notification_komentar(oprava: Oprava, autor: Organizator, request: HttpRequest):
|
||||||
|
''' Rozesle e-mail pri pridani komentare / opravy,
|
||||||
|
ktery obsahuje text vlakna opravy.
|
||||||
|
'''
|
||||||
|
|
||||||
|
# parametry e-mailu
|
||||||
|
#odkaz = "https://mam.mff.cuni.cz/korektury/{}/".format(oprava.pdf.pk)
|
||||||
|
odkaz = request.build_absolute_uri(reverse('korektury', kwargs={'pdf': oprava.pdf.pk}))
|
||||||
|
odkaz = f"{odkaz}#op{oprava.id}-pointer"
|
||||||
|
from_email = 'korekturovatko@mam.mff.cuni.cz'
|
||||||
|
subject = 'Nová korektura od {} v {}'.format(autor, oprava.pdf.nazev)
|
||||||
|
texty = [(oprava.autor.osoba.plne_jmeno(),oprava.text)]
|
||||||
|
for kom in Komentar.objects.filter(oprava=oprava):
|
||||||
|
texty.append((kom.autor.osoba.plne_jmeno(),kom.text))
|
||||||
|
optext = "\n\n\n".join([": ".join(t) for t in texty])
|
||||||
|
text = u"Text komentáře:\n\n{}\n\n=== Konec textu komentáře ===\n\
|
||||||
|
\nodkaz do korekturovátka: {}\n\
|
||||||
|
\nVaše korekturovátko\n".format(optext, odkaz)
|
||||||
|
|
||||||
|
# Prijemci e-mailu
|
||||||
|
emails = set()
|
||||||
|
# e-mail autora korektury
|
||||||
|
email = oprava.autor.osoba.email
|
||||||
|
if email:
|
||||||
|
emails.add(email)
|
||||||
|
|
||||||
|
# nalezeni e-mailu na autory komentaru
|
||||||
|
for komentar in oprava.komentar_set.all():
|
||||||
|
email_komentujiciho = komentar.autor.osoba.email
|
||||||
|
if email_komentujiciho:
|
||||||
|
emails.add(email_komentujiciho)
|
||||||
|
|
||||||
|
# zodpovedni orgove
|
||||||
|
for org in oprava.pdf.orgove.all():
|
||||||
|
email_zobpovedny = org.osoba.email
|
||||||
|
if email_zobpovedny:
|
||||||
|
emails.add(email_zobpovedny)
|
||||||
|
|
||||||
|
# odstran e-mail autora opravy
|
||||||
|
email = autor.osoba.email
|
||||||
|
if email:
|
||||||
|
emails.discard(email)
|
||||||
|
|
||||||
|
EmailMessage(
|
||||||
|
subject=subject,
|
||||||
|
body=text,
|
||||||
|
from_email=from_email,
|
||||||
|
to=list(emails),
|
||||||
|
).send()
|
|
@ -2,9 +2,9 @@ from django.shortcuts import get_object_or_404, render
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import HttpResponseForbidden
|
from django.http import HttpResponseForbidden
|
||||||
from django.core.mail import EmailMessage
|
|
||||||
from django.db.models import Count,Q
|
from django.db.models import Count,Q
|
||||||
|
|
||||||
|
from .utils import send_email_notification_komentar
|
||||||
from .models import Oprava,Komentar,KorekturovanePDF, Organizator
|
from .models import Oprava,Komentar,KorekturovanePDF, Organizator
|
||||||
|
|
||||||
class KorekturyListView(generic.ListView):
|
class KorekturyListView(generic.ListView):
|
||||||
|
@ -82,7 +82,7 @@ class KorekturyView(generic.TemplateView):
|
||||||
|
|
||||||
op = Oprava(x=x,y=y, autor=autor, text=text, strana=strana, pdf=self.pdf)
|
op = Oprava(x=x,y=y, autor=autor, text=text, strana=strana, pdf=self.pdf)
|
||||||
op.save()
|
op.save()
|
||||||
self.send_email_notification_komentar(op,autor)
|
send_email_notification_komentar(op, autor, request)
|
||||||
elif (action == 'del'):
|
elif (action == 'del'):
|
||||||
id = int(q.get('id'))
|
id = int(q.get('id'))
|
||||||
op = Oprava.objects.get(id=id)
|
op = Oprava.objects.get(id=id)
|
||||||
|
@ -105,7 +105,7 @@ class KorekturyView(generic.TemplateView):
|
||||||
text = q.get('txt')
|
text = q.get('txt')
|
||||||
kom = Komentar(oprava=op,autor=autor,text=text)
|
kom = Komentar(oprava=op,autor=autor,text=text)
|
||||||
kom.save()
|
kom.save()
|
||||||
self.send_email_notification_komentar(op,autor)
|
send_email_notification_komentar(op, autor, request)
|
||||||
elif (action == 'update-comment'):
|
elif (action == 'update-comment'):
|
||||||
id = int(q.get('id'))
|
id = int(q.get('id'))
|
||||||
kom = Komentar.objects.get(id=id)
|
kom = Komentar.objects.get(id=id)
|
||||||
|
@ -127,57 +127,6 @@ class KorekturyView(generic.TemplateView):
|
||||||
context['autor'] = autor
|
context['autor'] = autor
|
||||||
return render(request, 'korektury/opraf.html',context)
|
return render(request, 'korektury/opraf.html',context)
|
||||||
|
|
||||||
def send_email_notification_komentar(self, oprava, autor):
|
|
||||||
''' Rozesle e-mail pri pridani komentare / opravy,
|
|
||||||
ktery obsahuje text vlakna opravy.
|
|
||||||
'''
|
|
||||||
|
|
||||||
# parametry e-mailu
|
|
||||||
#odkaz = "https://mam.mff.cuni.cz/korektury/{}/".format(oprava.pdf.pk)
|
|
||||||
from django.urls import reverse
|
|
||||||
odkaz = self.request.build_absolute_uri(reverse('korektury', kwargs={'pdf': oprava.pdf.pk}))
|
|
||||||
odkaz = f"{odkaz}#op{oprava.id}-pointer"
|
|
||||||
from_email = 'korekturovatko@mam.mff.cuni.cz'
|
|
||||||
subject = 'Nová korektura od {} v {}'.format(autor, oprava.pdf.nazev)
|
|
||||||
texty = [(oprava.autor.osoba.plne_jmeno(),oprava.text)]
|
|
||||||
for kom in Komentar.objects.filter(oprava=oprava):
|
|
||||||
texty.append((kom.autor.osoba.plne_jmeno(),kom.text))
|
|
||||||
optext = "\n\n\n".join([": ".join(t) for t in texty])
|
|
||||||
text = u"Text komentáře:\n\n{}\n\n=== Konec textu komentáře ===\n\
|
|
||||||
\nodkaz do korekturovátka: {}\n\
|
|
||||||
\nVaše korekturovátko\n".format(optext, odkaz)
|
|
||||||
|
|
||||||
# Prijemci e-mailu
|
|
||||||
emails = set()
|
|
||||||
# e-mail autora korektury
|
|
||||||
email = oprava.autor.osoba.email
|
|
||||||
if email:
|
|
||||||
emails.add(email)
|
|
||||||
|
|
||||||
# nalezeni e-mailu na autory komentaru
|
|
||||||
for komentar in oprava.komentar_set.all():
|
|
||||||
email_komentujiciho = komentar.autor.osoba.email
|
|
||||||
if email_komentujiciho:
|
|
||||||
emails.add(email_komentujiciho)
|
|
||||||
|
|
||||||
# zodpovedni orgove
|
|
||||||
for org in oprava.pdf.orgove.all():
|
|
||||||
email_zobpovedny = org.osoba.email
|
|
||||||
if email_zobpovedny:
|
|
||||||
emails.add(email_zobpovedny)
|
|
||||||
|
|
||||||
# odstran e-mail autora opravy
|
|
||||||
email = autor.osoba.email
|
|
||||||
if email:
|
|
||||||
emails.discard(email)
|
|
||||||
|
|
||||||
EmailMessage(
|
|
||||||
subject=subject,
|
|
||||||
body=text,
|
|
||||||
from_email=from_email,
|
|
||||||
to=list(emails),
|
|
||||||
).send()
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context['pdf'] = self.pdf
|
context['pdf'] = self.pdf
|
||||||
|
|
Loading…
Reference in a new issue