Fakultas Ilmu Komputer UI

Commit 79c68b7d authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Merge branch 'development' into 'PBI-12-info_kegiatan'

# Conflicts:
#   informasi_fasilitas/models.py
#   informasi_fasilitas/urls.py
#   informasi_fasilitas/views.py
parents 99952ed6 0aae65a3
Pipeline #81046 failed with stages
in 2 minutes and 22 seconds
......@@ -2,11 +2,11 @@ from django.contrib import admin, messages
from django.utils.translation import gettext_lazy as _
from notification.utils import send_komentar_notification, get_target_fcm_device
from .models import Lokasi, Fasilitas, Komentar, Kegiatan, KomentarKegiatan, FotoKegiatan
from .models import Lokasi, Fasilitas, KomentarFasilitas, Kegiatan, KomentarKegiatan, FotoKegiatan
class KomentarAdmin(admin.ModelAdmin):
list_display = ('__str__', 'deskripsi', 'user', 'notify_to')
list_display = ('__str__', 'user', 'created', 'notify_to')
actions = ('send_notification',)
def send_notification(self, request, queryset):
......@@ -30,7 +30,7 @@ class KomentarAdmin(admin.ModelAdmin):
# Register your models here.
admin.site.register(Lokasi)
admin.site.register(Fasilitas)
admin.site.register(Komentar, KomentarAdmin)
admin.site.register(KomentarFasilitas, KomentarAdmin)
admin.site.register(Kegiatan)
admin.site.register(KomentarKegiatan, KomentarAdmin)
admin.site.register(FotoKegiatan)
# Generated by Django 3.1.7 on 2021-05-31 10:03
from django.conf import settings
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('informasi_fasilitas', '0016_kegiatan_links'),
]
operations = [
migrations.RenameModel(
old_name='Komentar',
new_name='KomentarFasilitas',
),
migrations.RenameField(
model_name='komentarfasilitas',
old_name='date_time',
new_name='created',
),
]
# Generated by Django 3.1.7 on 2021-05-31 12:02
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('informasi_fasilitas', '0017_auto_20210531_1003'),
]
operations = [
migrations.RenameField(
model_name='komentarfasilitas',
old_name='fasilitas',
new_name='informasi',
),
migrations.RenameField(
model_name='komentarkegiatan',
old_name='kegiatan',
new_name='informasi',
),
]
# Generated by Django 3.1.7 on 2021-05-31 12:41
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('informasi_fasilitas', '0018_auto_20210531_1202'),
('informasi_fasilitas', '0019_auto_20210527_2251'),
]
operations = [
]
# Generated by Django 3.1.7 on 2021-05-31 15:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('informasi_fasilitas', '0020_merge_20210531_1241'),
]
operations = [
migrations.AlterField(
model_name='kegiatan',
name='zona_waktu',
field=models.CharField(choices=[('WIB', 'WIB'), ('WITA', 'WITA'), ('WIT', 'WIT')], default='WIB', max_length=4),
),
]
......@@ -100,14 +100,19 @@ class Kegiatan(models.Model):
class Komentar(models.Model):
objects = models.Manager()
fasilitas = models.ForeignKey(Fasilitas, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
date_time = models.DateTimeField(auto_now_add=True)
deskripsi = models.TextField()
created = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
def __str__(self):
return self.deskripsi
class KomentarFasilitas(Komentar):
informasi = models.ForeignKey(Fasilitas, on_delete=models.CASCADE)
class Likes(models.Model):
objects = models.Manager()
user = models.ForeignKey(User, on_delete=models.CASCADE)
......@@ -126,15 +131,9 @@ class Dislikes(models.Model):
class Meta:
unique_together = ('user', 'fasilitas')
class KomentarKegiatan(models.Model):
objects = models.Manager()
user = models.ForeignKey(User, on_delete=models.CASCADE)
kegiatan = models.ForeignKey(Kegiatan, on_delete=models.CASCADE)
deskripsi = models.TextField()
created = models.DateTimeField(auto_now_add=True)
class KomentarKegiatan(Komentar):
informasi = models.ForeignKey(Kegiatan, on_delete=models.CASCADE)
def __str__(self):
return self.deskripsi
class FotoKegiatan(models.Model):
objects = models.Manager()
......
......@@ -113,10 +113,11 @@ class FotoKegiatanSerializer(serializers.ModelSerializer):
class KegiatanSerializer(serializers.ModelSerializer):
place_id = serializers.CharField(source='lokasi.place_id', read_only=True)
creator = serializers.CharField(source='user.last_name', read_only=True)
creator_email = serializers.CharField(source='user.email', read_only=True)
class Meta:
model = Kegiatan
fields = ('id', 'place_id', 'creator', 'lokasi', 'user',
fields = ('id', 'place_id', 'creator', 'lokasi', 'user', 'creator_email',
'nama_kegiatan', 'penyelenggara', 'deskripsi',
"links", "nomor_kontak", "nama_kontak",
"zona_waktu", 'time_start', 'time_end')
......@@ -150,11 +151,14 @@ class KegiatanSerializerRead(KegiatanSerializer):
return time.strftime('%Y-%m-%d %H:%M')
class KomentarKegiatanSerializer(serializers.ModelSerializer):
kegiatan = serializers.IntegerField(source='kegiatan.id', read_only=True)
kegiatan = serializers.IntegerField(source='informasi.id', read_only=True)
creator = serializers.CharField(source='user.last_name', read_only=True)
creator_email = serializers.CharField(source='user.email', read_only=True)
class Meta:
model = KomentarKegiatan
fields = ('id', 'creator', 'kegiatan', 'deskripsi', 'created')
fields = ('id', 'creator', 'kegiatan', 'deskripsi', 'created',
'creator_email')
extra_kwargs = {
'deskripsi': {'required': True},
'created': {'required' : True}
......
......@@ -37,7 +37,7 @@ class TestAdmin(TestCase):
def test_list_display(self):
list_display = self.komentar_admin.get_list_display(request)
self.assertEqual(list_display, ('__str__', 'deskripsi', 'user',
self.assertEqual(list_display, ('__str__', 'user', 'created',
'notify_to'))
def test_list_actions(self):
......
......@@ -12,7 +12,7 @@ from django.core.files.base import ContentFile
from .models import (
Lokasi,
Fasilitas,
Komentar,
KomentarFasilitas,
KURSI_RODA,
Likes,
Dislikes,
......@@ -257,10 +257,10 @@ class InformasiFasilitasTest(TestCase):
fasilitas_dict=fasilitas_dict,
)
return Komentar.objects.create(
return KomentarFasilitas.objects.create(
**komentar_dict,
user=user,
fasilitas=fasilitas,
informasi=fasilitas,
)
def create_likes_test(
......@@ -364,19 +364,19 @@ class InformasiFasilitasTest(TestCase):
user=None,
komentar_kegiatan_dict=mock_komentar_kegiatan_test,
user_dict=mock_user_test,
kegiatan=None
informasi=None
):
user = self.get_or_create_user_test(
user=user,
user_dict=user_dict,
)
if kegiatan is None:
kegiatan = self.create_kegiatan_test()
if informasi is None:
informasi = self.create_kegiatan_test()
return KomentarKegiatan.objects.create(
**komentar_kegiatan_dict,
kegiatan=kegiatan,
informasi=informasi,
user=user
)
......
from django.db.utils import IntegrityError
from .test_base import InformasiFasilitasTest
from .models import (Lokasi, Fasilitas, Komentar, Likes, Dislikes,
from .models import (Lokasi, Fasilitas, KomentarFasilitas, Likes, Dislikes,
Kegiatan, FotoKegiatan, KomentarKegiatan)
......@@ -49,13 +49,13 @@ class InformasiFasilitasModelTest(InformasiFasilitasTest):
def test_models_komentar_not_created(self):
with self.assertRaises(IntegrityError) as ex:
obj = Komentar(fasilitas=None)
obj = KomentarFasilitas(informasi=None)
obj.save()
self.assertEqual(ex.expected, IntegrityError)
def test_models_create_new_komentar(self):
self.create_komentar_test()
count = Komentar.objects.all().count()
count = KomentarFasilitas.objects.all().count()
self.assertNotEqual(count, 0)
def test_models_komentar_str(self):
......
from unittest.mock import patch
from datetime import timedelta
from http import HTTPStatus
import json
import shutil
import os
from datetime import timedelta
from http import HTTPStatus
from django.test import Client
from django.urls import reverse
from django.test import override_settings
from django.utils import timezone
from django.core.files.uploadedfile import SimpleUploadedFile
from django.conf import settings
from unittest.mock import patch
from .test_base import InformasiFasilitasViewTest
from .models import Lokasi, Kegiatan, FotoKegiatan
from pplbackend.utils import response_decode
from .models import Kegiatan, FotoKegiatan
class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
......@@ -120,6 +122,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
expected_json = self.mock_kegiatan_test.copy()
expected_json.update({'creator': 'mock last_name',
'place_id': 'mock_place_id',
'creator_email': self.mock_user_test['email'],
'time_start': self.kegiatan_time_start,
'time_end': self.kegiatan_time_end})
self.assertEqual(response.status_code, HTTPStatus.CREATED)
......@@ -155,6 +158,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'id': self.kegiatan.id,
'place_id': self.kegiatan.lokasi.place_id,
'creator': self.kegiatan.user.last_name,
'creator_email': self.kegiatan.user.email,
'nama_kegiatan': self.kegiatan.nama_kegiatan,
'penyelenggara': self.kegiatan.penyelenggara,
'deskripsi': self.kegiatan.deskripsi,
......@@ -176,6 +180,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'id': self.kegiatan.id,
'place_id': self.kegiatan.lokasi.place_id,
'creator': self.kegiatan.user.last_name,
'creator_email': self.kegiatan.user.email,
'nama_kegiatan': self.kegiatan.nama_kegiatan,
'penyelenggara': self.kegiatan.penyelenggara,
'deskripsi': self.kegiatan.deskripsi,
......@@ -204,6 +209,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'id': kegiatan_wita.id,
'place_id': kegiatan_wita.lokasi.place_id,
'creator': kegiatan_wita.user.last_name,
'creator_email': kegiatan_wita.user.email,
'nama_kegiatan': kegiatan_wita.nama_kegiatan,
'penyelenggara': kegiatan_wita.penyelenggara,
'deskripsi': kegiatan_wita.deskripsi,
......@@ -232,6 +238,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'id': kegiatan_wit.id,
'place_id': kegiatan_wit.lokasi.place_id,
'creator': kegiatan_wit.user.last_name,
'creator_email': kegiatan_wit.user.email,
'nama_kegiatan': kegiatan_wit.nama_kegiatan,
'penyelenggara': kegiatan_wit.penyelenggara,
'deskripsi': kegiatan_wit.deskripsi,
......@@ -252,6 +259,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'id': self.kegiatan.id,
'place_id': self.kegiatan.lokasi.place_id,
'creator': self.kegiatan.user.last_name,
'creator_email': self.kegiatan.user.email,
'nama_kegiatan': self.kegiatan.nama_kegiatan,
'penyelenggara': self.kegiatan.penyelenggara,
'deskripsi': self.kegiatan.deskripsi,
......@@ -283,6 +291,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
expected_json = self.mock_kegiatan_test.copy()
expected_json.update({'creator': 'mock last_name',
'place_id': 'mock_place_id',
'creator_email': self.mock_user_test['email'],
'time_start': self.kegiatan_time_start,
'time_end': self.kegiatan_time_end})
......@@ -365,7 +374,8 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'id': self.kegiatan.id,
'place_id': self.kegiatan.lokasi.place_id,
'creator': self.kegiatan.user.last_name,
'nama_kegiatan': self.kegiatan.nama_kegiatan,
'creator_email': self.kegiatan.user.email,
'nama_kegiatan' : self.kegiatan.nama_kegiatan,
'penyelenggara': self.kegiatan.penyelenggara,
'deskripsi': self.kegiatan.deskripsi,
'links': self.kegiatan.links,
......
import json
from http import HTTPStatus
from django.urls import reverse
from pplbackend.utils import response_decode
from .test_base import InformasiFasilitasViewTest
from .models import Komentar
from .models import KomentarFasilitas
from pplbackend.utils import response_decode
from .views import TIME_FORMAT
......@@ -26,7 +26,7 @@ class KomentarRelatedViewTest(InformasiFasilitasViewTest):
self.client.post(self.add_komentar_url, self.mock_komentar_test)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
count = Komentar.objects.filter(fasilitas=self.fasilitas).count()
count = KomentarFasilitas.objects.filter(informasi=self.fasilitas).count()
self.assertEqual(count, 1)
def test_cannot_comment_facility_if_deskripsi_empty(self):
......@@ -48,7 +48,8 @@ class KomentarRelatedViewTest(InformasiFasilitasViewTest):
'id': komentar.id,
'deskripsi': komentar.deskripsi,
'creator': komentar.user.last_name,
'date_time': komentar.date_time.strftime(TIME_FORMAT),
'creator_email': komentar.user.email,
'created': komentar.created.strftime(TIME_FORMAT),
}
}
self.assertEqual(response_json, expected_json)
......@@ -29,7 +29,7 @@ class KomentarKegiatanRelatedViewTest(InformasiFasilitasViewTest):
self.client.post(self.add_komentar_kegiatan_url, self.mock_komentar_kegiatan_test)
self.assertEqual(response.status_code, HTTPStatus.CREATED)
count = KomentarKegiatan.objects.filter(kegiatan=self.kegiatan).count()
count = KomentarKegiatan.objects.filter(informasi=self.kegiatan).count()
self.assertEqual(count, 1)
def test_fail_add_komentar_kegiatan(self):
......@@ -38,18 +38,18 @@ class KomentarKegiatanRelatedViewTest(InformasiFasilitasViewTest):
self.client.post(self.add_komentar_kegiatan_url, None)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
count = KomentarKegiatan.objects.filter(kegiatan=self.kegiatan).count()
count = KomentarKegiatan.objects.filter(informasi=self.kegiatan).count()
self.assertEqual(count, 0)
def test_can_get_list_komentar_kegiatan(self):
komentar = self.create_komentar_kegiatan_test(user=self.user, kegiatan=self.kegiatan)
komentar = self.create_komentar_kegiatan_test(user=self.user, informasi=self.kegiatan)
response = self.client.get(self.list_komentar_kegiatan_url)
self.assertEqual(response.status_code, HTTPStatus.OK)
response_json = response_decode(response)
expected_id = komentar.id
expected_creator = komentar.user.last_name
expected_kegiatan = komentar.kegiatan.id
expected_kegiatan = komentar.informasi.id
expected_deskripsi = komentar.deskripsi
self.assertEqual(response_json[str(komentar.id)]['id'], expected_id)
......@@ -59,7 +59,7 @@ class KomentarKegiatanRelatedViewTest(InformasiFasilitasViewTest):
self.assertEqual(True, ('created' in response_json[str(komentar.id)].keys()))
def test_can_get_komentar_kegiatan(self):
komentar = self.create_komentar_kegiatan_test(user=self.user, kegiatan=self.kegiatan)
komentar = self.create_komentar_kegiatan_test(user=self.user, informasi=self.kegiatan)
kwargs_get_komentar_kegiatan = {
'place_id': self.lokasi.place_id,
'kegiatan_id': self.kegiatan.id,
......@@ -73,7 +73,7 @@ class KomentarKegiatanRelatedViewTest(InformasiFasilitasViewTest):
response_json = response_decode(response)
expected_id = komentar.id
expected_creator = komentar.user.last_name
expected_kegiatan = komentar.kegiatan.id
expected_kegiatan = komentar.informasi.id
expected_deskripsi = komentar.deskripsi
self.assertEqual(response_json['id'], expected_id)
......@@ -88,7 +88,7 @@ class KomentarKegiatanRelatedViewTest(InformasiFasilitasViewTest):
def test_can_delete_komentar_kegiatan(self):
KomentarKegiatan.objects.all().delete()
komentar = self.create_komentar_kegiatan_test(user=self.user, kegiatan=self.kegiatan)
komentar = self.create_komentar_kegiatan_test(user=self.user, informasi=self.kegiatan)
kwargs_delete_komentar_kegiatan = {
'place_id': self.lokasi.place_id,
......@@ -102,7 +102,7 @@ class KomentarKegiatanRelatedViewTest(InformasiFasilitasViewTest):
response = self.client.delete(delete_komentar_kegiatan_url)
self.assertEqual(response.status_code, HTTPStatus.OK)
count = KomentarKegiatan.objects.filter(kegiatan=self.kegiatan).count()
count = KomentarKegiatan.objects.filter(informasi=self.kegiatan).count()
self.assertEqual(count, 0)
def test_fail_delete_komentar_kegiatan(self):
......
......@@ -13,7 +13,6 @@ from pplbackend.utils import response_decode
class LokasiRelatedViewTest(InformasiFasilitasViewTest):
def setUp(self):
super().setUp()
self.list_lokasi_url = reverse('list-lokasi')
......
......@@ -39,6 +39,9 @@ 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'),
......
......@@ -175,13 +175,13 @@ def add_komentar(request, place_id, id):
lokasi = Lokasi.objects.get(place_id=place_id)
fasilitas = Fasilitas.objects.get(lokasi=lokasi, id=id)
deskripsi = request.POST['deskripsi']
komentar = Komentar.objects.create(fasilitas=fasilitas,
komentar = KomentarFasilitas.objects.create(informasi=fasilitas,
user=request.user,
deskripsi=deskripsi)
send_komentar_notification(komentar)
return JsonResponse({'response': 'komentar added',
'id': komentar.id,
"created_date": komentar.date_time.strftime(TIME_FORMAT)}, status=HTTPStatus.CREATED)
"created_date": komentar.created.strftime(TIME_FORMAT)}, status=HTTPStatus.CREATED)
except KeyError as e:
return JsonResponse({'response': missing_key_message(str(e))}, status=HTTPStatus.BAD_REQUEST)
except Exception as e:
......@@ -195,7 +195,7 @@ def list_komentar(request, place_id, id):
try:
lokasi = Lokasi.objects.get(place_id=place_id)
fasilitas = Fasilitas.objects.get(lokasi=lokasi, id=id)
list_komentar = Komentar.objects.filter(fasilitas=fasilitas)
list_komentar = KomentarFasilitas.objects.filter(informasi=fasilitas)
return_json = {}
for komentar in list_komentar:
return_json[komentar.id] = {}
......@@ -203,7 +203,8 @@ def list_komentar(request, place_id, id):
komentar_details["id"] = komentar.id
komentar_details["deskripsi"] = komentar.deskripsi
komentar_details["creator"] = komentar.user.last_name
komentar_details["date_time"] = komentar.date_time.strftime(
komentar_details["creator_email"] = komentar.user.email
komentar_details["created"] = komentar.created.strftime(
TIME_FORMAT)
return JsonResponse(return_json, status=HTTPStatus.OK)
except KeyError as key:
......
......@@ -132,7 +132,7 @@ def update_foto_kegiatan(request, place_id, kegiatan_id, id):
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
def search_kegiatan(request, query):
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)
......
......@@ -15,7 +15,7 @@ from .models import Kegiatan, KomentarKegiatan
@permission_classes([])
def list_komentar_kegiatan(request, place_id, kegiatan_id):
try:
queryset = KomentarKegiatan.objects.filter(kegiatan__lokasi__place_id=place_id, kegiatan__id=kegiatan_id)
queryset = KomentarKegiatan.objects.filter(informasi__lokasi__place_id=place_id, informasi__id=kegiatan_id)
serializer = KomentarKegiatanSerializer(queryset, many=True)
data_response = serializer.data
new_dict = {item['id']: dict(item) for item in data_response}
......@@ -28,8 +28,8 @@ def list_komentar_kegiatan(request, place_id, kegiatan_id):
@permission_classes([])
def get_komentar_kegiatan(request, place_id, kegiatan_id, komentar_id):
try:
komentar = KomentarKegiatan.objects.get(kegiatan__lokasi__place_id=place_id,
kegiatan__id=kegiatan_id, id=komentar_id)
komentar = KomentarKegiatan.objects.get(informasi__lokasi__place_id=place_id,
informasi__id=kegiatan_id, id=komentar_id)
serializer = KomentarKegiatanSerializer(komentar, many=False)
return JsonResponse(serializer.data, status=HTTPStatus.OK)
except ObjectDoesNotExist:
......@@ -44,7 +44,7 @@ def add_komentar_kegiatan(request, place_id, kegiatan_id):
deskripsi = request.POST['deskripsi']
komentar = KomentarKegiatan.objects \
.create(
kegiatan=kegiatan,
informasi=kegiatan,
user=request.user,
deskripsi=deskripsi
)
......@@ -59,7 +59,7 @@ def add_komentar_kegiatan(request, place_id, kegiatan_id):
@permission_classes([IsAuthenticated])
def delete_komentar_kegiatan(request, place_id, kegiatan_id, komentar_id):
try:
komentar = KomentarKegiatan.objects.get(kegiatan__lokasi__place_id=place_id, kegiatan__id=kegiatan_id, id=komentar_id)
komentar = KomentarKegiatan.objects.get(informasi__lokasi__place_id=place_id, informasi__id=kegiatan_id, id=komentar_id)
komentar.delete()
return JsonResponse({'response': 'komentar kegiatan deleted', 'id': komentar_id},
status=HTTPStatus.OK)
......
import json
from http import HTTPStatus
from django.test import TestCase, Client, override_settings
from django.conf import settings
from django.test import TestCase, Client
from django.db.utils import IntegrityError
from django.contrib.auth.models import User
from django.urls import path, include, reverse
from pplbackend.utils import get_client_login_with_user
from .models import Sekolah, Penyandang, Komunitas
from .serializers import SekolahSerializer, KomunitasSerializer
import django
ID = 'id'
NAME = 'name'
......@@ -118,25 +118,11 @@ def penyandang_setup():
def auth_setup():
email = 'mock_user@email.com'
passcode = 'pass12345'
Client().post('/api/register/', {
'name': 'name',
'email': email,
'phone_number': 000000000,
'password': passcode
})
test_user = User.objects.get(username=email)
test_user.is_active = True
test_user.save()
token_response = Client().post('/api-token-auth/',
{'username': email, 'password': passcode})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
return Client(HTTP_AUTHORIZATION='token '+token)
user = User.objects.create_user(username=email)
return get_client_login_with_user(user)