Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lego/apps/lending/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
"LENDING_CHANGES_RESOLVED": {"value": "changes_resolved"},
}

OUTDOORS = "outdoors"
PHOTOGRAPHY = "photography"
INSTRUMENT = "instrument"
SPEAKER = "speaker"
FURNITURE = "furniture"
OTHER = "other"

LENDING_CATEGORIES = (
(OUTDOORS, OUTDOORS),
(PHOTOGRAPHY, PHOTOGRAPHY),
(INSTRUMENT, INSTRUMENT),
(SPEAKER, SPEAKER),
(FURNITURE, FURNITURE),
(OTHER, OTHER),
)

LENDING_REQUEST_DEFAULT = LENDING_REQUEST_STATUSES["LENDING_UNAPPROVED"]["value"]

Expand Down
29 changes: 29 additions & 0 deletions lego/apps/lending/migrations/0008_lendableobject_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.16 on 2025-10-11 17:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("lending", "0007_remove_lendingrequest_text"),
]

operations = [
migrations.AddField(
model_name="lendableobject",
name="category",
field=models.CharField(
choices=[
("outdoors", "outdoors"),
("photography", "photography"),
("instrument", "instrument"),
("speaker", "speaker"),
("furniture", "furniture"),
("other", "other"),
],
default="other",
max_length=64,
),
),
]
9 changes: 9 additions & 0 deletions lego/apps/lending/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from lego.apps.files.models import FileField
from lego.apps.lending.constants import (
LENDING_CATEGORIES,
LENDING_CHOICE_STATUSES,
LENDING_REQUEST_STATUSES,
OTHER,
)
from lego.apps.lending.permissions import (
LendableObjectPermissionHandler,
Expand All @@ -20,6 +22,13 @@ class LendableObject(BasisModel, ObjectPermissionsModel):
description = models.TextField(null=False, blank=True)
image = FileField(related_name="lendable_object_image")
location = models.CharField(max_length=128, null=False, blank=True)
category = models.CharField(
max_length=64,
choices=LENDING_CATEGORIES,
default=OTHER,
null=False,
blank=False,
)

class Meta:
permission_handler = LendableObjectPermissionHandler()
Expand Down
1 change: 1 addition & 0 deletions lego/apps/lending/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Meta:
"responsible_groups",
"location",
"can_lend",
"category",
)

def get_can_lend(self, obj):
Expand Down
32 changes: 31 additions & 1 deletion lego/apps/lending/tests/test_lendableobject_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils import timezone
from rest_framework import status

from lego.apps.lending.constants import LENDING_REQUEST_STATUSES
from lego.apps.lending.constants import LENDING_REQUEST_STATUSES, OUTDOORS, PHOTOGRAPHY
from lego.apps.lending.models import LendableObject, LendingRequest
from lego.apps.lending.serializers import (
LendableObjectAdminSerializer,
Expand Down Expand Up @@ -117,6 +117,19 @@ def test_fields(self):
len(LendableObjectSerializer.Meta.fields), len(lendable_object.keys())
)

def test_category_field_in_response(self):
self.permission_group.add_user(self.user)
self.client.force_authenticate(user=self.user)

lendable_object = create_lendable_object(category=PHOTOGRAPHY)
lendable_object.can_view_groups.add(self.permission_group)

response = self.client.get(get_list_url())
self.assertEqual(response.status_code, status.HTTP_200_OK)
first_obj = response.json()["results"][1]
self.assertIn("category", first_obj)
self.assertEqual(first_obj["category"], PHOTOGRAPHY)


class RetrieveLendableObjectTestCase(BaseAPITestCase):
def setUp(self):
Expand Down Expand Up @@ -274,6 +287,23 @@ def test_authenticated_with_object_create_permissions(self):
lendable_object = LendableObject.objects.get(title="New title")
self.assertEqual(lendable_object.title, "New title")

def test_create_lendable_object_with_category(self):
self.group.permissions = ["/sudo/admin/lendableobjects/create/"]
self.group.save()
self.client.force_authenticate(user=self.user)

data = {
"title": "Turgrill",
"description": "Grill for utendørsbruk",
"category": OUTDOORS,
}

response = self.client.post(get_list_url(), data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

lendable_object = LendableObject.objects.get(title="Turgrill")
self.assertEqual(lendable_object.category, OUTDOORS)


class LendableObjectAvailabilityTestCase(BaseAPITestCase):
def setUp(self):
Expand Down