Completed
Pull Request — master (#260)
by
unknown
01:06
created

ContactFormView   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %
Metric Value
wmc 2
dl 0
loc 45
rs 10
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
7
from django.template import Context, loader
8
from django.views.generic import UpdateView
9
from django.views.generic.list import ListView
10
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
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', {"message": "Profile does not exist"})
25
    return render(request, 'profile/index.html', {'object': p})
26
27
28
class UserDashboard(ListView):
29
    model = Profile
30
    template_name = 'profile/dashboard.html'
31
32
    def dispatch(self, request, *args, **kwargs):
33
        user_profile = Profile.objects.get(
34
            user__id=self.request.user.id)
35
        if not user_profile.get_user_type:
36
            return redirect('profiles:profile-edit', slug=request.user.username)
37
        return super(UserDashboard, self).dispatch(request, *args, **kwargs)
38
39
    def get_context_data(self, **kwargs):
40
        context = super(UserDashboard, self).get_context_data(**kwargs)
41
        user_profile = Profile.objects.get(user=self.request.user)
42
        workshop_all = Workshop.objects.all()
43
        accept_workshops = workshop_all.filter(
44
            status=WorkshopStatus.ACCEPTED)
45
        requested_workshops = workshop_all.filter(
46
            status=WorkshopStatus.REQUESTED)
47
        organisation_all = Organisation.objects.all()
48
        for each_type in user_profile.get_user_type:
49
            if each_type == 'tutor':
50
                context['is_tutor'] = True
51
                context['workshop_requested_tutor'] = accept_workshops.filter(
52
                    presenter=self.request.user)
53
                context['workshop_completed_tutor'] = requested_workshops.filter(
54
                    presenter=self.request.user)
55
            if each_type == 'lead':
56
                context['is_regional_lead'] = True
57
                context['workshops_accepted_under_rl'] = accept_workshops
58
                context['workshops_pending_under_rl'] = requested_workshops
59
                context['interested_tutors'] = Profile.objects.filter(
60
                    usertype__slug='tutor',
61
                    interested_locations__name__in=user_profile.get_interested_locations).exclude(
62
                    user=self.request.user).count()
63
                context['interested_locations'] = organisation_all.filter(
64
                    location__name__in=user_profile.get_interested_locations).count()
65
66
            if each_type == 'poc':
67
                context['is_college_poc'] = True
68
                context['users_organisation'] = organisation_all.filter(
69
                    user=self.request.user)
70
                context['workshop_requested_under_poc'] = workshop_all.filter(
71
                    requester__id__in=organisation_all.values_list(
72
                        'id', flat=True))
73
                context['workshops_accepted_under_poc'] = workshop_all.filter(
74
                    status=WorkshopStatus.ACCEPTED,
75
                    requester__id__in=organisation_all.values_list(
76
                        'id', flat=True))
77
78
            if each_type == 'admin':
79
                context['is_admin'] = True
80
                context['workshops_by_status'] = workshop_all.order_by(
81
                    'status')
82
                context['workshops_by_region'] = workshop_all.order_by(
83
                    'location')
84
85
        return context
86
87
88
class ProfileEditView(UpdateView):
89
    model = Profile
90
    template_name = 'profile/update.html'
91
    form_class = UserProfileForm
92
    slug_field = 'user__username'
93
94
    def form_valid(self, form):
95
        return super(ProfileEditView, self).form_valid(form)
96
97
    def get_success_url(self):
98
        return reverse('profiles:profile-page', kwargs={'slug': self.object.slug})
99
100
    def dispatch(self, *args, **kwargs):
101
        if self.request.user.pk == self.get_object().pk:
102
            return super(ProfileEditView, self).dispatch(*args, **kwargs)
103
        else:
104
            raise PermissionDenied
105
106
107
def contact(request):
108
    if request.method == 'POST':
109
        form = ContactUsForm(request.POST)
110
        if form.is_valid():
111
            email_context = Context({
112
                'contact_name': form.cleaned_data['name'],
113
                'contact_email': form.cleaned_data['email'],
114
                'comments': form.cleaned_data['comments'],
115
                'conatct_number': form.cleaned_data['contact_number'],
116
                'feedback_type': form.cleaned_data['feedback_type']
117
            })
118
119
            subject = "PythonExpress Feedback by %s" % (form.cleaned_data['name'])
120
            text_body = loader.get_template(
121
                'email_messages/contactus/message.txt').render(email_context)
122
            email_body = loader.get_template(
123
                'email_messages/contactus/message.html').render(email_context)
124
            user_subject = '[PythonExpress] Feedback Received'
125
            user_text_body = loader.get_template(
126
                'email_messages/contactus/message_user.txt').render(email_context)
127
            user_email_body = loader.get_template(
128
                'email_messages/contactus/message_user.html').render(email_context)
129
130
            try:
131
                regional_lead = Profile.objects.filter(
132
                    usertype__slug__in=['lead', 'admin']).values_list(
133
                    'user__email', flat=True)
134
                send_email_to_list(
135
                    subject,
136
                    users_list=regional_lead,
137
                    body=email_body,
138
                    text_body=text_body)
139
140
                send_email_to_id(
141
                    user_subject,
142
                    body=user_email_body,
143
                    email_id=form.cleaned_data['email'],
144
                    text_body=user_text_body)
145
            except Exception as e:
146
                print(e)
147
            return HttpResponseRedirect('/thankyou')
148
    else:
149
        form = ContactUsForm()
150
    return render(request, 'contact.html', {'form': form})
151