def put(self): data = Booking.parser.parse_args() userid = data['userid'] bikeid = data['bikeid'] user = AccountsModel.find_by_id(userid) bike = MotosModel.find_by_id(bikeid) if user is None: return "User not found", 404 if bike is None: return "Bike not found", 404 try: admin_user = AccountsModel.find_by_username('admin') if admin_user: book = BookingModel.finalize_book(userid, bikeid) if book is None: return "No renting found", 404 MotosModel.change_status(bikeid) admin_user.availableMoney += book.price user.availableMoney -= book.price return {"finalized_rent": book.json()}, 201 return "Admin user not found", 404 except: return "Something went wrong", 500
def verify_password(token, password): user = AccountsModel.verify_auth_token(token) if user: g.user = user return user return None
def post(self): data = Booking.parser.parse_args() userid = data['userid'] bikeid = data['bikeid'] user = AccountsModel.find_by_id(userid) bike = MotosModel.find_by_id(bikeid) if user is None: return "User not found", 404 if bike is None: return "Bike not found", 404 moto_active = MotosModel.is_active(bikeid) try: if user.availableMoney > 5: if moto_active is True: new_rent = BookingModel(userid, bikeid, None, None, None) new_rent.startDate = datetime.now() MotosModel.change_status(bikeid) new_rent.save_to_db() return {"new_rent": new_rent.json()}, 201 return "Moto selected is not active", 400 return "Not money enough", 400 except: return "Something went wrong", 500
def put(self, id): parser = reqparse.RequestParser() parser.add_argument('firstname', type=str, required=True, help="This field cannot be left blank") parser.add_argument('surname', type=str, required=True, help="This field cannot be left blank") parser.add_argument('email', type=str, required=True, help="This field cannot be left blank") parser.add_argument('dni', type=str, required=True, help="This field cannot be left blank") parser.add_argument('dataEndDrivePermission', type=str, required=True, help="This field cannot be left blank") parser.add_argument('creditCard', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() account = AccountsModel.find_by_id(id) if account: modified_account = AccountsModel( data['firstname'], data['surname'], data['email'], account.username, data['dni'], data['dataEndDrivePermission'], data['creditCard'], account.type, account.latitude, account.longitude) if account.firstname == modified_account.firstname and account.surname == modified_account.surname and account.email == modified_account.email and account.dni == modified_account.dni and account.dataEndDrivePermission == modified_account.dataEndDrivePermission and account.creditCard == modified_account.creditCard: return { "Error": "User {} is up to date".format(account.username) }, 400 AccountsModel.modify_account(id, modified_account) return {"account": account.json()}, 200 return { "Error": "Account with identifier {} not found".format(id) }, 404
def delete(self, username): with lock.lock: acc = AccountsModel.find_by_username(username) if acc: acc.delete_from_db() return { 'message' : 'success'}, 201 else: return {'message': "Not in db"}, 404
def post(self): parser = reqparse.RequestParser( ) # create parameters parser from request+ # define al input parameters need and its type parser.add_argument('username', type=str, required=True, help="This field cannot be left blank") parser.add_argument('password', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() user = AccountsModel.query.filter_by(username=data['username']).first() if user: return {'message': 'Username already exists'}, 404 new_user = AccountsModel(data['username']) token = new_user.generate_auth_token() new_user.hash_password(data['password']) new_user.save_to_db() return {'token': token.decode('ascii')}, 200
def get(self, username): user = AccountsModel.find_by_username(username) if user: return user.json(), 200 else: return { 'message': 'There is no client with username [{}] .'.format(username) }, 404
def post(self, username): data = self.parser() with lock.lock: acc = AccountsModel.find_by_username(username) if acc: return {'message': "username with id [{}] already exists".format(id)}, 404 else: available_money = data['available_money'] if data["available_money"] else AVAILABLE_MONEY is_admin = data['is_admin'] if data["is_admin"] else IS_ADMIN acc = AccountsModel(username, available_money=available_money, is_admin=is_admin) acc.hash_password(data['password']) acc.save_to_db() return {'message' : acc.json()}, 201 return {'account': acc.json()}, 201
def post(self): data = self.parser() with lock.lock: acc = AccountsModel.find_by_username(data['username']) if not acc: return {'message': 'username not in db'}, 404 else: token = acc.generate_auth_token() if acc.verify_password(data["password"]): return {'token': token.decode('ascii')}, 200 else: return {'message': "password invalid"}, 400
def post(self): parser = reqparse.RequestParser() parser.add_argument('username', type=str, required=True, help="This field cannot be left blank") parser.add_argument('password', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() user = AccountsModel.find_by_username(data['username']) if user: if user.verify_password(data['password']): token = user.generate_auth_token() return {'token': token.decode('ascii')}, 200 else: return {"message": "Password not correct"}, 400 else: return {"message": "User not found"}, 404
def post(self, username): data = self.parser() with lock.lock: if username == g.user.username: a = AccountsModel.find_by_username(username) e = EventModel.find_by_id(data['event_id']) if e.total_available_tickets >= data["tickets_bought"]: if a.available_money >= e.price * data["tickets_bought"]: e.total_available_tickets = EventModel.total_available_tickets - data[ "tickets_bought"] a.available_money -= data["tickets_bought"] * e.price o = OrdersModel(e.id, data["tickets_bought"]) a.orders.append(o) db.session.commit() return {"order": o.json()}, 201 else: return {'message': 'Not enough money'}, 501 else: return {'message': 'No enough tickets'}, 502 else: return { 'message': 'Endpoint username and g.user.username not equal' }, 400
def delete(self, username): user = AccountsModel.find_by_username(username) if not user: return {"message": "User not found"}, 404 user.delete_from_db() return {'message': "User deleted"}, 200
def get(self, username): with lock.lock: orders = AccountsModel.find_by_username(username).orders return { 'orders': list(map(lambda x: x.json(), orders)) }, 200 if orders else 404
def get(self, username): with lock.lock: account = AccountsModel.find_by_username(username) return {'account': account.json()}, 200 if account else 404
def get(self): with lock.lock: accounts = AccountsModel.find_all() return { 'accounts': list(map(lambda x: x.json(), accounts)) }, 200 if accounts else 404
def find_by_username(cls, username): return AccountsModel.find_by_username(username)
def post(self): parser = reqparse.RequestParser() parser.add_argument('firstname', type=str, required=True, help="This field cannot be left blank") parser.add_argument('surname', type=str, required=True, help="This field cannot be left blank") parser.add_argument('email', type=str, required=True, help="This field cannot be left blank") parser.add_argument('username', type=str, required=True, help="This field cannot be left blank") parser.add_argument('password', type=str, required=True, help="This field cannot be left blank") parser.add_argument('dni', type=str, required=True, help="This field cannot be left blank") parser.add_argument('dataEndDrivePermission', type=str, required=True, help="This field cannot be left blank") #parser.add_argument('status', type=str, required=True, help="This field cannot be left blank") parser.add_argument('creditCard', type=str, required=True, help="This field cannot be left blank") #######parser.add_argument('availableMoney', type=int, required=True, help="This field cannot be left blank") parser.add_argument('type', type=int, required=True, help="This field cannot be left blank") parser.add_argument('latitude', type=float, required=True, help="This field cannot be left blank") parser.add_argument('longitude', type=float, required=True, help="This field cannot be left blank") data = parser.parse_args() user = AccountsModel.find_by_username(data['username']) if user: return {"message": "User already exists"}, 400 else: new_user = AccountsModel(data['firstname'], data['surname'], data['email'], data['username'], data['dni'], data['dataEndDrivePermission'], data['creditCard'], data['type'], data['latitude'], data['longitude']) new_user.hash_password(data['password']) try: new_user.save_to_db() return new_user.json(), 200 except Exception as e: return {"message": "Database error"}, 500