Skip to content

Commit

Permalink
Add relationships to models
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimond committed Oct 26, 2019
1 parent 41338c1 commit 9e091be
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 16 deletions.
3 changes: 2 additions & 1 deletion app/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@


def session_middleware(func):
@wraps(func)
# @wraps(func)
def decorator(*args, **kwargs):
request_token = request.headers.get('Authorization')
if request_token is None:
abort(401)

request_token = request_token[7:]
user = Shipper.query.filter_by(token=request_token).first()
if user:
g.user = user
Expand Down
30 changes: 21 additions & 9 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True)

# stats
cost = db.Column(db.Integer)
weight = db.Column(db.Integer)
dimension = db.Column(db.String(50))

# relationships
# carrier =
# shipper =

# , index=True, unique=True
shipper_id = db.Column(db.Integer, db.ForeignKey('shipper.id'))
carrier_id = db.Column(db.Integer, db.ForeignKey('carrier.id'))
secret = db.Column(db.String(50), unique=True)

def __repr__(self):
return f'Order {self.cost} {self.weight}'

class Shipper(db.Model):
id = db.Column(db.Integer, primary_key=True)
Expand All @@ -23,10 +22,17 @@ class Shipper(db.Model):
password = db.Column(db.String(50))
token = db.Column(db.String(50), unique=True)

balance = db.Column(db.Integer)

address = db.Column(db.String(100))

orders = db.relationship('Order', backref='shipper', lazy=True)
def get_role(self):
return 'SHIPPER'

def __repr__(self):
return f'{self.username} {self.password} {self.id}'
return f'Shipper {self.username} {self.password} {self.id}'


class Carrier(db.Model):
id = db.Column(db.Integer, primary_key=True)
Expand All @@ -35,10 +41,16 @@ class Carrier(db.Model):
password = db.Column(db.String(50))
token = db.Column(db.String(50), unique=True)

deposit = db.Column(db.Integer)
balance = db.Column(db.Integer)
locked = db.Column(db.Integer)

address = db.Column(db.String(100))
vehicle = db.Column(db.String(100))
max_load = db.Column(db.Integer)

orders = db.relationship('Order', backref='carrier', lazy=True)
def get_role(self):
return 'CARRIER'

def __repr__(self):
return f'{self.username} {self.password} {self.id}'
return f'Carrier {self.username} {self.password} {self.id}'
76 changes: 70 additions & 6 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from uuid import uuid4


@app.route('/api/user/register', methods=['POST'])
@app.route('/api/register', methods=['POST'])
def register():
request_body = request.json
token = str(uuid4())
Expand All @@ -30,32 +30,46 @@ def register():
not Shipper.query.filter_by(username=username).first()
and not Carrier.query.filter_by(username=username).first()
):
if role == 'Shipper':
u = None
if role == 'SHIPPER':
u = Shipper(
username=username,
password=password,
token=token,
address=address,
balance=0,
)
elif role == 'Carrier':
elif role == 'CARRIER':
vehicle = request_body.get('vehicle')
max_load = request_body.get('maxLoad')

if not vehicle:
return jsonify({'error': 'No role provided'})
if not max_load:
return jsonify({'error': 'No role provided'})

u = Carrier(
username=username,
password=password,
token=token,
address=address,
balance=0,
locked=0,
vehicle=vehicle,
max_load=max_load,
)

try:
db.session.add(u)
db.session.commit()
except Exception as e: # TODO: change
except Exception as e:
return jsonify({'error': e})
return jsonify({'token': token})
else:
return jsonify({'error': 'Already registrated'})


@app.route('/api/user/login', methods=['POST'])
@app.route('/api/login', methods=['POST'])
def login():
request_body = request.json
username = request_body['username']
Expand All @@ -73,10 +87,60 @@ def login():
return jsonify({'token': user.token})


@app.route('/api/user')
@session_middleware
def user_info():
user = g.user
role = user.get_role()

if role == 'SHIPPER':
return {
'role': role,
'username': user.username,
'balance': user.balance,
'location': user.address,
}
elif role == 'CARRIER':
return {
'role': role,
'username': user.username,
'balance': user.balance,
'location': user.address,
'totalBalance': user.balance,
'lockedBalance': user.locked,
'availableBalance': user.balance - user.locked,
'smartContract': '0xDJKJKDr6yDS7aDdfghjk234567sdfghj',
'vehicle': user.vehicle,
'maxLoad': user.max_load,
}


@app.route('/api/debug/increase-balance', methods=['POST'])
def balance_increase():
request_body = request.json
username = request_body['username']
amount = request_body['amount']

user = Shipper.query.filter_by(username=username).first()
if not user:
user = Carrier.query.filter_by(username=username).first()
if not user:
return jsonify({'error': 'No such user'})

user.balance += amount
try:
db.session.add(u)
db.session.commit()
except Exception as e: # TODO: change
return jsonify({'error': e})
return jsonify({'status': 200})


@app.route('/api/debug/carriers')
def debug_carriers():
return jsonify([str(c) for c in Carrier.query.all()])


@app.route('/api/debug/shippers')
def debug_shippers():
return jsonify([str(s) for s in Shipper.query.all()])
return jsonify([str(s) for s in Shipper.query.all()])

0 comments on commit 9e091be

Please sign in to comment.