launchx/innovate/views.py

72 lines
2.4 KiB
Python

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')
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')
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')