workshop_reminder()   F
last analyzed

Complexity

Conditions 13

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 13
c 2
b 1
f 0
dl 0
loc 69
rs 2.4914

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like workshop_reminder() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import datetime
2
import os
3
from celery import task
4
5
from django.template import Context, loader
6
# from django.template.loader import get_template
7
8
from wye.workshops.models import Workshop
9
from wye.base.emailer_html import send_email_to_id
10
11
12
EMAIL_HOST = os.environ.get('EMAIL_HOST_USER', '')
13
14
15
def intro_emails(organisation, email_context, email_id):
16
    subject = '[PythonExpress] Intro for workshop at {}, {}'.format(
17
        organisation.name, organisation.location.name)
18
    email_body = loader.get_template(
19
        'email_messages/workshop/workshop_intro_email.html').render(
20
        email_context)
21
    text_body = loader.get_template(
22
        'email_messages/workshop/workshop_intro_email.txt').render(
23
        email_context)
24
    send_email_to_id(
25
        subject,
26
        body=email_body,
27
        email_id=email_id,
28
        text_body=text_body)
29
30
31
def remainder_email(organisation, email_context, email_id):
32
    subject = '[PythonExpress] Remainder for workshop at {}, {}'.format(
33
        organisation.name, organisation.location.name)
34
    email_body = loader.get_template(
35
        'email_messages/workshop/remainder.html').render(email_context)
36
    text_body = loader.get_template(
37
        'email_messages/workshop/remainder.txt').render(email_context)
38
    send_email_to_id(
39
        subject,
40
        body=email_body,
41
        email_id=email_id,
42
        text_body=text_body)
43
44
45
def feedback_emails(organisation, email_context, email_id):
46
    subject = '[PythonExpress] Request for Workshop Feedback {}, {}'.format(
47
        organisation.name, organisation.location.name)
48
    email_body = loader.get_template(
49
        'email_messages/workshop/feedback_email.html').render(
50
        email_context)
51
    text_body = loader.get_template(
52
        'email_messages/workshop/feedback_email.txt').render(
53
        email_context)
54
    send_email_to_id(
55
        subject,
56
        body=email_body,
57
        email_id=email_id,
58
        text_body=text_body)
59
60
61
@task
62
def workshop_reminder(days, intro=None, feedback=None):
63
    """
64
    send workshop reminder and intro email to all register user
65
    :return:
66
    """
67
    if feedback:
68
        today = datetime.datetime.today()
69
        if days:
70
            workshop_date = today + datetime.timedelta(days=2)
71
            workshops = Workshop.objects.filter(expected_date=workshop_date)
72
73
            for workshop in workshops:
74
                context_dict = {
75
                    'date': workshop_date,
76
                    'workshop_title': workshop.workshop_section.name,
77
                }
78
                email_context = Context(context_dict)
79
                organisation = workshop.requester
80
                for requester in workshop.requester.user.all():
81
                    feedback_emails(
82
                        organisation, email_context, requester.email)
83
                for presenter in workshop.presenter.all():
84
                    feedback_emails(
85
                        organisation, email_context, presenter.email)
86
    else:
87
        workshop_date = datetime.date.today() + datetime.timedelta(days=days)
88
        workshops = Workshop.objects.select_related(
89
            'workshop_section').filter(
90
            expected_date=workshop_date).filter(
91
            is_active=True)
92
93
        for workshop in workshops:
94
            organisation = workshop.requester
95
96
            context_dict = {
97
                'date': workshop_date,
98
                'workshop_title': workshop.workshop_section.name,
99
            }
100
            if workshop.get_presenter_list:
101
                context_dict['poc_list'] = workshop.requester.user.all()
102
                context_dict['presenter_list'] = workshop.presenter.all()
103
104
                if intro:
105
106
                    # Below  loop is to send email individually
107
                    email_context = Context(context_dict)
108
                    for requester in workshop.requester.user.all():
109
                            intro_emails(
110
                                organisation, email_context, requester.email)
111
                    for presenter in workshop.presenter.all():
112
                            intro_emails(
113
                                organisation, email_context, presenter.email)
114
                else:
115
                    context_dict['workshop_title'] = workshop.workshop_section.name
116
                    context_dict['workshop_date'] = workshop.expected_date
117
                    context_dict['trainer'] = workshop.presenter.all()
118
                    context_dict['venue'] = """{} , {}""".format(
119
                        workshop.requester.name,
120
                        workshop.requester.full_address)
121
                    context_dict['organiser'] = workshop.requester.name
122
                    email_context = Context(context_dict)
123
                    for requester in workshop.requester.user.all():
124
                        remainder_email(
125
                            organisation, email_context, requester.email)
126
                    for presenter in workshop.presenter.all():
127
                        remainder_email(
128
                            organisation, email_context, presenter.email)
129
    return True
130