From b6b29d9fedd1051f4e291f867c1b9eedad175ef4 Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 23 Nov 2024 14:40:13 +0100 Subject: [PATCH] update admin interface --- core/inventory/admin.py | 16 ++- core/inventory/models.py | 2 +- core/inventory/serializers.py | 8 ++ core/inventory/tests/v2/test_items.py | 176 ++++++++++++++------------ 4 files changed, 121 insertions(+), 81 deletions(-) diff --git a/core/inventory/admin.py b/core/inventory/admin.py index 2457b5b..24cd14a 100644 --- a/core/inventory/admin.py +++ b/core/inventory/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from inventory.models import Item, Container, Event +from inventory.models import Item, Container, ItemPlacement, Comment, Event class ItemAdmin(admin.ModelAdmin): @@ -22,3 +22,17 @@ class EventAdmin(admin.ModelAdmin): admin.site.register(Event, EventAdmin) + + +class ItemPlacementAdmin(admin.ModelAdmin): + pass + + +admin.site.register(ItemPlacement, ItemPlacementAdmin) + + +class CommentAdmin(admin.ModelAdmin): + pass + + +admin.site.register(Comment, CommentAdmin) diff --git a/core/inventory/models.py b/core/inventory/models.py index fbbe0a9..10796e1 100644 --- a/core/inventory/models.py +++ b/core/inventory/models.py @@ -92,7 +92,7 @@ class Comment(models.Model): timestamp = models.DateTimeField(auto_now_add=True) def __str__(self): - return str(self.issue_thread) + ' comment #' + str(self.id) + return str(self.item) + ' comment #' + str(self.id) class Event(models.Model): diff --git a/core/inventory/serializers.py b/core/inventory/serializers.py index 210ac0e..63f479b 100644 --- a/core/inventory/serializers.py +++ b/core/inventory/serializers.py @@ -106,6 +106,14 @@ class ItemSerializer(BasicItemSerializer): 'timestamp': relation.timestamp, 'issue_thread': BasicIssueSerializer(relation.issue_thread).data, }) + for placement in (obj.container_history.all()): + timeline.append({ + 'type': 'placement', + 'id': placement.id, + 'timestamp': placement.timestamp, + 'cid': placement.container.id, + 'box': placement.container.name + }) return sorted(timeline, key=lambda x: x['timestamp']) diff --git a/core/inventory/tests/v2/test_items.py b/core/inventory/tests/v2/test_items.py index 8a38b86..0c85eb4 100644 --- a/core/inventory/tests/v2/test_items.py +++ b/core/inventory/tests/v2/test_items.py @@ -7,7 +7,7 @@ from knox.models import AuthToken from authentication.models import ExtendedUser from files.models import File -from inventory.models import Event, Container, Item, Comment +from inventory.models import Event, Container, Item, Comment, ItemPlacement from base64 import b64encode @@ -19,7 +19,8 @@ class ItemTestCase(TestCase): def setUp(self): super().setUp() self.event = Event.objects.create(slug='EVENT', name='Event') - self.box = Container.objects.create(name='BOX') + self.box1 = Container.objects.create(name='BOX1') + self.box2 = Container.objects.create(name='BOX2') self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test') self.user.user_permissions.add(*Permission.objects.all()) self.token = AuthToken.objects.create(user=self.user) @@ -36,7 +37,7 @@ class ItemTestCase(TestCase): def test_members_and_timeline(self): now = datetime.now() - item = Item.objects.create(container=self.box, event=self.event, description='1') + item = Item.objects.create(container=self.box1, event=self.event, description='1') comment = Comment.objects.create( item=item, comment="test", @@ -44,7 +45,12 @@ class ItemTestCase(TestCase): ) match = ItemRelation.objects.create( issue_thread=self.issue, - item = item, + item=item, + timestamp=now + timedelta(seconds=4), + ) + placement = ItemPlacement.objects.create( + container=self.box2, + item=item, timestamp=now + timedelta(seconds=5), ) response = self.client.get(f'/api/2/{self.event.slug}/item/') @@ -52,25 +58,34 @@ class ItemTestCase(TestCase): self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0]['id'], item.id) self.assertEqual(response.json()[0]['description'], '1') - self.assertEqual(response.json()[0]['box'], 'BOX') - self.assertEqual(response.json()[0]['cid'], self.box.id) + self.assertEqual(response.json()[0]['box'], 'BOX2') + self.assertEqual(response.json()[0]['cid'], self.box2.id) self.assertEqual(response.json()[0]['file'], None) self.assertEqual(response.json()[0]['returned'], False) self.assertEqual(response.json()[0]['event'], self.event.slug) - self.assertEqual(len(response.json()[0]['timeline']), 2) - self.assertEqual(response.json()[0]['timeline'][0]['type'], 'comment') - self.assertEqual(response.json()[0]['timeline'][1]['type'], 'issue_relation') - self.assertEqual(response.json()[0]['timeline'][0]['id'], comment.id) - self.assertEqual(response.json()[0]['timeline'][1]['id'], match.id) - self.assertEqual(response.json()[0]['timeline'][0]['comment'], 'test') - self.assertEqual(response.json()[0]['timeline'][0]['timestamp'], - comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) - self.assertEqual(response.json()[0]['timeline'][1]['status'], 'possible') + self.assertEqual(len(response.json()[0]['timeline']), 4) + self.assertEqual(response.json()[0]['timeline'][0]['type'], 'placement') + self.assertEqual(response.json()[0]['timeline'][1]['type'], 'comment') + self.assertEqual(response.json()[0]['timeline'][2]['type'], 'issue_relation') + self.assertEqual(response.json()[0]['timeline'][3]['type'], 'placement') + self.assertEqual(response.json()[0]['timeline'][1]['id'], comment.id) + self.assertEqual(response.json()[0]['timeline'][2]['id'], match.id) + self.assertEqual(response.json()[0]['timeline'][3]['id'], placement.id) + self.assertEqual(response.json()[0]['timeline'][0]['box'], 'BOX1') + self.assertEqual(response.json()[0]['timeline'][0]['cid'], self.box1.id) + self.assertEqual(response.json()[0]['timeline'][1]['comment'], 'test') self.assertEqual(response.json()[0]['timeline'][1]['timestamp'], + comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) + self.assertEqual(response.json()[0]['timeline'][2]['status'], 'possible') + self.assertEqual(response.json()[0]['timeline'][2]['timestamp'], match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) - self.assertEqual(response.json()[0]['timeline'][1]['issue_thread']['name'], "test issue") - self.assertEqual(response.json()[0]['timeline'][1]['issue_thread']['event'], "EVENT") - self.assertEqual(response.json()[0]['timeline'][1]['issue_thread']['state'], "pending_new") + self.assertEqual(response.json()[0]['timeline'][2]['issue_thread']['name'], "test issue") + self.assertEqual(response.json()[0]['timeline'][2]['issue_thread']['event'], "EVENT") + self.assertEqual(response.json()[0]['timeline'][2]['issue_thread']['state'], "pending_new") + self.assertEqual(response.json()[0]['timeline'][3]['box'], 'BOX2') + self.assertEqual(response.json()[0]['timeline'][3]['cid'], self.box2.id) + self.assertEqual(response.json()[0]['timeline'][3]['timestamp'], + placement.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) self.assertEqual(len(response.json()[0]['related_issues']), 1) self.assertEqual(response.json()[0]['related_issues'][0]['name'], "test issue") self.assertEqual(response.json()[0]['related_issues'][0]['event'], "EVENT") @@ -78,15 +93,15 @@ class ItemTestCase(TestCase): def test_members_with_file(self): import base64 - item = Item.objects.create(container=self.box, event=self.event, description='1') + item = Item.objects.create(container=self.box1, event=self.event, description='1') file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')) 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]['id'], item.id) self.assertEqual(response.json()[0]['description'], '1') - self.assertEqual(response.json()[0]['box'], 'BOX') - self.assertEqual(response.json()[0]['cid'], self.box.id) + self.assertEqual(response.json()[0]['box'], 'BOX1') + self.assertEqual(response.json()[0]['cid'], self.box1.id) self.assertEqual(response.json()[0]['file'], file.hash) self.assertEqual(response.json()[0]['returned'], False) self.assertEqual(response.json()[0]['event'], self.event.slug) @@ -94,36 +109,38 @@ class ItemTestCase(TestCase): def test_members_with_two_file(self): import base64 - item = Item.objects.create(container=self.box, event=self.event, description='1') - file1 = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')) - file2 = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"bar").decode('utf-8')) + item = Item.objects.create(container=self.box1, event=self.event, description='1') + file1 = File.objects.create(item=item, + data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')) + file2 = File.objects.create(item=item, + data="data:text/plain;base64," + base64.b64encode(b"bar").decode('utf-8')) 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]['id'], item.id) self.assertEqual(response.json()[0]['description'], '1') - self.assertEqual(response.json()[0]['box'], 'BOX') - self.assertEqual(response.json()[0]['cid'], self.box.id) + self.assertEqual(response.json()[0]['box'], 'BOX1') + self.assertEqual(response.json()[0]['cid'], self.box1.id) self.assertEqual(response.json()[0]['file'], file2.hash) self.assertEqual(response.json()[0]['returned'], False) self.assertEqual(response.json()[0]['event'], self.event.slug) self.assertEqual(len(response.json()[0]['related_issues']), 0) def test_multi_members(self): - Item.objects.create(container=self.box, event=self.event, description='1') - Item.objects.create(container=self.box, event=self.event, description='2') - Item.objects.create(container=self.box, event=self.event, description='3') + Item.objects.create(container=self.box1, event=self.event, description='1') + Item.objects.create(container=self.box1, event=self.event, description='2') + Item.objects.create(container=self.box1, event=self.event, description='3') response = self.client.get(f'/api/2/{self.event.slug}/item/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 3) def test_create_item(self): - response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box.id, 'description': '1'}) + response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box1.id, 'description': '1'}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['id'], 1) self.assertEqual(response.json()['description'], '1') - self.assertEqual(response.json()['box'], 'BOX') - self.assertEqual(response.json()['cid'], self.box.id) + self.assertEqual(response.json()['box'], 'BOX1') + self.assertEqual(response.json()['cid'], self.box1.id) self.assertEqual(response.json()['file'], None) self.assertEqual(response.json()['returned'], False) self.assertEqual(response.json()['event'], self.event.slug) @@ -131,43 +148,43 @@ class ItemTestCase(TestCase): self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].id, 1) self.assertEqual(Item.objects.all()[0].description, '1') - self.assertEqual(Item.objects.all()[0].container.id, self.box.id) + self.assertEqual(Item.objects.all()[0].container.id, self.box1.id) def test_create_item_without_container(self): response = self.client.post(f'/api/2/{self.event.slug}/item/', {'description': '1'}) self.assertEqual(response.status_code, 400) def test_create_item_without_description(self): - response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box.id}) + response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box1.id}) self.assertEqual(response.status_code, 400) def test_create_item_with_file(self): import base64 response = self.client.post(f'/api/2/{self.event.slug}/item/', - {'cid': self.box.id, 'description': '1', + {'cid': self.box1.id, '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()['id'], 1) self.assertEqual(response.json()['description'], '1') - self.assertEqual(response.json()['box'], 'BOX') - self.assertEqual(response.json()['id'], self.box.id) + self.assertEqual(response.json()['box'], 'BOX1') + self.assertEqual(response.json()['id'], self.box1.id) self.assertEqual(len(response.json()['file']), 64) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].id, 1) self.assertEqual(Item.objects.all()[0].description, '1') - self.assertEqual(Item.objects.all()[0].container.id, self.box.id) + self.assertEqual(Item.objects.all()[0].container.id, self.box1.id) self.assertEqual(len(File.objects.all()), 1) def test_update_item(self): - item = Item.objects.create(container=self.box, event=self.event, description='1') + item = Item.objects.create(container=self.box1, event=self.event, description='1') response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/', {'description': '2'}, - content_type='application/json') + content_type='application/json') self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['id'], item.id) self.assertEqual(response.json()['description'], '2') - self.assertEqual(response.json()['box'], 'BOX') - self.assertEqual(response.json()['cid'], self.box.id) + self.assertEqual(response.json()['box'], 'BOX1') + self.assertEqual(response.json()['cid'], self.box1.id) self.assertEqual(response.json()['file'], None) self.assertEqual(response.json()['returned'], False) self.assertEqual(response.json()['event'], self.event.slug) @@ -175,60 +192,62 @@ class ItemTestCase(TestCase): self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].id, 1) self.assertEqual(Item.objects.all()[0].description, '2') - self.assertEqual(Item.objects.all()[0].container.id, self.box.id) + self.assertEqual(Item.objects.all()[0].container.id, self.box1.id) def test_update_item_with_file(self): import base64 - item = Item.objects.create(container=self.box, event=self.event, description='1') + item = Item.objects.create(container=self.box1, event=self.event, description='1') response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/', - {'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()['id'], 1) self.assertEqual(response.json()['description'], '2') - self.assertEqual(response.json()['box'], 'BOX') - self.assertEqual(response.json()['id'], self.box.id) + self.assertEqual(response.json()['box'], 'BOX1') + self.assertEqual(response.json()['id'], self.box1.id) self.assertEqual(len(response.json()['file']), 64) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].id, 1) self.assertEqual(Item.objects.all()[0].description, '2') - self.assertEqual(Item.objects.all()[0].container.id, self.box.id) + self.assertEqual(Item.objects.all()[0].container.id, self.box1.id) self.assertEqual(len(File.objects.all()), 1) def test_delete_item(self): - item = Item.objects.create(container=self.box, event=self.event, description='1') - Item.objects.create(container=self.box, event=self.event, description='2') + item = Item.objects.create(container=self.box1, event=self.event, description='1') + Item.objects.create(container=self.box1, event=self.event, description='2') self.assertEqual(len(Item.objects.all()), 2) response = self.client.delete(f'/api/2/{self.event.slug}/item/{item.id}/') self.assertEqual(response.status_code, 204) self.assertEqual(len(Item.objects.all()), 1) def test_delete_item2(self): - Item.objects.create(container=self.box, event=self.event, description='1') - item2 = Item.objects.create(container=self.box, event=self.event, description='2') + Item.objects.create(container=self.box1, event=self.event, description='1') + item2 = Item.objects.create(container=self.box1, event=self.event, description='2') self.assertEqual(len(Item.objects.all()), 2) response = self.client.delete(f'/api/2/{self.event.slug}/item/{item2.id}/') self.assertEqual(response.status_code, 204) self.assertEqual(len(Item.objects.all()), 1) - item3 = Item.objects.create(container=self.box, event=self.event, description='3') + item3 = Item.objects.create(container=self.box1, event=self.event, description='3') self.assertEqual(item3.id, 3) self.assertEqual(len(Item.objects.all()), 2) def test_item_count(self): - Item.objects.create(container=self.box, event=self.event, description='1') - Item.objects.create(container=self.box, event=self.event, description='2') + Item.objects.create(container=self.box1, event=self.event, description='1') + Item.objects.create(container=self.box1, event=self.event, description='2') response = self.client.get('/api/2/boxes/') self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.json()), 1) + self.assertEqual(len(response.json()), 2) self.assertEqual(response.json()[0]['itemCount'], 2) + self.assertEqual(response.json()[1]['itemCount'], 0) 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') + item = Item.objects.create(container=self.box1, 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) @@ -243,8 +262,8 @@ class ItemTestCase(TestCase): 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') + item1 = Item.objects.create(container=self.box1, event=self.event, description='1') + item2 = Item.objects.create(container=self.box1, 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) @@ -261,15 +280,15 @@ class ItemSearchTestCase(TestCase): def setUp(self): super().setUp() self.event = Event.objects.create(slug='EVENT', name='Event') - self.box = Container.objects.create(name='BOX') + self.box1 = Container.objects.create(name='BOX1') self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test') self.user.user_permissions.add(*Permission.objects.all()) self.token = AuthToken.objects.create(user=self.user) self.client = Client(headers={'Authorization': 'Token ' + self.token[1]}) - self.item1 = Item.objects.create(container=self.box, event=self.event, description='abc def') - self.item2 = Item.objects.create(container=self.box, event=self.event, description='def ghi') - self.item3 = Item.objects.create(container=self.box, event=self.event, description='jkl mno pqr') - self.item4 = Item.objects.create(container=self.box, event=self.event, description='stu vwx') + self.item1 = Item.objects.create(container=self.box1, event=self.event, description='abc def') + self.item2 = Item.objects.create(container=self.box1, event=self.event, description='def ghi') + self.item3 = Item.objects.create(container=self.box1, event=self.event, description='jkl mno pqr') + self.item4 = Item.objects.create(container=self.box1, event=self.event, description='stu vwx') def test_search(self): search_query = b64encode(b'abc').decode('utf-8') @@ -278,8 +297,8 @@ class ItemSearchTestCase(TestCase): self.assertEqual(1, len(response.json())) self.assertEqual(self.item1.id, response.json()[0]['id']) self.assertEqual('abc def', response.json()[0]['description']) - self.assertEqual('BOX', response.json()[0]['box']) - self.assertEqual(self.box.id, response.json()[0]['cid']) + self.assertEqual('BOX1', response.json()[0]['box']) + self.assertEqual(self.box1.id, response.json()[0]['cid']) self.assertEqual(1, response.json()[0]['search_score']) def test_search2(self): @@ -289,13 +308,13 @@ class ItemSearchTestCase(TestCase): self.assertEqual(2, len(response.json())) self.assertEqual(self.item1.id, response.json()[0]['id']) self.assertEqual('abc def', response.json()[0]['description']) - self.assertEqual('BOX', response.json()[0]['box']) - self.assertEqual(self.box.id, response.json()[0]['cid']) + self.assertEqual('BOX1', response.json()[0]['box']) + self.assertEqual(self.box1.id, response.json()[0]['cid']) self.assertEqual(1, response.json()[0]['search_score']) self.assertEqual(self.item2.id, response.json()[1]['id']) self.assertEqual('def ghi', response.json()[1]['description']) - self.assertEqual('BOX', response.json()[1]['box']) - self.assertEqual(self.box.id, response.json()[1]['cid']) + self.assertEqual('BOX1', response.json()[1]['box']) + self.assertEqual(self.box1.id, response.json()[1]['cid']) self.assertEqual(1, response.json()[0]['search_score']) def test_search3(self): @@ -305,8 +324,8 @@ class ItemSearchTestCase(TestCase): self.assertEqual(1, len(response.json())) self.assertEqual(self.item3.id, response.json()[0]['id']) self.assertEqual('jkl mno pqr', response.json()[0]['description']) - self.assertEqual('BOX', response.json()[0]['box']) - self.assertEqual(self.box.id, response.json()[0]['cid']) + self.assertEqual('BOX1', response.json()[0]['box']) + self.assertEqual(self.box1.id, response.json()[0]['cid']) self.assertEqual(1, response.json()[0]['search_score']) def test_search4(self): @@ -316,12 +335,11 @@ class ItemSearchTestCase(TestCase): self.assertEqual(2, len(response.json())) self.assertEqual(self.item1.id, response.json()[0]['id']) self.assertEqual('abc def', response.json()[0]['description']) - self.assertEqual('BOX', response.json()[0]['box']) - self.assertEqual(self.box.id, response.json()[0]['cid']) + self.assertEqual('BOX1', response.json()[0]['box']) + self.assertEqual(self.box1.id, response.json()[0]['cid']) self.assertEqual(2, response.json()[0]['search_score']) self.assertEqual(self.item2.id, response.json()[1]['id']) self.assertEqual('def ghi', response.json()[1]['description']) - self.assertEqual('BOX', response.json()[1]['box']) - self.assertEqual(self.box.id, response.json()[1]['cid']) + self.assertEqual('BOX1', response.json()[1]['box']) + self.assertEqual(self.box1.id, response.json()[1]['cid']) self.assertEqual(1, response.json()[1]['search_score']) -