2mG5P3PKomMTPV5stP2K0u changeset

Changeset323633356237 (b)
ParentNone (a)
ab
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+     
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
--- 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"
+
+