Fakultas Ilmu Komputer UI

Commit 02ff9851 authored by Muhammad Azhar Hassanuddin's avatar Muhammad Azhar Hassanuddin
Browse files

Merge branch 'PBI-7-Identifier_toko' into 'staging'

Pbi 7 identifier toko

See merge request ppl-fasilkom-ui/ppl-reguler-ganjil-2021-2021/walkiddie-toys/walkiddie-toys-backend!36
parents ba048584 72851ff9
# Generated by Django 3.1.7 on 2021-11-30 10:18
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('toko', '0023_mainanpengadaan_status'),
]
operations = [
migrations.AddField(
model_name='toko',
name='nomor_toko',
field=models.IntegerField(default=1),
preserve_default=False,
),
]
......@@ -47,9 +47,22 @@ class Toko(models.Model):
foto_profil_toko = models.ImageField(upload_to='toko/profil')
nomor_toko = models.IntegerField(default = 1)
def get_full_name(self):
return "{toko} - {cabang}".format(toko=self.nama_toko, cabang=self.nama_cabang)
def save(self, *args, **kwargs):
toko_with_same_name = Toko.objects.filter(nama_toko = self.nama_toko)\
.exclude(id = self.id)\
.order_by('-nomor_toko')
try:
last_added_toko = toko_with_same_name[0]
self.nomor_toko = last_added_toko.nomor_toko + 1
except IndexError:
self.nomor_toko = 1
super().save(*args, **kwargs)
class Meta:
constraints = [
models.UniqueConstraint(fields=["nama_toko", "nama_cabang"], name="unique_toko_cabang")
......
......@@ -7,6 +7,8 @@ from .models import Toko, Pengadaan, MainanPengadaan
class TokoSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.email')
foto_profil_toko = serializers.ImageField(read_only=True)
nomor_toko = serializers.IntegerField(read_only=True)
identifier_toko = serializers.SerializerMethodField('get_identifier')
class Meta:
model = Toko
fields = [
......@@ -14,6 +16,8 @@ class TokoSerializer(serializers.ModelSerializer):
'owner',
'nama_toko',
'nama_cabang',
'nomor_toko',
'identifier_toko',
'status',
'tipe_usaha',
'nomor_telepon',
......@@ -25,6 +29,10 @@ class TokoSerializer(serializers.ModelSerializer):
'foto_profil_toko'
]
@staticmethod
def get_identifier(obj):
return "{}_{:0>3}".format(obj.nama_toko, obj.nomor_toko)
class PengadaanReadSerializer(serializers.ModelSerializer):
toko = TokoSerializer(read_only=True)
......
......@@ -171,6 +171,116 @@ class TokoTest(TokoBaseTestCase):
self.assertEqual(response.status_code, 400)
def test_post_toko_increment_nomor_toko_jika_nama_sama_dengan_toko_yang_sudah_ada(self):
token = self._get_token()
response = self.client.post(
'/api/toko/',
{**self.toko_data, 'fotoProfilToko': self.dummy_image},
HTTP_AUTHORIZATION="JWT {}".format(token)
)
self.assertEqual(response.json()['nomorToko'], self.toko.nomor_toko + 1)
def test_update_toko_dengan_nama_yang_belum_pernah_ada_dapat_nomor_toko_1(self):
toko2 = Toko.objects.create(
daerah="Pekalongan",
deskripsi_toko='Desk',
latitude=12.001212,
lokasi_toko="Sebelah rumah masyarakat berbahagia",
longitude=13.0,
nama_cabang="Whaoo",
nama_toko="Dirgan",
nomor_telepon="0824665341",
owner=self.user,
tipe_usaha="Kelontong",
foto_profil_toko=self.dummy_image
)
self.assertEqual(toko2.nomor_toko, 2)
token = self._get_token()
response = self.client.put(
'/api/toko/{}'.format(toko2.pk),
{**self.toko_data, 'nama_toko': 'water'},
content_type="application/json",
HTTP_AUTHORIZATION="JWT {}".format(token))
self.assertEqual(response.json()['nomorToko'], 1)
def test_update_nama_toko_menjadi_nama_toko_yang_sudah_digunakan_increment_nomor_toko(self):
toko2 = Toko.objects.create(
daerah="Pekalongan",
deskripsi_toko='Desk',
latitude=12.001212,
lokasi_toko="Sebelah rumah masyarakat berbahagia",
longitude=13.0,
nama_cabang="Whaoo",
nama_toko="Water",
nomor_telepon="0824665341",
owner=self.user,
tipe_usaha="Kelontong",
foto_profil_toko=self.dummy_image
)
self.assertEqual(toko2.nomor_toko, 1)
token = self._get_token()
response = self.client.put(
'/api/toko/{}'.format(toko2.pk),
{**self.toko_data, 'nama_toko': 'Dirgan'},
content_type="application/json",
HTTP_AUTHORIZATION="JWT {}".format(token))
self.assertEqual(response.json()['nomorToko'], 2)
@staticmethod
def get_string_identifier(toko):
return "{}_{:0>3}".format(toko.nama_toko, toko.nomor_toko)
def test_get_toko_mengembalikan_string_identifier_toko(self):
toko2 = Toko.objects.create(
daerah="Pekalongan",
deskripsi_toko='Desk',
latitude=12.001212,
lokasi_toko="Sebelah rumah masyarakat berbahagia",
longitude=13.0,
nama_cabang="2",
nama_toko="Dirgan",
nomor_telepon="0824665341",
owner=self.user,
tipe_usaha="Kelontong",
foto_profil_toko=self.dummy_image
)
token = self._get_token()
response = self.client.get(
'/api/toko/{}'.format(toko2.pk),
HTTP_AUTHORIZATION='JWT {}'.format(token)
)
self.assertEqual(self.get_string_identifier(toko2), response.json()['identifierToko'])
def test_saat_update_toko_tidak_increment_nomor_toko_terhadap_diri_sendiri(self):
toko2 = Toko.objects.create(
daerah="Pekalongan",
deskripsi_toko='Desk',
latitude=12.001212,
lokasi_toko="Sebelah rumah masyarakat berbahagia",
longitude=13.0,
nama_cabang="2",
nama_toko="Dirgan",
nomor_telepon="0824665341",
owner=self.user,
tipe_usaha="Kelontong",
foto_profil_toko=self.dummy_image
)
toko2_nomor = toko2.nomor_toko
token = self._get_token()
response = self.client.put(
'/api/toko/{}'.format(toko2.pk),
{**self.toko_data, 'nama_cabang': '3'},
content_type="application/json",
HTTP_AUTHORIZATION="JWT {}".format(token))
self.assertEqual(response.json()['nomorToko'], toko2_nomor)
class PengadaanTest(TokoBaseTestCase):
def setUp(self):
super().setUp()
......
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