Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions applications/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv

from django.contrib import messages
from django.db import IntegrityError
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.defaultfilters import striptags
Expand Down Expand Up @@ -36,20 +37,34 @@ def apply(request, page_url):
menu = EventPageMenu.objects.filter(event=event)

form = ApplicationForm(request.POST or None, form=form_obj)

if form.is_valid():
form.save()
messages.success(request, _("Yay! Your application has been saved. You'll hear from us soon!"))

return render(
request,
"applications/apply.html",
{
"event": event,
"menu": menu,
"form_obj": form_obj,
},
)
try:
form.save()
messages.success(request, _("Yay! Your application has been saved. You'll hear from us soon!"))

return render(
request,
"applications/apply.html",
{
"event": event,
"menu": menu,
"form_obj": form_obj,
},
)
except IntegrityError:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this fixes the issue, it assumes that any IntegrityError is caused by this issue. Now this may be true at the moment, but things may change. So we could merge this and catch this issue at this point.

There is some validation on the form that appears to be trying to check against this same constraint, and that is where I'd prefer the fix to go; https://github.com/DjangoGirls/djangogirls/blob/main/applications/forms.py#L40

# getting email for the form as email is defind as question 2
email = request.POST.get("question_2", "")
# this email is already exist for the particulur event
messages.error(request, f"Application with email address {email} already exists for this event.")
return render(
request,
"applications/apply.html",
{
"event": event,
"menu": menu,
"form_obj": form_obj,
},
)

number_of_email_questions = Question.objects.filter(question_type="email", form=form_obj).count()

Expand Down