summaryrefslogtreecommitdiff
path: root/arduino_blog/decorators.py
blob: 6c185d5c8f3b5d6e2bfb2d188044426cb93534f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from django.shortcuts import render, redirect
from django.conf import settings
from arduino_blog.models import Proposal
from django.db.models import Q

def email_verified(func):
    """
    This decorator is used to check if email is verified.
    If email is not verified then redirect user for email
    verification.
    """

    def is_email_verified(request, *args, **kwargs):
        user = request.user
        context = {}
        try:
            if user.is_authenticated:
                if not user.profile.is_email_verified:
                    context['success'] = False
                    context['msg'] = "Your account is not verified. \
                                        Please verify your account"
                    return render(
                        request, 'activation-status.html', context
                    )
            return func(request, *args, **kwargs)
        except Exception as e:
            print (e)
            return redirect('/')
    return is_email_verified


def is_proposal_submitted(func):
    def is_submitted(request, *args, **kwargs):
        user = request.user
        context = {}
        try:
            if user.is_authenticated:
                if not user.profile.is_email_verified:
                    context['success'] = False
                    context['msg'] = "Your account is not verified. \
                                        Please verify your account"
                    return render(
                        request, 'activation-status.html', context
                    )
                else:
                    _q = Proposal.objects
                    is_sub = _q.get(Q(user_id=user.id)&Q(proposal_status='0'))
                    if is_sub.proposal_status == 0:
                        context['success'] = True
                        context['msg'] = "You have alredy submited a \
                                          proposal. Your proposal is under \
                                          review"
                        return render(
                            request, 'dashboard.html', context
                        )
                    else:
                        context['success'] = False
                        context['msg'] = "You can submit a new \
                                            proposal"
                        return render(
                            request, 'dashboard.html', context
                        )
            return func(request, *args, **kwargs)
        except Exception as e:
            print (e)
            return redirect('/')
    return is_submitted