Exemplo n.º 1
0
    def post(self, email):
        data = parse_entry()
        with lock:
            user = check_user(email)

            if not BooksModel.find_by_isbn(data['isbn']):
                return {
                    "message": f"Book with ['isbn': {data['isbn']}] Not Found"
                }, 404

            if LibraryModel.find_by_id_and_isbn(user.id,
                                                data['isbn']) is not None:
                return {
                    "message":
                    f"Entry with ['email': {email}, 'isbn': {data['isbn']}] already exists"
                }, 409

            data['user_id'] = user.id
            try:
                entry = LibraryModel(**data)
                entry.save_to_db()
            except Exception as e:
                return {"message": str(e)}, 500

        return entry.json(), 201
Exemplo n.º 2
0
    def test_model_update(self):
        with self.app.app_context():
            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()
            book2 = BooksModel(2, 1, 1, "test2")
            book2.save_to_db()

            entry = LibraryModel(book.isbn, 1, LibraryType.Bought, State.Finished)
            entry.save_to_db()

            data = {"isbn": book2.isbn, "user_id": 2, "state": "Reading", "visible": True, "library_type": "WishList"}
            entry.update_from_db(data)

            expected_json = data
            expected_json["book"] = BooksModel.find_by_isbn(book2.isbn).json()
            del expected_json["isbn"]
            self.assertEqual(expected_json, entry.json())