def seed_db(): from models.Book import Book from models.User import User from main import bcrypt from faker import Faker import random faker = Faker() users = [] for i in range(5): user = User() user.email = f"test{i}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() for i in range(20): book = Book() book.title = faker.catch_phrase() book.user_id = random.choice(users).id db.session.add(book) db.session.commit() print("Tables seeded")
def seed_db(): from models.Book import Book # Import the book model from models.User import User # User model from main import bcrypt # Hashing module from faker import Faker # Import the faker module import random faker = Faker() # Create an instance of faker users = [] # Initializing an empty list # Creating 5 users and appending them to the users list for i in range(5): user = User() user.email = f"test{i}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() for i in range(20): # 20 book = Book() # New instance of book book.title = faker.catch_phrase() # Add a title book.user_id = random.choice( users).id # Choosing a random user to assign the book to db.session.add(book) # add the book to the db session db.session.commit() # Commit all the books to the db print("Tables seeded")
def to_object(self, json_obj: dict): book = Book() book.title = json_obj['title'] book.description = json_obj['description'] book.cover_url = json_obj['coverUrl'] book.author_id = json_obj['author_id'] return book
def book_create(): # Create a new book book_fields = books_schema.load(request.json) new_book = Book() new_book.title = book_fields['title'] db.session.add(new_book) db.session.commit() return jsonify(books_schema.dump(new_book))
def book_create(user=None): # Create a new book data = book_schema.load(request.json) new_book = Book() new_book.title = data["title"] user.books.append(new_book) db.session.commit() return jsonify(books_schema.dump(Book.query.all()))
def map_to_model(entity: dict): model = Book() model.id = str(entity['_id']) model.title = entity['title'] model.description = entity['description'] model.cover_url = entity['coverUrl'] model.author_id = entity['author_id'] model.updated = entity['updated'] model.created = entity['created'] return model
def book_create(user): #Create a new book book_fields = book_schema.load(request.json) new_book = Book() new_book.title = book_fields["title"] user.books.append(new_book) db.session.commit() return jsonify(book_schema.dump(new_book))
def seed_db(): from models.Book import Book from faker import Faker faker = Faker() for i in range(20): book = Book() book.title = faker.catch_phrase() db.session.add(book) db.session.commit() print("Tables seeded!")
def seed_db(): from models.Book import Book from faker import Faker faker = Faker() for i in range(10): book = Book() book.title = faker.catch_phrase() db.session.add(book) print(f"{i+1} book record(s) created") db.session.commit() print("Tables seeded")
def book_create(user=None): # Define the create function. user=none to use the decorator book_fieds = book_schema.load(request.json) # Deserializing the json into something that can be used # user_id = get_jwt_identity() # Get identity returns the userid from the JWT # user = User.query.get(user_id) # Return the user from querying the DB with the DB # if not user: # If no user then return the id # return abort(401, description="Invalid user") new_book = Book() # Creating a new instance of book new_book.title = book_fieds["title"] # Update the title user.books.append(new_book) # Add this book to the the user who created it db.session.commit() # Commit the transaction return jsonify(book_schema.dump(new_book)) # Return the json format of the book
def book_create(): #Create a new book book_fields = book_schema.load(request.json) if "title" not in book_fields.keys(): return abort(400) new_book = Book() new_book.title = book_fields["title"] db.session.add(new_book) db.session.commit() return jsonify(book_schema.dump(new_book))
def book_create(): #Create a new book book_fields = book_schema.load(request.json) user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") new_book = Book() new_book.title = book_fields["title"] user.books.append(new_book) db.session.commit() return jsonify(book_schema.dump(new_book))
def seed_db(): from models.Book import Book from models.User import User from models.Page import Page from main import bcrypt from faker import Faker import random faker = Faker() users = [] for i in range(5): user = User() user.email = f"test{i}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() for i in range(20): book = Book() book.title = faker.catch_phrase() book.user_id = random.choice(users).id book.author = faker.name() db.session.add(book) db.session.commit() for i in range(100): page = Page() page.page_content = faker.text(1500) page.page_number = random.randint(1, 300) page.book_id = random.randint(1, 20) db.session.add(page) db.session.commit() print("Tables seeded")