Added edit/create asset

This commit is contained in:
Dirk Jahnke 2022-03-16 17:43:52 +01:00
parent fa2b83a63a
commit 5f2b2c92ab
5 changed files with 32 additions and 3 deletions

View File

@ -3,6 +3,7 @@ from django.conf import settings
from django.contrib.auth import get_user_model
# from django.contrib.auth.models import User
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]
@ -16,3 +17,6 @@ class Asset(models.Model):
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')
def get_absolute_url(self):
return reverse('asset:detail', kwargs={'pk': self.pk})

View File

@ -5,18 +5,23 @@
{% block content %}
<div class="container">
<form action="{% url 'asset:asset-save' asset.id %}" method="post">
<form method="post">
{% csrf_token %}
<!--
<div class="row">
<div class="col-sm-12">
<legend><h2>Asset: {{ asset.named_id }}</h2></legend>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<div><input type="text" name="named_id" id="named_id" value="{{ asset.named_id }}" max_length="60" size="60"> <label for="named_id">ID</label></div>
<div><input type="text" name="description" id="description" value="{{ asset.description }}" max_length="250" size="80"> <label for="description">Beschreibung</label></div>
<div><input type="number" name="quantity" id="quantity" value="{{ asset.quantity }}" size="6" min="0" max="99999"><label for="quantity">Quantity</label></div>
<div><input type="submit" class="btn btn-primary" value="save"></div>
</div>
</div>
-->
{{ form.as_p }}
<div><input type="submit" class="btn btn-primary" value="save"></div>
</form>
</div>

View File

@ -16,7 +16,17 @@
<p>No new assets are available.</p>
{% endif %}
</div>
<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

@ -5,8 +5,9 @@ from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, Asset
app_name = 'asset'
urlpatterns = [
path('', AssetIndexView.as_view(), name='index'),
path('add/', AssetCreateView.as_view(), name='asset-add'),
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'),
]

View File

@ -9,10 +9,19 @@ class AssetCreateView(CreateView):
model = Asset
fields = ['named_id', 'description', 'quantity']
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 AssetUpdateView(UpdateView):
model = Asset
fields = ['named_id', 'description', 'quantity']
def form_valid(self, form):
form.instance.changed_by = self.request.user
return super().form_valid(form)
class AssetDeleteView(DeleteView):
model = Asset
success_url = reverse_lazy('asset-index')