Refactored container views to new style.

This commit is contained in:
Dirk Jahnke 2022-03-17 10:27:31 +01:00
parent e5a81452f2
commit 1267e8a587
11 changed files with 161 additions and 103 deletions

View File

@ -5,7 +5,6 @@
{% block content %}
<div class="container">
<div>New Assets:</div>
{% if asset_list %}
<table class="table table-striped table-hover">
<thead>
@ -16,7 +15,7 @@
<tbody>
{% for asset in asset_list %}
<tr>
<td><a href="{% url 'asset:asset-update' asset.id %}"><div>{{ asset.named_id }}: {{ asset.description }}</div></a></td>
<td><a href="{% url 'asset:update' asset.id %}"><div>{{ asset.named_id }}: {{ asset.description }}</div></a></td>
<td>{{ asset.quantity }}</td>
<td><a class="btn btn-outline-primary btn-sm" href="{% url 'asset:delete' asset.id %}" role="button">Delete</a></td>
</tr>

View File

@ -6,9 +6,8 @@ app_name = 'asset'
urlpatterns = [
path('', AssetListView.as_view(), name='list'),
path('add/', AssetCreateView.as_view(), name='add'),
path('<int:pk>/', AssetUpdateView.as_view(), name='asset-update'),
path('<int:pk>/', AssetUpdateView.as_view(), name='update'),
path('<int:pk>/', AssetUpdateView.as_view(), name='detail'),
path('<int:asset_id>/save', views.asset_save, name='asset-save'),
path('<int:pk>/delete/', AssetDeleteView.as_view(), name='delete'),
]

View File

@ -27,7 +27,7 @@ class AssetUpdateView(UpdateView):
class AssetDeleteView(DeleteView):
model = Asset
success_url = reverse_lazy('asset-index')
success_url = reverse_lazy('asset:list')
class AssetListView(generic.ListView):
@ -48,4 +48,4 @@ def asset_save(request, asset_id):
asset.description = request.POST['description']
asset.quantity = request.POST['quantity']
asset.save();
return HttpResponseRedirect(reverse('asset:index'))
return HttpResponseRedirect(reverse('asset:list'))

View File

