def save_transaction(cls, user_id, isbns, prices, quantities): transactions = [] email_trans = [] for isbn, price, quantity in zip(isbns, prices, quantities): book = BooksModel.find_by_isbn(isbn) cls.check_stock(book, quantity) transaction = TransactionsModel(user_id, isbn, price, quantity) transactions.append(transaction.json()) email_trans.append(transaction.email_text()) db.session.add(transaction) user = UsersModel.find_by_id(user_id) book_library = LibraryModel.find_by_id_and_isbn( user.id, transaction.isbn) if book_library: # if the book was already in library if book_library.library_type == LibraryType.WishList: # if it was in the wish list book_library.library_type = LibraryType.Bought # change it to bought book_library.state = State.Pending else: # if it wasnt in the library, enter it entry = LibraryModel(book.isbn, user.id, LibraryType.Bought, State.Pending) db.session.add(entry) cls.it_transaction += 1 db.session.commit() cls.send_email(user_id, email_trans) return transactions
def put(self, key): data = parse_data(True) del data["email"] check_constraints_user(data) with lock: recovery = PasswordRecoveryModel.find_by_key(key) if recovery is None: return { "message": f"Password Recovery with ['key':{key}] is invalid." }, 403 if recovery.has_time_expired(): return {"message": "Password Recovery time has expired."}, 403 user = UsersModel.find_by_id(recovery.user_id) if user is None or not user.state: return { "message": "User doesn't exist or has deleted the account." }, 404 try: user.update_password_from_db(data['new_password']) recovery.delete_from_db() except Exception as e: return {"message": str(e)}, 500 return {"user": user.json()}, 200
def send_confirmation_mail(self): recipient = UsersModel.find_by_id(self.user_id).email quantity = str(self.quantity) isbn = str(self.isbn) subject = 'Order confirmation' message = 'Has comprat ' + quantity + ' llibre/s amb isbn ' + isbn send_email(recipient, subject, message)
def get(self, id_transaction): with lock: transaction = TransactionsModel.find_by_id(id_transaction) if not transaction: return {"message": f"Transaction with ['id_transaction':{id_transaction}] not found"}, 404 if UsersModel.find_by_id(transaction.user_id) != g.user: return {"message": "Invalid transaction, can only be yours"}, 401 return {"transaction": transaction.json()}, 200
def json(self): """ Returns a dictionary with pairs of string of name of attribute and it's value. """ _ignore = self.isbn # Forces execution to parse properly the class, fixing the bug of transient data atr = self.__dict__.copy() user = UsersModel.find_by_id(self.user_id) atr['username'] = user.username if user.state else None del atr["_sa_instance_state"] return atr
def post(self, key): with lock: verify = VerifyModel.find_by_key(key) if verify is None: return {"message": f"Verify email with ['key':{key}] is invalid"}, 404 if verify.has_time_expired(): return {"message": f"Password Recovery time has expired."}, 403 user = UsersModel.find_by_id(verify.user_id) user.confirmed_email.confirmed_email = True return {"user": user.json()}, 200
def save_to_db(self): if self.score < 1 or self.score > 5: raise ValueError( "Invalid value for score attribute: Value must be an integer from 1 to 5, both included." ) if ReviewsModel.find_by_isbn_user_id(self.isbn, self.user_id) is not None: raise Exception( f"Given user already posted a review. Did you meant to update it?" ) if UsersModel.find_by_id(self.user_id) is None: raise Exception("User with given id doesn't exist") if BooksModel.find_by_isbn(self.isbn) is None: raise Exception("Book with given isbn doesn't exist") ScoresModel.add_review(self) db.session.add(self) db.session.commit()
def put(self, user_id, isbn): data = parse_review(False) with lock: user = UsersModel.find_by_id(user_id) check_user_and_book(user, isbn) review = ReviewsModel.find_by_isbn_user_id(isbn, user_id) if review is None: return { "message": "Given user hasn't posted a review yet. Did you meant to post it?" }, 404 try: review.update_from_db(data) except Exception as e: return {"message": str(e)}, 500 return {"review": review.json()}, 200
def delete(self, user_id, isbn): with lock: user = UsersModel.find_by_id(user_id) check_user_and_book(user, isbn, True) review = ReviewsModel.find_by_isbn_user_id(isbn, user_id) if review is None: return { "message": "Given user hasn't posted a review yet. Did you meant to post it?" }, 404 try: review.delete_from_db() except Exception as e: return {"message": str(e)}, 500 return { "message": f"Review with ['user_id': {user_id}, 'isbn': {isbn}] deleted" }, 200
def send_email(cls, user_id, transactions): recipient = UsersModel.find_by_id(user_id).email msg = "Has comprat els seguents llibres:\n - " + ",\n - ".join( transactions) send_email(recipient, 'Confirmacio del correu', msg)