3
\$\begingroup\$

I have two cases here:

  1. the person who ordered a ticket clicks on "Permission" and the OrderConsentUpdate View updates the database.
  2. is the attendee him- or herself gives consent for one specific ticket. In that case, AttendeeConsentUpdate is called.

You can see both classes are very similar, but not still differ in essential variables. Is there a way to write that better?

class AttendeeConsentUpdate(View):

    @cached_property
    def attendee(self):
        return get_object_or_404(
            Attendee,
            ticket_reference=self.kwargs['ticket_reference'],
            ticket_code=self.kwargs['ticket_code'],
        )

    def post(self, request, *args, **kwargs):
        # Check if consent already exists which defines if consent entry will be
        # updated or if a new one will be created.
        consent = contact_by_email(self.attendee)

        # Check if consent already exists
        if consent:
            # If updated email already exists, selected object will be changed
            obj_email_exists = self.attendee.event.organizer.contacts.filter(
                email=self.attendee.email).first()
            if obj_email_exists:
                consent = obj_email_exists

            update_consent(
                consent=True,
                instance=consent,
                first_name=self.attendee.first_name,
                last_name=self.attendee.last_name,
                email=self.attendee.email,
                event=self.attendee.event,
            )

        # If consent is given and does not exist yet as entry
        else:
            new_consent = Contact(
                consent=True,
                email=self.attendee.email,
                first_name=self.attendee.first_name,
                last_name=self.attendee.last_name,
                organizer=self.attendee.event.organizer,
            )
            new_consent.save()
            new_consent.events.add(self.attendee.event)

            # Segment: Track assignment event
            distinct_id = segment_get_distinct_id(request)
            if distinct_id:
                analytics.track(distinct_id, 'Attendee consent via checklist', properties={
                    'consentEmail': self.attendee.email,
                })

        messages.success(request, _(
            "You will receive marketing updates. "
            "Unsubscribe by clicking the unsubscribe link in our emails."
        ))
        return redirect(self.attendee)


class OrderConsentUpdate(View):

    @cached_property
    def order(self):
        return get_object_or_404(
            Order,
            order_reference=self.kwargs['order_reference'],
            access_key=self.kwargs['access_key'],
        )

    def post(self, request, *args, **kwargs):
        # Check if consent already exists which defines if consent entry will be
        # updated or if a new one will be created.
        consent = contact_by_email(self.order)

        # Check if consent already exists
        if consent:
            # If updated email already exists, selected object will be changed
            obj_email_exists = self.order.event.organizer.contacts.filter(
                email=self.order.email).first()
            if obj_email_exists:
                consent = obj_email_exists

            update_consent(
                consent=True,
                instance=consent,
                first_name=self.order.billing.customer_first_name,
                last_name=self.order.billing.customer_last_name,
                email=self.order.email,
                event=self.order.event,
            )

        # If consent is given and does not exist yet as entry
        else:
            new_consent = Contact(
                consent=True,
                email=self.order.email,
                first_name=self.order.billing.customer_first_name,
                last_name=self.order.billing.customer_last_name,
                organizer=self.order.event.organizer,
            )
            new_consent.save()
            new_consent.events.add(self.order.event)

            # Segment: Track assignment event
            distinct_id = segment_get_distinct_id(request)
            if distinct_id:
                analytics.track(distinct_id, 'Order consent via checklist', properties={
                    'consentEmail': self.order.email,
                })

        messages.success(request, _(
            "You will receive marketing updates. "
            "Unsubscribe by clicking the unsubscribe link in our emails."
        ))
        return redirect(self.order)
\$\endgroup\$
  • \$\begingroup\$ Thank you for the feedback Sam. \$\endgroup\$ – Joey Coder Apr 23 '19 at 21:56
1
\$\begingroup\$

How about something like this:

class ConsentUpdate(View):
    def update_consent_if_exists(self):
        self.consent = contact_by_email(self.consent_test_obj)

        if self.consent:
            self.change_consent_object()
        else:
            self.add_new_consent()

    def add_new_consent(self):
        new_consent = Contact(
            consent=True,
            email=self.email,
            first_name=self.first_name,
            last_name=self.last_name,
            organizer=self.organizer,
        )
        new_consent.save()
        new_consent.events.add(self.event)
        self.track_assignment_event()

    def change_consent_object(self):
        obj_email_exists = self.organizer.contacts.filter(
            email=self.email).first()
        if obj_email_exists:
            self.consent = obj_email_exists
        update_consent(
            consent=True,
            instance=self.consent,
            first_name=self.first_name,
            last_name=self.last_name,
            email=self.email,
            event=self.event,
        )

    def track_assignment_event(self):
        distinct_id = segment_get_distinct_id(request)
        if distinct_id:
            analytics.track(distinct_id, self.consent_message, properties={
                'consentEmail': self.email,
            })
        messages.success(request, _(
            "You will receive marketing updates. "
            "Unsubscribe by clicking the unsubscribe link in our emails."
        ))


class AttendeeConsentUpdate(ConsentUpdate):

    @cached_property
    def attendee(self):
        my_attendee = get_object_or_404(
            Attendee,
            ticket_reference=self.kwargs['ticket_reference'],
            ticket_code=self.kwargs['ticket_code'],
        )

        self.email = my_attendee.email
        self.first_name = my_attendee.first_name
        self.last_name = my_attendee.last_name
        self.event = my_attendee.event
        self.organizer = my_attendee.event.organizer
        self.consent_message = 'Attendee consent via checklist'
        self.consent_test_obj = my_attendee
        return my_attendee

    def post(self, request, *args, **kwargs):
        self.update_consent_if_exists()
        return redirect(self.attendee)


class OrderConsentUpdate(ConsentUpdate):

    @cached_property
    def order(self):
        my_order = get_object_or_404(
            Order,
            order_reference=self.kwargs['order_reference'],
            access_key=self.kwargs['access_key'],
        )

        self.email = my_order.email
        self.first_name = my_order.billing.customer_first_name
        self.last_name = my_order.billing.customer_last_name
        self.event = my_order.event
        self.organizer = my_order.event.organizer
        self.consent_message = 'Attendee consent via checklist'
        self.consent_test_obj = my_order
        return my_order

    def post(self, request, *args, **kwargs):
        self.update_consent_if_exists()
        return redirect(self.order)
\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to Code Review! A code review should focus on the code in question and elaborate on possible weaknesses and ways to improve. At the moment your answer is merely an alternative solution to the task. See also "How to write a good answer?" in the Help Center. \$\endgroup\$ – AlexV Apr 23 '19 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.