From 2e29b8b04616dd2bfdf9f13ca2af257d4a9d0906 Mon Sep 17 00:00:00 2001 From: jedi Date: Sun, 7 Jan 2024 22:11:13 +0100 Subject: [PATCH] add virtual item field 'returned' in item --- core/inventory/api_v2.py | 18 +++++++-- core/inventory/models.py | 3 ++ core/inventory/tests/v2/test_items.py | 56 +++++++++++++++++++++------ 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/core/inventory/api_v2.py b/core/inventory/api_v2.py index f7306da..5ee00ee 100644 --- a/core/inventory/api_v2.py +++ b/core/inventory/api_v2.py @@ -46,10 +46,11 @@ class ItemSerializer(serializers.ModelSerializer): cid = serializers.SerializerMethodField() box = serializers.SerializerMethodField() file = serializers.SerializerMethodField() + returned = serializers.SerializerMethodField(required=False) class Meta: model = Item - fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage'] + fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage', 'returned'] read_only_fields = ['uid'] def get_cid(self, instance): @@ -63,13 +64,22 @@ class ItemSerializer(serializers.ModelSerializer): return instance.files.all().order_by('-created_at')[0].hash return None + def get_returned(self, instance): + return instance.returned_at is not None + def to_internal_value(self, data): + container = None + returned = False if 'cid' in data: container = Container.objects.get(cid=data['cid']) - internal = super().to_internal_value(data) + if 'returned' in data: + returned = data['returned'] + internal = super().to_internal_value(data) + if container: internal['container'] = container - return internal - return super().to_internal_value(data) + if returned: + internal['returned_at'] = datetime.now() + return internal def validate(self, attrs): return super().validate(attrs) diff --git a/core/inventory/models.py b/core/inventory/models.py index c3a3961..73f4582 100644 --- a/core/inventory/models.py +++ b/core/inventory/models.py @@ -13,6 +13,9 @@ class ItemManager(SoftDeleteManager): kwargs['uid'] = uid return super().create(**kwargs) + def get_queryset(self): + return super().get_queryset().filter(returned_at__isnull=True) + class Item(SoftDeleteModel): iid = models.AutoField(primary_key=True) diff --git a/core/inventory/tests/v2/test_items.py b/core/inventory/tests/v2/test_items.py index 8e2adba..ef61333 100644 --- a/core/inventory/tests/v2/test_items.py +++ b/core/inventory/tests/v2/test_items.py @@ -1,3 +1,5 @@ +from datetime import datetime + from django.test import TestCase, Client from django.contrib.auth.models import Permission from knox.models import AuthToken @@ -28,7 +30,8 @@ class ItemTestCase(TestCase): response = self.client.get(f'/api/2/{self.event.slug}/item/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), - [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None}]) + [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None, + 'returned': False}]) def test_members_with_file(self): import base64 @@ -37,7 +40,8 @@ class ItemTestCase(TestCase): response = self.client.get(f'/api/2/{self.event.slug}/item/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), - [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash}]) + [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash, + 'returned': False}]) def test_multi_members(self): Item.objects.create(container=self.box, event=self.event, description='1') @@ -51,7 +55,8 @@ class ItemTestCase(TestCase): response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box.cid, 'description': '1'}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), - {'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None}) + {'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None, + 'returned': False}) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].uid, 1) self.assertEqual(Item.objects.all()[0].description, '1') @@ -60,9 +65,9 @@ class ItemTestCase(TestCase): def test_create_item_with_file(self): import base64 response = self.client.post(f'/api/2/{self.event.slug}/item/', - {'cid': self.box.cid, 'description': '1', - 'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode( - 'utf-8')}, content_type='application/json') + {'cid': self.box.cid, 'description': '1', + 'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode( + 'utf-8')}, content_type='application/json') self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['uid'], 1) self.assertEqual(response.json()['description'], '1') @@ -78,10 +83,11 @@ class ItemTestCase(TestCase): def test_update_item(self): item = Item.objects.create(container=self.box, event=self.event, description='1') response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/', {'description': '2'}, - content_type='application/json') + content_type='application/json') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), - {'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None}) + {'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None, + 'returned': False}) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].uid, 1) self.assertEqual(Item.objects.all()[0].description, '2') @@ -91,9 +97,9 @@ class ItemTestCase(TestCase): import base64 item = Item.objects.create(container=self.box, event=self.event, description='1') response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/', - {'description': '2', - 'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')}, - content_type='application/json') + {'description': '2', + 'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')}, + content_type='application/json') self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['uid'], 1) self.assertEqual(response.json()['description'], '2') @@ -136,3 +142,31 @@ class ItemTestCase(TestCase): def test_item_nonexistent(self): response = self.client.get(f'/api/2/NOEVENT/item/') self.assertEqual(response.status_code, 404) + + def test_item_return(self): + item = Item.objects.create(container=self.box, event=self.event, description='1') + self.assertEqual(item.returned_at, None) + response = self.client.get(f'/api/2/{self.event.slug}/item/') + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.json()), 1) + response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.uid}/', {'returned': True}, + content_type='application/json') + self.assertEqual(response.status_code, 200) + item.refresh_from_db() + self.assertNotEqual(item.returned_at, None) + response = self.client.get(f'/api/2/{self.event.slug}/item/') + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.json()), 0) + + def test_item_show_not_returned(self): + item1 = Item.objects.create(container=self.box, event=self.event, description='1') + item2 = Item.objects.create(container=self.box, event=self.event, description='2') + response = self.client.get(f'/api/2/{self.event.slug}/item/') + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.json()), 2) + item2.returned_at = datetime.now() + item2.save() + response = self.client.get(f'/api/2/{self.event.slug}/item/') + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.json()), 1) + self.assertEqual(response.json()[0]['uid'], item1.uid)