Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)