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'),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Category(models.Model):
|
|
|
|
name = models.CharField(max_length=127)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.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-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
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
def print(self):
|
|
|
|
resp = requests.post(
|
|
|
|
'{}/api/1/print/{}'.format(settings.LABEL_API, self.id))
|
|
|
|
resp.raise_for_status()
|