def delete(self, username): with lock.lock: user = AccountsModel.find_by_username(username) if user: user.delete_from_db() return {n.MSG: "User deleted successfully"}, 200 else: return {n.MSG: "User not found"}, 404
def put(self, username,account_id): data = request.get_json(force=True) account = AccountsModel.find_by_id(username, account_id) if account: try: account.account_name = data['account_name'] account.update_to_db() except: return {"message": "An error occurred updating the account."}, 500 return account.json(), 201
def post(self): parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument(n.USR, type=str, required=True, help="Username") parser.add_argument(n.PSW, type=str, required=True, help="Password") data = parser.parse_args() with lock.lock: user = AccountsModel.find_by_username(data[n.USR]) if user: return {n.MSG: "User already in database"}, 400 user = AccountsModel(data[n.USR]) user.hash_password(data[n.PSW]) user.save_to_db() return user.json(), 200
def post(self): parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument(n.USR, type=str, required=True, help="Username") parser.add_argument(n.PSW, type=str, required=True, help="Password") data = parser.parse_args() user = AccountsModel.find_by_username(data[n.USR]) if user: verification = user.check_password(data[n.PSW]) if verification: return {n.TKN: user.generate_auth_token().decode('utf-8')}, 200 else: return {n.MSG: "Invalid password"}, 400 else: return {n.MSG: "User not found"}, 404
def post(self, username): data = request.get_json(force=True) data['username'] = username data['_id'] = 0 if AccountsModel.find_by_name(username, data['account_name']): return {'message': "An account '{}' already exists.".format(account_name)}, 400 account = AccountsModel(data) try: account.insert_to_db() except: return {"message": "An error occurred creating the store."}, 500 return account.json(), 201
def post(self, username): user = AccountsModel.find_by_username(username) if user: parser = reqparse.RequestParser() parser.add_argument(n.IDEVNT, type=int, required=True, help="Id of the event") parser.add_argument(n.TCK, type=int, required=True, help="Number of tickets the user wants to buy") data = parser.parse_args() with lock.lock: if username == g.user.username: event = EventModel.find_by_id(data[n.IDEVNT]) if event: price = event.price*data[n.TCK] if user.available_money >= price: if event.total_available_tickets >= data[n.TCK]: try: user.pay(price) event.sell(data[n.TCK]) new_order = OrdersModel(data[n.IDEVNT], data[n.TCK]) user.orders.append(new_order) new_order.save_to_db() user.save_to_db() event.save_to_db() return new_order.json(), 201 except: return {n.MSG: "Error Description"}, 500 else: return {n.MSG: "The event does not have enough available ticket"}, 400 else: return {n.MSG: "The user does not have enough money"}, 400 else: return {n.MSG: "Event not found"}, 404 else: return {n.MSG: "Username error"}, 400 else: return {n.MSG: "Username not found"}, 404
new_artist2 = ArtistModel('Txarango', 'Spain', 'POP') db.session.add(new_artist2) new_artist3 = ArtistModel('Bad Gyal', 'Spain', 'TRAP') db.session.add(new_artist3) new_artist4 = ArtistModel('Billie Eilish', 'USA', 'INDIE') db.session.add(new_artist4) new_event1 = EventModel('Festival Cruilla 2020', 'Parc del Forum', 'Barcelona', '2020-07-03', 60, 15000) new_event1.artists.append(new_artist1) new_event1.artists.append(new_artist2) db.session.add(new_event1) new_event2 = EventModel('Canet Rock 2020', 'Canet de Mar Beach', 'Barcelona', '2020-07-05', 45, 35000) new_event2.artists.append(new_artist1) new_event2.artists.append(new_artist4) db.session.add(new_event2) new_user1 = AccountsModel(username='******') new_user1.hash_password('1234') db.session.add(new_user1) new_user1 = AccountsModel(username='******', is_admin=1) new_user1.hash_password('admin') db.session.add(new_user1) db.session.commit() db.session.close()
def get(self, username): account = AccountsModel.find_by_username(username) if account: return {n.ORDRS: account.orders}, 200 else: return {n.MSG: "Username not found"}, 404
def get(self, username): return {'accounts': list(map(lambda x: x, AccountsModel.all(username) ))} #Lambda way
def delete(self, username, account_id): account = AccountsModel.find_by_id(username, account_id) if account: account.delete_from_db() return {'message': 'Acount deleted'}
def get(self, username, account_id): account = AccountsModel.find_by_id(username, account_id) if account: return account.json() return {'message': 'account not found'}, 404
def get(self, username): user = AccountsModel.find_by_username(username) if user: return user.json(), 200 else: return {n.MSG: "User not found"}, 404
def get(self): return AccountsModel.return_all(), 200