def addRecord(data):
    """
    The incoming 'data' paramater should be a tuple of two dictionaries in the following 
    format:
    
    ("author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890", 
             "publisher":"Packt"}
    )
    """
    # Create an instance of the Book and Person classes and populate them with the values
    # collected from the 'Add' dialog and passed in as the tuple of dictionaries.
    book = Book()
    book.title = data['book']['title']
    book.isbn = data['book']['isbn']
    book.publisher = data['book']['publisher']

    author = Person()
    author.first_name = data['author']['first_name']
    author.last_name = data['author']['last_name']
    book.person = author

    # Create a session, connect to the database, commit and close the connection.
    session = connectToDatabase()
    session.add(book)
    session.commit()
    session.close
示例#2
0
def add_record(session, data):
    book = Book()
    book.title = data["book"]["title"]
    book.isbn = data["book"]["isbn"]
    book.publisher = data["book"]["publisher"]
    author = Person()
    author.first_name = data["author"]["first_name"]
    author.last_name = data["author"]["last_name"]
    book.person = author

    # try:
    session.add(book)
    session.commit()
def add_record(session, data):
    """
    Data should be a dictionary of two dictionaries in the following format:

    {"author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890",
             "publisher":"Packt"}
    }
    """
    book = Book()
    book.title = data["book"]["title"]
    book.isbn = data["book"]["isbn"]
    book.publisher = data["book"]["publisher"]
    author = Person()
    author.first_name = data["author"]["first_name"]
    author.last_name = data["author"]["last_name"]
    book.person = author

    session.add(book)
    session.commit()
示例#4
0
def addBook():
    global apiInfo

    orgBook = Book()

    form = EditBookForm()

    if request.method == 'GET':
        form.id.data = orgBook.id
        form.name.data = orgBook.name
        form.price.data = orgBook.price
        form.isbn.data = orgBook.isbn
        form.isObsolete.data = orgBook.isObsolete
        form.bookType.data = orgBook.bookType

    if request.method == 'POST' and form.validate(
    ):  # Equivalent to validate_on_submit()
        newBook = Book()
        newBook.name = request.form['name']
        newBook.isbn = request.form['isbn']
        newBook.price = request.form['price']
        newBook.isObsolete = form.isObsolete.data  # TODO (bug) request.form['<booelan>'] does not return
        newBook.bookType = request.form['bookType']

        # TODO (bug) Error when doing the api-call
        addedBook = json.loads(
            requests.post(app.config['API_ROOT_URL'] + '/books',
                          json=vars(newBook)).content)
        newBook.id = addedBook['id']

        flash('Added book {}'.format(vars(newBook)))
        return redirect('/books')

    return render_template('books/edit.html',
                           actionTitle='Add book',
                           appTitle=app.config['APP_TITLE'],
                           api=apiInfo,
                           book=vars(orgBook),
                           form=form)
示例#5
0
def addRecord(data):
    """
    Data should be a tuple of two dictionaries in the following format:
    
    ("author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890", 
             "publisher":"Packt"}
    )
    """
    book = Book()
    book.title = data["book"]["title"]
    book.isbn = data["book"]["isbn"]
    book.publisher = data["book"]["publisher"]
    author = Person()
    author.first_name = data["author"]["first_name"]
    author.last_name = data["author"]["last_name"]
    book.person = author
    
    # connect to session and commit data to database
    session = connectToDatabase()
    session.add(book)
    session.commit()
    session.close()