def createAuthor(self, **kwargs): default = { 'first_name': 'first_name_test-' + uuid.uuid4().hex, 'last_name': 'last_name_test-' + uuid.uuid4().hex, } default.update(kwargs) author = Author(**default) author.save() return author
def populate_author(N=2): fake_gen = Faker() for i in range(N): first_name = fake_gen.first_name() last_name = fake_gen.last_name() date_of_birth = fake_gen.date_time() author = Author(first_name=first_name, last_name=last_name, date_of_birth=date_of_birth) author.save()
def authorCreate(request): if request.method == 'POST': form = AuthorForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] date_of_birth = form.cleaned_data['date_of_birth'] date_of_death = form.cleaned_data['date_of_death'] author = Author(first_name=first_name, last_name=last_name, date_of_birth=date_of_birth, date_of_death=date_of_death) author.save() return redirect('/catalog/authors') else: form = AuthorForm() return render(request, 'catalog/author_form.html', {'form': form})
def newBook(): """ CREATE - (GET) displays the form needed to add a new book for a logged-in user (POST) creates a new book and its corresponding author from the data provided in the form, the user is bound by its id to the book he created """ if 'username' not in user_session: # in case the user hasn't signed in return render_template('error.html', header='You Can\'t Add a New Book !', message="""You need to log-in first before attempting to create a new book.""") if request.method == 'POST': category_id = get_category_id(request.form['category']) # Create a book book = Book(title=request.form['title'], category_id=category_id, publish_year=request.form['year'], link=request.form['link'], cover_url=request.form['coverUrl'], summary=request.form['summary'], isbn=request.form['isbn'], user_id=user_session['user_id'], slug=slugify(request.form['title'])) db.session.add(book) # flush() is used To get the id of the newly created book db.session.flush() # Create an author # Every book has an author, even if his f_name, l_name is blank author = Author(book_id=book.id, first_name=request.form['authorFName'], last_name=request.form['authorLName']) db.session.add(author) db.session.commit() # Give feedback to the user flash('The Book was Added Successfully !') # redirect tp the book's category page category = Category.query.filter_by(id=category_id).one() return redirect(url_for('showCategory', category_slug=category.slug)) else: # GET request, renders the template containing the form categories = Category.query.all() return render_template('new.html', categories=categories, user_session=user_session)
Genre(name="Mystery"), ] # Save the genres to the database for genre in genres: genre.save() # Create Authors authors = [] for i in range(1, 10): a_fname = fake.first_name() a_lname = fake.last_name() a_dob = fake.date_of_birth() a_dod = a_dob + timedelta(days=365 * fake.random_int(65, 100)) author = Author(first_name=a_fname, last_name=a_lname, date_of_birth=a_dob, date_of_death=a_dod) author.save() authors.append(author) # Create Books books = [] for i in range(1, 10): a_title = fake.text(50) a_author = authors[fake.random_int(0, len(authors)) - 1] a_summary = fake.text(1000) a_isbn = fake.isbn13() book = Book(title=a_title, author=a_author, summary=a_summary, isbn=a_isbn) book.save() book.genre.add(genres[fake.random_int(0, len(genres)) - 1]) book.save()
def setUpTestData(cls): # Set up non-modified objects used by all test methods au = Author(first_name='Big', last_name='Bob') au.save()