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('' % (self.get_image_url())) admin_thumbnail.short_description = 'Thumbnail'