summaryrefslogtreecommitdiff
path: root/app/products/models.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2019-11-05 11:15:30 -0500
committerluxagraf <sng@luxagraf.net>2019-11-05 11:15:30 -0500
commitb1057c9dc9ff6b6e1786d31e3401ad9571eb7f1d (patch)
treec3247bc40c63bf5df1c6fecee75a81b7c2ebafde /app/products/models.py
parent725e3d2f798d228a386466823490b6f25647a4db (diff)
added products
Diffstat (limited to 'app/products/models.py')
-rw-r--r--app/products/models.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/app/products/models.py b/app/products/models.py
new file mode 100644
index 0000000..bf33db4
--- /dev/null
+++ b/app/products/models.py
@@ -0,0 +1,89 @@
+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
+
+from photos.utils import resize_image
+from utils.util import render_images, render_products, parse_video, markdown_to_html
+
+
+def get_upload_path(self, filename):
+ return "images/products/%s" % (filename)
+
+class Brand(models.Model):
+ name = models.CharField(max_length=200)
+ slug = models.CharField(max_length=50)
+ pub_date = models.DateTimeField(auto_now_add=True)
+
+ def __str__(self):
+ return self.name
+
+class Product(models.Model):
+ name = models.CharField(max_length=200)
+ brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
+ slug = models.CharField(max_length=50)
+ pub_date = models.DateTimeField()
+ body_markdown = models.TextField(blank=True)
+ body_html = models.TextField(null=True, blank=True)
+ primary_offer_retailer = models.CharField(max_length=400)
+ primary_offer_url = models.CharField(max_length=400)
+ primary_offer_price = models.IntegerField()
+ secondary_offer_retailer = models.CharField(max_length=400)
+ secondary_offer_url = models.CharField(max_length=400, blank=True, null=True)
+ secondary_offer_price = models.IntegerField(blank=True, null=True)
+ rating = models.IntegerField()
+ is_public = models.BooleanField(default=True)
+ image = models.FileField(upload_to=get_upload_path, null=True, blank=True)
+
+ class Meta:
+ ordering = ('-pub_date',)
+
+ 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('<img src="%s" width="100" style="width:100px" />' % (self.get_image_url()))
+ admin_thumbnail.short_description = 'Thumbnail'
+
+ def get_full_name(self):
+ return "%s %s" % (self.brand.name, self.name)
+
+ def get_image_url(self):
+ return '%sbook-covers/%s' % (settings.IMAGES_URL, self.image.name.split('/')[-1])
+
+ def save(self, *args, **kwargs):
+ md = render_images(self.body_markdown)
+ prods = render_products(md)
+ self.body_html = markdown_to_html(prods)
+ super(Product, self).save()
+
+
+@receiver(post_save, sender=Product)
+def post_save_events(sender, update_fields, created, instance, **kwargs):
+ #base_path = "%s/%s/" % (settings.MEDIA_ROOT, "/".join(str(i) for i in instance.image.name.split('/')[:-1]))
+ #filename, file_extension = os.path.splitext(instance.image.path)
+ #img = Image.open(instance.image.path)
+ #resize_image(img, None, 160, 78, base_path, "%s_tn%s" % (filename.split('/')[-1], file_extension))
+ #resize_image(img, None, 650, 78, base_path, "%s_small%s" % (filename.split('/')[-1], file_extension))
+ pass
+