summaryrefslogtreecommitdiff
path: root/app/unused_apps/stuff/models.py
blob: 05a7f4f0dbc8ac8271237b4aa176cbd16731581d (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
from django.db import models
from django.urls import reverse
from django.utils.html import format_html
from django.utils import timezone
from django.conf import settings


class Item(models.Model):
    isbn = models.CharField(max_length=100, blank=True, null=True)
    serial_number = models.CharField(max_length=100, blank=True, null=True)
    ITEM_TYPES = (
        (0, 'Book'),
        (1, 'Apparel'),
        (2, 'Gadget'),
        (3, 'Furniture'),
    )
    item_type = models.IntegerField(choices=ITEM_TYPES, default=0)
    title = models.CharField(max_length=200)
    creator = models.CharField(max_length=200, blank=True, null=True)
    creator_sort_last_name = models.CharField(max_length=200, blank=True, null=True)
    creator_sort_first_name = models.CharField(max_length=200, blank=True, null=True)
    current_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    retail_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    purchase_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    amazon_url = models.CharField(max_length=400, blank=True, null=True)
    image_url = models.CharField(max_length=400, blank=True, null=True)
    image = models.FileField(upload_to='stuff/', null=True, blank=True)
    item_format = models.CharField(max_length=255, blank=True, null=True)
    date_released = models.DateTimeField(blank=True, null=True)
    date_purchased = models.DateTimeField(blank=True, null=True, default=timezone.now)
    date_added = models.DateTimeField(blank=True, null=True, default=timezone.now)

    class Meta:
        ordering = ['-date_added']

    def __str__(self):
        return self.title

    @property
    def get_previous_published(self):
        return self.get_previous_by_date_added(status__exact=1)

    @property
    def get_next_published(self):
        return self.get_next_by_date_added(status__exact=1)

    @property
    def get_previous_admin_url(self):
        n = self.get_previous_by_date_added()
        return reverse('admin:%s_%s_change' % (self._meta.app_label,  self._meta.model_name),  args=[n.id])

    @property
    def get_next_admin_url(self):
        model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name)
        try:
            return reverse('admin:%s_%s_change' % (self._meta.app_label,  self._meta.model_name),  args=[self.get_next_by_date_added().pk])
        except model.DoesNotExist:
            return ''

    def get_image_url(self):
        return '%s%s' % (settings.MEDIA_URL, self.image)

    def admin_thumbnail(self):
        return format_html('<img src="%s" width="100" style="width:100px">' % (self.get_image_url()))
    admin_thumbnail.short_description = 'Thumbnail'