def deleteAuthor(request, pk):
    author = Author.get_by_id(pk)
    if request.method == 'POST':
        Author.delete_by_id(pk)
        return redirect('/authors')
    context = {'item': author}
    return render(request, 'author/delete.html', context)
Example #2
0
 def test_get_by_id_positive(self):
     """Positive test of the CustomUser.get_by_id() method"""
     author = Author.get_by_id(101)
     self.assertEqual(author.id, 101)
     self.assertEqual(author.name, 'author1')
     self.assertEqual(author.surname, "s1")
     self.assertEqual(author.patronymic, "p1")
def updateAuthor(request, pk):
    author = Author.get_by_id(pk)
    form = AuthorForm(instance=author)
    if request.method == 'POST':
        form = AuthorForm(request.POST, instance=author)
        if form.is_valid():
            form.save()
            return redirect("/authors")
    context = {'form': form}
    return render(request, 'author/author_form.html', context)
def get_author_by_id(request, id=0):
    author_by_id = Author.get_by_id(id)
    bks = [
        book for book in list(Book.get_all())
        if author_by_id in book.authors.all()
    ]
    # books = list(Book.get_all())
    return render(request,
                  'author/get_author_by_id.html',
                  context={
                      'author_by_id': author_by_id,
                      'books': bks
                  })
Example #5
0
def book_add_new_view(request):
    context = {}
    template_name = "book_add_new.html"
    context["page_title"] = "New book"
    if request.method == "POST":
        form = AddBookForm(request.POST)
        if form.is_valid():
            authors = []
            for author_id in form.data["authors"]:
                authors.append(Author.get_by_id(author_id))
            new_book = Book.create(name=form.data["name"],
                                   description=form.data["description"],
                                   count=form.data["count"],
                                   authors=authors)
            return redirect("book_details", pk=new_book.id)
    else:
        context["form"] = AddBookForm
    return render(request, template_name, context)
def author_detail_view(request, pk):
    template_name = "author/author_detail.html"
    author = Author.get_by_id(pk)
    books = list(Book.objects.filter(authors__id=pk))

    return render(request, template_name, {"books": books, "author": author})
Example #7
0
 def test_get_by_id_negative(self):
     """Negative test of the CustomUser.get_by_id() method"""
     author = Author.get_by_id(999)
     self.assertIsNone(author)