You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce the service layer (services.py) and separate business logic from views.
E.g.
defform_valid(self, form):
email=form.cleaned_data['email']
survey=Survey.objects.first()
invitation=Invitation.objects.create(survey=survey)
token=invitation.token# Generate the survey link with the tokensurvey_link=f"http://localhost:8000/survey/{survey.pk}/{token}/"# Send the emailsend_mail(
'Your Survey Invitation',
f'Click here to start the survey: {survey_link}',
'[email protected]',
[email],
fail_silently=False,
)
# Show success messagemessages.success(self.request, f'Invitation sent to {email}.')
returnsuper().form_valid(form)
can be rewritten as:
defsend_survey_invitation(email, request):
survey=Survey.objects.first()
ifnotsurvey:
raiseValueError("No survey available to send invitations.")
# Create an invitationinvitation=Invitation.objects.create(survey=survey)
token=invitation.token# Generate the survey link with the tokensurvey_link=f"http://localhost:8000/survey/{survey.pk}/{token}/"# Send the emailsend_mail(
'Your Survey Invitation',
f'Click here to start the survey: {survey_link}',
'[email protected]',
[email],
fail_silently=False,
)
# Add a success messageadd_message(request, message_constants.SUCCESS, f'Invitation sent to {email}.')
and
defform_valid(self, form):
email=form.cleaned_data['email']
try:
# Call the service to handle the business logicsend_survey_invitation(email, self.request)
exceptValueErrorase:
# Handle cases where the service raises an errormessages.error(self.request, str(e))
returnself.form_invalid(form)
returnsuper().form_valid(form)
This separation makes the send_survey_invitation function easier to test.
The text was updated successfully, but these errors were encountered:
Introduce the service layer (
services.py
) and separate business logic from views.E.g.
can be rewritten as:
and
This separation makes the
send_survey_invitation
function easier to test.The text was updated successfully, but these errors were encountered: