added locations to pantry items

This commit is contained in:
Jens Timmerman 2018-09-30 16:14:39 +02:00
parent fe56c86007
commit c61e36acfc
6 changed files with 118 additions and 18 deletions

View File

@ -1,8 +1,6 @@
from django.contrib import admin
# Register your models here.
from .models import PantryItem, PantryItemLine, Unit, Category
from .models import PantryItem, PantryItemLine, Unit, Category, Location
class PantryItemInLine(admin.TabularInline):
@ -43,22 +41,27 @@ class PantryItemInLineAdmin(admin.ModelAdmin):
'info',
)
class AutocompleteAdmin(admin.ModelAdmin):
"""Class used to satisfy an admin check"""
search_fields = ["name"]
class PantryItemAdmin(admin.ModelAdmin):
list_filter = ['category', 'unit', 'min_quantity']
list_filter = ['category', 'unit', 'min_quantity', 'location']
search_fields = ['info', 'name', 'category__name', 'unit__name']
autocomplete_fields = ['category', 'unit']
inlines = [PantryItemInLine]
# TODO: make category a model
#autocomplete_fields = ['category',]
fields = (
'name',
'category',
('min_quantity', 'unit'),
'location',
'info',
'expiry_duration',
)
list_display = (
@ -73,3 +76,4 @@ admin.site.register(PantryItem, PantryItemAdmin)
admin.site.register(PantryItemLine, PantryItemInLineAdmin)
admin.site.register(Unit, AutocompleteAdmin)
admin.site.register(Category, AutocompleteAdmin)
admin.site.register(Location, AutocompleteAdmin)

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2018-09-27 17:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('inventory', '0012_auto_20180923_2037'),
]
operations = [
migrations.AddField(
model_name='pantryitem',
name='expiry_duration',
field=models.DurationField(blank=True, null=True),
),
]

View File

@ -0,0 +1,32 @@
# Generated by Django 2.1.1 on 2018-09-30 14:06
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('inventory', '0013_pantryitem_expiry_duration'),
]
operations = [
migrations.CreateModel(
name='Location',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=200, null=True)),
('in_location', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='inventory.Location')),
],
),
migrations.AlterField(
model_name='pantryitem',
name='expiry_duration',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='pantryitem',
name='location',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='inventory.Location'),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 2.1.1 on 2018-09-30 14:08
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('inventory', '0014_auto_20180930_1406'),
]
operations = [
migrations.AlterField(
model_name='location',
name='in_location',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='inventory.Location'),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 2.1.1 on 2018-09-30 14:09
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('inventory', '0015_auto_20180930_1408'),
]
operations = [
migrations.AlterField(
model_name='location',
name='in_location',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='inventory.Location'),
),
]

View File

@ -1,18 +1,5 @@
from django.db import models
# Create your models here.
UNITS = (
('L', 'miliLiter'),
('GR', 'Grams'),
('Roll', 'Roll'),
('Tube', 'Tube'),
('Bag', 'Bag'),
('Bar', 'Bar'),
('Pack', 'Pack'),
)
class Category(models.Model):
"""A category for a pantry item to be in
@ -48,13 +35,33 @@ class Unit(models.Model):
def __str__(self):
return self.name
class Location(models.Model):
"""Location for a pantry item line"""
name = models.CharField(max_length=200, null=True, blank=True)
in_location = models.ForeignKey("self", on_delete=models.PROTECT, null=True, blank=True)
def __str__(self):
if self.in_location and self.in_location.id != self.id:
return self.name + ' ' + str(self.in_location)
return self.name
class PantryItem(models.Model):
# user?
"""A think you keep in your pantry """
name = models.CharField(max_length=200)
category = models.ForeignKey(Category, on_delete=models.PROTECT)
min_quantity = models.IntegerField(default=1) #, decimal_places=3, max_digits=32)
unit = models.ForeignKey(Unit, on_delete=models.PROTECT, null=True)
info = models.CharField(max_length=200, null=True, blank=True)
# some things don't have a fixed expiry date, like legumes or garlic, we can specify a default expiration for
# this.
# if expiry duration is set the expiration date for a pantryitemline will be set to now + duration on save
# represents days
expiry_duration = models.IntegerField(null=True, blank=True)
# location is saved on a per pantryitem base, not pantryitemline
# you can have multiple pantries with subpantries
# if a pantryitem moves to the kitchen or fridge it is no longer a pantry item
location = models.ForeignKey(Location, on_delete=models.PROTECT, null=True)
def __str__(self):
return self.name
@ -75,3 +82,4 @@ class PantryItemLine(models.Model):
def __str__(self):
return ' '.join([str(x) for x in [self.pantry_item.name, self.quantity, 'X', self.size, self.pantry_item.unit]])