models.py 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from uuid import uuid4 as uuid

from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _

from django.contrib.auth.models import User
from django.contrib.postgres.fields import JSONField

from locations.models import Location


class Category(models.Model):
    class Meta:
        verbose_name_plural = "categories"

    name = models.CharField(max_length=80, null=False, unique=True)
    description = models.TextField()
dani2612's avatar
dani2612 committed
19
    parent = models.ForeignKey('self', verbose_name='the parent category', blank=True, null=True,on_delete=models.CASCADE)
20

21 22 23
    def __str__(self):
        return f"{self.name} ({self.description})"

24 25 26 27 28

class State(models.Model):
    name = models.CharField(max_length=80)
    description = models.TextField()

29 30 31 32 33 34
    def __str__(self):
        if self.description != '':
            return f"{self.name} ({self.description})"
        else:
            return f"{self.name}"

35 36 37 38 39 40

class Thing(models.Model):
    """
    Represents a thing, which should either be stored in another thing or
    placed in a certain location.
    """
41
    id = models.UUIDField(primary_key=True, default=uuid)
42 43 44 45
    name = models.CharField(max_length=80, null=False)
    description = models.TextField()
    categories = models.ManyToManyField(Category)

Aljaž Srebrnič's avatar
Aljaž Srebrnič committed
46
    quantity = models.FloatField(null=True, blank=True)
47 48 49 50 51

    consumable = models.BooleanField(default=False)
    borrowable = models.BooleanField(default=False)
    private = models.BooleanField(default=False)

dani2612's avatar
dani2612 committed
52
    location = models.ForeignKey(Location, null=True, blank=True,on_delete=models.CASCADE)
53 54 55
    parent = models.ForeignKey('self',
                               verbose_name='contained inside of',
                               on_delete=models.SET_NULL,
Aljaž Srebrnič's avatar
Aljaž Srebrnič committed
56 57
                               null=True,
                               blank=True)
58

dani2612's avatar
dani2612 committed
59 60
    owner = models.ForeignKey(User, verbose_name='owner of the thing',on_delete=models.CASCADE)
    state = models.ForeignKey(State,on_delete=models.CASCADE)
61
    metadata = JSONField()
Aljaž Srebrnič's avatar
Aljaž Srebrnič committed
62
    expiry_date = models.DateField(null=True, blank=True)
63 64 65 66 67 68 69 70 71 72 73

    def clean(self):
        if self.location and self.parent:
            raise ValidationError({'location': ValidationError(_('Both location and parent defined'), code='invalid'),
                                   'parent': ValidationError(_('Both location and parent defined'), code='invalid')})

        if not self.location and not self.parent:
            raise ValidationError({'location': ValidationError(_('Location or parent must be defined'),
                                                               code='required'),
                                   'parent': ValidationError(_('Location or parent must be defined'),
                                                             code='required')})