Exemplo n.º 1
0
def update_book(request):
    if request.method == 'GET':
        id = request.GET["id"]
        book = Book.get_by_id(id)
        return render(request, 'update_book.html', {'book': book})

    if request.method == 'POST':
        id = request.POST.get("id")
        book = Book.get_by_id(id)
        book.name = request.POST.get("name")
        book.description = request.POST.get("description")
        book.count = request.POST.get("count")
        book.save()
        return redirect('/book')
def deleteBook(request, pk):
    book = Book.get_by_id(pk)
    if request.method == 'POST':
        Book.delete_by_id(pk)
        return redirect('/books')
    context = {'item': book}
    return render(request, 'book_delete.html', context)
Exemplo n.º 3
0
 def test_get_by_id_positive(self):
     """Positive test of the CustomUser.get_by_id() method"""
     book = Book.get_by_id(101)
     self.assertEqual(book.id, 101)
     self.assertEqual(book.name, 'book1')
     self.assertEqual(book.description, "description1")
     self.assertEqual(book.count, 1)
     self.assertListEqual(list(book.authors.all()), [self.author1])
Exemplo n.º 4
0
def new_order(request, id):
    if not request.user.is_authenticated:
        messages.info(request, "Log in first!")
        return redirect("authorise")
    book = Book.get_by_id(id)
    Order.create(request.user, book, datetime.now() + timedelta(days=14))
    messages.info(request, "Order saved!")
    return redirect("home")
def book_detail_view(request, pk):
    template_name = "book_detail.html"
    book = Book.get_by_id(pk)

    return render(request, template_name, {
        "book": book,
        "page_title": book.name
    })
Exemplo n.º 6
0
def create_new_order(request):
    req = request.POST
    user = CustomUser.get_by_email(req.get('user_email'))
    book = Book.get_by_id(req.get('book_id'))
    plated_end_at = req.get('plated_end_at')
    new_order = Order.create(user, book, plated_end_at)
    return render(request,
                  'order/neworder.html',
                  context={'new_order': new_order})
def updateBook(request, pk):
    book = Book.get_by_id(pk)
    form = BookForm(instance=book)
    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            form.save()
            return redirect("/books")
    context = {'form': form}
    return render(request, 'book_form.html', context)
Exemplo n.º 8
0
def order_on_click(request, id):
    user = request.user
    book = Book.get_by_id(book_id=id)
    order_form = OrderOnClick()
    if request.method == 'POST':
        order_form = OrderOnClick(request.POST)
        if order_form.is_valid():
            temp_order = Order.create(user=user, book=book, planned_end_at=datetime.now())
            order = temp_order.update(**order_form.cleaned_data)
            return redirect('order_by_id', temp_order.id)
    return render(request, 'order_confirm_.html', context={'form': order_form, 'user': user, 'book': book})
Exemplo n.º 9
0
def get_order_by_id(request, id=0):

    order_found = Order.get_by_id(id)
    usr = CustomUser.get_by_id(order_found.user_id)
    bk = Book.get_by_id(order_found.book_id)
    al = [authors for authors in list(bk.authors.all())]
    return render(request,
                  'order/get_order_by_id.html',
                  context={
                      'order': order_found,
                      'user': usr,
                      'book': bk,
                      'authors_list': al
                  })
Exemplo n.º 10
0
def make_order_view(request):
    context = {}
    template_name = "make_order.html"
    user = CustomUser.get_by_id(request.user.id)

    if request.method == "GET":
        form = MakeOrderForm(request.GET)
        if form.is_valid():
            book = Book.get_by_id(request.GET.get("book").split("//")[1])
            date = form.data["plated_end_at"]
            new_order = Order.create(user=user, book=book, plated_end_at=date)
            return redirect("order_details", pk=new_order.id)
    else:
        form = MakeOrderForm()
    context["page_title"] = "Make order"
    context["form"] = form
    return render(request, template_name, context)
Exemplo n.º 11
0
def find_order(request):
    context = {}
    orders = None
    template_name = "order-search.html"
    target_book = request.GET.get('book')
    if target_book:
        target_book = target_book.split("//")[-1]
    target_user = request.GET.get('user')
    if target_user:
        target_user = target_user.split("//")[-1]
    if request.method == "GET":
        if target_book:
            orders = Order.objects.filter(book=Book.get_by_id(target_book))
        if target_user:
            orders = Order.objects.filter(
                user=CustomUser.get_by_id(target_user))

        context["page_title"] = "Find order"
        context["orders"] = orders
    return render(request, template_name, context)


# def find_order(request):
#     context = {}
#     orders = None
#     template_name = "order-search.html"
#     target_book = request.GET.get('book')
#     if target_book:
#         target_book = target_book.split("//")[-1]
#     target_user = request.GET.get('user')
#     if target_user:
#         target_user = target_user.split("//")[-1]
#     if request.method == "GET":
#         if target_book:
#             orders = Order.objects.filter(book=Book.get_by_id(target_book))
#         if target_user:
#             orders = Order.objects.filter(user=CustomUser.get_by_id(target_user))
#
#         context["page_title"] = "Find order"
#         context["orders"] = orders
#     return render(request, template_name, context)
Exemplo n.º 12
0
def fill_db_orders():
    for i in range(len(names)):
        Order.create(user=CustomUser.get_by_id(i + 1),
                     book=Book.get_by_id((i % (len(books)) + 1)),
                     plated_end_at=(timezone.now() + timedelta(days=15)))
Exemplo n.º 13
0
def get_book_by_id(request, id=0):
    context = {'book_by_id': Book.get_by_id(id)}
    return render(request, 'book/get_book_by_id.html', context)
Exemplo n.º 14
0
 def test_get_by_id_negative(self):
     """Negative test of the CustomUser.get_by_id() method"""
     book = Book.get_by_id(999)
     self.assertIsNone(book)