def post(self, id_account, id_book): acc = AccountModel.find_by_id(id_account) if not acc: return { 'message': "Account with ['id': {}] not found".format(id_account) }, 404 book = BookModel.find_by_id(id_book) if not book: return { 'message': "Book with ['id': {}] not found".format(id_book) }, 404 wl = WishlistModel.find_by_account(id_account) if book in wl.books: return { 'message': "Book with ['id': {}] is already is this wish list".format( id_book) }, 400 wl.books.append(book) wl.save_to_db() return { 'message': "Book with ['id': {}] added to wish list".format(id_book), 'wl': wl.json() }, 200
def put(self, idd): account = AccountModel.find_by_id(idd) if account: if g.user != account: return { "error: ": "You cannot modify an account which you are not log with" }, 401 parser = reqparse.RequestParser() # define the input parameters need and its type parser.add_argument('old_password', type=str, required=True, help="This field cannot be left blank") parser.add_argument('new_password', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() if account.verify_password(data["old_password"]): account.hash_password(data['new_password']) account.save_to_db() return {"message": "Password changed correctly"}, 200 return {'message': "Incorrect password"}, 406
def post(self, ): parser = reqparse.RequestParser() # define the input parameters need and its type parser.add_argument('account_id', type=int, required=True, help="This field cannot be left blank") parser.add_argument('order_id', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() account_id, order_id = data['account_id'], data['order_id'] account = AccountModel.find_by_id(account_id) order = OrdersModel.find_by_id(order_id) if account and order: try: MailSender.send_ticket_order_mail(account.email, account.name, order) return {"message": "Email sended correctly"}, 200 except (FileNotFoundError, smtplib.SMTPException) as e: print(e) return {'message': 'Something went wrong'}, 500 else: return {"message": "Account or Order not found"}, 404
def put(self, idd): data = self.__parse_request__() review = ReviewModel.find_by_id(idd) if not review: return { 'message': "There is no review with ['id': {}]".format(idd) }, 404 review.delete_from_db() user = AccountModel.find_by_id(data.get('user_id')) book = BookModel.find_by_id(data.get('book_id')) if not user or not book: if not user: return { 'message': "There is no account with ['id': {}]".format( data.get('user_id')) }, 404 if not book: return { 'message': "There is no book with ['id': {}]".format( data.get('book_id')) }, 404 new_review = ReviewModel(data.get('title'), data.get('user_id'), data.get('book_id'), data.get('date'), data.get('valuation'), data.get('comment')) new_review.save_to_db() user.reviews.append(new_review) book.reviews.append(new_review) return new_review.json(), 200
def get(self, user_id): user = AccountModel.find_by_id(user_id) if not user: return { 'message': "User with ['id': {}] not found".format(user_id) }, 404 return { 'reviews': [i.json()['review'] for i in ReviewModel.find_by_user(user_id)] }, 200
def post(self, idd): parser = reqparse.RequestParser( ) # create parameters parser from request parser.add_argument('date', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('total', type=float, required=True, help="This field cannot be left blanck") parser.add_argument('shipping', type=float, required=True, help="This field cannot be left blanck") parser.add_argument('taxes', type=float, required=True, help="This field cannot be left blanck") parser.add_argument('state', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('send_type', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('card_id', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('address_id', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() acc = AccountModel.find_by_id(idd) if not acc: return {'message': "There isn't a user with this id"}, 409 new_order = OrdersModel(idd, data.date, data.total, data.shipping, data.taxes, data.state, data.send_type, data.card_id, data.address_id) acc.orders.append(new_order) db.session.add(new_order) db.session.commit() return new_order.id, 200
def get(self, account_id, idd): account = AccountModel.find_by_id(account_id) card = CardModel.find_by_id(idd) if card is not None and account is not None: if card in account.cards: return {'card': card.json()}, 200 return { 'message': "This account doesn't have a card with id [{}] ".format(idd) }, 409 elif card is None: return {'message': "Card with id [{}] Not found".format(idd)}, 404 return {'message': "Account with id [{}] Not found".format(idd)}, 404
def get(self, account_id, idd): account = AccountModel.find_by_id(account_id) address = AddressModel.find_by_id(idd) if address is not None and account is not None: if address in account.addresses: return {'address': address.json()}, 200 return { 'message': "This account doesn't have an address with id [{}] ".format( idd) }, 409 return {'message': "Address with id [{}] Not found".format(idd)}, 404
def json(self): account = AccountModel.find_by_id(self.user_id) return { "review": { "name": "" + account.name + " " + account.lastname[0] + ".", "id": self.id, "title": self.title, "user_id": self.user_id, "book_id": self.book_id, "date": self.date, "valuation": self.valuation, "comment": self.comment } }
def delete(self, account_id, idd): account = AccountModel.find_by_id(account_id) card = CardModel.find_by_id(idd) if card is not None and account is not None: if card in account.cards: card.delete_from_db() return {"Message": "Card deleted correctly"}, 200 return { 'message': "This account doesn't have an card with id [{}] ".format(idd) }, 409 elif card is None: return {'message': "Card with id [{}] Not found".format(idd)}, 404 return {'message': "Account with id [{}] Not found".format(idd)}, 404
def delete(self, idd): exists = ReviewModel.find_by_id(idd) if not exists: return { 'message': "There is no review with ['id': {}], therefore it cannot be deleted" .format(idd) }, 404 user = AccountModel.find_by_id(exists.user_id) book = BookModel.find_by_id(exists.book_id) exists.delete_from_db() return { 'message': "Review with ['id': {}] has successfully been deleted".format(idd) }, 200
def delete(self, account_id, idd): account = AccountModel.find_by_id(account_id) address = AddressModel.find_by_id(idd) if address is not None and account is not None: if address in account.addresses: address.delete_from_db() return {"Message": "Address deleted correctly"}, 200 return { 'message': "This account doesn't have an address with id [{}] ".format(id) }, 409 elif address is None: return { 'message': "Address with id [{}] Not found".format(id) }, 404
def delete(self, idd): account = AccountModel.find_by_id(idd) if not account: return { 'message': 'Account with id [{}] not found'.format(idd) }, 404 tmp = [add.delete_from_db() for add in account.addresses] tmp = [rev.delete_from_db() for rev in account.reviews] tmp = [c.delete_from_db() for c in account.cards] tmp = [o.delete_from_db() for o in account.orders] account.delete_from_db() return { 'message': 'Account with id[{}] deleted correctly'.format(idd) }, 200
def post(self, account_id, idd=None): parser = reqparse.RequestParser() account = AccountModel.find_by_id(account_id) if account is None: return { 'message': "Account with id [{}] Not found".format(account_id) }, 404 if len(account.cards) == 2: return { 'message': "Account with id [{}] cannot have more cards".format( account_id) }, 403 # define the input parameters need and its type parser.add_argument('card_owner', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('number', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('date', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('payment_method', type=str, required=True, help="This field cannot be left blanck") data = parser.parse_args() card = CardModel(data['card_owner'], data['number'], data['date'], data['payment_method']) account.cards.append(card) account.save_to_db() return {"Message": "Card saved correctly"}, 200
def put(self, idd): account = AccountModel.find_by_id(idd) if account: if g.user != account: return { "error: ": "You cannot modify an account which you are not log with" }, 401 else: return {"error: ": "Account not found"}, 404 parser = reqparse.RequestParser() # define the input parameters need and its type parser.add_argument('name', type=str, required=True, help="This field cannot be left blank") parser.add_argument('lastname', 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") data = parser.parse_args() if AccountModel.find_by_email( data['email']) and account.email != data['email']: return { "message": "Account already registered for that email address" }, 409 account.name, account.lastname, account.email = data['name'], data[ 'lastname'], data['email'] account.save_to_db() return {"token": account.generate_auth_token().decode('ascii')}, 200
def delete(self, id_account, id_book): acc = AccountModel.find_by_id(id_account) if not acc: return { 'message': "Account with ['id': {}] not found".format(id_account) }, 404 book = BookModel.find_by_id(id_book) if not book: return { 'message': "Book with ['id': {}] not found".format(id_book) }, 404 wl = WishlistModel.find_by_account(id_account) if book not in wl.books: return { 'message': "Book with ['id': {}] is not in the wish list".format(id_book) }, 400 wl.books.remove(book) wl.save_to_db() return { 'message': "Book with ['id': {}] removed from wish list".format(id_book) }, 200
def put(self, account_id, idd): account = AccountModel.find_by_id(account_id) address = AddressModel.find_by_id(idd) if address is not None and account is not None: if address not in account.addresses: return { 'message': "This account doesn't have an address with id [{}] ". format(idd) }, 409 elif address is None: return { 'message': "Address with id [{}] Not found".format(idd) }, 404 parser = reqparse.RequestParser() # define the input parameters need and its type parser.add_argument('label_name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('surnames', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('street', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('number', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('cp', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('city', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('province', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('telf', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() address.label_name = data['label_name'] address.name = data['name'] address.surnames = data['surnames'] address.street = data['street'] address.number = data['number'] address.cp = data['cp'] address.city = data['city'] address.province = data['province'] address.telf = data['telf'] address.save_to_db() return {"Message": "Address saved correctly"}, 200
def post(self, account_id, idd=None): parser = reqparse.RequestParser() account = AccountModel.find_by_id(account_id) if account is None: return { 'message': "Account with id [{}] Not found".format(account_id) }, 404 if len(account.addresses) == 3: return { 'message': "Account with id [{}] cannot have more addresses".format( account_id) }, 403 # define the input parameters need and its type parser.add_argument('label_name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('surnames', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('street', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('number', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('cp', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('city', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('province', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('telf', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() address = AddressModel(data['label_name'], data['name'], data['surnames'], data['street'], data['number'], data['cp'], data['city'], data['province'], data['telf']) account.addresses.append(address) account.save_to_db() return {"Message": "Address saved correctly"}, 200
def get(self, account_id): account = AccountModel.find_by_id(account_id) addresses = [] for a in account.addresses: addresses.append(a.json()) return {"accounts_addresses": addresses}, 200
def get(self, account_id): account = AccountModel.find_by_id(account_id) cards = [] for a in account.cards: cards.append(a.json()) return {"accounts_cards": cards}, 200
def test_model_find_non_existent_account(self): acc = AccountModel.find_by_id(92) self.assertEqual(None, acc)
def post(self, idd, id_sub, address_id=None): order = OrdersModel.find_by_id(idd) account = AccountModel.find_by_id(id_sub) if account is None: return { 'message': "Account with id [{}] Not found".format(id_sub) }, 404 if order is None: return { "message": "Order with id [{}] not found ".format(idd) }, 404 # Si no pasamos address id por parametro pedimos los parametros para crear una nueva direccion address = None if address_id is None: if len(account.addresses) == 3: return { 'message': "Account with id [{}] cannot have more addresses".format( id_sub) }, 404 parser = reqparse.RequestParser() # define the input parameters need and its type parser.add_argument('label_name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('surnames', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('street', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('number', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('cp', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('city', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('province', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('telf', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() address = AddressModel(data['label_name'], data['name'], data['surnames'], data['street'], data['number'], data['cp'], data['city'], data['province'], data['telf']) account.addresses.append(address) # Si pasamos la id de la direccion la buscamos en la cuenta else: address = account.find_address_by_id(address_id) if address is not None: order.add_address(address) order.save_to_db() return {'message': "OK"}, 200 return { 'message': "Address with id [{}] Not found".format(address_id) }, 404
def get(self, idd): account = AccountModel.find_by_id(idd) if account: return {"account": account.json()}, 200 return {"error: ": "Account not found"}, 404