Completed
Push — master ( 2b3bff...10952a )
by Vijay
02:16 queued 01:05
created

verify_user_profile()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 12
rs 9.4285
c 2
b 0
f 0
1
from django.views.generic import TemplateView
2
from django.http import HttpResponseRedirect
3
from wye.organisations.models import Organisation
4
from wye.profiles.models import Profile
5
from wye.workshops.models import Workshop
6
7
from .constants import WorkshopStatus
8
9
10
class HomePageView(TemplateView):
11
    template_name = 'index.html'
12
13
    def get_context_data(self, **kwargs):
14
        context = super(HomePageView, self).get_context_data(**kwargs)
15
        organisationObj = Organisation.objects.filter(active=True)
16
        workshopObj = Workshop.objects.filter(is_active=True)
17
        context['organisation_registered_count'] = organisationObj.count()
18
        context['completed_workshop_count'] = workshopObj.filter(
19
            status=WorkshopStatus.COMPLETED).count()
20
        context['requested_workshop_count'] = workshopObj.count()
21
        context['tutor_registered_count'] = Profile.objects.filter(
22
            usertype__slug="tutor").count()
23
        return context
24
25
26
def verify_user_profile(f):
27
    '''
28
    This decorator check  whether the user are valid for certain views
29
    '''
30
    def wrap(request, *args, **kwargs):
31
        user_profile, created = Profile.objects.get_or_create(
32
            user__id=request.user.id)
33
        if not user_profile.is_profile_filled:
34
            return HttpResponseRedirect(
35
                '/profile/{}/edit'.format(request.user.username))
36
        return f(request, *args, **kwargs)
37
    return wrap
38