class Mascara(EditorialModel): NORMAL = 0 SAZONAL = 1 CATEGORIAS = ( (NORMAL, u'Normal'), (SAZONAL, u'Sazonal') ) def new_filename(instance, filename): fname, dot, extension = filename.rpartition('.') fname = slugify(fname) return os.path.join('mascaras', '%s.%s' % (fname, extension)) imagem = models.ImageField(u'Imagem', upload_to=new_filename, null=True, blank=True) img_250x250 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(250, 250)], source='imagem', format='PNG', options={'quality': 90}) img_376x376 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(376, 376)], source='imagem', format='PNG', options={'quality': 90}) thumb = models.ImageField(u'Thumbnail', upload_to=new_filename, null=True, blank=True) thumb_98x98 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(98, 98)], source='thumb', format='PNG', options={'quality': 90}) tipo = models.IntegerField(u'Tipo de categoria', choices=CATEGORIAS, null=True, blank=False, default=NORMAL) categoria = models.ForeignKey(Categoria, null=True, blank=True, verbose_name=u'Categoria', related_name='mascaras') class Meta: verbose_name = u'Imagem para compartilhar' verbose_name_plural = u'Imagens para compartilhar' def __unicode__(self): return u'%s' % self.imagem @classmethod def normais_serializadas(cls): return [i.to_dict() for i in cls.objects.filter(tipo=cls.NORMAL, publicada=True)] def to_dict(self): return {'id': self.id, 'imagem': self.img_376x376.url, 'thumb': self.thumb_98x98.url}
class Categoria(EditorialModel): shopping = models.ForeignKey(Shopping, verbose_name=u'Shopping', related_name='categorias', null=True, blank=True) nome = models.CharField(u'Nome', max_length=100, blank=False, null=True) slug = models.SlugField(max_length=150, blank=False, null=False, unique=False) sazonal = models.BooleanField(u'Categoria sazonal?', default=False) imagem = models.ImageField(u'Imagem', upload_to='categorias', null=True, blank=True) img_162x27 = ImageSpecField( [Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(162, 27)], source='imagem', format='PNG', options={'quality': 90}) class Meta: verbose_name = u'Categoria' verbose_name_plural = u'Categorias' ordering = ['nome'] unique_together = ('shopping', 'slug') def __unicode__(self): shopping = ' (%s)' % self.shopping.nome if self.shopping else '' return u'%s%s' % (self.nome, shopping) def to_dict(self): contexto = {'id': self.id, 'nome': self.nome, 'slug': self.slug} if self.sazonal: contexto.update( {'imagem': self.img_162x27.url if self.imagem else None}) return contexto @classmethod def publicadas_com_oferta(cls, shopping=1): categorias = cls.objects.filter(shopping_id=shopping, sazonal=False, publicada=True).select_related() categorias = categorias.filter(ofertas__status=1, ofertas__fim__gte=date.today()) \ .order_by('nome') \ .distinct() # categorias = cls.objects.filter(ofertas__fim__gt=date.today(), ofertas__status=1, shopping_id=shopping, sazonal=False, publicada=True) \ # .order_by('nome') \ # .distinct() return categorias '''return separa_tres_colunas([c.to_dict() for c in categorias])'''
class ImagemOferta(OrderedModel): def new_filename(instance, filename): fname, dot, extension = filename.rpartition('.') fname = '%s_%s' % (slugify(unicode(fname)), datetime.now().strftime("%Y%m%d%H%M%S")) return os.path.join('ofertas', '%s.%s' % (fname, extension)) oferta = models.ForeignKey(Oferta, verbose_name=u'Oferta', related_name='imagens') imagem = models.ImageField(u'Imagem', upload_to=new_filename, null=True, blank=True) img_600x600 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(600, 600)], source='imagem', format='PNG', options={'quality': 90}) img_376x376 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(376, 376)], source='imagem', format='PNG', options={'quality': 90}) img_250x250 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(250, 250)], source='imagem', format='PNG', options={'quality': 90}) img_172x172 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(172, 172)], source='imagem', format='PNG', options={'quality': 90}) img_120x120 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(120, 120)], source='imagem', format='PNG', options={'quality': 90}) img_94x94 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(94, 94)], source='imagem', format='PNG', options={'quality': 90}) evento_180x445 = ImageSpecField([Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(180, 445)], source='imagem', format='PNG', options={'quality': 90}) principal = models.BooleanField(u'Imagem principal', default=False) vertical = models.BooleanField(u'Imagem vertical', default=False) class Meta: verbose_name = u'Imagem' verbose_name_plural = u'Imagens das ofertas' ordering = ['oferta', 'ordem'] def __unicode__(self): return u'%s - %s' % (self.oferta.nome, self.ordem)
class Loja(EditorialModel): def new_filename(instance, filename): fname, dot, extension = filename.rpartition('.') fname = slugify(fname) return os.path.join('lojas', '%s.%s' % (fname, extension)) shopping = models.ForeignKey(Shopping, verbose_name=u'Shopping', related_name='lojas') id_multilan = models.IntegerField('Id na Multiplan', null=True, blank=False) nome = models.CharField(u'Nome', max_length=100, null=True, blank=False) slug = models.CharField(u'Slug', max_length=150, null=True, blank=True) logo = models.ImageField(u'Imagem', upload_to=new_filename, null=True, blank=True) logo_120x50 = ImageSpecField( [Adjust(contrast=1.1, sharpness=1.1), resize.ResizeToFill(120, 50)], source='logo', format='PNG', options={'quality': 90}) telefone = models.CharField(u'telefone', max_length=100, null=True, blank=True) class Meta: verbose_name = u'Loja' verbose_name_plural = u'Lojas' ordering = ['nome'] def __unicode__(self): return u'%s - %s' % (self.nome, self.shopping) def to_dict(self): return { 'id': self.id, 'nome': self.nome, 'slug': self.slug, 'logo': self.logo_120x50.url if self.logo else None, 'telefone': self.telefone, 'shopping': self.shopping.to_dict() } @classmethod def publicadas_com_oferta(cls, shopping): lojas = cls.objects.filter(ofertas__status=1, ofertas__inicio__lte=date.today(), ofertas__fim__gt=datetime.now(), ofertas__loja__shopping_id=shopping)\ .order_by('nome')\ .distinct() return lojas '''return separa_tres_colunas([l.to_dict() for l in lojas])''' @classmethod def publicadas_sem_oferta(cls, shopping): lojas = cls.objects.filter(publicada=True, shopping_id=shopping).order_by('nome') return [l.to_dict() for l in lojas if not l.ofertas.filter(status=1)] @classmethod def relatorio_visitas(cls, shopping_id, date=None): if not date: return Loja.objects.annotate(vistas=Count( 'pk', only=Q(ofertas__logs__acao=1, shopping=shopping_id))).order_by('-vistas') else: return Loja.objects.annotate(vistas=Count('pk', only=Q(ofertas__logs__acao=1, shopping=shopping_id, ofertas__logs__data_criacao__gte=date)))\ .order_by('-vistas') @classmethod def relatorio_solicitacoes(cls, shopping_id, date=None): if not date: return Loja.objects.annotate( pedidos=Count('solicitacoes', only=Q( shopping=shopping_id))).order_by('-pedidos') else: return Loja.objects.annotate( pedidos=Count('solicitacoes', only=Q(shopping=shopping_id, solicitacoes__data_criacao__gte=date) )).order_by('-pedidos')