From 04f8f9ad2c5ffa30540f254f1847f7e8d710a8f5 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Wed, 10 Feb 2021 22:08:27 -0500 Subject: [PATCH] feat: added email confirmation --- config/settings.py | 12 +++++ innovate/forms.py | 6 ++- .../migrations/0003_auto_20210211_0122.py | 23 +++++++++ innovate/models.py | 4 +- .../templates/innovate/email_template.html | 18 +++++++ innovate/templates/innovate/signup.html | 10 ++++ innovate/views.py | 49 +++++++++++++++++-- my_secrets/definitions.py | 4 +- 8 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 innovate/migrations/0003_auto_20210211_0122.py create mode 100644 innovate/templates/innovate/email_template.html diff --git a/config/settings.py b/config/settings.py index 3ba6717..6312ccc 100644 --- a/config/settings.py +++ b/config/settings.py @@ -154,3 +154,15 @@ MEDIA_URL = '/media/' LOGIN_REDIRECT_URL = 'blog-home' LOGIN_URL = 'login' + + +# Email + +EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' + +EMAIL_HOST = 'smtp.gmail.com' +EMAIL_USE_TLS = True +EMAIL_PORT = 587 +EMAIL_HOST_USER = secrets.EMAIL_USER +EMAIL_HOST_PASSWORD = secrets.EMAIL_PASSWORD + diff --git a/innovate/forms.py b/innovate/forms.py index 45c3d1f..9b6c54c 100644 --- a/innovate/forms.py +++ b/innovate/forms.py @@ -6,10 +6,12 @@ from .models import Competitor, Team class CompetitorForm(forms.ModelForm): name = forms.CharField(label='Full Name', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'John Doe'})) email = forms.EmailField(label='Email', widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'example@email.com'})) - + school = forms.CharField(label='School', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'TJHSST'})) + county = forms.CharField(label='County', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Fairfax'})) + class Meta: model = Competitor - fields = ['name', 'email'] + fields = ['name', 'email', 'school', 'county'] def __init__(self, *args, **kwargs): super(CompetitorForm, self).__init__(*args, **kwargs) diff --git a/innovate/migrations/0003_auto_20210211_0122.py b/innovate/migrations/0003_auto_20210211_0122.py new file mode 100644 index 0000000..4ceb135 --- /dev/null +++ b/innovate/migrations/0003_auto_20210211_0122.py @@ -0,0 +1,23 @@ +# Generated by Django 3.1.6 on 2021-02-11 01:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('innovate', '0002_auto_20210208_1549'), + ] + + operations = [ + migrations.AddField( + model_name='competitor', + name='county', + field=models.CharField(blank=True, default='', max_length=20), + ), + migrations.AddField( + model_name='competitor', + name='school', + field=models.CharField(blank=True, default='', max_length=20), + ), + ] diff --git a/innovate/models.py b/innovate/models.py index 9133dea..93387d6 100644 --- a/innovate/models.py +++ b/innovate/models.py @@ -22,7 +22,9 @@ class Team(models.Model): class Competitor(models.Model): name = models.CharField(max_length=20, blank=True, default='') - email = models.EmailField(max_length = 254) + email = models.EmailField(max_length=254) + school = models.CharField(max_length=20, blank=True, default='') + county = models.CharField(max_length=20, blank=True, default='') is_leader = models.BooleanField(default=False) team = models.ForeignKey(Team, related_name="competitors", on_delete=models.CASCADE) diff --git a/innovate/templates/innovate/email_template.html b/innovate/templates/innovate/email_template.html new file mode 100644 index 0000000..5b27de0 --- /dev/null +++ b/innovate/templates/innovate/email_template.html @@ -0,0 +1,18 @@ +

Hey team, {{ team.name }}!

+ +

Thanks for registering for InnovateTJ! This is a confirmation email for your registration. Your team is number {{ team.number }}. This number will be used for pitching and grading purposes. If you have any questions prior to the event, please email us here.

