Fakultas Ilmu Komputer UI

Commit 9460065c authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Merge branch 'dev-rafif' into 'PBI-12-info_kegiatan'

dev-rafif to PBI-12-Info_kegiatan

See merge request !61
parents a6a5f84b 94d63fd8
Pipeline #80492 passed with stages
in 8 minutes and 28 seconds
......@@ -34,10 +34,11 @@ class LokasiSerializer(serializers.ModelSerializer):
class FotoKegiatanSerializer(serializers.ModelSerializer):
place_id = serializers.CharField(source='lokasi.place_id', read_only=True)
place_id = serializers.CharField(source='kegiatan.lokasi.place_id', read_only=True)
class Meta:
model = FotoKegiatan
fields = '__all__'
fields = ("id", "kegiatan", "place_id", "foto")
extra_kwargs = {
'foto': {'required': True},
'kegiatan': {'required': True},
......@@ -73,7 +74,7 @@ class KegiatanSerializerRead(KegiatanSerializer):
time = obj.time_start
zona_waktu = obj.zona_waktu
time = time_converter(time, zona_waktu)
return time.strftime('%Y-%m-%dT%H:%M')
return time.strftime('%Y-%m-%d %H:%M')
def get_time_end(self, obj):
time = obj.time_end
......@@ -81,8 +82,7 @@ class KegiatanSerializerRead(KegiatanSerializer):
if time is None:
return None
time = time_converter(time, zona_waktu)
return time.strftime('%Y-%m-%dT%H:%M')
return time.strftime('%Y-%m-%d %H:%M')
class KomentarKegiatanSerializer(serializers.ModelSerializer):
kegiatan = serializers.IntegerField(source='kegiatan.id', read_only=True)
......
......@@ -58,15 +58,15 @@ class InformasiFasilitasTest(TestCase):
time_end = timezone.now() + timedelta(days=2)
time_start_zone = {
"WIB": time_start.astimezone(TIMEZONE_INDONESIA["WIB"]).strftime("%Y-%m-%dT%H:%M%z"),
"WITA": time_start.astimezone(TIMEZONE_INDONESIA["WITA"]).strftime("%Y-%m-%dT%H:%M%z"),
"WIT": time_start.astimezone(TIMEZONE_INDONESIA["WIT"]).strftime("%Y-%m-%dT%H:%M%z"),
"WIB": time_start.astimezone(TIMEZONE_INDONESIA["WIB"]).strftime("%Y-%m-%d %H:%M%z"),
"WITA": time_start.astimezone(TIMEZONE_INDONESIA["WITA"]).strftime("%Y-%m-%d %H:%M%z"),
"WIT": time_start.astimezone(TIMEZONE_INDONESIA["WIT"]).strftime("%Y-%m-%d %H:%M%z"),
}
time_end_zone = {
"WIB": time_end.astimezone(TIMEZONE_INDONESIA["WIB"]).strftime("%Y-%m-%dT%H:%M%z"),
"WITA": time_end.astimezone(TIMEZONE_INDONESIA["WITA"]).strftime("%Y-%m-%dT%H:%M%z"),
"WIT": time_end.astimezone(TIMEZONE_INDONESIA["WIT"]).strftime("%Y-%m-%dT%H:%M%z"),
"WIB": time_end.astimezone(TIMEZONE_INDONESIA["WIB"]).strftime("%Y-%m-%d %H:%M%z"),
"WITA": time_end.astimezone(TIMEZONE_INDONESIA["WITA"]).strftime("%Y-%m-%d %H:%M%z"),
"WIT": time_end.astimezone(TIMEZONE_INDONESIA["WIT"]).strftime("%Y-%m-%d %H:%M%z"),
}
# mock_kegiatan_test = {
......
......@@ -31,6 +31,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'place_id': self.lokasi.place_id,
'kegiatan_id': self.kegiatan.id,
}
self.kwargs_search_kegiatan = {'query': 'mock', }
self.kwargs_search_kegiatan_fail = {'query': 'this shouldnt exist', }
......@@ -51,26 +52,43 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
content_type='image/jpeg')
self.kegiatan_images = {'images': [image1, image2]}
self.foto = None
for image in self.kegiatan_images["images"]:
FotoKegiatan.objects.create(kegiatan=self.kegiatan, foto=image)
foto = FotoKegiatan.objects.create(kegiatan=self.kegiatan, foto=image)
self.foto = foto
image.seek(0)
self.kwargs_update_foto_kegiatan = {
'place_id': self.lokasi.place_id,
'kegiatan_id': self.kegiatan.id,
'id': self.foto.id,
}
self.get_list_kegiatan_url = \
reverse('list-kegiatan', kwargs=self.kwargs_place_id)
self.get_detail_kegiatan_url = \
reverse('detail-kegiatan', kwargs=self.kwargs_add_or_update_kegiatan)
self.get_nearest_kegiatan_url = \
reverse('nearest-kegiatan')
self.add_kegiatan_url = \
reverse('add-kegiatan', kwargs=self.kwargs_place_id)
self.update_kegiatan_url = reverse('update-kegiatan',
kwargs=self.kwargs_add_or_update_kegiatan)
self.search_kegiatan_url = reverse('search-kegiatan',
kwargs=self.kwargs_search_kegiatan)
self.search_kegiatan_fail_url = reverse('search-kegiatan',
kwargs=self.kwargs_search_kegiatan_fail)
self.list_foto_kegiatan_url = \
reverse('list-foto-kegiatan', kwargs=self.kwargs_get_foto_kegiatan)
self.add_foto_kegiatan_url = \
reverse('add-foto-kegiatan', kwargs=self.kwargs_get_foto_kegiatan)
self.update_foto_kegiatan_url = \
reverse('update-foto-kegiatan', kwargs=self.kwargs_update_foto_kegiatan)
self.list_kegiatan_by_latest_added_url = \
reverse('list-kegiatan-by-latest-added', kwargs=self.kwargs_list_kegiatan_in_order)
self.list_kegiatan_by_name_url = \
......@@ -302,6 +320,42 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
self.assertEqual(item["kegiatan"], self.kegiatan.id)
counter += 1
def test_can_add_foto_kegiatan(self):
data = {"foto": self.kegiatan_images["images"][0]}
response = self.client.post(self.add_foto_kegiatan_url, data=data)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
kegiatan_fotos = FotoKegiatan.objects.all()
self.assertEqual(len(kegiatan_fotos), 3)
def test_failed_add_foto_kegiatan(self):
data = {"foto": self.kegiatan_images["images"][0]}
url = reverse('add-foto-kegiatan', kwargs={
'place_id': "IDSNKCM",
'kegiatan_id': 222,
})
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
kegiatan_fotos = FotoKegiatan.objects.all()
self.assertEqual(len(kegiatan_fotos), 2)
def test_can_put_update_foto_kegiatan(self):
data = {"foto": self.kegiatan_images["images"][0]}
response = self.client.put(self.update_foto_kegiatan_url, data=data)
self.assertEqual(response.status_code, HTTPStatus.ACCEPTED)
output = response.json()
self.assertIsNotNone(output.get("foto"))
self.assertNotEqual(self.foto.foto, output.get("foto"))
def test_failed_put_update_foto_kegiatan(self):
data = {"foto": self.kegiatan_images["images"][0]}
url = reverse('update-foto-kegiatan', kwargs={
'place_id': "IDSNKCM",
'kegiatan_id': 222,
'id': 444,
})
response = self.client.put(url, data=data)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
def test_can_search_kegiatan(self):
response = Client().get(self.search_kegiatan_url)
self.assertEqual(response.status_code, HTTPStatus.OK)
......
......@@ -50,6 +50,12 @@ urlpatterns = [
path('lokasi/list-foto-kegiatan/<str:place_id>/<int:kegiatan_id>',
views_kegiatan.list_foto_kegiatan, name='list-foto-kegiatan'),
path('lokasi/add-foto-kegiatan/<str:place_id>/<int:kegiatan_id>',
views_kegiatan.add_foto_kegiatan, name='add-foto-kegiatan'),
path('lokasi/update-foto-kegiatan/<str:place_id>/<int:kegiatan_id>/<int:id>',
views_kegiatan.update_foto_kegiatan, name='update-foto-kegiatan'),
path('lokasi/list-kegiatan-by-latest-added/<int:start_index>/<int:query_limit>',
views_kegiatan.list_kegiatan_by_latest_added, name='list-kegiatan-by-latest-added'),
......
......@@ -95,6 +95,40 @@ def list_foto_kegiatan(request, place_id, kegiatan_id):
new_dict = {item['id']: dict(item) for item in data_response}
return JsonResponse(new_dict, status=HTTPStatus.OK)
@api_view(['POST'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def add_foto_kegiatan(request, place_id, kegiatan_id):
try:
kegiatan = Kegiatan.objects.get(lokasi__place_id=place_id, id=kegiatan_id)
data = request.data.dict()
data.update({"kegiatan": kegiatan.id})
serializer = FotoKegiatanSerializer(data=data)
serializer.is_valid(raise_exception=True)
serializer.save()
return JsonResponse(serializer.data, status=HTTPStatus.CREATED)
except Kegiatan.DoesNotExist:
raise NotFound(detail="Kegiatan doesn't exist")
@api_view(['PUT'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def update_foto_kegiatan(request, place_id, kegiatan_id, id):
try:
foto_kegiatan = FotoKegiatan.objects.get(kegiatan__lokasi__place_id=place_id,
kegiatan_id=kegiatan_id,
id=id)
serializer = FotoKegiatanSerializer(foto_kegiatan, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
return JsonResponse(serializer.data, status=HTTPStatus.ACCEPTED)
except FotoKegiatan.DoesNotExist:
raise NotFound(detail="Foto Kegiatan doesn't exist")
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
......
......@@ -202,7 +202,7 @@ SOCIALACCOUNT_PROVIDERS = {
LOGIN_REDIRECT_URL = '/'
REST_FRAMEWORK = {
'DATETIME_FORMAT': "%Y-%m-%dT%H:%M%z",
'DATETIME_FORMAT': "%Y-%m-%d %H:%M%z",
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'DEFAULT_AUTHENTICATION_CLASSES': [
......
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