spejstore-new/storage/models.py

168 lines
4.4 KiB
Python
Raw Permalink Normal View History

2016-09-29 20:20:10 +00:00
from __future__ import unicode_literals
import os
2016-09-29 20:20:10 +00:00
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
2017-02-15 03:01:47 +00:00
from tree.fields import PathField
from tree.models import TreeModelMixin
from django.contrib.postgres.fields import HStoreField
from django.contrib.auth.models import UserManager
2017-02-15 03:01:47 +00:00
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 = (
2023-07-11 13:34:35 +00:00
("present", "Present"),
("taken", "Taken"),
("broken", "Broken"),
("missing", "Missing"),
("depleted", "Depleted"),
2016-09-29 20:20:10 +00:00
)
2024-01-30 23:27:48 +00:00
def api_print(item):
resp = requests.post(
"{}/api/2/print".format(settings.LABEL_API),
params={
2024-01-31 08:25:15 +00:00
"id": item.short_id(),
2024-01-30 23:27:48 +00:00
"name": item.name,
"owner": item.owner,
}
)
2023-07-17 18:14:06 +00:00
resp.raise_for_status()
2016-09-29 20:20:10 +00:00
class Category(models.Model):
name = models.CharField(max_length=127)
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
class Meta:
2023-07-11 13:34:35 +00:00
ordering = ["name"]
2018-09-26 20:39:51 +00:00
verbose_name_plural = "categories"
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
2023-07-11 13:34:35 +00:00
class StaffManager(UserManager):
2023-08-04 16:02:08 +00:00
pass
class StaffProxyModel(User):
objects = StaffManager()
class Meta:
proxy = True
verbose_name = "User"
verbose_name_plural = "Users"
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, on_delete=models.CASCADE)
2017-02-15 03:01:47 +00:00
path = PathField()
name = models.TextField()
2024-01-13 23:24:44 +00:00
wiki_link = models.TextField(null=True, blank=True)
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)
owner = models.ForeignKey(
StaffProxyModel,
null=True,
blank=True,
related_name="owned_items",
on_delete=models.CASCADE,
)
2016-09-29 20:20:10 +00:00
2023-07-11 13:34:35 +00:00
taken_by = models.ForeignKey(
StaffProxyModel,
null=True,
blank=True,
related_name="taken_items",
on_delete=models.CASCADE,
2023-07-11 13:34:35 +00:00
)
2016-09-29 20:20:10 +00:00
taken_on = models.DateTimeField(blank=True, null=True)
taken_until = models.DateTimeField(blank=True, null=True)
props = HStoreField(blank=True)
2016-09-29 20:20:10 +00:00
def short_id(self):
# let's just hope we never have 4 294 967 296 things :)
2023-07-11 13:34:35 +00:00
return str(self.pk)[:8] # collisions? what collisions?
2016-09-29 20:20:10 +00:00
def __str__(self):
2023-07-11 13:34:35 +00:00
return "- " * (self.get_level() or 0) + self.name
def get_absolute_url(self):
from django.urls import reverse
2023-07-11 13:34:35 +00:00
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 = {
2023-07-11 13:34:35 +00:00
"id": re.sub("[^A-Z0-9]", "", self.name.upper())[:16],
}
2017-05-28 20:00:54 +00:00
defaults.update(kwargs)
obj, created = self.labels.get_or_create(**kwargs, defaults=defaults)
return obj
@property
def primary_category(self):
return next((c for c in self.categories.all() if c.icon_id), None)
def print(self):
2024-01-30 23:27:48 +00:00
api_print(self)
2017-02-15 03:01:47 +00:00
class Meta:
2023-07-11 13:34:35 +00:00
ordering = ("path",)
2016-09-29 20:20:10 +00:00
class ImageFieldWithUuid(models.ImageField):
def generate_filename(self, instance, filename):
ext = filename.split(".")[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return super().generate_filename(instance, filename)
2016-09-29 20:20:10 +00:00
class ItemImage(models.Model):
item = models.ForeignKey(Item, related_name="images", on_delete=models.CASCADE)
image = ImageFieldWithUuid()
2017-04-28 12:14:27 +00:00
def __str__(self):
2023-07-11 13:34:35 +00:00
return "{}".format(self.image.name)
2017-04-28 12:14:27 +00:00
# Deprecated, left in db due to legacy reasons
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", on_delete=models.CASCADE)
2023-07-11 13:34:35 +00:00
style = models.CharField(
max_length=32,
choices=(("basic_99012_v1", "Basic Dymo 89x36mm label"),),
default="basic_99012_v1",
)
2017-05-28 20:00:54 +00:00
created = models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
2023-07-11 13:34:35 +00:00
return "{}".format(self.id)