+
+Your team includes: +
+{% for member in members %} +

Team Member {{ forloop.counter }}: {{ member.name }}

+

From {{ member.school }} @ {{ member.school }}

+
+{% endfor %} +

We'll be seeing you on February 20th, 2021 from 9AM to 5AM, and more information will be sent out as the event nears! But until then, keep innovating.

+
+

+ Best,
+ InnovateTJ Team
+ tjhsstlaunchx@gmail.com +

\ No newline at end of file diff --git a/innovate/templates/innovate/signup.html b/innovate/templates/innovate/signup.html index 203b5d5..b90bbce 100644 --- a/innovate/templates/innovate/signup.html +++ b/innovate/templates/innovate/signup.html @@ -41,6 +41,16 @@ {{ form.email.label_tag }} +
+
+ {{ form.school }} + {{ form.school.label_tag }} +
+
+ {{ form.county }} + {{ form.county.label_tag }} +
+
{% endfor %} {% for hidden in team_form.hidden_fields %} diff --git a/innovate/views.py b/innovate/views.py index edead18..7e9c563 100644 --- a/innovate/views.py +++ b/innovate/views.py @@ -3,29 +3,70 @@ from django.shortcuts import render, redirect from .forms import CompetitorFormset, CompetitorForm, TeamForm from .models import Competitor +from django.template.loader import render_to_string +from django.utils.html import strip_tags +from config.settings import EMAIL_HOST_USER +from django.core.mail import EmailMultiAlternatives, send_mail + # Create your views here. def home(request): return render(request, 'innovate/index.html') def signup(request): + formset = CompetitorFormset() + team_form = TeamForm() + if request.method == 'POST': formset = CompetitorFormset(request.POST) team_form = TeamForm(request.POST, request.FILES) if formset.is_valid() and team_form.is_valid(): team = team_form.save() is_leader = True + members = [] for form in formset: name = form.cleaned_data.get('name') email = form.cleaned_data.get('email') - if name and email: - m = Competitor(name=name, email=email, is_leader=is_leader, team=team) + school = form.cleaned_data.get('school') + county = form.cleaned_data.get('school') + if name and email and school and county: + m = Competitor(name=name, email=email, school=school, county=county, is_leader=is_leader, team=team) m.save() is_leader = False + members.append(m) + send_confirmation(request, team,members) return redirect('landing') - formset = CompetitorFormset() - team_form = TeamForm() return render(request, 'innovate/signup.html', {'formset': formset, 'team_form': team_form}) + +def send_confirmation(request, team, members): + #subject = "🥳 InnovateTJ Signup Confirmation 🥳" + subject = "OK NO THIS ONE IS THE LAST EMAIL" + #recepients = [] + #for member in members: + # recepients.append(member.email) + recepients = ['rushilwiz@gmail.com', 'ssuganuma04@gmail.com'] + + context = { + 'team': team, + 'members': members + } + + html_message = render_to_string('innovate/email_template.html', context=context) + plain_message = strip_tags(html_message) + sender = [EMAIL_HOST_USER] + email = EmailMultiAlternatives( + subject, + plain_message, + EMAIL_HOST_USER, + recepients, + reply_to=sender + ) + print(request.POST.getlist('rep')) + email.attach_alternative(html_message, "text/html") + print("### EMAIL SENT ###") + print (recepients) + email.send(fail_silently=False) + def confirm(request): return render(request, 'innovate/confirm.html') \ No newline at end of file diff --git a/my_secrets/definitions.py b/my_secrets/definitions.py index bde4720..022499f 100644 --- a/my_secrets/definitions.py +++ b/my_secrets/definitions.py @@ -8,5 +8,7 @@ SECRET_KEYS = [ # start with your Django secret key like this: "SECRET_KEY", - "DEBUG" + "DEBUG", + "EMAIL_USER", + "EMAIL_PASSWORD" ]