Guest User

Untitled

a guest
Aug 17th, 2016
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.db import models
  2. from mptt.models import MPTTModel, TreeForeignKey
  3. from django.core.urlresolvers import reverse
  4. import urllib.request
  5. import os
  6. from django.core.files import File
  7. import sys
  8. from django.core.exceptions import ObjectDoesNotExist
  9.  
  10.  
  11. class Category(MPTTModel):
  12.     name = models.CharField(max_length=255, verbose_name='Категория')
  13.     slug = models.SlugField(max_length=255)
  14.     parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
  15.     short_desc = models.TextField(null=True, blank=True, verbose_name='Короткое описание')
  16.     long_desc = models.TextField(null=True, blank=True, verbose_name='Полное писание')
  17.     photo = models.ImageField(max_length=255, upload_to="category/", blank=True, null=True)
  18.     active = models.BooleanField(default=True, verbose_name='Активность')
  19.  
  20.     class Meta:
  21.         verbose_name = 'Категория'
  22.         verbose_name_plural = 'Категории'
  23.  
  24.     class MPTTMeta:
  25.         order_insertion_by = ['name']
  26.  
  27.     def __str__(self):
  28.         return self.name
  29.  
  30.     def get_absolute_url(self):
  31.         parents_slug_list = [ancestor.slug for ancestor in self.get_ancestors()]
  32.         parents_path = ''
  33.         if parents_slug_list:
  34.             parents_path = '/'.join(parents_slug_list) + '/'
  35.         return reverse('in_catalog', args=[parents_path + self.slug])
  36.  
  37.  
  38. class Attribute(models.Model):
  39.     name = models.CharField(max_length=255, verbose_name='Название')
  40.     xml_id = models.CharField(max_length=255, verbose_name='XML_ID')
  41.     active = models.BooleanField(default=True, verbose_name='Активность')
  42.  
  43.     def __str__(self):
  44.         return self.name
  45.  
  46.     class Meta:
  47.         verbose_name = 'Свойство'
  48.         verbose_name_plural = 'Свойства'
  49.  
  50.  
  51. class Product(models.Model):
  52.     xml_id = models.CharField(max_length=255, verbose_name='XML_ID', unique=True)
  53.     name = models.CharField(max_length=255, verbose_name='Название')
  54.     slug = models.SlugField(max_length=255)
  55.     category = models.ManyToManyField(Category,
  56.                                       verbose_name='Категории',
  57.                                       related_name='products',
  58.                                       through='ProductCategory')
  59.     short_desc = models.TextField(null=True, blank=True, verbose_name='Короткое описание')
  60.     long_desc = models.TextField(null=True, blank=True, verbose_name='Полное писание')
  61.     active = models.BooleanField(default=True, verbose_name='Активность')
  62.     attributes = models.ManyToManyField(Attribute, through='ProductAttribute', verbose_name='Свойства')
  63.  
  64.     price = models.FloatField(verbose_name='Цена')
  65.     price1 = models.FloatField(verbose_name='Цена1')
  66.     price2 = models.FloatField(verbose_name='Цена2')
  67.     price3 = models.FloatField(verbose_name='Цена3')
  68.     sku = models.CharField(max_length=255, verbose_name='Артикул')
  69.     xml_url = models.URLField(max_length=255, verbose_name='Страница поставщика', blank=True)
  70.  
  71.     class Meta:
  72.         verbose_name = 'Товар'
  73.         verbose_name_plural = 'Товары'
  74.  
  75.     def __str__(self):
  76.         return self.name
  77.  
  78.     def intro(self):
  79.         return self.short_desc or self.long_desc
  80.  
  81.     def get_photo(self):
  82.         try:
  83.             return self.photos.all()[0]
  84.         except IndexError:
  85.             return None
  86.  
  87.     def get_photos(self):
  88.         return self.photos.all()
  89.  
  90.     def get_absolute_url(self):
  91.         try:
  92.             main_cat = self.category.get(productcategory__main=True)
  93.         except ObjectDoesNotExist:
  94.             main_cat = self.category.all()[0]
  95.  
  96.         category_ancestor_slug_list = [ancestor.slug for ancestor in main_cat.get_ancestors()]
  97.         ancestor_path = ''
  98.         if category_ancestor_slug_list:
  99.             ancestor_path = '/'.join(category_ancestor_slug_list) + '/'
  100.         return reverse('in_catalog', args=[ancestor_path + main_cat.slug + '/' + self.slug])
  101.  
  102.  
  103. class ProductCategory(models.Model):
  104.     product = models.ForeignKey(Product, verbose_name='Товар')
  105.     category = models.ForeignKey(Category, verbose_name='Категория')
  106.     main = models.BooleanField(default=False)
  107.  
  108.     def save(self, *args, **kwargs):
  109.         if self.main:
  110.             try:
  111.                 temp = ProductCategory.objects.get(main=True)
  112.                 if self != temp:
  113.                     temp.main = False
  114.                     temp.save()
  115.             except ProductCategory.DoesNotExist:
  116.                 pass
  117.         super(ProductCategory, self).save(*args, **kwargs)
  118.  
  119.     class Meta:
  120.         verbose_name = 'Продукт-Категория'
  121.         verbose_name_plural = 'Продукты-Категории'
  122.  
  123.     def __str__(self):
  124.         return self.product.name + '/' + self.category.name
  125.  
  126.  
  127. class ProductAttribute(models.Model):
  128.     product = models.ForeignKey(Product, verbose_name='Товар')
  129.     attribute = models.ForeignKey(Attribute, verbose_name='Свойство', related_name='with_products')
  130.     value = models.CharField(max_length=255, verbose_name='Значение')
  131.  
  132.     class Meta:
  133.         verbose_name = 'Продукт-Свойство'
  134.         verbose_name_plural = 'Продукты-Свойства'
  135.  
  136.     def __str__(self):
  137.         return self.product.name + '/' + self.attribute.name + '/' + self.value
  138.  
  139.  
  140. class ProductPhoto(models.Model):
  141.     url = models.CharField(max_length=255, unique=True)
  142.     photo = models.ImageField(max_length=255, upload_to='products/', blank=True)
  143.     product = models.ForeignKey(Product, verbose_name='Товар', related_name='photos')
  144.  
  145.     def cache(self):
  146.         """Store image locally if we have a URL"""
  147.         if self.url and not self.photo:
  148.             result = urllib.request.urlretrieve(self.url)
  149.             self.photo.save(
  150.                 os.path.basename(self.url),
  151.                 File(open(result[0], 'rb'))
  152.             )
  153.             self.save()
  154.  
  155.     def __str__(self):
  156.         return self.product.name
  157.  
  158.     class Meta:
  159.         verbose_name = 'Изображение'
  160.         verbose_name_plural = 'Изображения'
RAW Paste Data