コード例 #1
0
ファイル: routes.py プロジェクト: NNemanjaMM/WebLib
def books_add():
    form = BookCreateForm()
    if form.validate_on_submit():
        book = Book()
        book.inv_number = form.inv_number.data
        book.signature = form.signature.data
        book.title = form.title.data
        book.author = form.author.data
        db.session.add(book)
        db.session.flush()
        EventWriter.write(
            EventType.book_add, book.id,
            _g('Following book is added') + ' (' + _g('Book id') + ': ' +
            str(book.id) + '):' + book.log_data())
        db.session.commit()
        flash(_g('Book is successfully added') + '.', 'success')
        return redirect(url_for('books.bookss'))
    return render_template('books_cu.html', form=form, is_creating=True)
コード例 #2
0
ファイル: routes.py プロジェクト: NNemanjaMM/WebLib
def book_rent(member_id):
    member = Member.query.get_or_404(member_id)
    if member.is_membership_expired or member.number_of_rented_books >= MAX_RENTED_BOOKS:
        abort(405)
    message = None
    form = RentForm()
    if form.search.data and form.validate():
        book_inv = to_value = FieldValidator.convert_and_validate_number(
            form.inv_number)
        if book_inv:
            book = Book.query.filter(Book.inv_number == book_inv).first()
            form.inv_number.data = book_inv
            if book:
                form.signature.data = book.signature
                form.title.data = book.title
                form.author.data = book.author
                if book.is_rented:
                    message = _g('Book with this inventory number') + ' ' + _g(
                        'is already rented'
                    ) + '. ' + _g(
                        'Please check your inventory number and then continue'
                    ) + '.'
            else:
                form.signature.data = None
                form.title.data = None
                form.author.data = None
                message = _g('Book with this inventory number') + ' ' + _g(
                    'was not found')
    elif form.submit.data and form.validate():
        failed_1 = not FieldValidator.convert_and_validate_number(
            form.inv_number)
        failed_2 = not FieldValidator.validate_required_field(
            form, form.signature, [signature_cust, length_cust_max_15])
        failed_3 = not FieldValidator.validate_required_field(
            form, form.title, [string_cust, length_cust_max])
        failed_4 = not FieldValidator.validate_required_field(
            form, form.author, [string_cust, length_cust_max])
        date_value = FieldValidator.convert_and_validate_date(
            form.date_rented, False,
            (date.today() -
             timedelta(RENTAL_DATE_LIMIT)).strftime(DATE_FORMAT))
        if not (failed_1 or failed_2 or failed_3 or failed_4
                or date_value == None):
            book_id = None
            rented_book = None
            book_duplicate = Book.query.filter(
                Book.inv_number == form.inv_number.data).first()
            if book_duplicate and not book_duplicate.is_rented:
                book_id = book_duplicate.id
                book_duplicate.is_rented = True
                rented_book = book_duplicate
                flash(
                    _g('Book with this inventory number') + ' ' +
                    _g('already exists') + '.', 'info')
            else:
                new_book = Book()
                new_book.inv_number = form.inv_number.data
                new_book.signature = form.signature.data
                new_book.title = form.title.data
                new_book.author = form.author.data
                new_book.is_rented = True
                if book_duplicate and book_duplicate.is_rented:
                    book_duplicate.has_error = True
                    new_book.has_error = True
                    flash(
                        _g('Book with this inventory number') + ' ' +
                        _g('is already rented') + '! ' +
                        _g('An error flag is set to the books with same inventory number'
                           ) + '.', 'warning')
                db.session.add(new_book)
                db.session.flush()
                if new_book.has_error:
                    EventWriter.write(
                        EventType.book_error_add, new_book.id,
                        _g('An error is set to the following book') + ' (' +
                        _g('Book id') + ': ' + str(new_book.id) + '):' +
                        new_book.log_data())
                EventWriter.write(
                    EventType.book_add, new_book.id,
                    _g('Following book is added') + ' (' + _g('Book id') +
                    ': ' + str(new_book.id) + '):' + new_book.log_data())
                db.session.commit()
                book_id = new_book.id
                rented_book = new_book
                flash(_g('Book is successfully added') + '.', 'info')
            rental = Rental()
            rental.date_performed = date_value
            rental.date_deadline = date_value + timedelta(BOOK_RENT_PERIOD)
            rental.book_id = book_id
            rental.member_id = member_id
            db.session.add(rental)
            member.number_of_rented_books = db.session.query(
                func.count(Rental.id)).filter(
                    and_(Rental.member_id == member_id,
                         Rental.is_terminated == False)).scalar()
            member.total_books_rented = db.session.query(func.count(
                Rental.id)).filter(Rental.member_id == member_id).scalar()
            EventWriter.write(
                EventType.rent_rent, rented_book.id,
                _g('Following book') + ' (' + _g('Book id') + ': ' +
                str(rented_book.id) + '):' + rented_book.log_data() + '<br/>' +
                _g('Is rented to following member') + ' (' + _g('Member id') +
                ' ' + str(member.id) + '):' + member.log_data())
            db.session.commit()
            flash(_g('Book is successfully rented') + '.', 'info')
            return redirect(
                url_for('members.members_details', member_id=member_id))
    return render_template('renting.html', form=form, message=message)