Fakultas Ilmu Komputer UI

Commit ea79045a authored by Bimo Iman Smartadi's avatar Bimo Iman Smartadi
Browse files

[REFACTOR] Deleted commented code and revamped error message

parent c8ab001a
Pipeline #40904 passed with stages
in 5 minutes and 6 seconds
......@@ -2,9 +2,6 @@ from django.urls import path, include
from rest_framework import routers
from . import views
#router = routers.DefaultRouter()
#router.register(r'lokasis', views.LokasiViewSet)
urlpatterns = [
path('lokasi/list/', views.lokasi_list,name='lokasi-list'),
path('lokasi/detail/<str:name>', views.lokasi_details,name='lokasi-details'),
......
......@@ -10,8 +10,11 @@ from rest_framework.authentication import TokenAuthentication
from .serializers import LokasiSerializer
from .models import Lokasi, Fasilitas, Komentar
def request_error_message(request):
return "get {} request instead".format(request)
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)
@csrf_exempt
@api_view(['GET'])
......@@ -37,7 +40,7 @@ def lokasi_details(request,name):
else:
return JsonResponse({'response' : request_error_message("get")}, status = 400)
except KeyError as e:
return JsonResponse({'response':'bad request. {} key needed'.format(str(e))}, status = 500)
return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
except:
return JsonResponse({'response':'lokasi not found'}, status = 404)
......@@ -58,7 +61,7 @@ def add_lokasi(request):
else:
return JsonResponse({'response' : request_error_message("post")}, status = 400)
except KeyError as e:
return JsonResponse({'response':'bad request. {} key needed'.format(str(e))}, status = 500)
return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
@api_view(['GET'])
@authentication_classes([TokenAuthentication,])
......@@ -85,9 +88,9 @@ def list_fasilitas(request, nama_lokasi):
fasilitas_details["is_verified"] = fasilitas.is_verified
return JsonResponse(return_json, status = 201)
except KeyError as e:
return JsonResponse({'response':'bad request. {} key needed'.format(str(e))}, status = 500)
except:
return JsonResponse({'response': 'lokasi might not be here'})
return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
except Exception as e:
return JsonResponse({'response': str(e)})
@api_view(['POST'])
@authentication_classes([TokenAuthentication,])
......@@ -107,9 +110,9 @@ def add_fasilitas(request, nama_lokasi):
else:
return JsonResponse({'response' : request_error_message("post")}, status = 400)
except KeyError as e:
return JsonResponse({'response':'bad request. {} key needed'.format(str(e))}, status = 500)
except:
return JsonResponse({'response':'lokasi might not be here'}, status = 404)
return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
except Exception as e:
return JsonResponse({'response': str(e)}, status = 404)
@api_view(['GET'])
@authentication_classes([])
......@@ -126,6 +129,6 @@ def detail_fasilitas(request, nama_lokasi, id):
else:
return JsonResponse({'response' : request_error_message("get")}, status = 400)
except KeyError as e:
return JsonResponse({'response':'bad request. {} key needed'.format(str(e))}, status = 500)
return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
except Exception as e:
return JsonResponse({'response': str(e)}, status = 404)
\ No newline at end of file
......@@ -16,6 +16,13 @@ from rest_framework.decorators import api_view, permission_classes, authenticati
from registrasi.models import BisaGoUser
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)
@csrf_exempt
@api_view(['GET'])
@authentication_classes([])
......@@ -31,7 +38,7 @@ def user_list(request):
"phone_number": user.phone_number})
return JsonResponse(json_return, safe=False)
else:
return JsonResponse({'response' : 'try get request instead'}, status = 400)
return JsonResponse({'response' : request_error_message("get")}, status = 400)
@api_view(['GET'])
@authentication_classes([])
......@@ -61,5 +68,5 @@ def register_user(request):
BisaGoUser.objects.create(user= user, phone_number=phone_number)
return JsonResponse({'response' : 'User created', 'email':email, 'name':name}, status = 201)
except KeyError as e:
return JsonResponse({'response':'bad request. {} key needed'.format(str(e))}, status = 500)
return JsonResponse({'response':missing_key_message(str(e))}, status = 500)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment