summaryrefslogtreecommitdiff
path: root/procurement/models.py
blob: 1e707368407fc6a5e39d15b622f3023198b7c3ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from django.db import models
from django.utils.timezone import now as timezone_now

MONTH = 30 * 24 * 60 * 60
WEEK = 7 * 24 * 60 * 60
DAY = 24 * 60 * 60
HOUR = 60 * 60
MINUTE = 60


class DashboardModel(models.Model):
    """
    Abstract base model for things which will be displayed on the dashboard, adds in created and updated fields,
    and provides a convenience method which provides a nicely formatted string of the time since update.
    """
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

    @property
    def time_since_update(self):
        update_delta = timezone_now() - self.updated
        seconds_since_update = update_delta.seconds

        if seconds_since_update / MONTH >= 1:
            quantity = seconds_since_update / MONTH
            units = 'months' if quantity > 1 else 'month'

        elif seconds_since_update / WEEK >= 1:
            quantity = seconds_since_update / WEEK
            units = 'weeks' if quantity > 1 else 'week'

        elif seconds_since_update / DAY >= 1:
            quantity = seconds_since_update / DAY
            units = 'days' if quantity > 1 else 'day'

        elif seconds_since_update / HOUR >= 1:
            quantity = seconds_since_update / HOUR
            units = 'hours' if quantity > 1 else 'hour'

        elif seconds_since_update / MINUTE >= 1:
            quantity = seconds_since_update / MINUTE
            units = 'minutes' if quantity > 1 else 'minute'

        else:
            return "updated just now"

        # Ensure the quantity output is rounded to 2 decimal places
        base_string = 'updated {quantity:.2f} {units} ago'
        return base_string.format(quantity=quantity, units=units)


class Supplier(DashboardModel):
    """
    Model which represents an individual or organisation which supplies components
    """
    name = models.CharField(max_length=255)
    representative_name = models.CharField(max_length=255, null=True, blank=True)
    representative_email = models.EmailField(max_length=255, null=True, blank=True)
    is_authorized = models.BooleanField()

    def __str__(self):
        return '{}'.format(self.name)


class Component(DashboardModel):
    """
    Model which represents items which may be supplied.
    """
    name = models.CharField(max_length=255)
    sku = models.CharField(max_length=50)
    suppliers = models.ManyToManyField(Supplier, related_name='components', blank=True)

    class Meta:
        ordering = ("name",)

    def __str__(self):
        return '{} ({})'.format(
            self.name,
            self.sku
        )