Advertisement
Guest User

base_products.py

a guest
Mar 26th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. from __future__ import unicode_literals
  2. import re
  3.  
  4. from django.core.urlresolvers import reverse
  5. from django.utils.encoding import python_2_unicode_compatible
  6. from django.db import models
  7. from django.utils.safestring import mark_safe
  8. from django.utils.translation import pgettext_lazy
  9. from mptt.models import MPTTModel
  10. from satchless.item import ItemRange
  11. from unidecode import unidecode
  12.  
  13. from core.utils.models import Subtyped
  14.  
  15.  
  16. @python_2_unicode_compatible
  17. class Category(MPTTModel):
  18. name = models.CharField(
  19. pgettext_lazy('Category field', 'name'), max_length=128)
  20. slug = models.SlugField(
  21. pgettext_lazy('Category field', 'slug'), max_length=50, unique=True)
  22. description = models.TextField(
  23. pgettext_lazy('Category field', 'description'), blank=True)
  24. parent = models.ForeignKey(
  25. 'self', null=True, blank=True, related_name='children',
  26. verbose_name=pgettext_lazy('Category field', 'parent'))
  27.  
  28. def __str__(self):
  29. return self.name
  30.  
  31. def get_absolute_url(self):
  32. return reverse('product:category', kwargs={'slug': self.slug})
  33.  
  34. class Meta:
  35. verbose_name_plural = 'categories'
  36. app_label = 'product'
  37.  
  38.  
  39. @python_2_unicode_compatible
  40. class Product(Subtyped, ItemRange):
  41. name = models.CharField(
  42. pgettext_lazy('Product field', 'name'), max_length=128)
  43. category = models.ForeignKey(
  44. Category, verbose_name=pgettext_lazy('Product field', 'category'),
  45. related_name='products')
  46. description = models.TextField(
  47. verbose_name=pgettext_lazy('Product field', 'description'))
  48.  
  49. collection = models.CharField(db_index=True, max_length=100, blank=True)
  50.  
  51. class Meta:
  52. app_label = 'product'
  53.  
  54. def __str__(self):
  55. return self.name
  56.  
  57. def get_absolute_url(self):
  58. return reverse('product:details', kwargs={'slug': self.get_slug(),
  59. 'product_id': self.id})
  60.  
  61. def get_slug(self):
  62. value = unidecode(self.name)
  63. value = re.sub(r'[^\w\s-]', '', value).strip().lower()
  64.  
  65. return mark_safe(re.sub(r'[-\s]+', '-', value))
  66.  
  67. def get_products_from_collection(self):
  68. return Product.objects.filter(
  69. collection=self.collection) if self.collection else None
  70.  
  71. def __iter__(self):
  72. return iter(self.variants.all())
  73.  
  74. def get_formatted_price(self, price):
  75. return "{0} {1}".format(price.gross, price.currency)
  76.  
  77. def admin_get_price_min(self):
  78. price = self.get_price_range().min_price
  79. return self.get_formatted_price(price)
  80.  
  81. admin_get_price_min.short_description = pgettext_lazy(
  82. 'Product admin page', 'Minimal price'
  83. )
  84.  
  85. def admin_get_price_max(self):
  86. price = self.get_price_range().max_price
  87. return self.get_formatted_price(price)
  88.  
  89. admin_get_price_max.short_description = pgettext_lazy(
  90. 'Product admin page', 'Maximal price'
  91. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement