Modify Objects

+ sort the object list
+ alter objects
+ delete objects
This commit is contained in:
Clemens-Dautermann 2018-12-26 23:41:37 +01:00
parent 32d60f51ac
commit 51c942746b
6 changed files with 249 additions and 27 deletions

View file

@ -1,6 +1,9 @@
from django.urls import path from django.urls import path
from . import views from . import views
import uuid
urlpatterns = [ urlpatterns = [
path('', views.objlist, name='objlist'), path('', views.objlist, name='objlist'),
path('<str:orderstr>', views.objlist, name='objlist_ordered'),
path('<str:uuid_url>/delete', views.delete, name='del_obj')
] ]

View file

@ -1,13 +1,80 @@
from django.shortcuts import render from django.shortcuts import render, get_object_or_404
from object_adder.models import Object from object_adder.models import Object, Category
from object_adder.forms import ObjectForm
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
import re
# Create your views here. # Create your views here.
@login_required @login_required
def objlist(request): def objlist(request, orderstr=None):
uuidv4pattern = r"(\d|[a-z]){8}-(\d|[a-z]){4}-4(\d|[a-z]){3}-(\d|[a-z]){4}-(\d|[a-z]){12}"
if request.method == 'GET':
if orderstr is None:
objects = Object.objects.all() objects = Object.objects.all()
elif orderstr == '0':
objects = Object.objects.all().order_by('title')
elif orderstr == '1':
objects = Object.objects.all().order_by('ammout')
elif orderstr == '2':
objects = Object.objects.all().order_by('category')
elif orderstr == '3':
objects = Object.objects.all().order_by('inventarized_date')
elif orderstr == '4':
objects = Object.objects.all().order_by('user_added')
else:
result = re.fullmatch(uuidv4pattern, orderstr)
if result is None:
objects = Object.objects.all()
else:
uuid = result.group(0)
obj = get_object_or_404(Object, pk=uuid)
context = {'title': 'Inventar', 'objects': objects} form = ObjectForm(
initial={'ammout': obj.ammout, 'title': obj.title, 'description': obj.description,
'category': obj.category}
)
context = {'title': 'Details', 'obj': obj, 'form': form}
return render(request, 'object_lister/details.html', context)
categories = Category.categories.all()
context = {'title': 'Inventar', 'objects': objects, 'objammout': len(objects), 'categories': categories,
'ncats': len(categories)}
return render(request, 'object_lister/index.html', context) return render(request, 'object_lister/index.html', context)
elif request.method == 'POST':
form = ObjectForm(request.POST)
if form.is_valid():
result = re.fullmatch(uuidv4pattern, orderstr)
uuid = result.group(0)
obj = get_object_or_404(Object, pk=uuid)
obj.ammout = form.cleaned_data['ammout']
obj.title = form.cleaned_data['title']
obj.description = form.cleaned_data['description']
obj.category = form.cleaned_data['category']
obj.save()
form = ObjectForm(
initial={'ammout': obj.ammout, 'title': obj.title, 'description': obj.description,
'category': obj.category}
)
context = {'title': 'Details', 'obj': obj, 'form': form, 'message': 'changed'}
return render(request, 'object_lister/details.html', context)
@login_required
def delete(request, uuid_url):
obj = get_object_or_404(Object, pk=uuid_url)
obj.delete()
return render(request, 'object_lister/delete.html', {'uuid': uuid_url})

View file

@ -100,19 +100,53 @@
} }
.objecttable-data, .objecttable-head { .objecttable-data, .objecttable-head {
border: 1px solid #dddddd; border-bottom: 1px solid #dddddd;
text-align: center; text-align: center;
padding: 0.4em; padding: 0.4em;
} }
.objecttable-row:nth-child(even) { .objecttable-row:hover {
background-color: #dddddd; background-color: #f8f8f8;
} }
.objecttable-row:hover { .cattable {
-webkit-box-shadow: 3px 3px 28px -3px rgba(0, 0, 0, 0.1); border-collapse: collapse;
-moz-box-shadow: 3px 3px 28px -3px rgba(0, 0, 0, 0.1); width: 50%;
box-shadow: 3px 3px 28px -3px rgba(0, 0, 0, 0.1); margin-top: 2em;
border-radius: 10px; margin-bottom: 2em;
transition: 0.1s; }
.cattable * {
text-align: left;
}
.lower-box {
margin-top: 2em;
width: 50%;
}
.celllink, .celllink:hover, .celllink:visited, .celllink:active {
width: 100%;
height: 100%;
display: block;
padding: 0.4em;
text-decoration: none;
color: #333333;
cursor: default;
}
.alert {
margin-top: 1em;
}
.nodisplay {
display: none;
}
.delaleret {
display: none;
}
.delbtn{
margin-bottom: 1em !important;
} }

