satchmo-imagekit / satchmo / apps / satchmo_ext / brand / models.py

from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from django.db.models import ImageField
from django.utils.translation import ugettext_lazy as _
from l10n.mixins import TranslatedObjectMixin
import product
from product.models import Product
from signals_ahoy.signals import collect_urls
import logging
from imagekit.models import ImageModel

log = logging.getLogger('brand.models')

class BrandManager(models.Manager):
    
    def active(self, site=None):
        if not site:
            site = Site.objects.get_current()
        return self.filter(site=site, active=True)
    
    def by_slug(self, slug):
        site = Site.objects.get_current()
        return self.get(slug=slug, site=site)

class Brand(models.Model, TranslatedObjectMixin):
    """A product brand"""
    site = models.ForeignKey(Site)
    slug = models.SlugField(_("Slug"), unique=True,
    help_text=_("Used for URLs"))
    products = models.ManyToManyField(Product, blank=True, verbose_name=_("Products"), through='BrandProduct')
    ordering = models.IntegerField(_("Ordering"))
    active = models.BooleanField(default=True)
    
    objects = BrandManager()
    
    def _active_categories(self):
        return [cat for cat in self.categories.all() if cat.has_content()]
    
    active_categories = property(fget=_active_categories)
    
    def _translation(self):
        return self._find_translation()
    translation = property(fget=_translation)

    def _get_absolute_url(self):
        return ('satchmo_brand_view', None, {'brandname' : self.slug})
        
    get_absolute_url = models.permalink(_get_absolute_url)
        
    def active_products(self):
        return self.products.filter(site=self.site, active=True)        

    def has_categories(self):
        return self.active_categories().count() > 0

    def has_content(self):
        return self.has_products() or self.has_categories()

    def has_products(self):
        return self.active_products().count() > 0
            
    def __unicode__(self):
        return u"%s" % self.slug
            
    class Meta:
        ordering=('ordering', 'slug')
        verbose_name = _('Brand')
        verbose_name_plural = _('Brands')

class BrandProduct(models.Model):
    brand = models.ForeignKey(Brand)
    product = models.ForeignKey(Product)
    
    class Meta:
        verbose_name=_("Brand Product")
        verbose_name_plural=_("Brand Products")

class BrandTranslation(ImageModel):
    brand = models.ForeignKey(Brand, related_name="translations")
    languagecode = models.CharField(_('language'), max_length=10, choices=settings.LANGUAGES)
    name = models.CharField(_('title'), max_length=100, blank=False)
    short_description = models.CharField(_('Short Description'), blank=True, max_length=200)
    description = models.TextField(_('Full Description'), blank=True)
    picture = ImageField(verbose_name=_('Picture'),
        upload_to='photos')
    num_views = models.IntegerField(_("Number of views"), default=0)
    
    def _get_filename(self):
        if self.brand:
            return '%s-%s' % (self.brand.slug, self.id)
        else:
            return 'default'
    _filename = property(_get_filename)

    class Meta:
        ordering=('languagecode', )      
        verbose_name = _('Brand Translation')
        verbose_name_plural = _('Brand Translations')  

    class IKOptions:
        spec_module = 'satchmo_ext.brand.specs'
        cache_dir = 'photos'
        image_field = 'picture'
        save_count_as = 'num_views'

class BrandCategoryManager(models.Manager):
    def by_slug(self, brandname, slug):
        brand = Brand.objects.by_slug(brandname)
        return brand.categories.get(slug=slug)

class BrandCategory(models.Model, TranslatedObjectMixin):
    """A category within a brand"""
    slug = models.SlugField(_("Slug"),
        help_text=_("Used for URLs"))
    brand = models.ForeignKey(Brand, related_name="categories")
    products = models.ManyToManyField(Product, blank=True, verbose_name=_("Products"), through='BrandCategoryProduct')
    ordering = models.IntegerField(_("Ordering"))
    active = models.BooleanField(default=True)

    objects = BrandCategoryManager()

    def _translation(self):
        return self._find_translation()
    translation = property(fget=_translation)

    def _get_absolute_url(self):
        return ('satchmo_brand_category_view', None, {'brandname' : self.brand.slug, 'catname' : self.slug})
    
    get_absolute_url = models.permalink(_get_absolute_url)
        
    def active_products(self):
        return self.products.filter(site=self.brand.site).filter(active=True)                
        
    def has_categories(self):
        return False    
    
    def has_content(self):
        return self.active_products()

    def has_products(self):
        return self.active_products().count > 0

    def __unicode__(self):
        return u"%s: %s" % (self.brand.slug, self.slug)

    class Meta:
        ordering=('ordering', 'slug')
        verbose_name = _('Brand Category')
        verbose_name_plural = _('Categories')

class BrandCategoryProduct(models.Model):
    brandcategory = models.ForeignKey(BrandCategory)
    product = models.ForeignKey(Product)
    
    class Meta:
        verbose_name = _('Brand Category Product')
        verbose_name_plural = _('Brand Category Products')

class BrandCategoryTranslation(ImageModel):

    brandcategory = models.ForeignKey(BrandCategory, related_name="translations")
    languagecode = models.CharField(_('language'), max_length=10, choices=settings.LANGUAGES)
    name = models.CharField(_('title'), max_length=100, blank=False)
    short_description = models.CharField(_('Short Description'), blank=True, max_length=200)
    description = models.TextField(_('Description'), blank=True)
    picture = ImageField(verbose_name=_('Picture'),
        upload_to='photos')
    num_views = models.IntegerField(_("Number of views"), default=0)
    
    def _get_filename(self):
        if self.brandcategory:
            return '%s-%s' % (self.brandcategory.brand.slug, self.id)
        else:
            return 'default'
    _filename = property(_get_filename)
    
    class Meta:
        ordering=('languagecode', )
        verbose_name_plural = _('Brand Category Translations')

    class IKOptions:
        spec_module = 'satchmo_ext.brand.specs'
        cache_dir = 'photos'
        image_field = 'picture'
        save_count_as = 'num_views'

#import config        
from urls import add_brand_urls
collect_urls.connect(add_brand_urls, sender=product)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.