aboutsummaryrefslogtreecommitdiffstats
path: root/client/views.py
diff options
context:
space:
mode:
authorAndrea Lepori <alepori@student.ethz.ch>2020-06-20 15:55:28 +0200
committerAndrea Lepori <alepori@student.ethz.ch>2020-06-20 15:55:28 +0200
commitccc5d1f1b2cb4b5e2a95b97bbd1a5760ce585f08 (patch)
tree6912e8d96bce8bdf7782c7877a7a0928560c7a1a /client/views.py
parentDocument support (diff)
downloadscout-subs-ccc5d1f1b2cb4b5e2a95b97bbd1a5760ce585f08.tar.gz
scout-subs-ccc5d1f1b2cb4b5e2a95b97bbd1a5760ce585f08.zip
Document custom field
Diffstat (limited to 'client/views.py')
-rw-r--r--client/views.py77
1 files changed, 55 insertions, 22 deletions
diff --git a/client/views.py b/client/views.py
index 78ca7d1..f70d930 100644
--- a/client/views.py
+++ b/client/views.py
@@ -1,7 +1,8 @@
from random import randint
from django.contrib.auth.models import Group, Permission, User
-from client.models import UserCode, Keys, DocumentType, Document, PersonalData
+from client.models import UserCode, Keys, DocumentType, Document, PersonalData, KeyVal
from django.db.models import Q
+from django.http import HttpResponseRedirect
from django.shortcuts import render
@@ -21,10 +22,10 @@ def index(request):
userCode = UserCode(user=request.user, code=code)
userCode.save()
- documents = Document.objects.filter(user=request.user)
+ documents = Document.objects.filter(Q(user=request.user) & ~Q(status='archive'))
out = []
for i in documents:
- out.append(i)
+ out.append([i, KeyVal.objects.filter(container=i)])
context = {
"docs": out,
"empty": len(out) == 0,
@@ -51,31 +52,63 @@ def create(request):
parent_group = request.user.groups.values_list('name', flat=True)[
0]
group = Group.objects.get(name=parent_group)
- public_types = DocumentType.objects.filter(
- Q(group_private=False) | Q(group=group))
+ doctypes = DocumentType.objects.filter(
+ (Q(group_private=False) | Q(group=group)) & Q(enabled=True))
out = []
- for doc in public_types:
- out.append(doc)
+ for doc in doctypes:
+ if len(Document.objects.filter(Q(user=request.user) & Q(document_type=doc))) == 0:
+ out.append(doc)
context = {'docs': out}
if request.method == "POST":
- usercode = UserCode.objects.filter(user=request.user)[0]
- code = 0
- status = 0
- document_type = DocumentType.objects.get(
- id=request.POST["doctype"])
- personal_data = PersonalData(parent_name=usercode.parent_name, via=usercode.via, cap=usercode.cap, country=usercode.country,
- nationality=usercode.nationality, born_date=usercode.born_date, home_phone=usercode.home_phone, phone=usercode.phone)
- personal_data.save()
+ if request.POST["action"] == "details":
+ if "doctype" not in request.POST.keys():
+ context['error'] = True
+ context['error_text'] = "Seleziona un documento"
+ else:
+ context['next'] = True
+ document_type = DocumentType.objects.get(
+ id=request.POST["doctype"])
+ context['doctype'] = document_type
+ context['personal_data'] = document_type.personal_data
+ context['medical_data'] = document_type.medical_data
+ context['custom_data'] = document_type.custom_data
+ keys = Keys.objects.filter(container=document_type)
+ out_keys = []
+ for i in keys:
+ out_keys.append([i, ""])
+ context['keys'] = out_keys
+ context['custom_message'] = document_type.custom_message
+ context['custom_message_text'] = document_type.custom_message_text
+ elif request.POST["action"] == "save":
+ usercode = UserCode.objects.filter(user=request.user)[0]
+ code = 0
+ status = "wait"
+ personal_data = None
+ document_type = DocumentType.objects.get(
+ id=request.POST["doctype"])
+ keys = []
+ if document_type.personal_data:
+ personal_data = PersonalData(parent_name=usercode.parent_name, via=usercode.via, cap=usercode.cap, country=usercode.country,
+ nationality=usercode.nationality, born_date=usercode.born_date, home_phone=usercode.home_phone, phone=usercode.phone)
+ personal_data.save()
- while (True):
- code = randint(100000, 999999)
- if len(Document.objects.filter(code=code)) == 0:
- break
+ while (True):
+ code = randint(100000, 999999)
+ if len(Document.objects.filter(code=code)) == 0:
+ break
+
+ document = Document(
+ user=request.user, group=group, code=code, status=status, document_type=document_type, personal_data=personal_data)
+ document.save()
+
+ for i in request.POST.keys():
+ if i == "doctype" or i=="csrfmiddlewaretoken" or i=="action":
+ continue
+ key = KeyVal(container=document, key=Keys.objects.get(id=i).key, value=request.POST[i])
+ key.save()
- document = Document(
- user=request.user, group=group, code=code, status=status, document_type=document_type, personal_data=personal_data)
- document.save()
+ return HttpResponseRedirect('/')
return render(request, 'client/doc_create.html', context)
else: