Completed
Push — master ( a5ee20...4f370a )
by Vijay
9s
created

partner_view()   B

Complexity

Conditions 4

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 53
rs 8.9849

How to fix   Long Method   

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:

1
# -*- coding: utf-8 -*-
2
3
from django.core.exceptions import PermissionDenied
4
from django.core.urlresolvers import reverse
5
from django.http import HttpResponseRedirect
6
from django.shortcuts import redirect, render, get_object_or_404
7
from django.template import Context, loader
8
from django.views.generic import UpdateView
9
from django.views.generic.list import ListView
10
from django.contrib.auth import logout
11
from wye.base.constants import WorkshopStatus
12
from wye.base.emailer_html import send_email_to_id, send_email_to_list
13
from wye.organisations.models import Organisation
14
from wye.profiles.models import Profile
15
from wye.workshops.models import Workshop
16
17
from .forms import ContactUsForm, UserProfileForm, PartnerForm
18
19
20
def profile_view(request, slug):
21
    try:
22
        p = Profile.objects.get(user__username=slug)
23
    except Profile.DoesNotExist:
24
        return render(request, 'error.html', {
25
            "message": "Profile does not exist"})
26
    return render(request, 'profile/index.html', {'object': p})
27
28
29
class UserDashboard(ListView):
30
    model = Profile
31
    template_name = 'profile/dashboard.html'
32
33
    def dispatch(self, request, *args, **kwargs):
34
        user_profile = get_object_or_404(
35
            Profile, user__id=self.request.user.id)
36
        if not user_profile.get_user_type:
37
            return redirect('profiles:profile-edit',
38
                            slug=request.user.username)
39
        return super(UserDashboard, self).dispatch(request, *args, **kwargs)
40
41
    def get_context_data(self, **kwargs):
42
        context = super(UserDashboard, self).get_context_data(**kwargs)
43
        profile = Profile.objects.get(user=self.request.user)
44
        workshop_all = Workshop.objects.all()
45
        accept_workshops = workshop_all.filter(
46
            status=WorkshopStatus.ACCEPTED)
47
        requested_workshops = workshop_all.filter(
48
            status=WorkshopStatus.REQUESTED)
49
        organisation_all = Organisation.objects.all()
50
        for each_type in profile.get_user_type:
51
            if each_type == 'tutor':
52
                context['is_tutor'] = True
53
                context['workshop_requested_tutor'] = accept_workshops.filter(
54
                    presenter=self.request.user)
55
                context['workshop_completed_tutor'] = \
56
                    requested_workshops.filter(
57
                    presenter=self.request.user)
58
            if each_type == 'lead':
59
                context['is_regional_lead'] = True
60
                context['workshops_accepted_under_rl'] = accept_workshops
61
                context['workshops_pending_under_rl'] = requested_workshops
62
                context['interested_tutors'] = Profile.objects.filter(
63
                    usertype__slug='tutor',
64
                    interested_locations__name__in=profile.get_interested_locations)\
65
                    .exclude(user=self.request.user).count()
66
                context['interested_locations'] = organisation_all.filter(
67
                    location__name__in=profile.get_interested_locations)\
68
                    .count()
69
70
            if each_type == 'poc':
71
                context['is_college_poc'] = True
72
                context['users_organisation'] = organisation_all.filter(
73
                    user=self.request.user)
74
                context['workshop_requested_under_poc'] = workshop_all.filter(
75
                    requester__id__in=organisation_all.values_list(
76
                        'id', flat=True))
77
                context['workshops_accepted_under_poc'] = workshop_all.filter(
78
                    status=WorkshopStatus.ACCEPTED,
79
                    requester__id__in=organisation_all.values_list(
80
                        'id', flat=True))
81
82
            if each_type == 'admin':
83
                context['is_admin'] = True
84
                context['workshops_by_status'] = workshop_all.order_by(
85
                    'status')
86
                context['workshops_by_region'] = workshop_all.order_by(
87
                    'location')
88
89
        return context
90
91
92
class ProfileEditView(UpdateView):
93
    model = Profile
94
    template_name = 'profile/update.html'
95
    form_class = UserProfileForm
96
    slug_field = 'user__username'
97
98
    def form_valid(self, form):
99
        return super(ProfileEditView, self).form_valid(form)
100
101
    def get_success_url(self):
