Send link via email

This commit is contained in:
Gabriel Augendre 2018-05-22 00:45:54 +02:00
parent bcd7f6dc18
commit c16d0db8ca
8 changed files with 59 additions and 15 deletions

View file

@ -13,8 +13,8 @@ gunicorn = "*"
dj-database-url = "*"
"psycopg2-binary" = "*"
django-dotenv = "*"
django-mailgun = "*"
uuid = "*"
django-anymail = {extras = ["mailgun"]}
[dev-packages]

17
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "43a1e1084af30da4a5ce423a0086c023152fb78a989cf7425035ea6d2a02fd0e"
"sha256": "9a19cd2cc896f2304c510ff49bada489a31b1b1ac412af5c44c36f5cf43605a3"
},
"pipfile-spec": 6,
"requires": {
@ -46,6 +46,14 @@
"index": "pypi",
"version": "==2.0.5"
},
"django-anymail": {
"hashes": [
"sha256:9058c1009adbd926213fb2f18d20a8592e23c63f845a4aaa63c51b6476a4638a",
"sha256:969ea878da44b54ef2229241da9f0e9a72886c9dea14186ba6733939b63320f4"
],
"index": "pypi",
"version": "==2.2"
},
"django-bootstrap4": {
"hashes": [
"sha256:6db4a27b33851833e68b96344f9df063150dcace8d4787ebfc21eceb55196945"
@ -61,13 +69,6 @@
"index": "pypi",
"version": "==1.4.2"
},
"django-mailgun": {
"hashes": [
"sha256:d795076d18c0aa66fbac37f8b428f036417a3ec7ecc2d6499c021d318d60bfff"
],
"index": "pypi",
"version": "==0.9.1"
},
"gunicorn": {
"hashes": [
"sha256:7ef2b828b335ed58e3b64ffa84caceb0a7dd7c5ca12f217241350dec36a1d5dc",

View file

@ -1,4 +1,4 @@
# Generated by Django 2.0.5 on 2018-05-21 21:21
# Generated by Django 2.0.5 on 2018-05-21 21:57
from django.db import migrations, models
import django.db.models.deletion
@ -51,6 +51,7 @@ class Migration(migrations.Migration):
('first_name', models.CharField(max_length=100, verbose_name='prénom')),
('last_name', models.CharField(max_length=100, verbose_name='nom')),
('phone_number', models.CharField(max_length=10, verbose_name='numéro de téléphone')),
('email', models.EmailField(max_length=254, verbose_name='adresse email')),
],
options={
'verbose_name': 'enseignant',

View file

@ -13,6 +13,7 @@ class Teacher(models.Model):
first_name = models.CharField('prénom', max_length=100)
last_name = models.CharField('nom', max_length=100)
phone_number = models.CharField('numéro de téléphone', max_length=10)
email = models.EmailField('adresse email')
def get_absolute_url(self):
from django.urls import reverse

View file

@ -0,0 +1,10 @@
<html>
<p>Bonjour {{ teacher.first_name }},</p>
<p>
Voici votre lien pour la gestion des manuels scolaires :
</p>
<p style="text-align: center;">
{{ link }}
</p>
</html>

View file

@ -11,7 +11,9 @@
</div>
<div class="alert alert-warning" role="alert">
<h4 class="alert-heading">Attention</h4>
<p class="mb-0">Pensez à <strong>sauvegarder cette page dans vos favoris</strong>.<br>
<p class="mb-0">
Ce lien vous a été envoyé par email. N'oubliez pas de vérifier vos courriers indésirables.<br>
Pensez à <strong>sauvegarder cette page dans vos favoris</strong>.<br>
Si vous perdez ce lien, vous ne risquez de ne plus être en mesure d'accéder à votre espace personnel
et vous ne pourrez plus ajouter de livre à votre liste.
</p>

View file

@ -1,5 +1,10 @@
import os
from django.conf import settings
from django.contrib import messages
from django.core.mail import EmailMultiAlternatives
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.urls import reverse
from django.views.generic import CreateView, ListView
@ -9,7 +14,7 @@ from manuels.models import Teacher, Book
class HomePageView(CreateView):
model = Teacher
fields = ['first_name', 'last_name', 'phone_number']
fields = '__all__'
template_name = 'manuels/home_page.html'
def get(self, request, *args, **kwargs):
@ -19,6 +24,26 @@ class HomePageView(CreateView):
return super().get(request, *args, **kwargs)
def form_valid(self, form):
response = super().form_valid(form)
dest = form.cleaned_data['email']
link = self.request.build_absolute_uri(reverse('list_books', args=[str(self.object.pk)]))
msg = EmailMultiAlternatives(
subject='Gestion des manuels scolaires',
body=f'Bonjour {self.object.first_name},\n'
f'Voici votre lien pour la gestion des manuels scolaires : {link}',
from_email=settings.SERVER_EMAIL,
to=[dest],
)
reply_to = [os.getenv('REPLY_TO')]
if reply_to:
msg.reply_to = reply_to
msg.attach_alternative(
render_to_string('manuels/email.html', {'link': link, 'teacher': self.object}), "text/html"
)
msg.send()
return response
class BaseTeacherView:
teacher = None

View file

@ -50,6 +50,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'anymail',
'bootstrap4',
'manuels',
]
@ -134,6 +135,9 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
LOGIN_REDIRECT_URL = 'rooms-list'
EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
MAILGUN_ACCESS_KEY = os.getenv('MAILGUN_ACCESS_KEY', '')
MAILGUN_SERVER_NAME = os.getenv('MAILGUN_SERVER_NAME', '')
ANYMAIL = {
"MAILGUN_API_KEY": os.getenv('MAILGUN_ACCESS_KEY', ''),
"MAILGUN_SENDER_DOMAIN": os.getenv('MAILGUN_SERVER_NAME', ''),
}
EMAIL_BACKEND = 'anymail.backends.mailgun.EmailBackend'