View file

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
object {{ uuid }} deleted
</body>
</html>

View file

@ -0,0 +1,76 @@
{% extends 'bases/navbar.html' %}
<html>
{% block content %}
<div class="container shadow">
{% if not message is None %}
<div class="alert alert-info alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
Das Objekt <strong>{{ obj.title }}</strong> wurde verändert.
</div>
{% endif %}
<div class="alert alert-danger alert-dismissible nodisplay" id="abortalert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
Abbruch.
</div>
<form method="POST" class="post-form invform">
{% csrf_token %}
<div class="ammout-wrapper">
{{ form.ammout.errors }}
<label for="{{ form.ammout.id_for_lable }}">Anzahl:</label>
{{ form.ammout }}
</div>
<div class="title-wrapper">
{{ form.title.errors }}
<label for="{{ form.title.id_for_lable }}">Objekt:</label>
{{ form.title }}
</div>
<div class="category-wrapper">
{{ form.category.errors }}
<label for="{{ form.category.id_for_lable }}">Kategorie:</label>
{{ form.category }}
</div>
<div class="img-wrapper">
{{ form.img.errors }}
<label for="{{ form.img.id_for_lable }}">Bild:</label>
{{ form.img }}
</div>
<div class="description-wrapper">
{{ form.description.errors }}
<label for="{{ form.description.id_for_lable }}">Beschreibung:</label>
{{ form.description }}
</div>
<div class="btnwrapper">
<button type="submit" class="save btn btn-primary">speichern</button>
</div>
</form>
<button type="button" class="delbtn btn btn-danger" onclick="del()">Objekt löschen</button>
</div>
<script>
function del() {
if (confirm('Möchten sie das Objekt {{ obj.title }} wirklich löschen?')) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
window.location.href = "./"
}
};
xhttp.open("GET", './{{ obj.uuid }}/delete', true);
xhttp.send();
} else {
abox = document.getElementById('abortalert');
abox.style.display = "block";
}
}
</script>
{% endblock %}
</html>

View file

@ -4,24 +4,57 @@
<body> <body>
<div class="container shadow"> <div class="container shadow">
<form>
{% csrf_token %}
<table class="objecttable"> <table class="objecttable">
<tr class="objecttable-row"> <tr>
<th class="objecttable-head">Name</th> <th class="objecttable-head"><a href="./0">Name</a></th>
<th class="objecttable-head">Anzahl</th> <th class="objecttable-head"><a href="./1">Anzahl</a></th>
<th class="objecttable-head">Kategorie</th> <th class="objecttable-head"><a href="./2">Kategorie</a></th>
<th class="objecttable-head">Inventarisierungsdatum</th> <th class="objecttable-head"><a href="./3">Inventarisierungsdatum</a></th>
<th class="objecttable-head">Hinzugefügt von</th> <th class="objecttable-head"><a href="./4">Hinzugefügt von</a></th>
</tr> </tr>
{% for object in objects %} {% for object in objects %}
<tr class="objecttable-row"> <tr class="objecttable-row">
<td class="objecttable-data">{{ object.title }}</td> <td class="objecttable-data">
<td class="objecttable-data">{{ object.ammout }}</td> <a class="celllink" href="./{{ object.uuid }}">{{ object.title }}</a>
<td class="objecttable-data">{{ object.category }}</td> </td>
<td class="objecttable-data">{{ object.inventarized_date }}</td> <td class="objecttable-data">
<td class="objecttable-data">{{ object.user_added }}</td> <a class="celllink" href="./{{ object.uuid }}">{{ object.ammout }}</a>
</td>
{% if object.category is not None %}
<td class="objecttable-data">
<a class="celllink" href="./{{ object.uuid }}">{{ object.category }}</a>
</td>
{% else %}
<td class="objecttable-data">
<a class="celllink" href="./{{ object.uuid }}">-------</a>
</td>
{% endif %}
<td class="objecttable-data">
<a class="celllink" href="./{{ object.uuid }}">{{ object.inventarized_date }}</a>
</td>
<td class="objecttable-data">
<a class="celllink" href="./{{ object.uuid }}">{{ object.user_added }}</a>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
<p>{{ objammout }} Objekte insgesamt</p>
</form>
</div>
<div class="container shadow lower-box">
<table class="cattable">
<tr>
<th class="objecttable-head">Kategorie</th>
</tr>
{% for category in categories %}
<tr class="objecttable-row">
<td class="objecttable-data">{{ category.name }}</td>
</tr>
{% endfor %}
</table>
<p>{{ ncats }} Kategorien insgesamt</p>
</div> </div>
</body> </body>
{% endblock %} {% endblock %}