diff options
author | Andrea Lepori <alepori@student.ethz.ch> | 2020-06-20 23:32:55 +0200 |
---|---|---|
committer | Andrea Lepori <alepori@student.ethz.ch> | 2020-06-20 23:32:55 +0200 |
commit | 26fb6c43a27c014383127d0e4b3fb29f8b923690 (patch) | |
tree | 05fd542c342cf12cac72b49e5b4b44db7411d91a /client/views.py | |
parent | Document custom field (diff) | |
download | scout-subs-26fb6c43a27c014383127d0e4b3fb29f8b923690.tar.gz scout-subs-26fb6c43a27c014383127d0e4b3fb29f8b923690.zip |
Doc, doctypes, users all done
Diffstat (limited to 'client/views.py')
-rw-r--r-- | client/views.py | 113 |
1 files changed, 101 insertions, 12 deletions
diff --git a/client/views.py b/client/views.py index f70d930..b610962 100644 --- a/client/views.py +++ b/client/views.py @@ -1,6 +1,6 @@ from random import randint from django.contrib.auth.models import Group, Permission, User -from client.models import UserCode, Keys, DocumentType, Document, PersonalData, KeyVal +from client.models import UserCode, Keys, DocumentType, Document, PersonalData, KeyVal, MedicalData from django.db.models import Q from django.http import HttpResponseRedirect @@ -19,17 +19,54 @@ def index(request): code = randint(100000, 999999) if len(UserCode.objects.filter(code=code)) == 0: break - userCode = UserCode(user=request.user, code=code) + medic = MedicalData() + medic.save() + userCode = UserCode(user=request.user, code=code, medic=medic) userCode.save() + if request.method == "POST": + document = Document.objects.get(id=request.POST["action"][1:]) + if request.POST["action"][0] == 'f': + pass + elif request.POST["action"][0] == 'a': + document.status = "ok" + document.save() + elif request.POST["action"][0] == 'd': + document.delete() + elif request.POST["action"][0] == 'e': + document_type = document.document_type + context = { + 'doctype': document_type, + } + context['doc'] = document + 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, KeyVal.objects.filter(Q(container=document) & Q(key=i.key))[0].value]) + context['keys'] = out_keys + context['custom_message'] = document_type.custom_message + context['custom_message_text'] = document_type.custom_message_text + return edit_wrapper(request, context) + documents = Document.objects.filter(Q(user=request.user) & ~Q(status='archive')) out = [] for i in documents: - out.append([i, KeyVal.objects.filter(container=i)]) + personal = None + medical = None + if i.document_type.personal_data: + personal = i.personal_data.__dict__.values() + if i.document_type.medical_data: + medical = i.medical_data.__dict__.values() + + out.append([i, KeyVal.objects.filter(container=i), personal, medical]) context = { "docs": out, "empty": len(out) == 0, } + return render(request, 'client/index.html', context) @@ -45,7 +82,6 @@ def approve(request): else: return render(request, 'client/index.html', context) - def create(request): context = {} if request.user.is_authenticated: @@ -59,7 +95,7 @@ def create(request): if len(Document.objects.filter(Q(user=request.user) & Q(document_type=doc))) == 0: out.append(doc) - context = {'docs': out} + context['docs'] = out if request.method == "POST": if request.POST["action"] == "details": if "doctype" not in request.POST.keys(): @@ -85,31 +121,84 @@ def create(request): code = 0 status = "wait" personal_data = None + medical_data = None document_type = DocumentType.objects.get( id=request.POST["doctype"]) + + if document_type.auto_sign: + status = "autosign" + keys = [] if document_type.personal_data: - personal_data = PersonalData(parent_name=usercode.parent_name, via=usercode.via, cap=usercode.cap, country=usercode.country, + personal_data = PersonalData(email=request.user.email, 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 document_type.medical_data: + medic = usercode.medic + medical_data = MedicalData(emer_name=medic.emer_name, emer_relative=medic.emer_relative, cell_phone=medic.cell_phone, address=medic.address, emer_phone=medic.emer_phone, health_care=medic.health_care, injuries=medic.injuries, rc=medic.rc, rega=medic.rega, medic_name=medic.medic_name, medic_phone=medic.medic_phone, medic_address=medic.medic_address, sickness=medic.sickness, vaccine=medic.vaccine, tetanus_date=medic.tetanus_date, allergy=medic.allergy, drugs_bool=medic.drugs_bool, drugs=medic.drugs, misc_bool=medic.misc_bool, misc=medic.misc) + medical_data.save() + 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) + user=request.user, group=group, code=code, status=status, document_type=document_type, personal_data=personal_data, medical_data=medical_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() + if document_type.custom_data: + 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() return HttpResponseRedirect('/') return render(request, 'client/doc_create.html', context) else: return render(request, 'client/index.html', context) + +def edit(request): + return edit_wrapper(request, {}) + +def edit_wrapper(request, context): + if request.user.is_authenticated: + if request.method == "POST": + if "action" not in request.POST.keys(): + document = Document.objects.get(id=request.POST["doc"]) + usercode = UserCode.objects.filter(user=document.user)[0] + + if document.document_type.personal_data: + personal_data = PersonalData(email=request.user.email, 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() + old_data = document.personal_data + document.personal_data = personal_data + document.save() + old_data.delete() + + if document.document_type.medical_data: + medic = usercode.medic + medical_data = MedicalData(emer_name=medic.emer_name, emer_relative=medic.emer_relative, cell_phone=medic.cell_phone, address=medic.address, emer_phone=medic.emer_phone, health_care=medic.health_care, injuries=medic.injuries, rc=medic.rc, rega=medic.rega, medic_name=medic.medic_name, medic_phone=medic.medic_phone, medic_address=medic.medic_address, sickness=medic.sickness, vaccine=medic.vaccine, tetanus_date=medic.tetanus_date, allergy=medic.allergy, drugs_bool=medic.drugs_bool, drugs=medic.drugs, misc_bool=medic.misc_bool, misc=medic.misc) + medical_data.save() + old_data = document.medical_data + document.medical_data = medical_data + document.save() + old_data.delete() + + if document.document_type.custom_data: + for i in request.POST.keys(): + if i == "doc" or i=="csrfmiddlewaretoken": + continue + key = KeyVal.objects.filter(Q(container=document) & Q(key=Keys.objects.get(id=i).key))[0] + key.value = request.POST[i] + key.save() + + return HttpResponseRedirect('/') + + return render(request, 'client/doc_edit.html', context) + else: + return render(request, 'client/index.html', context) |