Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple business logic from views #56

Open
yld-weng opened this issue Jan 15, 2025 · 0 comments
Open

Decouple business logic from views #56

yld-weng opened this issue Jan 15, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@yld-weng
Copy link
Collaborator

Introduce the service layer (services.py) and separate business logic from views.

E.g.

    def form_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 token
        survey_link = f"http://localhost:8000/survey/{survey.pk}/{token}/"

        # Send the email
        send_mail(
            'Your Survey Invitation',
            f'Click here to start the survey: {survey_link}',
            '[email protected]',
            [email],
            fail_silently=False,
        )

        # Show success message
        messages.success(self.request, f'Invitation sent to {email}.')
        return super().form_valid(form)

can be rewritten as:

def send_survey_invitation(email, request):
    survey = Survey.objects.first()

    if not survey:
        raise ValueError("No survey available to send invitations.")

    # Create an invitation
    invitation = Invitation.objects.create(survey=survey)
    token = invitation.token

    # Generate the survey link with the token
    survey_link = f"http://localhost:8000/survey/{survey.pk}/{token}/"

    # Send the email
    send_mail(
        'Your Survey Invitation',
        f'Click here to start the survey: {survey_link}',
        '[email protected]',
        [email],
        fail_silently=False,
    )

    # Add a success message
    add_message(request, message_constants.SUCCESS, f'Invitation sent to {email}.')

and

    def form_valid(self, form):
        email = form.cleaned_data['email']

        try:
            # Call the service to handle the business logic
            send_survey_invitation(email, self.request)
        except ValueError as e:
            # Handle cases where the service raises an error
            messages.error(self.request, str(e))
            return self.form_invalid(form)

        return super().form_valid(form)

This separation makes the send_survey_invitation function easier to test.

@yld-weng yld-weng added the enhancement New feature or request label Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant