Fakultas Ilmu Komputer UI

views.py 2.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.shortcuts import render
from django.contrib.auth.models import User, Group
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.hashers import make_password, check_password

from allauth.socialaccount.providers.oauth2.client import OAuth2Client

from rest_auth.registration.serializers import SocialLoginSerializer
from rest_auth.registration.views import SocialLoginView
from rest_framework import viewsets
from rest_framework.parsers import JSONParser
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes, authentication_classes

from registrasi.models import BisaGoUser

19
20
21
22
23
24
25
def request_error_message(request_kind):
    return "get {} request instead".format(request_kind)

def missing_key_message(key):
    return "bad request. {} key needed".format(key)


26
27
28
29
30
31
32
@csrf_exempt
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def user_list(request):
    if request.method == 'GET':
        user_list = BisaGoUser.objects.all()
Bimo Iman Smartadi's avatar
Bimo Iman Smartadi committed
33
34
35
36
37
38
39
        json_return = []
        for user in user_list:
            json_return.append({"username":user.user.email,
                       "name": user.user.last_name,
                       "email": user.user.email,
                       "phone_number": user.phone_number})
        return JsonResponse(json_return, safe=False)
40
    else:
41
        return JsonResponse({'response' : request_error_message("get")}, status = 400)
42
43
44
45
46
47

@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def user_details(request,email):
    if request.method == 'GET':
Bimo Iman Smartadi's avatar
Bimo Iman Smartadi committed
48
49
50
51
52
53
54
        user = User.objects.get(username = email)
        bisa_go_user = BisaGoUser.objects.get(user=user)
        json_return = {"username":user.email,
                       "name": user.last_name,
                       "email": user.email,
                       "phone_number": bisa_go_user.phone_number}
        return JsonResponse(json_return, safe=False)
55
56
57
58
59
60
61
62
63
64
65
66


@api_view(['POST'])
@authentication_classes([])
@permission_classes([])
def register_user(request):
    try:
        if request.method == 'POST':
            name = request.POST['name']
            phone_number = request.POST['phone_number']
            email = request.POST['email']
            password = request.POST['password']
Bimo Iman Smartadi's avatar
Bimo Iman Smartadi committed
67
68
            user = User.objects.create_user(username=email, email=email, password=password, last_name=name)
            BisaGoUser.objects.create(user= user, phone_number=phone_number)
69
70
            return JsonResponse({'response' : 'User created', 'email':email, 'name':name}, status = 201)
    except KeyError as e:
71
        return JsonResponse({'response':missing_key_message(str(e))}, status = 500)
72