Skip to content

Commit

Permalink
Expand tutorial to allow create/delete/update through UI
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishwillee committed Sep 26, 2023
1 parent 0304530 commit b5606da
Show file tree
Hide file tree
Showing 18 changed files with 449 additions and 44 deletions.
16 changes: 14 additions & 2 deletions catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Genre(models.Model):
help_text="Enter a book genre (e.g. Science Fiction, French Poetry etc.)"
)

def get_absolute_url(self):
"""Returns the url to access a particular genre instance."""
return reverse('genre-detail', args=[str(self.id)])

def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
return self.name
Expand All @@ -22,6 +26,10 @@ class Language(models.Model):
name = models.CharField(max_length=200,
help_text="Enter the book's natural language (e.g. English, French, Japanese etc.)")

def get_absolute_url(self):
"""Returns the url to access a particular language instance."""
return reverse('language-detail', args=[str(self.id)])

def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
return self.name
Expand All @@ -42,7 +50,7 @@ class Book(models.Model):
# ManyToManyField used because a genre can contain many books and a Book can cover many genres.
# Genre class has already been defined so we can specify the object above.
language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)

class Meta:
ordering = ['title', 'author']

Expand All @@ -53,7 +61,7 @@ def display_genre(self):
display_genre.short_description = 'Genre'

def get_absolute_url(self):
"""Returns the url to access a particular book instance."""
"""Returns the url to access a particular book record."""
return reverse('book-detail', args=[str(self.id)])

def __str__(self):
Expand Down Expand Up @@ -99,6 +107,10 @@ class Meta:
ordering = ['due_back']
permissions = (("can_mark_returned", "Set book as returned"),)

def get_absolute_url(self):
"""Returns the url to access a particular book instance."""
return reverse('bookinstance-detail', args=[str(self.id)])

def __str__(self):
"""String for representing the Model object."""
return '{0} ({1})'.format(self.id, self.book.title)
Expand Down
35 changes: 23 additions & 12 deletions catalog/templates/base_generic.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>

{% block title %}<title>Local Library</title>{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
Expand All @@ -21,35 +21,46 @@
{% block sidebar %}
<ul class="sidebar-nav">
<li><a href="{% url 'index' %}">Home</a></li>
<li><a href="{% url 'bookinstances' %}">All book copies</a></li>
<li><a href="{% url 'books' %}">All books</a></li>
<li><a href="{% url 'authors' %}">All authors</a></li>
<li><a href="{% url 'genres' %}">All genres</a></li>
<li><a href="{% url 'languages' %}">All languages</a></li>
</ul>

<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: {{ user.get_username }}</li>
<li><a href="{% url 'my-borrowed' %}">My Borrowed</a></li>
<li><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
<li><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
{% else %}
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}
</ul>

{% if user.is_staff %}
<hr>
<ul class="sidebar-nav">
<li>Staff</li>
{% if perms.catalog.can_mark_returned %}
<li><a href="{% url 'all-borrowed' %}">All borrowed</a></li>
<li><a href="{% url 'genre-create' %}">Create Genre</a></li>
<li><a href="{% url 'language-create' %}">Create Language</a></li>
<li><a href="{% url 'author-create' %}">Create Author</a></li>
<li><a href="{% url 'book-create' %}">Create Book</a></li>
<li><a href="{% url 'bookinstance-create' %}">Create BookInstance</a></li>
{% endif %}



</ul>
{% endif %}

{% endblock %}
</div>
<div class="col-sm-10 ">
{% block content %}{% endblock %}

{% block pagination %}
{% if is_paginated %}
<div class="pagination">
Expand All @@ -66,9 +77,9 @@
</span>
</div>
{% endif %}
{% endblock %}
{% endblock %}


</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions catalog/templates/catalog/author_confirm_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

{% block content %}

<h1>Delete Author</h1>
<h1>Delete Author: {{ author }}</h1>

<p>Are you sure you want to delete the author: {{ author }}?</p>
<p>Are you sure you want to delete the author?</p>

<form action="" method="POST">
{% csrf_token %}
Expand Down
32 changes: 24 additions & 8 deletions catalog/templates/catalog/book_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,37 @@ <h1>Title: {{ book.title }}</h1>

<p><strong>Author:</strong> <a href="{{ book.author.get_absolute_url }}">{{ book.author }}</a></p>
<p><strong>Summary:</strong> {{ book.summary }}</p>
<p><strong>ISBN:</strong> {{ book.isbn }}</p>
<p><strong>Language:</strong> {{ book.language }}</p>
<p><strong>ISBN:</strong> {{ book.isbn }}</p>
<p><strong>Language:</strong> {{ book.language }}</p>
<p><strong>Genre:</strong> {{ book.genre.all|join:", " }}</p>

<div style="margin-left:20px;margin-top:20px">
<h4>Copies</h4>

{% for copy in book.bookinstance_set.all %}
<hr>
<p class="{% if copy.status == 'a' %}text-success{% elif copy.status == 'd' %}text-danger{% else %}text-warning{% endif %}">{{ copy.get_status_display }}</p>
{% if copy.status != 'a' %}<p><strong>Due to be returned:</strong> {{copy.due_back}}</p>{% endif %}
<p><strong>Imprint:</strong> {{copy.imprint}}</p>
<p class="text-muted"><strong>Id:</strong> {{copy.id}}</p>

<hr>
<p class="{% if copy.status == 'a' %}text-success{% elif copy.status == 'd' %}text-danger{% else %}text-warning{% endif %}">{{ copy.get_status_display }}</p>
{% if copy.status != 'a' %}<p><strong>Due to be returned:</strong> {{copy.due_back}}</p>{% endif %}
<p><strong>Imprint:</strong> {{copy.imprint}}</p>
<p class="text-muted"><strong>Id:</strong> <a href="{{ copy.get_absolute_url }}">{{copy.id}}</a></p>
{% empty %}
<p>The library has no copies of this book.</p>
{% endfor %}
</div>
{% endblock %}


