homelog/asset/views.py

44 lines
1.4 KiB
Python
Raw Normal View History

2022-03-10 16:28:39 +00:00
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views import generic
from asset.models import Asset
2022-03-16 13:13:20 +00:00
from django.shortcuts import get_object_or_404, reverse
from django.http import HttpResponse, HttpResponseRedirect
2022-03-10 16:28:39 +00:00
class AssetCreateView(CreateView):
model = Asset
2022-03-10 17:19:13 +00:00
fields = ['named_id', 'description', 'quantity']
2022-03-10 16:28:39 +00:00
2022-03-16 16:43:52 +00:00
def form_valid(self, form):
form.instance.changed_by = self.request.user
form.instance.created_by = self.request.user
return super().form_valid(form)
2022-03-10 16:28:39 +00:00
class AssetUpdateView(UpdateView):
model = Asset
2022-03-10 17:19:13 +00:00
fields = ['named_id', 'description', 'quantity']
2022-03-10 16:28:39 +00:00
2022-03-16 16:43:52 +00:00
def form_valid(self, form):
form.instance.changed_by = self.request.user
return super().form_valid(form)
2022-03-10 16:28:39 +00:00
class AssetDeleteView(DeleteView):
model = Asset
2022-03-10 17:19:13 +00:00
success_url = reverse_lazy('asset-index')
2022-03-10 16:28:39 +00:00
class AssetIndexView(generic.ListView):
template_name = 'asset/asset_index.html'
context_object_name = 'asset_list'
def get_queryset(self):
# Return the last 20 created containers
return Asset.objects.order_by('-created_ts')[:20]
2022-03-16 13:13:20 +00:00
def asset_save(request, asset_id):
asset = get_object_or_404(Asset, pk=asset_id)
asset.description = request.POST['description']
asset.quantity = request.POST['quantity']
asset.save();
return HttpResponseRedirect(reverse('asset:index'))