2016-09-29 20:20:10 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-28 20:00:54 +00:00
|
|
|
import uuid
|
|
|
|
import re
|
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
from django.db import models
|
2017-05-28 20:00:54 +00:00
|
|
|
from django.conf import settings
|
2016-09-29 20:20:10 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django_hstore import hstore
|
2017-02-15 03:01:47 +00:00
|
|
|
from tree.fields import PathField
|
|
|
|
from tree.models import TreeModelMixin
|
|
|
|
|
2017-05-28 20:00:54 +00:00
|
|
|
import requests
|
|
|
|
|
2017-02-15 03:01:47 +00:00
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
STATES = (
|
|
|
|
('present', 'Present'),
|
|
|
|
('taken', 'Taken'),
|
|
|
|
('broken', 'Broken'),
|
|
|
|
('missing', 'Missing'),
|
2018-03-01 16:05:12 +00:00
|
|
|
('depleted', 'Depleted'),
|
2016-09-29 20:20:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Category(models.Model):
|
|
|
|
name = models.CharField(max_length=127)
|
|
|
|
|
2017-10-24 18:59:08 +00:00
|
|
|
icon_id = models.CharField(max_length=64, null=True, blank=True)
|
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-12-13 20:38:44 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ['name']
|
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
|
2017-10-22 21:51:22 +00:00
|
|
|
# TODO label versioning
|
|
|
|
# Zapisywać w URL na naklejce jej wersję, aby można było łatwo wyłapać
|
|
|
|
# przedawnione informacje
|
|
|
|
# Also przechowywać "id" z qrkodów/barkodów w historycznej bazie.
|
|
|
|
# also qrcody w stylu //s/ID (żeby się resolvowało w sieci lokalnej)
|
|
|
|
# Also ID zawierające część name
|
|
|
|
|
2017-02-15 03:01:47 +00:00
|
|
|
class Item(models.Model, TreeModelMixin):
|
2016-09-29 20:20:10 +00:00
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2017-02-15 03:01:47 +00:00
|
|
|
|
|
|
|
parent = models.ForeignKey('self', null=True, blank=True)
|
|
|
|
path = PathField()
|
|
|
|
|
2017-02-16 15:14:37 +00:00
|
|
|
name = models.TextField()
|
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
description = models.TextField(blank=True, null=True)
|
|
|
|
state = models.CharField(max_length=31, choices=STATES, default=STATES[0][0])
|
2017-02-15 03:01:47 +00:00
|
|
|
categories = models.ManyToManyField(Category, blank=True)
|
2016-09-29 20:20:10 +00:00
|
|
|
owner = models.ForeignKey(User, null=True, blank=True, related_name='owned_items')
|
|
|
|
|
|
|
|
taken_by = models.ForeignKey(User, null=True, blank=True, related_name='taken_items')
|
|
|
|
taken_on = models.DateTimeField(blank=True, null=True)
|
|
|
|
taken_until = models.DateTimeField(blank=True, null=True)
|
|
|
|
|
2017-02-15 03:01:47 +00:00
|
|
|
props = hstore.DictionaryField(blank=True)
|
2016-09-29 20:20:10 +00:00
|
|
|
|
|
|
|
objects = hstore.HStoreManager()
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-02-16 01:01:36 +00:00
|
|
|
return '- ' * (self.get_level() or 0) + self.name
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
from django.urls import reverse
|
|
|
|
return reverse('item-display', kwargs={'pk': str(self.pk)})
|
2017-02-15 03:01:47 +00:00
|
|
|
|
2017-05-28 20:00:54 +00:00
|
|
|
def get_or_create_label(self, **kwargs):
|
|
|
|
defaults = {
|
|
|
|
'id': re.sub('[^A-Z0-9]', '', self.name.upper())[:16],
|
|
|
|
}
|
|
|
|
|
|
|
|
defaults.update(kwargs)
|
|
|
|
|
|
|
|
obj, created = self.labels.get_or_create(**kwargs, defaults=defaults)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
2017-10-24 18:59:08 +00:00
|
|
|
@property
|
|
|
|
def primary_category(self):
|
|
|
|
return next((c for c in self.categories.all() if c.icon_id), None)
|
|
|
|
|
2017-02-15 03:01:47 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('path',)
|
2016-09-29 20:20:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ItemImage(models.Model):
|
|
|
|
item = models.ForeignKey(Item, related_name='images')
|
|
|
|
image = models.ImageField()
|
2017-04-28 12:14:27 +00:00
|
|
|
|
2017-12-13 20:30:15 +00:00
|
|
|
def __str__(self):
|
|
|
|
return '{}'.format(self.image.name)
|
|
|
|
|
2017-04-28 12:14:27 +00:00
|
|
|
|
|
|
|
class Label(models.Model):
|
|
|
|
id = models.CharField(max_length=64, primary_key=True)
|
|
|
|
item = models.ForeignKey(Item, related_name='labels')
|
2017-05-28 20:00:54 +00:00
|
|
|
style = models.CharField(max_length=32, choices=(
|
|
|
|
('basic_99012_v1', 'Basic Dymo 89x36mm label'),
|
|
|
|
), default='basic_99012_v1')
|
|
|
|
created = models.DateTimeField(auto_now_add=True, blank=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-09-05 23:22:54 +00:00
|
|
|
return '{}'.format(self.id)
|
2017-05-28 20:00:54 +00:00
|
|
|
|
|
|
|
def print(self):
|
|
|
|
resp = requests.post(
|
|
|
|
'{}/api/1/print/{}'.format(settings.LABEL_API, self.id))
|
|
|
|
resp.raise_for_status()
|