2024-11-19 22:03:44 +00:00
|
|
|
from itertools import groupby
|
|
|
|
|
2023-11-18 11:57:50 +00:00
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
from django.db import models, IntegrityError
|
2023-11-20 14:56:32 +00:00
|
|
|
from django_softdelete.models import SoftDeleteModel, SoftDeleteManager
|
2023-11-18 11:57:50 +00:00
|
|
|
|
|
|
|
|
2023-11-20 14:56:32 +00:00
|
|
|
class ItemManager(SoftDeleteManager):
|
2023-11-18 11:57:50 +00:00
|
|
|
|
|
|
|
def create(self, **kwargs):
|
2024-11-19 22:03:44 +00:00
|
|
|
container = kwargs.pop('container')
|
|
|
|
if 'uid_deprecated' in kwargs:
|
|
|
|
raise ValueError('uid_deprecated must not be set manually')
|
|
|
|
uid_deprecated = Item.all_objects.filter(event=kwargs['event']).count() + 1
|
|
|
|
kwargs['uid_deprecated'] = uid_deprecated
|
|
|
|
item = super().create(**kwargs)
|
|
|
|
item.container = container
|
|
|
|
return item
|
2023-11-18 11:57:50 +00:00
|
|
|
|
2024-01-07 21:11:13 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(returned_at__isnull=True)
|
|
|
|
|
2023-11-18 11:57:50 +00:00
|
|
|
|
2023-11-20 11:24:27 +00:00
|
|
|
class Item(SoftDeleteModel):
|
2024-11-19 22:03:44 +00:00
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
uid_deprecated = models.IntegerField()
|
2023-11-18 11:57:50 +00:00
|
|
|
description = models.TextField()
|
|
|
|
event = models.ForeignKey('Event', models.CASCADE, db_column='eid')
|
2024-11-19 22:03:44 +00:00
|
|
|
# container = models.ForeignKey('Container', models.CASCADE, db_column='cid')
|
2023-11-18 11:57:50 +00:00
|
|
|
returned_at = models.DateTimeField(blank=True, null=True)
|
2024-01-22 16:21:22 +00:00
|
|
|
created_at = models.DateTimeField(null=True, auto_now_add=True)
|
2023-11-18 11:57:50 +00:00
|
|
|
updated_at = models.DateTimeField(blank=True, null=True)
|
2024-11-19 22:03:44 +00:00
|
|
|
#related_issues = models.ManyToManyField('IssueThread', through='ItemRelation')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def container(self):
|
|
|
|
try:
|
|
|
|
return self.container_history.order_by('-timestamp').first().container
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@container.setter
|
|
|
|
def container(self, value):
|
|
|
|
if self.container == value:
|
|
|
|
return
|
|
|
|
self.container_history.create(container=value)
|
2023-11-18 11:57:50 +00:00
|
|
|
|
|
|
|
objects = ItemManager()
|
2023-11-20 14:56:32 +00:00
|
|
|
all_objects = models.Manager()
|
2023-11-18 11:57:50 +00:00
|
|
|
|
|
|
|
class Meta:
|
2024-11-19 22:03:44 +00:00
|
|
|
unique_together = (('uid_deprecated', 'event'),)
|
2024-01-07 18:53:25 +00:00
|
|
|
permissions = [
|
|
|
|
('match_item', 'Can match item')
|
|
|
|
]
|
2023-11-18 11:57:50 +00:00
|
|
|
|
2024-06-22 23:20:13 +00:00
|
|
|
def __str__(self):
|
2024-11-19 22:03:44 +00:00
|
|
|
return '[' + str(self.id) + ']' + self.description
|
2024-06-22 23:20:13 +00:00
|
|
|
|
2024-01-22 16:21:22 +00:00
|
|
|
|
2023-11-20 11:24:27 +00:00
|
|
|
class Container(SoftDeleteModel):
|
2024-11-19 22:03:44 +00:00
|
|
|
id = models.AutoField(primary_key=True)
|
2023-11-18 11:57:50 +00:00
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
created_at = models.DateTimeField(blank=True, null=True)
|
|
|
|
updated_at = models.DateTimeField(blank=True, null=True)
|
|
|
|
|
2024-11-19 22:03:44 +00:00
|
|
|
@property
|
|
|
|
def items(self):
|
|
|
|
try:
|
|
|
|
history = self.item_history.order_by('-timestamp').all()
|
|
|
|
return [v for k, v in groupby(history, key=lambda item: item.item.id)]
|
|
|
|
except AttributeError:
|
|
|
|
return []
|
|
|
|
|
2024-06-22 23:20:13 +00:00
|
|
|
def __str__(self):
|
2024-11-19 22:03:44 +00:00
|
|
|
return '[' + str(self.id) + ']' + self.name
|
|
|
|
|
|
|
|
|
|
|
|
class ItemPlacement(models.Model):
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
item = models.ForeignKey('Item', models.CASCADE, db_column='iid', related_name='container_history')
|
|
|
|
container = models.ForeignKey('Container', models.CASCADE, db_column='cid', related_name='item_history')
|
|
|
|
timestamp = models.DateTimeField(auto_now_add=True)
|
2024-06-22 23:20:13 +00:00
|
|
|
|
2023-11-18 11:57:50 +00:00
|
|
|
|
|
|
|
class Event(models.Model):
|
|
|
|
eid = models.AutoField(primary_key=True)
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
slug = models.CharField(max_length=255, unique=True)
|
|
|
|
start = models.DateTimeField(blank=True, null=True)
|
|
|
|
end = models.DateTimeField(blank=True, null=True)
|
|
|
|
pre_start = models.DateTimeField(blank=True, null=True)
|
|
|
|
post_end = models.DateTimeField(blank=True, null=True)
|
2024-01-22 16:21:22 +00:00
|
|
|
created_at = models.DateTimeField(null=True, auto_now_add=True)
|
2023-11-18 11:57:50 +00:00
|
|
|
updated_at = models.DateTimeField(blank=True, null=True)
|
2024-06-22 23:20:13 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '[' + str(self.slug) + ']' + self.name
|