Skip to content

Commit

Permalink
add restaurant_id and address_id in Address model
Browse files Browse the repository at this point in the history
  • Loading branch information
william57m committed Nov 8, 2014
1 parent 4600482 commit b14307e
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 13 deletions.
2 changes: 1 addition & 1 deletion webserver/controllers/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def create():
return make_response(gettext(u"Le pays n'existe pas."), 404)

# Create address
address = Address(address=datas['address'], zipcode=datas['zipcode'], city=datas['city'], country=country)
address = Address(address=datas['address'], zipcode=datas['zipcode'], city=datas['city'], country=country, personne_id=personne_id)

# Add address
db.session.add(address)
Expand Down
32 changes: 30 additions & 2 deletions webserver/controllers/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from flask.ext.babel import gettext
from webserver import db, app
from webserver.lib.base import jsonify
from webserver.models import Order, StateOrder, Client, LineOrder
from webserver.models import Address, Order, StateOrder, Client, LineOrder, Restaurant

from twilio.rest import TwilioRestClient
from webserver.config import TwilioConfig
Expand All @@ -28,6 +28,7 @@ def list():
URI: */orders*
Parameters: ?state=id
?client_id=id
?restaurant_id=id
0: En attente
1: En préparation
2: Prête
Expand All @@ -53,6 +54,9 @@ def list():
if 'client_id' in request.values:
query = query.filter(Order.client_id == request.values['client_id'])

if 'restaurant_id' in request.values:
query = query.filter(Order.restaurant_id == request.values['restaurant_id'])

orders = query.all()

# Build the response
Expand Down Expand Up @@ -107,6 +111,30 @@ def create():
except:
return make_response(gettext(u"Le format de la date est invalide."), 400)

# Check address
if 'address_id' not in datas:
return make_response(gettext(u"L'adresse de livraison est obligatoire."), 400)
try:
address_id = int(datas['address_id'])
except Exception: # pragma: no cover
return make_response(gettext(u"address_id doit être un identifiant."), 400)

address = db.session.query(Address).get(address_id)
if address is None:
return make_response(gettext(u"L'adresse n'existe pas."), 404)

# Check restaurant
if 'restaurant_id' not in datas:
return make_response(gettext(u"restaurant_id est obligatoire."), 400)
try:
restaurant_id = int(datas['restaurant_id'])
except Exception: # pragma: no cover
return make_response(gettext(u"restaurant_id doit être un identifiant."), 400)

restaurant = db.session.query(Restaurant).get(restaurant_id)
if restaurant is None:
return make_response(gettext(u"Le restaurant n'existe pas."), 404)

# Get client id from logged used
from flask.ext.login import current_user
if not hasattr(current_user, "id"):
Expand All @@ -120,7 +148,7 @@ def create():

# Create menu
# TODO: generate order number !
order = Order(number=1, date=date, client_id=current_user.id, state_id=state.id)
order = Order(number=1, date=date, client_id=current_user.id, state_id=state.id, address_id=address_id, restaurant_id=restaurant.id)

# Create line order
if 'dishes' not in datas:
Expand Down
14 changes: 13 additions & 1 deletion webserver/models/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ class Order(Base):
client_id = Column(Integer, ForeignKey('client.id'))
client = relationship("Client")

address_id = Column(Integer, ForeignKey('address.id'))
address = relationship("Address")

state_id = Column(Integer, ForeignKey('state_order.id'))
state = relationship("StateOrder")

restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
restaurant = relationship("Restaurant")

lines_order = relationship("LineOrder", cascade="save-update, merge, delete")

def to_dict(self, lines_order=True, state=True):
def to_dict(self, lines_order=True, state=True, address=True, restaurant=True):
my_dict = dict()

my_dict['id'] = self.id
Expand All @@ -31,6 +37,12 @@ def to_dict(self, lines_order=True, state=True):

if state:
my_dict['state'] = self.state.to_dict() if self.state else None

if address:
my_dict['address'] = self.address.to_dict() if self.address else None

if restaurant:
my_dict['restaurant'] = self.restaurant.to_dict() if self.restaurant else None

if lines_order:
my_dict['lines_orders'] = [lo.to_dict() for lo in self.lines_order]
Expand Down
4 changes: 2 additions & 2 deletions webserver/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ def delete_menus():


# Order
def build_order(id, number=1, date=None, client_id=None, state=None):
def build_order(id, number=1, date=None, client_id=None, restaurant_id=None, state=None):
""" Builder to create a menu in database """

date = datetime.datetime(2014, 4, 4) if date is None else date
state = StateOrder(name="En attente") if state is None else state
db.session.add(state)

order = Order(id=id, number=number, date=date, client_id=client_id, state=state)
order = Order(id=id, number=number, date=date, client_id=client_id, restaurant_id=restaurant_id, state=state)
db.session.add(order)

return order
Expand Down
26 changes: 23 additions & 3 deletions webserver/tests/functional/orders/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from webserver import db
from webserver.models import Order
from webserver.tests import build_order, build_state_order, build_client
from webserver.tests import delete_orders, delete_states_orders, delete_clients
from webserver.tests import build_order, build_state_order, build_client, build_restaurant
from webserver.tests import delete_orders, delete_states_orders, delete_clients, delete_restaurants
from webserver.tests.functional import FunctionalTest


Expand Down Expand Up @@ -100,6 +100,9 @@ def setup_class(cls):
build_order(id=5, state=so1, client_id=78)
build_order(id=6, state=so2, client_id=78)

build_restaurant(id=532)
build_order(id=15, state=so1, restaurant_id=532)

db.session.commit()

@classmethod
Expand All @@ -109,6 +112,7 @@ def teardown_class(cls):
delete_orders()
delete_states_orders()
delete_clients()
delete_restaurants()

db.session.commit()

Expand All @@ -121,7 +125,7 @@ def test_list(self):

# Check length
result = self.parse(response.data)
assert len(result) == 5
assert len(result) == 6

# Check id
result_id = [r['id'] for r in result]
Expand All @@ -130,6 +134,7 @@ def test_list(self):
assert 3 in result_id
assert 5 in result_id
assert 6 in result_id
assert 15 in result_id

def test_list_with_state(self):
""" GET /orders?state=1: list with state """
Expand All @@ -148,6 +153,21 @@ def test_list_with_state(self):
assert 3 in result_id
assert 6 in result_id

def test_list_with_restaurant(self):
""" GET /orders?restaurant_id=532: list with restaurant_id """

# Check request
response = self.get('/orders?restaurant_id=532')
assert response.status_code == 200

# Check length
result = self.parse(response.data)
assert len(result) == 1

# Check id
result_id = [r['id'] for r in result]
assert 15 in result_id

def test_list_with_client(self):
""" GET /orders?client_id=78: list with client_id """

Expand Down
Loading

0 comments on commit b14307e

Please sign in to comment.