Fakultas Ilmu Komputer UI

views.py 3.35 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
from django.conf import settings
from django.contrib import messages
6
from .services import LoginService
7
8
import urllib
import json
9

10
11


12
class Login(TemplateView):
Steven Kusuman's avatar
Steven Kusuman committed
13
14
15
    
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
16
            redirect_to = LoginService.get_user_redirect_url(request)
Steven Kusuman's avatar
Steven Kusuman committed
17
18
19
            return HttpResponseRedirect(redirect_to)
        return super(Login, self).dispatch(request, *args, **kwargs)

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

    def do_login(self, context, email, password, request, result):
        if result['success']:
            return self.handle_recaptcha_success(context, email, password, request)
        else:
            messages.error(request, 'Invalid reCAPTCHA. Please try again.')
            return self.render_to_response(context=context)

    def handle_recaptcha_success(self, context, email, password, request):
        user = authenticate(email=email, password=password)
        if user is not None:
            redirect_to = LoginService.get_redirect_login_sucess(request, user)
            return HttpResponseRedirect(redirect_to)
        else:
            context["error_message"] = "Email atau Password anda salah."
            return self.render_to_response(context=context)

    def get_recaptcha_result(self, 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())
        return result

49
    def get_template_names(self):
50
51
        if self.request.path == "/login_admin/":
            template_name = "login_admin.html"
52
        else:
53
            template_name = "login.html"
54
        return template_name
55
56
57
58
59
60
61

    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)
62
        context['captcha_site_key'] = settings.GOOGLE_RECAPTCHA_SITE_KEY
63
64
65
66
67
        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)
68
        context = self.get_context_data(*args, **kwargs)
69

70
        if email is None or password is None:
71
72
73
            context = self.get_context_data(*args, **kwargs)
            context["error_message"] = "Email atau Password anda kosong."
            return self.render_to_response(context=context)
74
75
        else: 
            recaptcha_response = request.POST.get('g-recaptcha-response')
76
77
78
79
            result = self.get_recaptcha_result(recaptcha_response)

            return self.do_login(context, email, password, request, result)

80
81
82
83
84
85
86
87
88
from app.models import NotifikasiKontributor
def kontributor_notif_context(request):
    if request.user.is_authenticated and request.user.is_contributor:
        notifs = NotifikasiKontributor.objects.filter(user=request.user)
        return {
            'kontributor_notif':notifs
        }
    else:
        return {}