import os from PIL import Image from django.db import models from django.db.models.signals import post_save from django.contrib.sitemaps import Sitemap from django.dispatch import receiver from django.urls import reverse from django.apps import apps from django.utils.html import format_html from django.conf import settings from django.template.defaultfilters import slugify class Retailer(models.Model): name = models.CharField(max_length=200) slug = models.CharField(max_length=50) date_created = models.DateTimeField(auto_now_add=True) def __str__(self): return self.name class Brand(models.Model): name = models.CharField(max_length=200) slug = models.CharField(max_length=50) date_created = models.DateTimeField(auto_now_add=True) def __str__(self): return self.name class Product(models.Model): name = models.CharField(max_length=200) wired_name = models.CharField(max_length=200) normalized_name = models.CharField(max_length=200) brand = models.ForeignKey(Brand, null=True, on_delete=models.SET_NULL) slug = models.CharField(max_length=250) date_created = models.DateTimeField() body_markdown = models.TextField(blank=True) body_html = models.TextField(null=True, blank=True) wired_price = models.IntegerField() lowest_price_ever = models.IntegerField(null=True) rating = models.IntegerField() class Meta: ordering = ('-date_created',) def __str__(self): return self.name @property def get_previous_admin_url(self): n = self.get_previous_by_read_date() 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_read_date().pk]) except model.DoesNotExist: return '' def admin_thumbnail(self): return format_html('' % (self.featured_image.get_thumbnail_url())) admin_thumbnail.short_description = 'Thumbnail' def get_full_name(self): return "%s %s" % (self.brand.name, self.name) def save(self, *args, **kwargs): super(Product, self).save() class ProductLink(models.Model): retailer = models.ForeignKey(Retailer, null=True, on_delete=models.SET_NULL) url = models.CharField(max_length=255, blank=True, null=True) date_last_checked = models.DateTimeField(blank=True, null=True) date_created = models.DateTimeField(auto_now_add=True) product = models.ForeignKey(Brand, null=True, on_delete=models.SET_NULL) is_active = models.BooleanField(default=True) class Meta: ordering = ('date_last_checked',) def __str__(self): return self.url