This commit is contained in:
sidya82
2015-02-05 01:21:43 +01:00
parent 2a1099c450
commit b7b6a84643
22 changed files with 460 additions and 316 deletions

View File

@ -1,36 +1,38 @@
import random
import string
import time
from src.m.Voiture import Voiture
from src.m.connexionBDD import connexionBDD
import random
import string
import time
from src.m.Voiture import Voiture
from src.m.connexionBDD import connexionBDD
from src.m.Voiture import Voiture
from src.m.connexionBDD import connexionBDD
__author__ = 'sidya'
class Parking:
parkings = []
@staticmethod
def get(id):
if len(Parking.parkings) == 0 :
if len(Parking.parkings) == 0:
Parking.getAllActif()
for p in Parking.parkings :
if p.id == id :
for p in Parking.parkings:
if p.id == id:
return p
@staticmethod
def getAllActif():
if len(Parking.parkings) == 0 :
if len(Parking.parkings) == 0:
c = connexionBDD()
r = c.execute("SELECT * FROM parking WHERE actif = 1")
rows = r.fetchall()
c.seDeconnecter()
for row in rows :
for row in rows:
Parking(row["idParking"], row["nom"], None)
return Parking.parkings
@ -38,7 +40,7 @@ class Parking:
def remove(parking):
Parking.parkings.remove(parking)
c = connexionBDD()
c.execute("UPDATE parking SET actif = 0 WHERE idParking='"+str(parking.id)+"'")
c.execute("UPDATE parking SET actif = 0 WHERE idParking='" + str(parking.id) + "'")
c.seDeconnecter()
@staticmethod
@ -48,17 +50,22 @@ class Parking:
def __init__(self, id, nom=None, listeTypePlace=None):
self.__nom = nom
if id is None :
if id is None:
c = connexionBDD()
c.execute("INSERT INTO parking (nom) VALUES ('"+str(self.__nom)+"')", ())
c.execute("INSERT INTO parking (nom) VALUES ('" + str(self.__nom) + "')", ())
self.__id = c.lastId()
#Crea des places
# Crea des places
n = 0
for typePlace in listeTypePlace :
for i in range(typePlace.nombre) :
print(Place(None,self,typePlace,n,1,True,False))
n += 1
else :
placeParNiveau = {}
for typePlace in listeTypePlace:
try:
i = placeParNiveau[typePlace.niveau]
except KeyError:
i = 0
placeParNiveau[typePlace.niveau] = i + typePlace.nombre
for i in range(placeParNiveau[typePlace.niveau]):
Place(None, self, typePlace, i, True, False)
else:
self.__id = id
self.parkings.append(self)
@ -91,24 +98,23 @@ class Parking:
return Place.placeValide(self.__id, voiture)
def addPlaceSuperAbo(self, parking):
return Place(None, parking, None, None, None, True)
return Place(None, parking, None, None, False, True)
def __str__(self):
return "[Parking : nom = " + self.__nom +"]"
return "[Parking : nom = " + self.__nom + "]"
class Place:
def __init__(self, id=None, parking=None, typePlace=None, numero=None, niveau=None,estLibre=True, estSuperAbo=False):
if id is None :
def __init__(self, id=None, parking=None, typePlace=None, numero=None, estLibre=True, estSuperAbo=False):
if id is None:
self.__parking = parking
self.__typePlace = typePlace
self.__numero = numero
self.__niveau = niveau
self.__estLibre = estLibre
self.__estSuperAbo = estSuperAbo
if self.__typePlace is None:
t = "NULL"
else :
else:
t = self.__typePlace.id
c = connexionBDD()
c.execute("INSERT INTO place (idParking, idTypePlace, numero, estLibre, estSuperAbo) "
@ -117,16 +123,16 @@ class Place:
self.__numero, int(self.__estLibre), int(self.__estSuperAbo)))
self.__id = c.lastId()
c.seDeconnecter()
else :
else:
c = connexionBDD()
r = c.execute("SELECT * FROM place WHERE idPlace='"+str(id)+"'")
r = c.execute("SELECT * FROM place WHERE idPlace='" + str(id) + "'")
row = r.fetchone()
if row is None :
if row is None:
raise IndexError("Invalid id")
c.seDeconnecter()
self.__parking = Parking.get(row["idParking"])
self.__typePlace = TypePlace(row["idTypePlace"])
self.__numero = row["numero"]
self.__numero = row["numero"]
self.__estLibre = row["estLibre"]
self.__estSuperAbo = row["estSuperAbo"]
self.__id = id
@ -135,6 +141,7 @@ class Place:
def id(self):
return self.__id
def prendre(self):
"""
Rend la place indisponible
@ -145,7 +152,7 @@ class Place:
raise Exception("Place déjà prise")
self.__estLibre = False
c = connexionBDD()
c.execute("UPDATE place SET estLibre = 0 WHERE idPlace ='"+str(self.__id)+"'")
c.execute("UPDATE place SET estLibre = 0 WHERE idPlace ='" + str(self.__id) + "'")
c.seDeconnecter()
def liberer(self):
@ -157,17 +164,22 @@ class Place:
raise Exception("Impossible de liberer une place vide")
self.__estLibre = True
c = connexionBDD()
c.execute("UPDATE place SET estLibre = 1 WHERE idPlace ='"+str(self.__id)+"'")
c.execute(
"UPDATE place SET estLibre = 1, fin ='" + str(time.time()) + "' WHERE idPlace ='" + str(self.__id) + "'")
c.seDeconnecter()
@property
def identification(self):
return TypePlace(self.__typePlace).niveau + ":" + self.__numero
return str(chr(self.__typePlace.niveau + ord('A')) + ":" + str(self.__numero))
@property
def estlibre(self):
return self.__estLibre
@property
def typePlace(self):
return self.__typePlace
@staticmethod
def nbPlaceParking(idParking):
c = connexionBDD()
@ -179,7 +191,7 @@ class Place:
@staticmethod
def nbPlaceLibreParking(idParking):
c = connexionBDD()
r = c.execute("SELECT COUNT(*) FROM place WHERE idParking = "+str(idParking)+" AND estLibre = 1")
r = c.execute("SELECT COUNT(*) FROM place WHERE idParking = " + str(idParking) + " AND estLibre = 1")
row = r.fetchone()
c.seDeconnecter()
return row[0]
@ -187,7 +199,7 @@ class Place:
@staticmethod
def nbSuperAbo(idParking):
c = connexionBDD()
r = c.execute("SELECT COUNT(*) FROM place WHERE idParking = "+str(idParking)+" AND estSuperAbo = 1")
r = c.execute("SELECT COUNT(*) FROM place WHERE idParking = " + str(idParking) + " AND estSuperAbo = 1")
row = r.fetchone()
c.seDeconnecter()
return row[0]
@ -198,31 +210,29 @@ class Place:
r = c.execute("SELECT * FROM place WHERE idParking= ? AND estLibre = 1 "
"AND idTypePlace =(SELECT idTypePlace FROM typePlace "
"WHERE hauteur>? AND longueur>? ORDER BY longueur) ",
(str(idPArking),str(voiture.hauteur),str(voiture.longueur)))
(str(idPArking), str(voiture.hauteur), str(voiture.longueur)))
row = r.fetchone()
c.seDeconnecter()
if row is None :
if row is None:
return None
else :
return Place(row["idPlace"],row["idParking"], row["idTypePlace"],
else:
return Place(row["idPlace"], row["idParking"], row["idTypePlace"],
row["numero"], bool(row["estLibre"]), bool(row["estSuperAbo"]))
def __str__(self):
return "[Place : " \
"Parking = " + str(self.__parking) + "," \
"typePlace = " + str(self.__typePlace) + "," \
"numero = " + str(self.__numero) + "," \
"estLibre = " + str(self.__estLibre) + "," \
"estSuperAbo = " + str(self.__estSuperAbo) + "]" \
"typePlace = " + str(self.__typePlace) + "," \
"numero = " + str(
self.__numero) + "," \
"estLibre = " + str(self.__estLibre) + "," \
"estSuperAbo = " + str(self.__estSuperAbo) + "]" \
\
\
class TypePlace:
def __init__(self, id ,longueur=None, hauteur=None, nombre=None, prix=None, niveau=None):
if id is None :
def __init__(self, id, longueur=None, hauteur=None, nombre=None, prix=None, niveau=None):
if id is None:
self.__longueur = longueur
self.__hauteur = hauteur
self.__nombre = nombre
@ -230,14 +240,14 @@ class TypePlace:
self.__niveau = niveau
c = connexionBDD()
c.execute("INSERT INTO typePlace (longueur,hauteur,nombre, prix, niveau) VALUES (?,?,?,?,?)",
(self.__longueur, self.__hauteur, self.__nombre,self.__prix, self.__niveau))
(self.__longueur, self.__hauteur, self.__nombre, self.__prix, self.__niveau))
self.__id = c.lastId()
c.seDeconnecter()
else:
c = connexionBDD()
r = c.execute("SELECT * FROM typePlace WHERE idTypePlace='"+str(id)+"'")
r = c.execute("SELECT * FROM typePlace WHERE idTypePlace='" + str(id) + "'")
row = r.fetchone()
if row is None :
if row is None:
raise IndexError("Invalid id")
c.seDeconnecter()
self.__longueur = row["longueur"]
@ -274,11 +284,13 @@ class TypePlace:
def __str__(self):
return "[TypePlace : " \
"id = " + str(self.__id) + "," \
"longueur = " + str(self.__longueur) + "," \
"hauteur = " + str(self.__hauteur) + "," \
"nombre = " + str(self.__nombre) + "," \
"prix = " + str(self.__prix) + "," \
"niveau = " + str(self.__niveau) + "]"
"longueur = " + str(self.__longueur) + "," \
"hauteur = " + str(
self.__hauteur) + "," \
"nombre = " + str(self.__nombre) + "," \
"prix = " + str(self.__prix) + "," \
"niveau = " + str(
self.__niveau) + "]"
class Placement:
@ -289,14 +301,14 @@ class Placement:
:param place: Place
:return:
"""
if id is None :
if id is None:
self.__voiture = voiture
self.__place = place
self.__debut = time.time()
self.__fin = None
while True:
id = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in
range(random.randint(1, 10)))
range(random.randint(1, 10)))
try:
Placement(id)
except IndexError:
@ -308,9 +320,9 @@ class Placement:
c.seDeconnecter()
else:
c = connexionBDD()
r = c.execute("SELECT * FROM placement WHERE idPlacement='"+str(id)+"'")
r = c.execute("SELECT * FROM placement WHERE idPlacement='" + str(id) + "'")
row = r.fetchone()
if row is None :
if row is None:
raise IndexError("Invalid id")
c.seDeconnecter()
self.__voiture = Voiture(row["idVoiture"])
@ -327,16 +339,22 @@ class Placement:
def place(self):
return self.__place
@property
def voiture(self):
return self.__voiture
def end(self):
self.__fin = time.time()
c = connexionBDD()
c.execute("UPDATE placement SET fin='"+str(self.__fin)+"' WHERE idPlacement='"+str(id)+"'")
c.execute("UPDATE placement SET fin='" + str(self.__fin) + "' WHERE idPlacement='" + str(id) + "'")
c.seDeconnecter()
self.__place.liberer()
def __str__(self):
return "[Placement : " \
"id = " + str(self.__id) +"," \
"Voiture = " + str(self.__voiture) +"," \
"Place = " + str(self.__place) +"," \
"Debut = " + str(self.__debut) +"," \
"Fin = " + str(self.__fin) +"]"
"id = " + str(self.__id) + "," \
"Voiture = " + str(self.__voiture) + "," \
"Place = " + str(self.__place) + "," \
"Debut = " + str(
self.__debut) + "," \
"Fin = " + str(self.__fin) + "]"