From f4894d3a8c8bea946c769a99832b5a366f452d61 Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 17 Feb 2024 19:32:19 +0100 Subject: [PATCH] hide private items from friends in search --- backend/toolshed/api/inventory.py | 3 ++- ..._alter_inventoryitem_availability_policy.py | 18 ++++++++++++++++++ backend/toolshed/models.py | 10 +++++++++- backend/toolshed/tests/test_inventory.py | 18 +++++++++--------- 4 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 backend/toolshed/migrations/0005_alter_inventoryitem_availability_policy.py diff --git a/backend/toolshed/api/inventory.py b/backend/toolshed/api/inventory.py index 1d7db7f..c39a5c8 100644 --- a/backend/toolshed/api/inventory.py +++ b/backend/toolshed/api/inventory.py @@ -24,7 +24,8 @@ def inventory_items(identity): for friend in identity.friends.all(): if friend_user := friend.user.get(): for item in friend_user.inventory_items.all(): - yield item + if item.availability_policy != 'private': + yield item class InventoryItemViewSet(viewsets.ModelViewSet): diff --git a/backend/toolshed/migrations/0005_alter_inventoryitem_availability_policy.py b/backend/toolshed/migrations/0005_alter_inventoryitem_availability_policy.py new file mode 100644 index 0000000..fa86880 --- /dev/null +++ b/backend/toolshed/migrations/0005_alter_inventoryitem_availability_policy.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2024-02-20 15:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('toolshed', '0004_storagelocation_inventoryitem_storage_location'), + ] + + operations = [ + migrations.AlterField( + model_name='inventoryitem', + name='availability_policy', + field=models.CharField(choices=[('sell', 'Sell'), ('rent', 'Rent'), ('lend', 'Lend'), ('share', 'Share'), ('private', 'Private')], default='private', max_length=20), + ), + ] diff --git a/backend/toolshed/models.py b/backend/toolshed/models.py index 14ee76b..de32826 100644 --- a/backend/toolshed/models.py +++ b/backend/toolshed/models.py @@ -50,12 +50,20 @@ class Tag(models.Model): class InventoryItem(SoftDeleteModel): + AVAILABILITY_POLICY_CHOICES = ( + ('sell', 'Sell'), + ('rent', 'Rent'), + ('lend', 'Lend'), + ('share', 'Share'), + ('private', 'Private'), + ) + published = models.BooleanField(default=False) name = models.CharField(max_length=255, null=True, blank=True) description = models.TextField(null=True, blank=True) category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True, related_name='inventory_items') - availability_policy = models.CharField(max_length=255, default="private") + availability_policy = models.CharField(max_length=20, choices=AVAILABILITY_POLICY_CHOICES, default='private') owned_quantity = models.IntegerField(default=1, validators=[MinValueValidator(0)]) owner = models.ForeignKey(ToolshedUser, on_delete=models.CASCADE, related_name='inventory_items') created_at = models.DateTimeField(auto_now_add=True) diff --git a/backend/toolshed/tests/test_inventory.py b/backend/toolshed/tests/test_inventory.py index 747c19e..a05421c 100644 --- a/backend/toolshed/tests/test_inventory.py +++ b/backend/toolshed/tests/test_inventory.py @@ -38,7 +38,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): def test_post_new_item(self): reply = client.post('/api/inventory_items/', self.f['local_user1'], { - 'availability_policy': 'friends', + 'availability_policy': 'rent', 'category': 'cat2', 'name': 'test3', 'description': 'test', @@ -50,7 +50,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(reply.status_code, 201) self.assertEqual(InventoryItem.objects.count(), 3) item = InventoryItem.objects.get(name='test3') - self.assertEqual(item.availability_policy, 'friends') + self.assertEqual(item.availability_policy, 'rent') self.assertEqual(item.category, Category.objects.get(name='cat2')) self.assertEqual(item.name, 'test3') self.assertEqual(item.description, 'test') @@ -61,7 +61,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): def test_post_new_item2(self): reply = client.post('/api/inventory_items/', self.f['local_user1'], { - 'availability_policy': 'friends', + 'availability_policy': 'share', 'name': 'test3', 'description': 'test', 'owned_quantity': 1, @@ -70,7 +70,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(reply.status_code, 201) self.assertEqual(InventoryItem.objects.count(), 3) item = InventoryItem.objects.get(name='test3') - self.assertEqual(item.availability_policy, 'friends') + self.assertEqual(item.availability_policy, 'share') self.assertEqual(item.category, None) self.assertEqual(item.name, 'test3') self.assertEqual(item.description, 'test') @@ -80,7 +80,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): def test_post_new_item_empty(self): reply = client.post('/api/inventory_items/', self.f['local_user1'], { - 'availability_policy': 'friends', + 'availability_policy': 'rent', 'owned_quantity': 1, 'image': '', }) @@ -89,7 +89,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): def test_post_new_item3(self): reply = client.post('/api/inventory_items/', self.f['local_user1'], { - 'availability_policy': 'friends', + 'availability_policy': 'private', 'name': 'test3', 'description': 'test', 'owned_quantity': 1, @@ -99,7 +99,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(reply.status_code, 201) self.assertEqual(InventoryItem.objects.count(), 3) item = InventoryItem.objects.get(name='test3') - self.assertEqual(item.availability_policy, 'friends') + self.assertEqual(item.availability_policy, 'private') self.assertEqual(item.category, None) self.assertEqual(item.name, 'test3') self.assertEqual(item.description, 'test') @@ -109,7 +109,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): def test_put_item(self): reply = client.put('/api/inventory_items/1/', self.f['local_user1'], { - 'availability_policy': 'friends', + 'availability_policy': 'sell', 'name': 'test4', 'description': 'new description', 'owned_quantity': 100, @@ -121,7 +121,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(reply.status_code, 200) self.assertEqual(InventoryItem.objects.count(), 2) item = InventoryItem.objects.get(id=1) - self.assertEqual(item.availability_policy, 'friends') + self.assertEqual(item.availability_policy, 'sell') self.assertEqual(item.category, None) self.assertEqual(item.name, 'test4') self.assertEqual(item.description, 'new description')