--- Revision None +++ Revision 323633356237 @@ -0,0 +1,114 @@ +# -*- coding: utf-8 -*- +from django.db import models +from django.core.exceptions import ValidationError +from django.template.defaultfilters import filesizeformat +from compte.models import Employe +from utils.models import CommonField +from world.models import Ville +from utils import helpme +from parametres.models import Operation,Categorie +from parametres.conf import IMAGE_SIZE,ANNONCES_LIFETIMES +import datetime,time,os,os.path + +# Create your models here. + +LIFE_STEP = [ + ("1","Libre"), + ("2","En visite"), + ("3","Reserver"), + ("4",u"Occuper") +] + + +class Annonce(models.Model): + reference = models.CharField(max_length=6,unique=True,editable=False) + + annonceur = models.ForeignKey(Employe,limit_choices_to={'is_active':True}) + operation = models.ForeignKey(Operation,limit_choices_to={'is_active':True}) + localisation = models.ForeignKey(Ville,limit_choices_to={'is_active':True}) + quartier = models.CharField(max_length=100,blank=True,help_text=u"Séparer les différentes appelation du quartier par des virgules") + type_bien = models.ForeignKey(Categorie,limit_choices_to={"is_active":True},verbose_name='Type de bien') + price = models.PositiveIntegerField(u"Coût du bien") + periode = models.CharField(max_length=1,choices=[('3','Annuel'),('4','Hebdo'),('2','Journalier'),('1','Mensuel')],help_text='Ce champ est obligatoire si et seulement si le type d\'operation est une location',blank=True) + + def upload_path(self,filename): + extension=os.path.splitext(filename)[1] + new_name=str(time.time())+extension + return "annonceimage/"+new_name + + p_image = models.ImageField("Image principale",upload_to=upload_path,null=True,blank=True,help_text="La taille maximale est de %s"%filesizeformat(IMAGE_SIZE)) + nb_chambre = models.PositiveIntegerField('Nombre de chambre',null=True,blank=True) + nb_salon = models.PositiveIntegerField('Nombre de salon',null=True,blank=True) + nb_wc = models.PositiveIntegerField('Nombre de WC',null=True,blank=True) + nb_douche = models.PositiveIntegerField('Nombre de douche',null=True,blank=True) + nb_cuisine = models.PositiveIntegerField('Nombre de cuisine',null=True,blank=True) + nb_car = models.PositiveIntegerField(u'Capacité garage',null=True,blank=True) + nb_piscine = models.PositiveIntegerField('Nombre de piscine',null=True,blank=True) + + year_build = models.PositiveIntegerField(u'Année de construction',null=True,blank=True) + sup_all = models.PositiveIntegerField('Superficie totale',null=True,blank=True) + sup_hab = models.PositiveIntegerField('Superfice habitable',null=True,blank=True) + + open_date = models.DateField('Date d\'ouverture',default=datetime.date.today) + close_date = models.DateField('Date de fermeture') + life = models.CharField('Suivie',max_length=1,choices=LIFE_STEP,default='1') + + created = models.DateField(auto_now_add=True) + updated = models.DateField(auto_now=True) + is_active= models.BooleanField("Actif",default=True) + + def __unicode__(self): + return "Annonce %s"%self.reference + + def save(self,*args,**kwargs): + if not self.reference: + self.reference = helpme.generate_random_code("annonces","Annonce","reference",length=6) + return super(Annonce,self).save(*args,**kwargs) + + + def clean(self): + + if self.operation.slug_libelle == 'location': + if not self.periode: + raise ValidationError("Pour une location, le champ 'periode' est obligatoire") + + if self.p_image and self.p_image.size() > IMAGE_SIZE: + raise ValidationError("La taille maximale est de %s"%filesizeformat(IMAGE_SIZE)) + + if self.open_date > self.close_date: + raise ValidationError("Mauvaise configuration des dates") + + if ANNONCES_LIFETIMES: + date_ecart = self.close_date - self.open_date + if date_ecart.days > ANNONCES_LIFETIMES: + raise ValidationError(u"La durée d'une annonce ne peut pas dépasser %s jour(s)"%ANNONCES_LIFETIMES) + + + class Meta: + verbose_name = "annonce" + verbose_name_plural = "Liste des annonces" + +class ImageSecondaire(CommonField): + def upload_path(self,filename): + extension=os.path.splitext(filename)[1] + new_name=str(time.time())+extension + return "annonceimage/"+new_name + + annonce = models.ForeignKey(Annonce) + s_image = models.ImageField("Image",upload_to=upload_path,help_text="La taille maximale est de %s"%filesizeformat(IMAGE_SIZE)) + + def __unicode__(self): + return "image de l'annonce %s"%self.annonce.reference + + def clean(self): + if not self.annonce.p_image: + raise ValidationError("Ajouter une image principale avant") + + if self.p_image and self.p_image.size() > IMAGE_SIZE: + raise ValidationError("La taille maximale est de %s"%filesizeformat(IMAGE_SIZE)) + + class Meta: + verbose_name = 'image' + verbose_name_plural = "Liste des images secondaire" + +