diff options
-rw-r--r-- | inventar/templates/base.html | 1 | ||||
-rw-r--r-- | inventar/templates/upload.html | 17 | ||||
-rw-r--r-- | inventar/urls.py | 3 | ||||
-rw-r--r-- | inventory/forms.py | 9 | ||||
-rw-r--r-- | inventory/views.py | 63 |
5 files changed, 92 insertions, 1 deletions
diff --git a/inventar/templates/base.html b/inventar/templates/base.html index a97ed7e..8304687 100644 --- a/inventar/templates/base.html +++ b/inventar/templates/base.html @@ -16,6 +16,7 @@ <li><a href="{% url "home" %}"><img src="/static/home.svg" width="24" height="24" /> {% trans "Home" %}</a></li> <li><a href="{% url "admin:index" %}"><img src="/static/admin.svg" width="24" height="24" /> {% trans "Admin Area" %}</a></li> <li><a href="{% url "stats" %}"><img src="/static/stats.svg" width="24" height="24" /> {% trans "Statistics" %}</a></li> + <li><a href="{% url "upload" %}"><img src="/static/stats.svg" width="24" height="24" /> {% trans "Upload" %}</a></li> <li><br></li> <li>{% trans "Search Item" %}: <ul> diff --git a/inventar/templates/upload.html b/inventar/templates/upload.html new file mode 100644 index 0000000..29a2dbe --- /dev/null +++ b/inventar/templates/upload.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} + {% if user.is_authenticated %} + <h2>Upload new Parent Tree:</h2> + + <form enctype="multipart/form-data" method="post" action="/upload/">{% csrf_token %} + {{form}} + <input type="submit" value="Submit" /> + <ul class="path"> + {% for code in data %} + <li>{{ code }}</li> + {% endfor %} + </ul> + {% endif %} +{% endblock %} diff --git a/inventar/urls.py b/inventar/urls.py index 7162524..370b283 100644 --- a/inventar/urls.py +++ b/inventar/urls.py @@ -24,16 +24,17 @@ from inventory.views import item from inventory.views import search from inventory.views import graph from inventory.views import stats +from inventory.views import upload urlpatterns = patterns('', url(r'^$', home, name='home'), - #(r'^openid/', include('django_openid_auth.urls')), url(r'^item/(?P<selectedid>[0-9A-Za-z]{4,6})/$', item, name='item'), url(r'^search/(?P<term>[^/]+)/$', search, name='search'), url(r'^graph/$', graph, name='graph'), url(r'^stats/$', stats, name='stats'), + url(r'^upload/$', upload, name='upload'), url(r'^admin/', include(admin.site.urls)), ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/inventory/forms.py b/inventory/forms.py new file mode 100644 index 0000000..264f8cb --- /dev/null +++ b/inventory/forms.py @@ -0,0 +1,9 @@ +from django import forms + + +class UploadFileForm(forms.Form): + + types = (('1', 'Parent Tree'), ('2', 'Regal')) + + file = forms.FileField() + type = forms.ChoiceField(widget=forms.RadioSelect, choices=types)
\ No newline at end of file diff --git a/inventory/views.py b/inventory/views.py index f54e6b7..82aab79 100644 --- a/inventory/views.py +++ b/inventory/views.py @@ -15,14 +15,21 @@ from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext_lazy as _ from django.shortcuts import render_to_response +from django.shortcuts import render from django.http import HttpResponse from django.db.models import Q import models from django.core.mail import mail_admins +from forms import UploadFileForm +from django.core.exceptions import ObjectDoesNotExist +from models import Barcode + + def home(request): return render_to_response('home.html') + def item(request, selectedid): p = get_object_or_404(models.Barcode, pk=selectedid.upper()) @@ -72,6 +79,7 @@ def item(request, selectedid): } return render_to_response('item.html', parameters) + def search(request, term): results = models.Item.objects.filter(Q(name__contains = term) | Q(description__contains = term)) @@ -80,6 +88,7 @@ def search(request, term): "results": results }) + def graph(request): items = models.Item.objects.all() @@ -88,6 +97,60 @@ def graph(request): return result + +def upload(request): + #if request.method == 'POST': + form = UploadFileForm(request.POST, request.FILES) + if form.is_valid() and request.user.is_authenticated: + codefile = request.FILES['file'] + data = codefile.read().splitlines() + + if request.POST['type'] == '1': + parent = '' + for code in data: + if code != 'NEWPARENT': + if parent == '': + p = get_object_or_404(models.Barcode, pk=code.upper()) + parent = code + else: + i = get_object_or_404(models.Barcode, pk=code.upper()) + i.item.parent = p.item + i.item.save() + else: + parent = '' + elif request.POST['type'] == '2': + print('Regal') + index = 0 + error = 0 + business_area = models.BusinessArea.objects.get(name='ideell') + category = models.Category.objects.get(name='Inventar') + parent = models.Barcode.objects.get(code='H0000') + tag = models.Tag.objects.get(name='Regale & Storage') + + for code in data: + if code[0] == 'H' and len(code) == 5: + try: + b = Barcode.objects.get(code=code.upper()) + error += 1 + data[index] = code + ' existiert bereits' + except ObjectDoesNotExist: + print('Yay!') + description = 'Borte im Hochregal \nReihe: ' + code[1] + '\nRegal: ' + code[2] + i = models.Item(name='Regalborte', description=description, business_area=business_area, category=category, parent=parent.item) + i.save() + barcode = Barcode(code=code, item=i) + barcode.save() + print(i) + else: + data[index] = code + ' ist nicht im richtigen Format' + error += 1 + index += 1 + return render(request, 'upload.html', {'form': form, 'data': data}) + else: + form = UploadFileForm() + return render(request, 'upload.html', {'form': form}) + + def stats(request): return render_to_response('stats.html', { "number_of_codes": models.Barcode.objects.count(), |