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