Improved asset list (was index)
This commit is contained in:
parent
5f2b2c92ab
commit
92176158bd
|
@ -5,18 +5,21 @@ from django.contrib.auth import get_user_model
|
||||||
from container.models import Container
|
from container.models import Container
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
|
||||||
def get_sentinel_user():
|
def get_sentinel_user():
|
||||||
return get_user_model().objects.get_or_create(username='deleted')[0]
|
return get_user_model().objects.get_or_create(username='deleted')[0]
|
||||||
|
|
||||||
|
|
||||||
class Asset(models.Model):
|
class Asset(models.Model):
|
||||||
named_id = models.CharField(max_length=40, unique=True)
|
named_id = models.CharField(max_length=40, unique=True)
|
||||||
description = models.CharField(max_length=250, blank=True)
|
description = models.CharField(max_length=250, blank=True)
|
||||||
quantity = models.IntegerField(default=1)
|
quantity = models.IntegerField(default=1)
|
||||||
created_ts = models.DateTimeField('datetime created', auto_now_add=True)
|
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_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):
|
def get_absolute_url(self):
|
||||||
return reverse('asset:detail', kwargs={'pk': self.pk})
|
return reverse('asset:detail', kwargs={'pk': self.pk})
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{% extends 'base.html' %}
|
{% extends 'base.html' %}
|
||||||
{% load statics %}
|
{% load static %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h2>Confirm Asset Delete</h2>
|
<h2>Confirm Asset Delete</h2>
|
||||||
|
|
|
@ -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 %}
|
|
||||||
|
|
|
@ -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 |<<</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 }}">>>| {{ 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 %}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from . import views
|
from . import views
|
||||||
from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetIndexView
|
from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetListView
|
||||||
|
|
||||||
app_name = 'asset'
|
app_name = 'asset'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', AssetIndexView.as_view(), name='index'),
|
path('', AssetListView.as_view(), name='index'),
|
||||||
path('add/', AssetCreateView.as_view(), name='add'),
|
path('add/', AssetCreateView.as_view(), name='add'),
|
||||||
path('<int:pk>/', AssetUpdateView.as_view(), name='asset-update'),
|
path('<int:pk>/', AssetUpdateView.as_view(), name='asset-update'),
|
||||||
path('<int:pk>/', AssetUpdateView.as_view(), name='detail'),
|
path('<int:pk>/', AssetUpdateView.as_view(), name='detail'),
|
||||||
path('<int:asset_id>/save', views.asset_save, name='asset-save'),
|
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'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ from asset.models import Asset
|
||||||
from django.shortcuts import get_object_or_404, reverse
|
from django.shortcuts import get_object_or_404, reverse
|
||||||
from django.http import HttpResponse, HttpResponseRedirect
|
from django.http import HttpResponse, HttpResponseRedirect
|
||||||
|
|
||||||
|
|
||||||
class AssetCreateView(CreateView):
|
class AssetCreateView(CreateView):
|
||||||
model = Asset
|
model = Asset
|
||||||
fields = ['named_id', 'description', 'quantity']
|
fields = ['named_id', 'description', 'quantity']
|
||||||
|
@ -14,6 +15,7 @@ class AssetCreateView(CreateView):
|
||||||
form.instance.created_by = self.request.user
|
form.instance.created_by = self.request.user
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
class AssetUpdateView(UpdateView):
|
class AssetUpdateView(UpdateView):
|
||||||
model = Asset
|
model = Asset
|
||||||
fields = ['named_id', 'description', 'quantity']
|
fields = ['named_id', 'description', 'quantity']
|
||||||
|
@ -22,17 +24,24 @@ class AssetUpdateView(UpdateView):
|
||||||
form.instance.changed_by = self.request.user
|
form.instance.changed_by = self.request.user
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
class AssetDeleteView(DeleteView):
|
class AssetDeleteView(DeleteView):
|
||||||
model = Asset
|
model = Asset
|
||||||
success_url = reverse_lazy('asset-index')
|
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):
|
def get_queryset(self):
|
||||||
# Return the last 20 created containers
|
# Return the last 20 created containers
|
||||||
return Asset.objects.order_by('-created_ts')[:20]
|
return Asset.objects.order_by('-created_ts')[:20]
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def asset_save(request, asset_id):
|
def asset_save(request, asset_id):
|
||||||
asset = get_object_or_404(Asset, pk=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.quantity = request.POST['quantity']
|
||||||
asset.save();
|
asset.save();
|
||||||
return HttpResponseRedirect(reverse('asset:index'))
|
return HttpResponseRedirect(reverse('asset:index'))
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ urlpatterns = [
|
||||||
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
||||||
path('edit/<int:pk>/', views.EditView.as_view(), name='edit'),
|
path('edit/<int:pk>/', views.EditView.as_view(), name='edit'),
|
||||||
path('delete/<int:pk>/', views.DeleteView.as_view(), name='delete'),
|
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/', views.TypeIndexView.as_view(), name='container_type_index'),
|
||||||
path('type/<int:pk>/', views.TypeDetailView.as_view(), name='container_type_detail'),
|
path('type/<int:pk>/', views.TypeDetailView.as_view(), name='container_type_detail'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -38,3 +38,5 @@ class EditView(generic.DetailView):
|
||||||
class DeleteView(generic.DetailView):
|
class DeleteView(generic.DetailView):
|
||||||
model = Container
|
model = Container
|
||||||
|
|
||||||
|
class AddView(generic.DetailView):
|
||||||
|
model = Container
|
||||||
|
|
Loading…
Reference in New Issue