Improved asset list (was index)

This commit is contained in:
Dirk Jahnke 2022-03-17 07:21:38 +01:00
parent 5f2b2c92ab
commit 92176158bd
9 changed files with 89 additions and 44 deletions

View File

@ -5,18 +5,21 @@ from django.contrib.auth import get_user_model
from container.models import Container
from django.urls import reverse
def get_sentinel_user():
return get_user_model().objects.get_or_create(username='deleted')[0]
class Asset(models.Model):
named_id = models.CharField(max_length=40, unique=True)
description = models.CharField(max_length=250, blank=True)
quantity = models.IntegerField(default=1)
created_ts = models.DateTimeField('datetime created', auto_now_add=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user), related_name='created_assets')
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user),
related_name='created_assets')
changed_ts = models.DateTimeField('datetime updated', auto_now=True)
changed_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user), related_name='changed_assets')
changed_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user),
related_name='changed_assets')
def get_absolute_url(self):
return reverse('asset:detail', kwargs={'pk': self.pk})

View File

@ -1,5 +1,5 @@
{% extends 'base.html' %}
{% load statics %}
{% load static %}
{% block content %}
<h2>Confirm Asset Delete</h2>

View File

@ -1,32 +0,0 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Asset: {{ asset.named_id }}{% endblock %}
{% block content %}
<div class="container">
<div>New Assets:</div>
{% if asset_list %}
<ul>
{% for asset in asset_list %}
<li><a href="{% url 'asset:asset-update' asset.id %}">{{ asset.named_id }}: {{ asset.description }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No new assets are available.</p>
{% endif %}
</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

@ -0,0 +1,63 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Asset: {{ asset.named_id }}{% endblock %}
{% block content %}
<div class="container">
<div>New Assets:</div>
{% if asset_list %}
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">Asset</th><th scope="col">Quantity</th><th scope="col">Action</th>
</tr>
</thead>
<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>{{ asset.quantity }}</td>
<td><a class="btn btn-outline-primary btn-sm" href="{% url 'asset:delete' asset.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,14 +1,14 @@
from django.urls import path
from . import views
from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetIndexView
from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetListView
app_name = 'asset'
urlpatterns = [
path('', AssetIndexView.as_view(), name='index'),
path('', AssetListView.as_view(), name='index'),
path('add/', AssetCreateView.as_view(), name='add'),
path('<int:pk>/', AssetUpdateView.as_view(), name='asset-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='asset-delete'),
path('<int:pk>/delete/', AssetDeleteView.as_view(), name='delete'),
]

View File

@ -5,6 +5,7 @@ from asset.models import Asset
from django.shortcuts import get_object_or_404, reverse
from django.http import HttpResponse, HttpResponseRedirect
class AssetCreateView(CreateView):
model = Asset
fields = ['named_id', 'description', 'quantity']
@ -14,6 +15,7 @@ class AssetCreateView(CreateView):
form.instance.created_by = self.request.user
return super().form_valid(form)
class AssetUpdateView(UpdateView):
model = Asset
fields = ['named_id', 'description', 'quantity']
@ -22,17 +24,24 @@ class AssetUpdateView(UpdateView):
form.instance.changed_by = self.request.user
return super().form_valid(form)
class AssetDeleteView(DeleteView):
model = Asset
success_url = reverse_lazy('asset-index')
class AssetIndexView(generic.ListView):
template_name = 'asset/asset_index.html'
context_object_name = 'asset_list'
class AssetListView(generic.ListView):
template_name = 'asset/asset_list.html'
context_object_name = 'asset_list'
model = Asset
paginate_by = 20
"""
def get_queryset(self):
# Return the last 20 created containers
return Asset.objects.order_by('-created_ts')[:20]
"""
def asset_save(request, asset_id):
asset = get_object_or_404(Asset, pk=asset_id)
@ -40,4 +49,3 @@ def asset_save(request, asset_id):
asset.quantity = request.POST['quantity']
asset.save();
return HttpResponseRedirect(reverse('asset:index'))

View File

@ -8,6 +8,7 @@ urlpatterns = [
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'),
]

View File

@ -38,3 +38,5 @@ class EditView(generic.DetailView):
class DeleteView(generic.DetailView):
model = Container
class AddView(generic.DetailView):
model = Container