Add item size property to disallow "add" action if container is full
This commit is contained in:
parent
02f67255dd
commit
28887f9fc0
3 changed files with 33 additions and 8 deletions
inventory
18
inventory/migrations/0003_item_size.py
Normal file
18
inventory/migrations/0003_item_size.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.1.4 on 2020-12-22 00:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('inventory', '0002_base_data'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='item',
|
||||
name='size',
|
||||
field=models.PositiveIntegerField(default=1),
|
||||
),
|
||||
]
|
|
@ -7,6 +7,7 @@ from .container import CanBeContained
|
|||
class Item(CanBeContained):
|
||||
name = models.TextField(max_length=255)
|
||||
description = models.CharField(max_length=4096)
|
||||
size = models.PositiveIntegerField(default=1, help_text="Number of sub-compartments this item takes up")
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
changed_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from typing import cast, Union
|
||||
from typing import cast, Union, List, Any
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.generic import ListView, DetailView
|
||||
|
@ -30,16 +30,22 @@ class BoxView(DetailView):
|
|||
idx = new_idx
|
||||
else:
|
||||
instances = obj.filter(index=idx)
|
||||
if instances.count() == 1:
|
||||
result.append(instances.first())
|
||||
elif instances.count() > 1:
|
||||
sub = list(instances)
|
||||
result.append(sub)
|
||||
else:
|
||||
result.append({
|
||||
items: List[dict[str, Any]] = []
|
||||
|
||||
# Add all items to the layout container
|
||||
if instances.count() > 0:
|
||||
items.extend(list(instances))
|
||||
|
||||
# Append "Add one more entry" if we do not exceed the maximum number
|
||||
# of items yet
|
||||
size = sum([item.size for item in items])
|
||||
if size < sublayout:
|
||||
items.append({
|
||||
"index": idx,
|
||||
"container_id": self.object.pk
|
||||
})
|
||||
|
||||
result.append(items)
|
||||
idx += 1
|
||||
return result, idx
|
||||
|
||||
|
|
Loading…
Reference in a new issue