diff --git a/manager.py b/manager.py index 7810b4c..666a763 100644 --- a/manager.py +++ b/manager.py @@ -30,14 +30,15 @@ def install_with_data(): rs1 = Restaurateur(firstname="Valentino", lastname="Rossi", mail="rossi@ducati.it", password="quarantesix", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1) rs2 = Restaurateur(firstname="Fernando", lastname="Alonso", mail="alonso@ferrari.it", password="asturie", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1) rs3 = Restaurateur(firstname="Marcel", lastname="Proust", mail="restau", password="rateur", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1) + db.session.add(rs1) db.session.add(rs2) db.session.add(rs3) # Add entrepreneurs from webserver.models import Entrepreneur - e1 = Entrepreneur(firstname="Jay", lastname="UnNom", mail="un@mail.com", password="passwd", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1) import datetime + e1 = Entrepreneur(firstname="Jay", lastname="UnNom", mail="un@mail.com", password="passwd", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1) e2 = Entrepreneur(firstname="Entre", lastname="Preneur", mail="entre", password="preneur", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1, birthdate=datetime.datetime(2010, 10, 10)) db.session.add(e1) db.session.add(e2) @@ -46,8 +47,22 @@ def install_with_data(): from webserver.models import Client c1 = Client(firstname="Yvon", lastname="Gagner", mail="cli", password="ent", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1) c2 = Client(firstname="Leo", lastname="Pard", mail="pard@ferrari.it", password="passwdc2", phone="123-456-7890", address="1001 Rue Notre Dame", city="Montreal", zipcode="H3S 1Z1", country=ct1) + db.session.add(c1) db.session.add(c2) + + # !!! Add adresses to personne !!! + db.session.flush() + + rs1.create_order_address() + rs2.create_order_address() + rs3.create_order_address() + + e1.create_order_address() + e2.create_order_address() + + c1.create_order_address() + c2.create_order_address() # Add restaurants from webserver.models import Restaurant diff --git a/webserver/controllers/addresses.py b/webserver/controllers/addresses.py index dfa5753..20d51b1 100644 --- a/webserver/controllers/addresses.py +++ b/webserver/controllers/addresses.py @@ -11,13 +11,37 @@ addresses = Blueprint('addresses', __name__) +# Get address +@addresses.route('', methods=['GET', 'OPTIONS']) +def list(): + """ Return addresses + + Method: *GET* + URI: */addresses* + Params: ?personne_id=id + """ + + # Query + query = db.session.query(Address) + + if 'personne_id' in request.values: + query = query.filter(Address.personne_id == request.values['personne_id']) + + addresses = query.all() + + # Build the response + response = make_response(jsonify([a.to_dict() for a in addresses])) + response.status_code = 200 + response.mimetype = 'application/json' + return response + # Get one address @addresses.route('/', methods=['GET', 'OPTIONS']) def index(id): """ Return one address by id. Method: *GET* - URI: */address/id* + URI: */addresses/id* """ # Query diff --git a/webserver/controllers/clients.py b/webserver/controllers/clients.py index 04c4add..2d266db 100644 --- a/webserver/controllers/clients.py +++ b/webserver/controllers/clients.py @@ -131,7 +131,9 @@ def create(): # Create client client = Client(firstname=datas['firstname'], lastname=datas['lastname'], phone=datas['phone'], address=datas['address'], zipcode=datas['zipcode'], city=datas['city'], country=country, mail=datas['mail'], password=datas['password']) - + db.session.flush() + client.create_order_address() + # Check birthdate if 'birthdate' in datas: try: diff --git a/webserver/controllers/entrepreneurs.py b/webserver/controllers/entrepreneurs.py index 0f6b539..093e7f9 100644 --- a/webserver/controllers/entrepreneurs.py +++ b/webserver/controllers/entrepreneurs.py @@ -131,7 +131,9 @@ def create(): # Create entrepreneur entrepreneur = Entrepreneur(firstname=datas['firstname'], lastname=datas['lastname'], phone=datas['phone'], address=datas['address'], zipcode=datas['zipcode'], city=datas['city'], country=country, mail=datas['mail'], password=datas['password']) - + db.session.flush() + entrepreneur.create_order_address() + # Check birthdate if 'birthdate' in datas: try: diff --git a/webserver/controllers/livreurs.py b/webserver/controllers/livreurs.py index 59464e5..75860e0 100644 --- a/webserver/controllers/livreurs.py +++ b/webserver/controllers/livreurs.py @@ -131,7 +131,9 @@ def create(): # Create livreur livreur = Livreur(firstname=datas['firstname'], lastname=datas['lastname'], phone=datas['phone'], address=datas['address'], zipcode=datas['zipcode'], city=datas['city'], country=country, mail=datas['mail'], password=datas['password']) - + db.session.flush() + livreur.create_order_address() + # Check birthdate if 'birthdate' in datas: try: diff --git a/webserver/controllers/restaurateurs.py b/webserver/controllers/restaurateurs.py index a804dbd..d3dd7b2 100644 --- a/webserver/controllers/restaurateurs.py +++ b/webserver/controllers/restaurateurs.py @@ -131,7 +131,9 @@ def create(): # Create restaurateur restaurateur = Restaurateur(firstname=datas['firstname'], lastname=datas['lastname'], phone=datas['phone'], address=datas['address'], zipcode=datas['zipcode'], city=datas['city'], country=country, mail=datas['mail'], password=datas['password']) - + db.session.flush() + restaurateur.create_order_address() + # Check birthdate if 'birthdate' in datas: try: diff --git a/webserver/models/order.py b/webserver/models/order.py index ce5494c..df4a559 100644 --- a/webserver/models/order.py +++ b/webserver/models/order.py @@ -33,7 +33,6 @@ def to_dict(self, lines_order=True, state=True): my_dict['state'] = self.state.to_dict() if self.state else None if lines_order: - print self.lines_order[0] my_dict['lines_orders'] = [lo.to_dict() for lo in self.lines_order] return my_dict \ No newline at end of file diff --git a/webserver/models/personne.py b/webserver/models/personne.py index d4a9b2f..05925e6 100644 --- a/webserver/models/personne.py +++ b/webserver/models/personne.py @@ -1,4 +1,4 @@ -from webserver.models import Base +from webserver.models import Base, Address from sqlalchemy import Column, Integer, String, ForeignKey, DateTime from sqlalchemy.orm import relationship @@ -21,7 +21,7 @@ class Personne(Base): mail = Column(String(100), nullable=False) password = Column(String(100), nullable=False) - order_addresses = relationship("Address", cascade="save-update, merge, delete") + addresses = relationship("Address", cascade="save-update, merge, delete") type = Column(String(20)) @@ -29,7 +29,13 @@ class Personne(Base): 'polymorphic_identity': 'personne', 'polymorphic_on': type } - + + def create_order_address(self): + address = Address(address=self.address, zipcode=self.zipcode, city=self.city, country_id=self.country_id, personne_id=self.id) + from webserver import db + db.session.add(address) + db.session.commit() + # Flask-Login integration def is_authenticated(self): return True