Example #1
0
def create_book_review(ISBN):
    form = CreateReviewForm()
    book = Book.query.get(ISBN)

    if form.validate_on_submit():
        review_params = {
            'ISBN': book.ISBN,
            'username': current_user.get_id(),
            'score': form.score.data,
            'description': form.description.data,
            'date': time.strftime("%Y-%m-%d")
        }

        if Review.query.filter_by(username=current_user.get_id(),
                                  ISBN=book.ISBN).first() is None:
            review = Review(**review_params)

            if save(review):
                flash("Review created successfully!")

                return redirect(url_for('my.show_book', ISBN=book.ISBN))
            else:
                flash("Failed to create review. Please try again.")
        else:
            flash("You have already entered a review for this book.")

    return render_template('my/book/review/new.html', book=book, form=form)
Example #2
0
def create_feedback(ISBN, user):
    feedback_form = CreateFeedbackForm()
    top_review_form = SetNofTopReviewsForm()
    book = Book.query.get(ISBN)
    review = Review.query.filter_by(username=user, ISBN=ISBN).first()

    if feedback_form.validate_on_submit():
        feedback_params = {
            'customer_feedback': current_user.get_id(),
            'rating': feedback_form.rating.data,
            'customer_review': review.username,
            'ISBN': ISBN
        }
        if Feedback.query.filter_by(customer_feedback=current_user.get_id(),
                                    ISBN=book.ISBN,
                                    customer_review=user).first() is None:
            if current_user.get_id() != review.username:
                feedback = Feedback(**feedback_params)

                if save(feedback):
                    flash("Feedback created successfully!")

                    return redirect(url_for('my.show_book', ISBN=book.ISBN))
                else:
                    flash("Failed to create feedback. Please try again")
            else:
                flash("You may not create feedback for your own review")
        else:
            flash("You have already entered a feedback.")

    return render_template('my/book/show.html',
                           book=book,
                           review=review,
                           feedback_form=feedback_form,
                           top_review_form=top_review_form)
Example #3
0
def update_order(order_id):
    # Check add book to order form validity
    form = AddBookToOrderForm(formdata=request.form)
    isbn = form.data['isbn']
    book = Book.query.get(isbn)

    if form.validate_on_submit():
        quantity = form.data['quantity']

        order = Order.query.get(order_id)
        books_orders = order.books_orders.filter_by(book=book).first()

        if not books_orders:
            books_orders = BooksOrders(quantity=quantity)
            books_orders.book = book
            order.books_orders.append(books_orders)
        else:
            books_orders.quantity = quantity

        if save(order):
            flash(
                'Successfully updated number of copies of {} in your current order to {}!'
                .format(book.title, quantity))

            return redirect(
                url_for('my.book_index', ordered_book_isbn=book.ISBN))
        else:
            flash('Failed to add {} to your current order. Please try again'.
                  format(book.title))

    # Render index template if add book to order form was invalid
    filter_form = FilterBooksForm()

    if filter_form.validate_on_submit():
        search_dict = {k: v for k, v in filter_form.data.items() if v}
        books = Book.query
        for k, v in search_dict.items():
            if k == 'authors':
                authors = [author.strip() for author in v.split(', ')]
                books = books.filter(
                    cast(getattr(Book, k), ARRAY(db.Text())).contains(authors))
            else:
                books = books.filter(getattr(Book, k).ilike('%{}%'.format(v)))
        books = books.all()
    else:
        books = Book.query.all()

    invalid_form_index = books.index(book)
    add_book_to_order_forms = [AddBookToOrderForm() for i in range(len(books))]
    add_book_to_order_forms[invalid_form_index] = form

    order_in_progress = current_user.order_in_progress()

    return render_template('my/book/index.html',
                           books=books,
                           filter_form=filter_form,
                           add_book_to_order_forms=add_book_to_order_forms,
                           order_in_progress=order_in_progress)
Example #4
0
def register():
    form = SignUpForm()
    if form.validate_on_submit():
        user = User(id=form.id.data, password=form.password.data)
        if save(user):
            login_user(user)
            flash('Sign Up Successfully')
            return redirect(url_for('user.main'))
        else:
            flash('Try again')
    return render_template('user/register.html', form=form)
Example #5
0
def submit_order():
    order = current_user.order_in_progress()

    if order:
        order.status = 'pending'
        order.date = time.strftime('%Y-%m-%d')

        if save(order):
            flash('Order was successfully submitted!')

            return redirect(url_for('my.show_order', order_id=order.id))

    flash('Something went wrong when submitting your order. Please try again.')

    return redirect(url_for('my.current_order'))
Example #6
0
def create_order():
    order_params = {
        'date': time.strftime('%Y-%m-%d'),
        'status': 'in_progress',
        'customer_username': current_user.get_id()
    }
    order = Order(**order_params)

    if save(order):
        flash(
            'You just started a new order! You can choose the books to add to your order below'
        )
    else:
        flash('Failed to create order')

    return redirect(url_for('my.book_index'))
def sign_up():
    form = CustomerSignUpForm()

    if form.validate_on_submit():
        customer_params = form.data.copy()
        del customer_params['password_confirmation']
        new_customer = Customer(**customer_params)

        if save(new_customer):
            login_user(new_customer)
            flash('Your account was successfully created! Welcome to DBookstore!')

            return redirect(url_for('pages.index'))
        else:
            flash('Account creation was unsuccessful. Please try again.')

    return render_template('customer/sign_up.html', form=form)
Example #8
0
def new_book():
    # Handle form POST request
    form = NewBookForm()

    if form.validate_on_submit():
        book_params = form.data.copy()
        book_params['authors'] = [author.strip() for author in book_params['authors'].split(',')]
        book_params['keywords'] = [keyword.strip() for keyword in book_params['keywords'].split(',')]

        book = Book(**book_params)

        if save(book):
            flash('New book created successfully!')

            return redirect(url_for('store_manager.edit_book', ISBN=book.ISBN))
        else:
            flash('Failed to create new book. Please try again.')

    return render_template('store_manager/book/new.html', form=form)
Example #9
0
def edit_book(ISBN):
    # Handle form PUT request
    book = Book.query.get(ISBN)
    form = EditBookForm(obj=book)

    if form.validate_on_submit():
        authors = [author.strip() for author in form.data['authors'].split(',')]
        keywords = [keyword.strip() for keyword in form.data['keywords'].split(',')]

        form.populate_obj(book)
        book.authors = authors
        book.keywords = keywords

        if save(book):
            flash('Book details updated successfully!')

        else:
            flash('Failed to update book details. Please try again.')

    return render_template('store_manager/book/edit.html', form=form, book=book)