Fakultas Ilmu Komputer UI

Commit 1455ccab authored by Christopher Samuel's avatar Christopher Samuel
Browse files

Merge branch 'PBI-14-Agenda-Kegiatan' into 'development'

Pbi 14 agenda kegiatan

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