Fakultas Ilmu Komputer UI

views.py 3.15 KB
Newer Older
1
from django.contrib.auth import authenticate, login
Azhar Rais's avatar
Azhar Rais committed
2
from django.http import HttpResponseRedirect, QueryDict
3
from django.views.generic import TemplateView
4
5
6
7
from django.conf import settings
from django.contrib import messages
import urllib
import json
8
9

class Login(TemplateView):
Steven Kusuman's avatar
Steven Kusuman committed
10
11
12
13
14
15
16
17
18
19
    
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            if request.user.is_admin:
                redirect_to = "/sukses-admin/"
            elif request.user.is_contributor:
                redirect_to = "/sukses-kontributor/"
            return HttpResponseRedirect(redirect_to)
        return super(Login, self).dispatch(request, *args, **kwargs)

20
    def get_template_names(self):
21
22
        if self.request.path == "/login_admin/":
            template_name = "login_admin.html"
23
        else:
24
            template_name = "login.html"
25
        return template_name
26
27
28
29
30
31
32

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
33
        context['captcha_site_key'] = settings.GOOGLE_RECAPTCHA_SITE_KEY
34
35
36
37
38
        return self.render_to_response(context=context)

    def post(self, request, *args, **kwargs):
        email = request.POST.get("email", None)
        password = request.POST.get("pass", None)
39
        context = self.get_context_data(*args, **kwargs)
40

41
        if email is None or password is None:
42
43
44
            context = self.get_context_data(*args, **kwargs)
            context["error_message"] = "Email atau Password anda kosong."
            return self.render_to_response(context=context)
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        else: 
            recaptcha_response = request.POST.get('g-recaptcha-response')
            url = 'https://www.google.com/recaptcha/api/siteverify'
            values = {
                'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
                'response': recaptcha_response
            }
            data = urllib.parse.urlencode(values).encode()
            req =  urllib.request.Request(url, data=data)
            response = urllib.request.urlopen(req)
            result = json.loads(response.read().decode())
 
            if result['success']:
                user = authenticate(email=email, password=password)
                if user is not None:
                    login(request, user)
                    redirect_to = "/"
                    querystring = QueryDict(request.META['QUERY_STRING'])
                    if request.user.is_admin:
                        redirect_to = "/sukses-admin/"
                    elif request.user.is_contributor:
                        redirect_to = "/sukses-kontributor/"
                    if 'next' in querystring:
                        redirect_to = querystring['next']
                    return HttpResponseRedirect(redirect_to)
                else:
                    context["error_message"] = "Email atau Password anda salah."
                    return self.render_to_response(context=context)
73
            else:
74
75
                messages.error(request, 'Invalid reCAPTCHA. Please try again.')
            return self.render_to_response(context=context)