Fakultas Ilmu Komputer UI

Commit 7a830061 authored by Christopher Samuel's avatar Christopher Samuel
Browse files

Pbi 14 agenda kegiatan

parent 0aae65a3
......@@ -37,13 +37,10 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
self.kwargs_search_kegiatan_fail = {'query': 'this shouldnt exist', }
self.kwargs_kegiatan_id = {'kegiatan_id': self.kegiatan.lokasi.place_id}
self.kwargs_list_kegiatan_in_order = {
'start_index': 0,
'query_limit': 10,
}
self.kwargs_list_kegiatan_in_order_fail = {
'start_index': 10,
'query_limit': 20,
self.kwargs_list_kegiatan_in_order = self.kwargs_search_kegiatan
self.kwargs_list_kegiatan_in_order_fail = self.kwargs_search_kegiatan_fail
self.kwargs_list_kegiatan_in_order_with_query = {
'query': 'aaaa mock kegiatan',
}
image_path1, image_path2 = ("test_file/test1.jpg", "test_file/test2.jpg")
......@@ -59,6 +56,8 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
self.get_list_kegiatan_url = \
reverse('list-kegiatan', kwargs=self.kwargs_place_id)
self.get_list_all_kegiatan_url = \
reverse('list-kegiatan-all')
self.get_detail_kegiatan_url = \
reverse('detail-kegiatan', kwargs=self.kwargs_add_or_update_kegiatan)
self.get_nearest_kegiatan_url = \
......@@ -85,6 +84,8 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
reverse('list-kegiatan-by-name', kwargs=self.kwargs_list_kegiatan_in_order_fail)
self.list_kegiatan_by_time_fail_url = \
reverse('list-kegiatan-by-time', kwargs=self.kwargs_list_kegiatan_in_order_fail)
self.list_kegiatan_by_name_with_query_url = \
reverse('list-kegiatan-by-name', kwargs=self.kwargs_list_kegiatan_in_order_with_query)
def tearDown(self):
try:
......@@ -340,6 +341,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
def test_can_get_list_kegiatan_by_name_order(self):
Kegiatan.objects.all().delete()
response_params = self.mock_kegiatan_test.copy()
response = self.client.post(self.add_kegiatan_url, response_params)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
......@@ -371,6 +373,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
def test_can_get_list_kegiatan_by_time_order(self):
Kegiatan.objects.all().delete()
response_params = self.mock_kegiatan_test.copy()
response = self.client.post(self.add_kegiatan_url, response_params)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
......@@ -402,6 +405,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
def test_can_get_list_kegiatan_by_latest_added(self):
Kegiatan.objects.all().delete()
response_params = self.mock_kegiatan_test.copy()
response = self.client.post(self.add_kegiatan_url, response_params)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
......@@ -422,3 +426,33 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
def test_fail_get_list_kegiatan_by_latest_added(self):
response = Client().get(self.list_kegiatan_by_latest_added_fail_url)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
def test_can_get_list_all_kegiatan(self):
response = Client().get(self.get_list_all_kegiatan_url)
data = response.json()
self.assertEqual(len(data), 1)
def test_can_get_list_kegiatan_by_name_order_with_query(self):
Kegiatan.objects.all().delete()
response_params = self.mock_kegiatan_test.copy()
response = self.client.post(self.add_kegiatan_url, response_params)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
content = json.loads(response.content.decode('utf-8'))
response_params = self.mock_kegiatan_name_order_test.copy()
response = self.client.post(self.add_kegiatan_url, response_params)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
content = json.loads(response.content.decode('utf-8'))
ordered_name_mock_id = str(content['id'])
response = self.client.get(self.list_kegiatan_by_name_with_query_url)
self.assertEqual(response.status_code, HTTPStatus.OK)
content = json.loads(response.content.decode('utf-8'))
response_id_in_order = list(content.keys())
self.assertEqual(len(response_id_in_order), 1)
self.assertEqual(response_id_in_order[0], ordered_name_mock_id)
self.assertEqual(content[ordered_name_mock_id]['nama_kegiatan'],
self.mock_kegiatan_name_order_test['nama_kegiatan'])
......@@ -32,6 +32,9 @@ urlpatterns = [
path('lokasi/list-kegiatan/<str:place_id>/',
views_kegiatan.list_kegiatan, name='list-kegiatan'),
path('lokasi/list-kegiatan-all',
views_kegiatan.list_kegiatan_all, name='list-kegiatan-all'),
path('lokasi/detail-kegiatan/<str:place_id>/<int:id>/',
views_kegiatan.detail_kegiatan, name='detail-kegiatan'),
......@@ -39,9 +42,6 @@ urlpatterns = [
path('lokasi/kegiatan-terdekat',
views_kegiatan.nearest_kegiatan, name='nearest-kegiatan'),
path('lokasi/search-kegiatan',
views_kegiatan.search_kegiatan, name='search-kegiatan'),
path('lokasi/search-kegiatan/<str:query>',
views_kegiatan.search_kegiatan, name='search-kegiatan'),
......@@ -54,22 +54,22 @@ urlpatterns = [
path('lokasi/list-foto-kegiatan/<str:place_id>/<int:kegiatan_id>',
views_kegiatan.list_foto_kegiatan, name='list-foto-kegiatan'),
path('lokasi/list-kegiatan-by-latest-added/<int:start_index>/<int:query_limit>',
path('lokasi/list-kegiatan-by-latest-added/<str:query>',
views_kegiatan.list_kegiatan_by_latest_added, name='list-kegiatan-by-latest-added'),
path('lokasi/list-kegiatan-by-latest-added/<int:start_index>',
path('lokasi/list-kegiatan-by-latest-added',
views_kegiatan.list_kegiatan_by_latest_added, name='list-kegiatan-by-latest-added'),
path('lokasi/list-kegiatan-by-name/<int:start_index>/<int:query_limit>',
path('lokasi/list-kegiatan-by-name/<str:query>',
views_kegiatan.list_kegiatan_by_name, name='list-kegiatan-by-name'),
path('lokasi/list-kegiatan-by-name/<int:start_index>',
path('lokasi/list-kegiatan-by-name',
views_kegiatan.list_kegiatan_by_name, name='list-kegiatan-by-name'),
path('lokasi/list-kegiatan-by-time/<int:start_index>/<int:query_limit>',
path('lokasi/list-kegiatan-by-time/<str:query>',
views_kegiatan.list_kegiatan_by_time, name='list-kegiatan-by-time'),
path('lokasi/list-kegiatan-by-time/<int:start_index>',
path('lokasi/list-kegiatan-by-time',
views_kegiatan.list_kegiatan_by_time, name='list-kegiatan-by-time'),
path('lokasi/get-komentar-kegiatan/<str:place_id>/<int:kegiatan_id>/<int:komentar_id>',
......
......@@ -11,6 +11,7 @@ from .models import Kegiatan, Lokasi, FotoKegiatan
from django.contrib.auth.models import User
from .serializers import KegiatanSerializer, KegiatanSerializerRead, FotoKegiatanSerializer
EMPTY_KEGIATAN = 'No Kegiatan available'
@api_view(['GET'])
@authentication_classes([])
......@@ -23,6 +24,17 @@ def list_kegiatan(request, place_id):
return JsonResponse(new_dict, status=HTTPStatus.OK)
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def list_kegiatan_all(request):
queryset = _filter_kegiatan()
serializer = KegiatanSerializerRead(queryset, many=True)
data_response = serializer.data
new_dict = {item['id']: _clean_json_kegiatan(dict(item)) for item in data_response}
return JsonResponse(new_dict, status=HTTPStatus.OK)
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
......@@ -42,7 +54,7 @@ def nearest_kegiatan(request):
time_now = timezone.now()
queryset = Kegiatan.objects.filter(time_start__gte=time_now).order_by('time_start').first()
if queryset is None:
raise NotFound(detail="No Kegiatan available")
raise NotFound(detail=EMPTY_KEGIATAN)
serializer = KegiatanSerializerRead(queryset, many=False)
return JsonResponse(_clean_json_kegiatan(serializer.data), status=HTTPStatus.OK)
......@@ -98,13 +110,10 @@ def list_foto_kegiatan(request, place_id, kegiatan_id):
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def search_kegiatan(request, query=''):
query_by_nama = Kegiatan.objects.filter(nama_kegiatan__icontains=query)
query_by_deskripsi = Kegiatan.objects.filter(deskripsi__icontains=query)
query_by_penyelenggara = Kegiatan.objects.filter(penyelenggara__icontains=query)
queryset = query_by_nama | query_by_deskripsi | query_by_penyelenggara
def search_kegiatan(request, query):
queryset = _filter_kegiatan(query)
if not queryset.exists():
raise NotFound(detail="No Kegiatan available")
raise NotFound(detail=EMPTY_KEGIATAN)
serializer = KegiatanSerializerRead(queryset, many=True)
new_dict = {item['id']: _clean_json_kegiatan(dict(item)) for item in serializer.data}
return JsonResponse(new_dict, status=HTTPStatus.OK)
......@@ -112,12 +121,12 @@ def search_kegiatan(request, query=''):
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def list_kegiatan_by_name(request, start_index, query_limit=9):
def list_kegiatan_by_name(request, query=None):
time_now = timezone.now()
queryset = Kegiatan.objects.filter(time_end__gte=time_now).order_by('nama_kegiatan')
queryset = queryset[start_index:start_index+query_limit]
queryset = _filter_kegiatan(query) \
.filter(time_end__gte=time_now).order_by('nama_kegiatan')
if not queryset.exists():
raise NotFound(detail="No Kegiatan available")
raise NotFound(detail=EMPTY_KEGIATAN)
serializer = KegiatanSerializerRead(queryset, many=True)
new_dict = {item['id']: _clean_json_kegiatan(dict(item)) for item in serializer.data}
return JsonResponse(new_dict, status=HTTPStatus.OK)
......@@ -125,12 +134,12 @@ def list_kegiatan_by_name(request, start_index, query_limit=9):
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def list_kegiatan_by_time(request, start_index, query_limit=9):
def list_kegiatan_by_time(request, query=None):
time_now = timezone.now()
queryset = Kegiatan.objects.filter(time_end__gte=time_now).order_by('time_start')
queryset = queryset[start_index:start_index+query_limit]
queryset = _filter_kegiatan(query) \
.filter(time_end__gte=time_now).order_by('time_start')
if not queryset.exists():
raise NotFound(detail="No Kegiatan available")
raise NotFound(detail=EMPTY_KEGIATAN)
serializer = KegiatanSerializerRead(queryset, many=True)
new_dict = {item['id']: _clean_json_kegiatan(dict(item)) for item in serializer.data}
return JsonResponse(new_dict, status=HTTPStatus.OK)
......@@ -138,12 +147,12 @@ def list_kegiatan_by_time(request, start_index, query_limit=9):
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def list_kegiatan_by_latest_added(request, start_index, query_limit=9):
def list_kegiatan_by_latest_added(request, query=None):
time_now = timezone.now()
queryset = Kegiatan.objects.filter(time_end__gte=time_now).order_by('-id')
queryset = queryset[start_index:start_index+query_limit]
queryset = _filter_kegiatan(query) \
.filter(time_end__gte=time_now).order_by('-id')
if not queryset.exists():
raise NotFound(detail="No Kegiatan available")
raise NotFound(detail=EMPTY_KEGIATAN)
serializer = KegiatanSerializerRead(queryset, many=True)
new_dict = {item['id']: _clean_json_kegiatan(dict(item)) for item in serializer.data}
return JsonResponse(new_dict, status=HTTPStatus.OK)
......@@ -166,3 +175,13 @@ def _create_list_kegiatan_foto(kegiatan, list_image):
foto = FotoKegiatan.objects.create(kegiatan=kegiatan, foto=image)
list_kegiatan_foto.append(foto)
return list_kegiatan_foto
def _filter_kegiatan(query=None):
if query is None:
return Kegiatan.objects.all()
else:
query_by_nama = Kegiatan.objects.filter(nama_kegiatan__icontains=query)
query_by_deskripsi = Kegiatan.objects.filter(deskripsi__icontains=query)
query_by_penyelenggara = Kegiatan.objects.filter(penyelenggara__icontains=query)
return query_by_nama | query_by_deskripsi | query_by_penyelenggara
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