Beispiel #1
0
def addAuthor(authorName, authorBirthDate, authorEducation, authorNationality,
              authorAlmaMater, authorWiki, authorImage, authorDesc, newBook):
    auth = session.query(Author).filter(Author.name == authorName).all()
    #print(authorName)
    if len(auth) == 0:
        auth = Author(name=authorName,
                      birthDate=authorBirthDate,
                      education=authorEducation,
                      nationality=authorNationality,
                      alma_mater=authorAlmaMater,
                      wikipedia=authorWiki,
                      image=authorImage,
                      description=authorDesc)
        auth.books = [newBook]
        return auth
    else:
        auth = auth[0]
        auth.books.append(newBook)
        return auth
Beispiel #2
0
def edit_author(id):
    '''Editing or adding author depending on given id.'''
    if id == 'new':
        author = Author()
    else:
        author = Author.query.filter_by(id=id).first_or_404()
    form = AuthorForm(request.form, obj=author)
    if request.method == 'GET':
        return render_template('edit_author.html', form=form)
    if request.method == 'POST' and form.validate():
        form.populate_obj(author)
        author.name = form.name.data
        author.books = form.books.data
        if id == 'new':
            db.session.add(author)
            flash('Author was successfully added')
        else:
            db.session.merge(author)
            flash('Author info was successfully updated')
        db.session.commit()
        return redirect(url_for('author', id=author.id))
    else:
        return render_template('edit_author.html', form=form)