@ -1,32 +0,0 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Edit Container: {{ object.named_id }}{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-sm-12">
<h2>Container: {{ container.named_id }}</h2>
<div>{{ container.description }}</div>
<div>Color: {{ container.color }}</div>
<div>Container Type: <a href="{% url 'container:container_type_detail' container.container_type.id %}">{{ container.container_type.named_id }}</a></div>
</div>
</div>
<!-- Action buttons -->
<div class="row">
<div class="col-sm-12">
<a class="btn btn-primary" href="{% url 'container:edit' %}" role="button">Edit</a>
<a class="btn btn-primary" href="{% url 'container:delete' %}" role="button">Delete</a>
</div>
</div>
<!-- End Action buttons -->
</div>
{% endblock content %}

View File

@ -0,0 +1,18 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Edit Container: {{ container.named_id }}{% endblock %}
{% block content %}
<div class="container">
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<div>
<input type="submit" class="btn btn-primary" value="Save">
<a class="btn btn-primary" href="{% url 'container:list' %}">Container List</a>
</div>
</form>
</div>
{% endblock content %}

View File

@ -1,33 +0,0 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Container: {{ container.named_id }}{% endblock %}
{% block content %}
<div class="container">
<div>New Container:</div>
{% if container_list %}
<ul>
{% for container in container_list %}
<li><a href="{% url 'container:detail' container.id %}">{{ container.named_id }}: {{ container.description }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No container are available.</p>
{% endif %}
</div>
<div>
{% if container_type_list %}
<div>New Container Types:</div>
<ul>
{% for ctype in container_type_list %}
<li><a href="{% url 'container:container_type_detail' ctype.id %}">{{ ctype.named_id }} {{ ctype.description }}</a></li>
{% endfor %}
</ul>
</div>
{% else %}
<p>No container types are available.</p>
{% endif %}
</div>
{% endblock content %}

View File

@ -0,0 +1,74 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Container: {{ container.named_id }}{% endblock %}
{% block content %}
<div class="container">
{% if container_list %}
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">Container</th>
<th scope="col">Type</th>
<th scope="col">Size (mm)</th>
<th scope="col">Cover</th>
<th scope="col">Color</th>
<th scope="col">Description</th>
<th scope="col">Changed</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{% for container in container_list %}
<tr>
<td><a href="{% url 'container:update' container.id %}"><div>{{ container.named_id }}</div></a></td>
<td><div>{{ container.container_type.named_id }}</div></td>
<td><div>{{ container.container_type.width }}x{{ container.container_type.length }}x{{ container.container_type.height }}</div></td>
<td><div>{% if container.container_type.has_cover %}yes{%else %}--{% endif %}</div></td>
<td><div>{{ container.color }}</div></td>
<td><div>{{ container.description }}</div></td>
<td><div>{{ container.changed_ts | date:'H:i:s d.m.Y' }}</div></td>
<td><a class="btn btn-outline-primary btn-sm" href="{% url 'container:delete' container.id %}" role="button">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">previous</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#" aria-disabled="true">previous</a></li>
{% endif %}
<li class="page-item"><a class="page-link" href="?page=1">1 |&lt;&lt;</a></li>
<li class="page-item disabled"><a class="page-link" href="#" aria-disabled="true">...</a></li>
<li class="page-item"><a class="page-link" href="#" aria-disabled="true">{{ page_obj.number }}</a></li>
<li class="page-item disabled"><a class="page-link" href="#" aria-disabled="true">...</a></li>
<li class="page-item"><a class="page-link" href="?page={{ paginator.num_pages }}">&gt;&gt;| {{ paginator.num_pages }}</a></li>
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">next</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#" aria-disabled="true">next</a></li>
{% endif %}
</ul>
</nav>
{% else %}
<p>No new assets are available.</p>
{% endif %}
</div>
<div class="row p-4"></div>
<!-- Action buttons -->
<div class="row">
<div class="col-sm-12">
<a class="btn btn-primary" href="{% url 'asset:add' %}" role="button">+ Asset</a>
<a class="btn btn-primary" href="{% url 'container:add' %}" role="button">+ Container</a>
</div>
</div>
<!-- End Action buttons -->
</div>
{% endblock content %}

View File

@ -1,15 +1,15 @@
from django.urls import path
from . import views
from container.views import ContainerListView, ContainerUpdateView, ContainerCreateView, ContainerDetailView, ContainerDeleteView
from container.views import ContainerTypeListView, ContainerTypeDetailView, ContainerTypeCreateView
app_name = 'container'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('edit/<int:pk>/', views.EditView.as_view(), name='edit'),
path('delete/<int:pk>/', views.DeleteView.as_view(), name='delete'),
path('add/', views.AddView.as_view(), name='add'),
path('type/', views.TypeIndexView.as_view(), name='container_type_index'),
path('type/<int:pk>/', views.TypeDetailView.as_view(), name='container_type_detail'),
path('', ContainerListView.as_view(), name='list'),
path('add/', ContainerCreateView.as_view(), name='add'),
path('edit/<int:pk>/', ContainerUpdateView.as_view(), name='update'),
path('delete/<int:pk>/', ContainerDeleteView.as_view(), name='delete'),
path('type/', ContainerTypeListView.as_view(), name='container_type_list'),
path('type/<int:pk>/', ContainerTypeDetailView.as_view(), name='container_type_detail'),
path('type/add/', ContainerTypeCreateView.as_view(), name='container_type_add'),
]

View File

@ -1,42 +1,75 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.http import Http404
from django.urls import reverse
from django.views import generic
from .models import Container, ContainerType
class IndexView(generic.ListView):
template_name = 'container/container_index.html'
context_object_name = 'container_list'
class ContainerListView(generic.ListView):
model = Container
template_name = 'container/container_list.html'
context_object_name = 'container_list'
paginate_by = 20
'''
def get_queryset(self):
# Return the last five created containers
return Container.objects.order_by('-created_ts')[:5]
'''
class TypeIndexView(generic.ListView):
template_name = 'container/container_type_index.html'
class ContainerCreateView(generic.CreateView):
model = Container
# template_name = 'container/detail.html'
fields = ['named_id', 'description', 'color', 'container_type']
def form_valid(self, form):
form.instance.changed_by = self.request.user
form.instance.created_by = self.request.user
return super().form_valid(form)
class ContainerUpdateView(generic.UpdateView):
model = Container
# template_name = 'container/detail.html'
fields = ['named_id', 'description', 'color', 'container_type']
def form_valid(self, form):
form.instance.changed_by = self.request.user
return super().form_valid(form)
class ContainerDetailView(generic.DetailView):
model = Container
class ContainerDeleteView(generic.DetailView):
model = Container
class ContainerTypeListView(generic.ListView):
template_name = 'container/container_type_list.html'
context_object_name = 'container_type_list'
paginate_by = 20
model = ContainerType
'''
def get_queryset(self):
# Return the last five created container types
return ContainerType.objects.order_by('-created_ts')[:5]
'''
class DetailView(generic.DetailView):
model = Container
# template_name = 'container/detail.html'
class TypeDetailView(generic.DetailView):
class ContainerTypeDetailView(generic.DetailView):
model = ContainerType
context_object_name = 'container_type'
template_name = 'container/container_type_detail.html'
class EditView(generic.DetailView):
model = Container
class DeleteView(generic.DetailView):
model = Container
class ContainerTypeCreateView(generic.CreateView):
model = ContainerType
# template_name = 'container/detail.html'
fields = ['named_id', 'description', 'width', 'length', 'height', 'inner_width', 'inner_length', 'inner_height',
'has_cover', 'contains_container']
class AddView(generic.DetailView):
model = Container
def form_valid(self, form):
form.instance.changed_by = self.request.user
form.instance.created_by = self.request.user
return super().form_valid(form)

View File

@ -4,8 +4,8 @@
{% block content %}
<h2>This is HOME</h2>
<a href="{% url 'container:container_type_index' %}">Container Types</a><br>
<a href="{% url 'container:index' %}">Container</a><br>
<a href="{% url 'container:container_type_list' %}">Container Types</a><br>
<a href="{% url 'container:list' %}">Container</a><br>
<a href="{% url 'asset:list' %}">Assets</a><br>