Fakultas Ilmu Komputer UI

views.py 6.08 KB
Newer Older
M. Abdurrahman's avatar
M. Abdurrahman committed
1
from django.shortcuts import render
M. Abdurrahman's avatar
M. Abdurrahman committed
2
3
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
4
from django.contrib.auth.models import User
M. Abdurrahman's avatar
M. Abdurrahman committed
5
6
7

from rest_framework import viewsets
from rest_framework.decorators import api_view, permission_classes, authentication_classes
8
9
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated 
M. Abdurrahman's avatar
M. Abdurrahman committed
10
11

from .serializers import LokasiSerializer
12
from .models import Lokasi, Fasilitas, Komentar
M. Abdurrahman's avatar
M. Abdurrahman committed
13

14
15
16
17
18
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)
M. Abdurrahman's avatar
M. Abdurrahman committed
19
20
21
22
23
24
25
26
27

@csrf_exempt
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def lokasi_list(request):
    if request.method == 'GET':
        lokasi_list = Lokasi.objects.all()
        serializer = LokasiSerializer(lokasi_list, many=True)
28
        return JsonResponse(serializer.data, safe=False, status = 200)
M. Abdurrahman's avatar
M. Abdurrahman committed
29
    else:
30
        return JsonResponse({'response' : request_error_message("get")}, status = 400)
M. Abdurrahman's avatar
M. Abdurrahman committed
31
32
33
34
35

@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def lokasi_details(request,name):
36
37
38
39
    try:
        if request.method == 'GET':
            lokasi = Lokasi.objects.get(name = name)
            serializer = LokasiSerializer(lokasi)
40
            return JsonResponse(serializer.data, safe=False, status = 200)
41
        else:
42
            return JsonResponse({'response' : request_error_message("get")}, status = 400)
43
    except KeyError as e:
44
        return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
45
46
    except:
        return JsonResponse({'response':'lokasi not found'}, status = 404)
M. Abdurrahman's avatar
M. Abdurrahman committed
47
48

@api_view(['POST'])
49
50
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
M. Abdurrahman's avatar
M. Abdurrahman committed
51
52
53
def add_lokasi(request):
    try:
        if request.method == 'POST':
54
55
56
57
58
59
60
61

            data = request.POST
            serializer = LokasiSerializer(data=data)
            if serializer.is_valid():
                serializer.save()
                return JsonResponse(serializer.data, status=201)
            else:
                return JsonResponse(serializer.errors, status=400)
62
        else:
63
            return JsonResponse({'response' : request_error_message("post")}, status = 400)
M. Abdurrahman's avatar
M. Abdurrahman committed
64
    except KeyError as e:
65
        return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
66

67
@api_view(['GET'])
68
@authentication_classes([])
69
70
71
72
73
74
75
76
77
78
@permission_classes([])
def list_fasilitas(request, nama_lokasi):
    try:
        if request.method == 'GET':
            lokasi = Lokasi.objects.get(name = nama_lokasi)
            list_fasilitas = Fasilitas.objects.filter(lokasi=lokasi)
            return_json = {}
            for fasilitas in list_fasilitas:
                return_json[fasilitas.id] = {}
                fasilitas_details = return_json[fasilitas.id]
79
80
81
82
83
84
85
86
87
88
89
                fasilitas_details["id"] = fasilitas.id
                fasilitas_details["nama_lokasi"] = fasilitas.lokasi.name
                fasilitas_details["deskripsi"] = fasilitas.deskripsi
                fasilitas_details["creator"] = fasilitas.user.last_name
                fasilitas_details["date_time"] = fasilitas.date_time
                fasilitas_details["like"] = fasilitas.like
                fasilitas_details["dislike"] = fasilitas.dislike
                fasilitas_details["rating"] = fasilitas.rating
                fasilitas_details["tag"] = fasilitas.tag
                fasilitas_details["image"] = str(fasilitas.image)
                fasilitas_details["is_verified"] = fasilitas.is_verified
90
            return JsonResponse(return_json, status = 200)
91
    except KeyError as e:
92
93
        return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
    except Exception as e:
94
        return JsonResponse({'response': str(e)}, status = 404)
95
96

@api_view(['POST'])
97
98
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
99
100
101
102
103
104
def add_fasilitas(request, nama_lokasi):
    try:
        if request.method == 'POST':
            lokasi = Lokasi.objects.get(name=nama_lokasi)
            user = User.objects.get(email=str(request.user))
            deskripsi = request.POST['deskripsi']
105
106
            rating = request.POST['rating']
            tag = request.POST['tag'].split()
107
108
109
            image = ""
            if 'image' in request.POST.keys():
                image = str(request.POST['image'])
110
            fasilitas = Fasilitas.objects.create(lokasi=lokasi,
111
                                     user=user,
112
113
                                     deskripsi=deskripsi,
                                     rating=rating,
114
115
                                     tag=tag,
                                     image = image)
116
            return JsonResponse({'response' : 'fasilitas added', 'id':fasilitas.id}, status = 201)
117
        else:
118
            return JsonResponse({'response' : request_error_message("post")}, status = 400)
119
    except KeyError as e:
120
121
122
        return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
    except Exception as e:
        return JsonResponse({'response': str(e)}, status = 404)
123
124
125
126

@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
127
def detail_fasilitas(request, nama_lokasi, id):
128
129
130
    try:
        if request.method == 'GET':
            lokasi = Lokasi.objects.get(name = nama_lokasi)
131
            fasilitas = Fasilitas.objects.get(lokasi=lokasi, id=id)
132
            user = fasilitas.user
133
            return_json = {"nama_lokasi": lokasi.name, "deskripsi":fasilitas.deskripsi, "creator":user.last_name, "date_time":fasilitas.date_time,
134
            "like":fasilitas.like, "dislike":fasilitas.dislike, "rating":fasilitas.rating, "tag":fasilitas.tag, "image":str(fasilitas.image), "is_verified":fasilitas.is_verified}
135
            return JsonResponse(return_json, status = 200)
136
        else:
137
            return JsonResponse({'response' : request_error_message("get")}, status = 400)
138
    except KeyError as e:
139
        return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
140
    except Exception as e:
141
        return JsonResponse({'response': str(e)}, status = 404)