102
        return reverse('profiles:profile-page', kwargs={
103
            'slug': self.object.slug})
104
105
    def dispatch(self, *args, **kwargs):
106
        if self.request.user.pk == self.get_object().pk:
107
            return super(ProfileEditView, self).dispatch(*args, **kwargs)
108
        else:
109
            raise PermissionDenied
110
111
112
def contact(request):
113
    if request.method == 'POST':
114
        form = ContactUsForm(request.POST)
115
        if form.is_valid():
116
            email_context = Context({
117
                'contact_name': form.cleaned_data['name'],
118
                'contact_email': form.cleaned_data['email'],
119
                'comments': form.cleaned_data['comments'],
120
                'conatct_number': form.cleaned_data['contact_number'],
121
                'feedback_type': form.cleaned_data['feedback_type']
122
            })
123
124
            subject = "PythonExpress Feedback by %s" % (
125
                form.cleaned_data['name'])
126
            text_body = loader.get_template(
127
                'email_messages/contactus/message.txt').render(email_context)
128
            email_body = loader.get_template(
129
                'email_messages/contactus/message.html').render(email_context)
130
            user_subject = '[PythonExpress] Feedback Received'
131
            user_text_body = loader.get_template(
132
                'email_messages/contactus/message_user.txt').render(
133
                email_context)
134
            user_email_body = loader.get_template(
135
                'email_messages/contactus/message_user.html').render(
136
                email_context)
137
138
            try:
139
                regional_lead = Profile.objects.filter(
140
                    usertype__slug__in=['lead', 'admin']).values_list(
141
                    'user__email', flat=True)
142
                send_email_to_list(
143
                    subject,
144
                    users_list=regional_lead,
145
                    body=email_body,
146
                    text_body=text_body)
147
148
                send_email_to_id(
149
                    user_subject,
150
                    body=user_email_body,
151
                    email_id=form.cleaned_data['email'],
152
                    text_body=user_text_body)
153
            except Exception as e:
154
                print(e)
155
            return HttpResponseRedirect('/thankyou')
156
    else:
157
        form = ContactUsForm()
158
    return render(request, 'contact.html', {'form': form})
159
160
161
def partner_view(request):
162
    if request.method == 'POST':
163
        form = PartnerForm(request.POST)
164
        if form.is_valid():
165
            email_context = Context({
166
                'org_name': form.cleaned_data['org_name'],
167
                'org_url': form.cleaned_data['org_url'],
168
                'partner_type': form.cleaned_data['partner_type'],
169
                'description': form.cleaned_data['description'],
170
                'python_use': form.cleaned_data['python_use'],
171
                'comments': form.cleaned_data['comments'],
172
                'name': form.cleaned_data['name'],
173
                'email': form.cleaned_data['email'],
174
                'contact_name': form.cleaned_data['name'],
175
                'contact_email': form.cleaned_data['email'],
176
                'conatct_number': form.cleaned_data['contact_number'],
177
178
            })
179
180
            subject = "[PythonExpress] Partnership request by %s" % (
181
                form.cleaned_data['org_name'])
182
            text_body = loader.get_template(
183
                'email_messages/partner/partner_message.txt').render(
184
                email_context)
185
            email_body = loader.get_template(
186
                'email_messages/partner/partner_message.html').render
187
            (email_context)
188
            user_subject = '[PythonExpress] Partnership request Received'
189
            user_text_body = loader.get_template(
190
                'email_messages/partner/message_user.txt').render(
191
                email_context)
192
            user_email_body = loader.get_template(
193
                'email_messages/partner/message_user.html').render(
194
                email_context)
195
196
            try:
197
                send_email_to_list(
198
                    subject,
199
                    users_list=['[email protected]'],
200
                    body=email_body,
201
                    text_body=text_body)
202
203
                send_email_to_id(
204
                    user_subject,
205
                    body=user_email_body,
206
                    email_id=form.cleaned_data['email'],
207
                    text_body=user_text_body)
208
            except Exception as e:
209
                print(e)
210
            return HttpResponseRedirect('/thankyou')
211
    else:
212
        form = PartnerForm()
213
    return render(request, 'partner.html', {'form': form})
214
215
216
def account_deactivate(request, slug):
217
    user = request.user
218
    user.is_active = False
219
    user.save()
220
    logout(request)
221
    return HttpResponseRedirect('/')
222