from django.core.files.base import ContentFile from django.db import models, IntegrityError from django_softdelete.models import SoftDeleteModel, SoftDeleteManager class ItemManager(SoftDeleteManager): def create(self, **kwargs): if 'uid' in kwargs: raise ValueError('uid must not be set manually') uid = Item.all_objects.filter(event=kwargs['event']).count() + 1 kwargs['uid'] = uid return super().create(**kwargs) def get_queryset(self): return super().get_queryset().filter(returned_at__isnull=True) class Item(SoftDeleteModel): iid = models.AutoField(primary_key=True) uid = models.IntegerField() description = models.TextField() event = models.ForeignKey('Event', models.CASCADE, db_column='eid') container = models.ForeignKey('Container', models.CASCADE, db_column='cid') returned_at = models.DateTimeField(blank=True, null=True) created_at = models.DateTimeField(null=True, auto_now_add=True) updated_at = models.DateTimeField(blank=True, null=True) objects = ItemManager() all_objects = models.Manager() class Meta: unique_together = (('uid', 'event'),) permissions = [ ('match_item', 'Can match item') ] def __str__(self): return '[' + str(self.uid) + ']' + self.description class Container(SoftDeleteModel): cid = models.AutoField(primary_key=True) name = models.CharField(max_length=255) created_at = models.DateTimeField(blank=True, null=True) updated_at = models.DateTimeField(blank=True, null=True) def __str__(self): return '[' + str(self.cid) + ']' + self.name 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) created_at = models.DateTimeField(null=True, auto_now_add=True) updated_at = models.DateTimeField(blank=True, null=True) def __str__(self): return '[' + str(self.slug) + ']' + self.name