Fakultas Ilmu Komputer UI

Verified Commit 26b9060d authored by Muhammad Ariq Basyar's avatar Muhammad Ariq Basyar
Browse files

[REFACTOR] related to informasi_fasilitas models and notification:

- now KomentarFasilitas and KomentarKegiatan models is subclass from Komentar
- change name of date_time field on KomentarFasilitas to created
- improved send_komentar_notification to check if fcm target is exists first then send
parent 8821cb68
Pipeline #80384 passed with stages
in 6 minutes
......@@ -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',
),
]
......@@ -89,14 +89,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):
fasilitas = models.ForeignKey(Fasilitas, on_delete=models.CASCADE)
class Likes(models.Model):
objects = models.Manager()
user = models.ForeignKey(User, on_delete=models.CASCADE)
......@@ -109,15 +114,8 @@ class Dislikes(models.Model):
fasilitas = models.ForeignKey(Fasilitas, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
class KomentarKegiatan(models.Model):
objects = models.Manager()
user = models.ForeignKey(User, on_delete=models.CASCADE)
class KomentarKegiatan(Komentar):
kegiatan = models.ForeignKey(Kegiatan, on_delete=models.CASCADE)
deskripsi = models.TextField()
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.deskripsi
class FotoKegiatan(models.Model):
objects = models.Manager()
......
......@@ -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):
......
......@@ -11,7 +11,7 @@ from django.core.files.base import ContentFile
from .models import (
Lokasi,
Fasilitas,
Komentar,
KomentarFasilitas,
KURSI_RODA,
Likes,
Dislikes,
......@@ -177,7 +177,7 @@ class InformasiFasilitasTest(TestCase):
fasilitas_dict=fasilitas_dict,
)
return Komentar.objects.create(
return KomentarFasilitas.objects.create(
**komentar_dict,
user=user,
fasilitas=fasilitas,
......
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(fasilitas=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):
......
......@@ -3,7 +3,7 @@ from http import HTTPStatus
from django.urls import reverse
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(fasilitas=self.fasilitas).count()
self.assertEqual(count, 1)
def test_cannot_comment_facility_if_deskripsi_empty(self):
......@@ -48,7 +48,7 @@ class KomentarRelatedViewTest(InformasiFasilitasViewTest):
'id': komentar.id,
'deskripsi': komentar.deskripsi,
'creator': komentar.user.last_name,
'date_time': komentar.date_time.strftime(TIME_FORMAT),
'created': komentar.created.strftime(TIME_FORMAT),
}
}
self.assertEqual(response_json, expected_json)
......@@ -9,7 +9,7 @@ from rest_framework import viewsets
from notification.utils import send_komentar_notification
from .serializers import LokasiSerializer
from .models import Lokasi, Fasilitas, Komentar, Likes, Dislikes
from .models import Lokasi, Fasilitas, KomentarFasilitas, Likes, Dislikes
from .permissions import UserPermission
TIME_FORMAT = "%d-%m-%Y %H:%M:%S"
......@@ -242,13 +242,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(fasilitas=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:
......@@ -262,7 +262,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(fasilitas=fasilitas)
return_json = {}
for komentar in list_komentar:
return_json[komentar.id] = {}
......@@ -270,7 +270,7 @@ 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["created"] = komentar.created.strftime(
TIME_FORMAT)
return JsonResponse(return_json, status=HTTPStatus.OK)
except KeyError as key:
......
......@@ -4,7 +4,7 @@ from django.db.models import QuerySet
from django.contrib.auth.models import User
from fcm_django.models import FCMDevice
from informasi_fasilitas.models import (Komentar, KomentarKegiatan,
from informasi_fasilitas.models import (KomentarFasilitas, KomentarKegiatan,
Fasilitas, Kegiatan, Lokasi)
from . import utils
from .test_base import BaseTestNotification
......@@ -12,7 +12,7 @@ from .test_base import BaseTestNotification
class TestUtilsNotification(BaseTestNotification):
def test_get_type_informasi_from_komentar_should_return_fasilitas(self):
komentar = Komentar()
komentar = KomentarFasilitas()
type_fasilitas = utils.get_type_informasi_from_komentar(komentar)
self.assertEqual(type_fasilitas, 'fasilitas')
......@@ -23,7 +23,7 @@ class TestUtilsNotification(BaseTestNotification):
def test_get_informasi_from_komentar_should_return_fasilitas(self):
fasilitas = Fasilitas()
komentar = Komentar(fasilitas=fasilitas)
komentar = KomentarFasilitas(fasilitas=fasilitas)
ret_fasilitas = utils.get_informasi(komentar)
self.assertTrue(isinstance(ret_fasilitas, Fasilitas))
......@@ -37,7 +37,7 @@ class TestUtilsNotification(BaseTestNotification):
place_id = 'dummy_place_id'
lokasi = Lokasi(place_id=place_id)
fasilitas = Fasilitas(lokasi=lokasi)
komentar = Komentar(fasilitas=fasilitas)
komentar = KomentarFasilitas(fasilitas=fasilitas)
ret_place_id = utils.get_place_id_from_komentar(komentar)
......@@ -56,7 +56,7 @@ class TestUtilsNotification(BaseTestNotification):
def test_get_informasi_id_from_komentar_should_return_its_informasi_id(self):
_id = 8
fasilitas = Fasilitas(id=_id)
komentar = Komentar(fasilitas=fasilitas)
komentar = KomentarFasilitas(fasilitas=fasilitas)
ret_place_id = utils.get_informasi_id_from_komentar(komentar)
......@@ -73,7 +73,7 @@ class TestUtilsNotification(BaseTestNotification):
def test_get_sender_last_name_from_komentar_not_null(self):
user = User(last_name='ariqbasyar')
komentar = Komentar(user=user)
komentar = KomentarFasilitas(user=user)
ret_last_name = utils.get_sender_last_name_from_komentar(komentar)
......@@ -89,7 +89,7 @@ class TestUtilsNotification(BaseTestNotification):
fcmdevice.save()
fasilitas = Fasilitas(user=user)
komentar = Komentar(fasilitas=fasilitas, user=user_sender)
komentar = KomentarFasilitas(fasilitas=fasilitas, user=user_sender)
ret_fcm_device = utils.get_target_fcm_device(komentar)
......@@ -110,7 +110,7 @@ class TestUtilsNotification(BaseTestNotification):
fcmdevice.save()
fasilitas = Fasilitas(user=user)
komentar = Komentar(fasilitas=fasilitas, user=user_sender)
komentar = KomentarFasilitas(fasilitas=fasilitas, user=user_sender)
ret_fcm_device = utils.get_target_fcm_device(komentar)
......@@ -124,64 +124,45 @@ class TestUtilsNotification(BaseTestNotification):
fcmdevice.save()
fasilitas = Fasilitas(user=user)
komentar = Komentar(fasilitas=fasilitas, user=user)
komentar = KomentarFasilitas(fasilitas=fasilitas, user=user)
ret_fcm_device = utils.get_target_fcm_device(komentar)
self.assertEqual(len(ret_fcm_device), 0)
def test_get_formatted_username_length_less_than_max_length_name_should_not_get_trimmed(self):
username = 'ariq'
ret_username = utils.get_formatted_user_name(username, max_length_name=10)
self.assertEqual(username, ret_username)
def test_get_formatted_message(self):
komentar = MagicMock()
komentar.deskripsi = 'ini deskripsi'
ret_formatted_message = utils.get_formatted_message(komentar)
self.assertEqual(ret_formatted_message, '"%s"' % komentar.deskripsi)
def test_get_formatted_username_length_equal_max_length_name_should_not_get_trimmed(self):
username = 'ariqbasyar'
ret_username = utils.get_formatted_user_name(username, max_length_name=10)
self.assertEqual(username, ret_username)
def test_get_formatted_username_length_more_than_max_length_name_should_get_trimmed(self):
username = 'ariqbasyarr'
max_length_name = 10
replacement_str = '...'
length_replacement = len(replacement_str)
ret_username = utils.get_formatted_user_name(username, max_length_name=max_length_name,
replacement_str=replacement_str)
self.assertEqual(ret_username, '%s%s' % (username[:max_length_name - length_replacement],
replacement_str))
self.assertEqual(ret_username, 'ariqbas...')
def test_get_formatted_message_username_should_not_get_trimmed(self):
username = 'ariqbasyar'
ret_formatted_message = utils.get_formatted_message(username)
self.assertEqual(ret_formatted_message, 'ariqbasyar menambahkan komentar baru')
def test_get_formatted_message_username_should_get_trimmed(self):
username = 'ariqbasyarr'
ret_formatted_message = utils.get_formatted_message(username)
self.assertEqual(ret_formatted_message, 'ariqbas... menambahkan komentar baru')
def test_get_formatted_title(self):
username = 'dummy username'
ret_formatted_title = utils.get_formatted_title(username)
self.assertEqual(ret_formatted_title, 'dummy username menambahkan komentar baru')
@patch('notification.utils.get_target_fcm_device')
@patch('notification.utils.get_formatted_message')
@patch('notification.utils.get_formatted_title')
@patch('notification.utils.get_sender_last_name_from_komentar')
@patch('notification.utils.get_type_informasi_from_komentar')
@patch('notification.utils.get_informasi_id_from_komentar')
@patch('notification.utils.get_place_id_from_komentar')
def test_send_komentar_notification(self, *args):
komentar = Komentar(deskripsi='dummy deskripsi')
komentar = KomentarFasilitas(deskripsi='dummy deskripsi')
fcmdevice_mock = QuerySet(model=FCMDevice)
fcmdevice_mock_return_value = (1,1,0,0,[])
fcmdevice_mock.send_message = MagicMock(return_value=fcmdevice_mock_return_value)
formatted_message = 'ariqbasyar menambahkan komentar baru'
formatted_title = 'ariqbasyar menambahkan komentar baru'
formatted_message = 'ini komentar'
args[0].return_value = 'dummy place_id'
args[1].return_value = 1
args[2].return_value = 'fasilitas'
args[3].return_value = 'ariqbasyar'
args[4].return_value = formatted_message
args[5].return_value = fcmdevice_mock
args[4].return_value = formatted_title
args[5].return_value = formatted_message
args[6].return_value = fcmdevice_mock
ret = utils.send_komentar_notification(komentar)
......@@ -192,13 +173,15 @@ class TestUtilsNotification(BaseTestNotification):
args[3].assert_called_once_with(komentar)
args[4].assert_called_once_with('ariqbasyar')
args[5].assert_called_once_with(komentar)
args[6].assert_called_once_with(komentar)
fcmdevice_mock.send_message.assert_called_once_with(
title=formatted_message,
body='"%s"' % komentar.deskripsi,
title=formatted_title,
body=formatted_message,
data={
'place_id': 'dummy place_id',
'id': 1,
'type': 'fasilitas',
'title': formatted_title,
'message': formatted_message,
}
)
......@@ -6,7 +6,7 @@ from django.contrib.auth.models import User
from fcm_django.models import FCMDevice, AbstractFCMDevice
from fcm_django.api.rest_framework import FCMDeviceSerializer
from informasi_fasilitas.models import Lokasi, Kegiatan, Fasilitas, Komentar, KomentarKegiatan
from informasi_fasilitas.models import Lokasi, Kegiatan, Fasilitas, KomentarFasilitas, KomentarKegiatan
from pplbackend.utils import get_client_login_with_user, response_decode
from .test_base import BaseTestNotification
......@@ -178,7 +178,7 @@ class TestViewsNotification(BaseTestNotification):
'deskripsi': 'dummy deskripsi',
})
data = response_decode(resp)
komentar = Komentar.objects.get(id=data['id'])
komentar = KomentarFasilitas.objects.get(id=data['id'])
mock_notif.assert_called_once_with(komentar)
......
from fcm_django.models import FCMDevice
from informasi_fasilitas.models import Komentar
from informasi_fasilitas.models import Komentar, KomentarFasilitas, KomentarKegiatan
def get_type_informasi_from_komentar(komentar):
if isinstance(komentar, Komentar):
if isinstance(komentar, KomentarFasilitas):
return 'fasilitas'
return 'kegiatan'
def get_informasi(komentar):
if isinstance(komentar, Komentar):
if isinstance(komentar, KomentarFasilitas):
return komentar.fasilitas
return komentar.kegiatan
......@@ -26,30 +26,30 @@ def get_target_fcm_device(komentar):
.filter(user=get_informasi(komentar).user, active=True)
.exclude(user=komentar.user))
def get_formatted_user_name(name, max_length_name=10, replacement_str='...'):
length_replacement = len(replacement_str)
return name if len(name) <= max_length_name else '%s%s' % (
name[:max_length_name - length_replacement], replacement_str
)
def get_formatted_title(name):
return '%s menambahkan komentar baru' % name
def get_formatted_message(sender, **kwargs):
return '%s menambahkan komentar baru' % get_formatted_user_name(sender, **kwargs)
def get_formatted_message(komentar):
return '"%s"' % komentar.deskripsi
def send_komentar_notification(komentar):
place_id = get_place_id_from_komentar(komentar)
informasi_id = get_informasi_id_from_komentar(komentar)
informasi_type = get_type_informasi_from_komentar(komentar)
sender = get_sender_last_name_from_komentar(komentar)
formatted_message = get_formatted_message(sender)
target_fcm_device = get_target_fcm_device(komentar)
return target_fcm_device.send_message(
title=formatted_message,
body='"%s"' % komentar.deskripsi,
data={
'place_id': place_id,
'id': informasi_id,
'type': informasi_type,
'message': formatted_message,
}
)
if target_fcm_device:
place_id = get_place_id_from_komentar(komentar)
informasi_id = get_informasi_id_from_komentar(komentar)
informasi_type = get_type_informasi_from_komentar(komentar)
sender = get_sender_last_name_from_komentar(komentar)
formatted_title = get_formatted_title(sender)
formatted_message = get_formatted_message(komentar)
return target_fcm_device.send_message(
title=formatted_title,
body=formatted_message,
data={
'place_id': place_id,
'id': informasi_id,
'type': informasi_type,
'title': formatted_title,
'message': formatted_message,
}
)
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