Skip to content

Commit

Permalink
add address order
Browse files Browse the repository at this point in the history
  • Loading branch information
william57m committed Nov 6, 2014
1 parent 945a73c commit b33f053
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 10 deletions.
17 changes: 16 additions & 1 deletion manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
26 changes: 25 additions & 1 deletion webserver/controllers/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/<int:id>', methods=['GET', 'OPTIONS'])
def index(id):
""" Return one address by id.
Method: *GET*
URI: */address/id*
URI: */addresses/id*
"""

# Query
Expand Down
4 changes: 3 additions & 1 deletion webserver/controllers/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion webserver/controllers/entrepreneurs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion webserver/controllers/livreurs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion webserver/controllers/restaurateurs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion webserver/models/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions webserver/models/personne.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -21,15 +21,21 @@ 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))

__mapper_args__ = {
'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
Expand Down

0 comments on commit b33f053

Please sign in to comment.