{% block sidebar %}
{{ block.super }}

{% if perms.catalog.can_mark_returned %}
<hr>
<ul class="sidebar-nav">
<li><a href="{% url 'book-update' book.id %}">Update Book</a></li>
{% if not book.bookinstance_set.all %}
<li><a href="{% url 'book-delete' book.id %}">Delete Book</a></li>
{% endif %}
</ul>
{% endif %}

{% endblock %}
14 changes: 14 additions & 0 deletions catalog/templates/catalog/bookinstance_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base_generic.html" %}

{% block content %}

<h1>Delete Book Copy: {{ bookinstance }}</h1>

<p>Are you sure you want to delete this copy of the book?</p>

<form action="" method="POST">
{% csrf_token %}
<input type="submit" action="" value="Yes, delete.">
</form>

{% endblock %}
32 changes: 32 additions & 0 deletions catalog/templates/catalog/bookinstance_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends "base_generic.html" %}

{% block content %}

<h1>BookInstance: {{ bookinstance.book.title }}</h1>

<p><strong>Author:</strong> <a href="{{ bookinstance.book.author.get_absolute_url }}">{{ bookinstance.book.author }}</a></p>

<p><strong>Imprint:</strong> {{ bookinstance.imprint }}</p>
<p><strong>Status:</strong> {{ bookinstance.get_status_display }} {% if bookinstance.status != 'a' %} (Due: {{bookinstance.due_back}}){% endif %}</p>

<hr>
<ul>
<li>
<a href="{{ bookinstance.book.get_absolute_url }}">All copies</a></p>
</li>
</ul>
{% endblock %}


{% block sidebar %}
{{ block.super }}

{% if perms.catalog.can_mark_returned %}
<hr>
<ul class="sidebar-nav">
<li><a href="{% url 'bookinstance-update' bookinstance.id %}">Update BookInstance</a></li>
<li><a href="{% url 'bookinstance-delete' bookinstance.id %}">Delete BookInstance</a></li>
</ul>
{% endif %}

{% endblock %}
13 changes: 13 additions & 0 deletions catalog/templates/catalog/bookinstance_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base_generic.html" %}

{% block content %}

<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">

</form>
{% endblock %}
16 changes: 16 additions & 0 deletions catalog/templates/catalog/bookinstance_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "base_generic.html" %}

{% block content %}
<h1>Book Copies in Library</h1>

<ul>
{% for bookinst in bookinstance_list %}
<li class="{% if bookinst.is_overdue %}text-danger{% endif %}">
<a href="{% url 'bookinstance-detail' bookinst.pk %}">{{bookinst.book.title}}</a> ({{ bookinst.due_back }}) {% if user.is_staff %}- {{ bookinst.borrower }}{% endif %} {% if perms.catalog.can_mark_returned %}- <a href="{% url 'renew-book-librarian' bookinst.id %}">Renew</a> {% endif %}
</li>
{% empty %}
<li>There are no book copies available.</li>
{% endfor %}
</ul>

{% endblock %}
14 changes: 14 additions & 0 deletions catalog/templates/catalog/genre_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base_generic.html" %}

{% block content %}

<h1>Delete Genre: {{ genre }}</h1>

<p>Are you sure you want to delete the genre?</p>

<form action="" method="POST">
{% csrf_token %}
<input type="submit" action="" value="Yes, delete.">
</form>

{% endblock %}
41 changes: 41 additions & 0 deletions catalog/templates/catalog/genre_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% extends "base_generic.html" %}

{% block content %}

<h1>Genre: {{ genre.name }}</h1>

<div style="margin-left:20px;margin-top:20px">
<h4>Books in genre</h4>

<ul>
{% for copy in genre.book_set.all %}
<li>
<a href="{{ copy.get_absolute_url }}">{{ copy.title }}</a> ({{copy.author}})
</li>
{% empty %}
<li>There are no books in this genre.</li>
{% endfor %}
</ul>

{% endblock %}


{% block sidebar %}
{{ block.super }}

{% if perms.catalog.can_mark_returned %}
<hr>
<ul class="sidebar-nav">
<li><a href="{% url 'genre-update' genre.id %}">Update Genre</a></li>
{% if not genre.book_set.all %}
<li><a href="{% url 'genre-delete' genre.id %}">Delete Genre</a></li>
{% endif %}
</ul>
{% endif %}

{% endblock %}





13 changes: 13 additions & 0 deletions catalog/templates/catalog/genre_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base_generic.html" %}

{% block content %}

<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">

</form>
{% endblock %}
25 changes: 25 additions & 0 deletions catalog/templates/catalog/genre_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends "base_generic.html" %}

{% block content %}

<h1>Genre List</h1>

{% if genre_list %}
<ul>

{% for genre in genre_list %}
<li>
<a href="{{ genre.get_absolute_url }}">
{{ genre }}
</a>
</li>
{% endfor %}

</ul>
{% else %}
<p>There are no genres available.</p>
{% endif %}


{% endblock %}

14 changes: 14 additions & 0 deletions catalog/templates/catalog/language_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base_generic.html" %}

{% block content %}

<h1>Delete Language: {{ language }}</h1>

<p>Are you sure you want to delete the language?</p>

<form action="" method="POST">
{% csrf_token %}
<input type="submit" action="" value="Yes, delete.">
</form>

{% endblock %}
Loading

0 comments on commit b5606da

Please sign in to comment.