add virtual item field 'returned' in item
This commit is contained in:
parent
83565d4b2a
commit
2e29b8b046
3 changed files with 62 additions and 15 deletions
|
@ -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'])
|
||||
if 'returned' in data:
|
||||
returned = data['returned']
|
||||
internal = super().to_internal_value(data)
|
||||
if container:
|
||||
internal['container'] = container
|
||||
if returned:
|
||||
internal['returned_at'] = datetime.now()
|
||||
return internal
|
||||
return super().to_internal_value(data)
|
||||
|
||||
def validate(self, attrs):
|
||||
return super().validate(attrs)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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')
|
||||
|
@ -81,7 +86,8 @@ class ItemTestCase(TestCase):
|
|||
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')
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue