def books():
    book_edit_form_errors = None
    book_edit_forms = []
    # generate book edit forms
    for book in Book.query.all():
        book_edit_form = BookEditForm()
        writers = Writer.query.all()
        book_edit_form.writers.data = [writer.id for writer in book.writers]
        book_edit_form.title.data = book.title
        book_edit_form.id.data = book.id
        book_edit_forms.append(book_edit_form)

    if request.method == 'GET':
        book_add_form = BookAddForm()
    else:
        if request.form['btn'] == 'Edit':
            book_add_form = BookAddForm()
            book_edit_form_errors = BookEditForm(request.form)
            if book_edit_form_errors.validate():
                writers = Writer.query.filter(Writer.id.in_(book_edit_form_errors.writers.data)).all()
                book = Book.query.filter(Book.id == book_edit_form_errors.id.data).first()
                book.edit(title=book_edit_form_errors.title.data, writers=writers)
                return redirect(url_for('books'))
        else:
            book_add_form = BookAddForm(request.form)
            if book_add_form.validate():
                writer_ids = [writer.id for writer in book_add_form.writers.data]
                writers = Writer.query.filter(Writer.id.in_(writer_ids)).all()
                Book.add(title=book_add_form.title.data, writers=writers)
                return redirect(url_for('books'))
    return render_template('books.html', book_add_form=book_add_form,
        book_edit_forms=book_edit_forms, book_edit_form_errors=book_edit_form_errors)
Example #2
0
def book_add_page():
    searchform = SearchForm()
    if not current_user.is_admin:
        abort(401)
    form = BookEditForm()
    if form.validate_on_submit():
        title = form.data["title"]
        author = form.data["author"]
        year = form.data["year"]
        genres = form.data["genres"]
        genres = genres.split(', ')
        pageNumber = form.data["pageNumber"]
        description = form.data["description"]
        cover = form.data["cover"]
        book = Book(title=title,
                    author=author,
                    year=year,
                    genres=genres,
                    pageNumber=pageNumber,
                    description=description,
                    cover=cover)
        db = current_app.config["db"]
        book_id = db.add_book(book)
        return redirect(url_for("book_page", book_id=book_id))
    return render_template("book_edit.html", form=form, searchform=searchform)
Example #3
0
    def edit(request, book_id):
        qdata = books.get_nav_list(request)

        book = get_object_or_404(Book, isbn13=book_id)
        if request.method == 'POST':  # If the form has been submitted...
            form = BookEditForm(
                instance=book,
                data=request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                # Process the data in form.cleaned_data
                # ...
                item_name = form.cleaned_data['isbn13']
                if item_name != book_id:
                    try:
                        Book.objects.get(isbn13=item_name)
                    except ObjectDoesNotExist:
                        form.save()
                        # Download book face
                        # noinspection PyBroadException
                        try:
                            douban_book_image(book.face_l, book.pubdate)
                            douban_book_image(book.face_m, book.pubdate)
                            douban_book_image(book.face_s, book.pubdate)
                        except:
                            pass
                else:
                    form.save()
                    # Download book face
                    # noinspection PyBroadException
                    try:
                        douban_book_image(book.face_l, book.pubdate)
                        douban_book_image(book.face_m, book.pubdate)
                        douban_book_image(book.face_s, book.pubdate)
                    except:
                        pass

                return HttpResponseRedirect('/books/' +
                                            book_id)  # Redirect after POST
        else:
            form = BookEditForm(instance=book)

            context = {
                'form': form,
                'book': book,
            }
            context = books.make_context(qdata, context)
            return render(request, 'default/book_edit.html', context)

        context = {
            'form': form,
        }
        context = books.make_context(qdata, context)
        return render(request, 'default/book_edit.html', context)
Example #4
0
    def add_multi(request):
        qdata = books.get_nav_list(request)

        default_values = {}
        form = BookEditForm(initial=default_values)
        location = form.fields['location'].widget.render('location',
                                                         'location',
                                                         attrs={
                                                             'id':
                                                             'id_location',
                                                         })
        category = form.fields['category'].widget.render('category',
                                                         'category',
                                                         attrs={
                                                             'id':
                                                             'id_category',
                                                         })
        media = form.fields['media'].widget.render('media',
                                                   'media',
                                                   attrs={
                                                       'id': 'id_media',
                                                   })

        context = {
            'location': location,
            'category': category,
            'media': media,
        }
        context = books.make_context(qdata, context)
        return render(request, 'default/book_add_multi.html', context)
Example #5
0
def inventory_new(request):
    """
    Inventory Edit Page
    """
    type = request.path.split("/")[-2]
    template = ""

    if type == "book":
        template = "capstone/book_edit.html"
    elif type == "dvd":
        template = "capstone/dvd_edit.html"
    else:
        template = ""

    if request.method == 'POST':
        if type == "book":
            form = BookEditForm(request.POST)
        elif type == "dvd":
            form = DVDEditForm(request.POST)
        else:
            form = {}

        if form.is_valid():
            new_item = form.save(commit=False)

            inventory_type = InventoryType.objects.filter(
                type_name__contains=type).first()

            inventory_item = InventoryItem(available=True,
                                           active=True,
                                           price=form.cleaned_data['price'],
                                           inventory_type=inventory_type)

            inventory_item.save()
            new_item.save()

            new_item.inventory = inventory_item
            new_item.save()

            return redirect('inventory_details',
                            pk=inventory_item.inventory_id)
    else:
        if type == "book":
            form = BookEditForm()
        elif type == "dvd":
            form = DVDEditForm()
        else:
            form = {}

    responseData = {
        "form": form,
    }

    return render(request, template, responseData)
Example #6
0
def inventory_edit(request, pk):
    """
    Inventory Edit Page
    """
    template = ""
    inventory_item = get_object_or_404(InventoryItem, inventory_id=pk)

    if inventory_item.inventory_type.type_name.lower() == "book":
        inventory_item.details = Book.objects.filter(
            inventory__inventory_id=inventory_item.pk).first()
        template = "capstone/book_edit.html"
    elif inventory_item.inventory_type.type_name.lower() == "dvd":
        inventory_item.details = Dvd.objects.filter(
            inventory__inventory_id=inventory_item.pk).first()
        template = "capstone/dvd_edit.html"
    else:
        inventory_item.details = {}

    if request.method == 'POST':
        if inventory_item.inventory_type.type_name.lower() == "book":
            form = BookEditForm(request.POST, instance=inventory_item.details)
        elif inventory_item.inventory_type.type_name.lower() == "dvd":
            form = DVDEditForm(request.POST, instance=inventory_item.details)
        else:
            form = {}

        if form.is_valid():
            inventory_item.details = form.save(commit=False)

            inventory_item.price = form.cleaned_data['price']

            inventory_item.save()
            inventory.details.save()

            return redirect('inventory_details',
                            pk=inventory_item.inventory_id)
    else:
        if inventory_item.inventory_type.type_name.lower() == "book":
            form = BookEditForm(instance=inventory_item.details)
        elif inventory_item.inventory_type.type_name.lower() == "dvd":
            form = DVDEditForm(instance=inventory_item.details)
        else:
            form = {}

        form.fields["price"].initial = \
            inventory_item.price

    responseData = {
        "inventory_item": inventory_item,
        "form": form,
    }

    return render(request, template, responseData)
Example #7
0
def book_edit_page(book_id):
    searchform = SearchForm()
    if not current_user.is_admin:
        abort(401)
    db = current_app.config["db"]
    book, _ = db.get_book(book_id)
    if book is None:
        abort(404)
    form = BookEditForm()
    if form.validate_on_submit():
        title = form.data["title"]
        author = form.data["author"]
        year = form.data["year"]
        genres = form.data["genres"]
        genres = genres.split(', ')
        pageNumber = form.data["pageNumber"]
        description = form.data["description"]
        cover = form.data["cover"]
        book = Book(title=title,
                    author=author,
                    year=year,
                    genres=genres,
                    pageNumber=pageNumber,
                    description=description,
                    cover=cover)
        db.update_book(book_id, book)
        return redirect(url_for("book_page", book_id=book_id))
    seperator = ", "
    genres = seperator.join(book.genres)
    form.title.data = book.title
    form.author.data = book.author if book.author else ""
    form.year.data = book.year if book.year else ""
    form.genres.data = genres if genres else ""
    form.pageNumber.data = book.pageNumber if book.pageNumber else ""
    form.description.data = book.description if book.description else ""
    form.cover.data = book.cover if book.cover else ""
    return render_template("book_edit.html", form=form, searchform=searchform)
Example #8
0
def bookedit():
    form = BookEditForm()
    form.list_of_books.choices = [(book.id, book.bookname)
                                  for book in Book.query.all()]
    form.list_of_authors.choices = [(author.id, author.authorname)
                                    for author in Author.query.all()]
    book = Book.query.all()[0]
    if request.method == 'POST':
        if request.form[
                'submit'] == u'Показать авторский состав выбранной книги':
            book = Book.query.get(form.list_of_books.data)
            return render_template('editing.html',
                                   form=form,
                                   user=current_user,
                                   book=book)
        if request.form[
                'submit'] == u'Добавить выбранного автора в авторский состав выбранной книги':
            author = Author.query.get(form.list_of_authors.data)
            book = Book.query.get(form.list_of_books.data)
            book.authors.append(author)
            dbs.session.add(book)
            dbs.session.commit()
            return render_template('editing.html',
                                   form=form,
                                   user=current_user,
                                   book=book)
        if request.form[
                'submit'] == u'Удалить выбранного автора из авторского состава выбранной книги':
            author = Author.query.get(form.list_of_authors.data)
            book = Book.query.get(form.list_of_books.data)
            if author in book.authors:
                book.authors.remove(author)
                dbs.session.add(book)
                dbs.session.commit()
                return render_template('editing.html',
                                       form=form,
                                       user=current_user,
                                       book=book)
    return render_template('editing.html',
                           form=form,
                           user=current_user,
                           book=book)
Example #9
0
    def add(request):
        qdata = books.get_nav_list(request)

        if request.method == 'POST':  # If the form has been submitted...
            form = BookEditForm(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                # Process the data in form.cleaned_data
                # ...
                book_id = form.cleaned_data['isbn13']
                try:
                    Book.objects.get(isbn13=book_id)
                except ObjectDoesNotExist:
                    book = Book.objects.create()

                    book.isbn10 = form.cleaned_data['isbn10']
                    book.isbn13 = form.cleaned_data['isbn13']
                    book.face_l = form.cleaned_data['face_l']
                    book.face_m = form.cleaned_data['face_m']
                    book.face_s = form.cleaned_data['face_s']
                    book.title = form.cleaned_data['title']
                    book.subtitle = form.cleaned_data['subtitle']
                    book.pages = form.cleaned_data['pages']
                    book.author = form.cleaned_data['author']
                    book.translator = form.cleaned_data['translator']
                    book.publisher = form.cleaned_data['publisher']
                    book.price = form.cleaned_data['price']
                    book.boughtprice = form.cleaned_data['boughtprice']
                    book.binding = form.cleaned_data['binding']
                    book.pubdate = form.cleaned_data['pubdate']
                    book.boughtdate = form.cleaned_data['boughtdate']
                    book.author_intro = form.cleaned_data['author_intro']
                    book.summary = form.cleaned_data['summary']
                    book.tags = form.cleaned_data['tags']
                    book.rating = form.cleaned_data['rating']
                    book.read_pages = form.cleaned_data['read_pages']
                    book.read_start = form.cleaned_data['read_start']
                    book.read_end = form.cleaned_data['read_end']
                    book.read_tags = form.cleaned_data['read_tags']
                    book.memo = form.cleaned_data['memo']
                    book.state = form.cleaned_data['state']
                    book.category = form.cleaned_data['category']
                    book.location = form.cleaned_data['location']
                    book.media = form.cleaned_data['media']

                    book.save()

                    # Download book face
                    # noinspection PyBroadException
                    try:
                        douban_book_image(book.face_l, book.pubdate)
                        douban_book_image(book.face_m, book.pubdate)
                        douban_book_image(book.face_s, book.pubdate)
                    except:
                        pass

                return HttpResponseRedirect('/books/' +
                                            book_id)  # Redirect after POST
            else:
                context = {
                    'form': form,
                }
                context = books.make_context(qdata, context)
                return render(request, 'default/book_edit.html', context)
        else:
            default_values = {
                'isbn13': '',
                'title': _('Unknown'),
                'summery': _('Unknown'),
                'author': _('Unknown'),
                'boughtdate': '2013-01-01',
                'publisher': _('Unknown'),
                'read_pages': '0',
            }
            form = BookEditForm(
                initial=default_values)  # A form bound to the POST data

            context = {
                'form': form,
            }
            context = books.make_context(qdata, context)
            return render(request, 'default/book_edit.html', context)