def add_forms(form): fields = [] for i in range(1, int(form.get('totalFields')) + 1): question = { 'type': form.get(f'{i}_type'), 'question': form.get(f'{i}_question') } options = {} if question['type'] in ['radio', 'checkbox']: for j in range(1, int(form.get(f"{i}_total_options")) + 1): options[f'{j}'] = form.get(f"{i}_{j}_option") question['options'] = options fields.append(question) data = { 'creator': User().first_name + " " + User().last_name, 'title': form.get('formName'), 'description': form.get('formDesc'), 'fields': fields } url = daftar.settings.DAFTAR_HOST + "/form" hed = {'Authorization': 'Bearer ' + User().token} response = requests.post(url, json=data, headers=hed) if response.status_code == requests.codes.ok: form = json.loads(response.text) return form['id'] else: return False
def get_applications(user_id=None, filter=None, limit=None): url = daftar.settings.DAFTAR_HOST + "/applications" if filter is not None: url += '?filter=' + str(filter) + '&' if limit is not None: url += '?limit=' + str(limit) hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) if response.status_code != 200: return False docs = [] if user_id is not None: for doc in response.json(): application = Application(doc) print(application.authorities) if User().id in application.authorities: docs.append(application) else: for doc in response.json(): docs.append(Application(doc)) return docs
def add_workflows(form): stages = [] for i in range(1, int(form.get('totalStages')) + 1): id_name = form.get(f'{i}_name').split('_') stage = { 'authId': id_name[0], 'authRole': form.get(f'{i}_role'), 'authName': id_name[1] } stages.append(stage) data = { "name": form.get('workflowName'), "creatorId": User().id, "totalStages": form.get('totalStages'), "stages": stages } url = daftar.settings.DAFTAR_HOST + "/workflow" hed = {'Authorization': 'Bearer ' + User().token} response = requests.post(url, json=data, headers=hed) if response.status_code == requests.codes.ok: return json.loads(response.text) else: return False
def new_document(request): if verify_token(request): return render( request, 'new_document.html', { 'title': 'Daftar | New Document', 'user_type': User().type, 'first_name': User().first_name }) else: return redirect('/')
def my_account(request): if verify_token(request): return render( request, 'account.html', { 'title': 'Daftar | My Account', 'user_type': User().type, 'first_name': User().first_name, 'user': User() }) else: return redirect('/')
def new_application_step1(request): if verify_token(request): if request.method == "GET": forms = get_forms() if forms is False: # TODO: Error handling print('Error Loading Forms') return render( request, 'new_application_step1.html', { 'title': 'Daftar | New Application Step 1', 'user_type': User().type, 'first_name': User().first_name, 'forms': forms }) elif request.method == "POST": application_name = request.POST.get('name') if request.POST.get('form_id') is None: form_id = add_forms(request.POST) print(form_id) form_name = get_form(form_id).title else: form_id = request.POST.get('form_id') form_name = get_form(form_id).title workflows = get_workflows() if workflows is False: # TODO: Error handling print('Error Loading Workflows') auth_list = get_authorities() if auth_list is False: # TODO: Error handling print('Error Loading Documents') return return render( request, 'new_application_step2.html', { 'title': 'Daftar | New Application Step 2', 'user_type': User().type, 'first_name': User().first_name, 'workflows': workflows, 'application_name': application_name, 'form_id': form_id, 'form_name': form_name, 'authorities': auth_list }) else: return redirect('/')
def add_application(request): if verify_token(request): if request.method == 'POST': if request.POST.get('workflow_id') is None: workflow = add_workflows(request.POST) workflow_id = workflow['id'] else: workflow_id = request.POST.get('workflow_id') data = { 'name': request.POST.get('name'), 'formId': request.POST.get('form_id'), 'workflowId': workflow_id } url = daftar.settings.DAFTAR_HOST + "/applications/templates" hed = {'Authorization': 'Bearer ' + User().token} response = requests.post(url, json=data, headers=hed) print(response.text) if response.status_code == requests.codes.ok: return HttpResponseRedirect(reverse('application_templates')) else: return HttpResponseRedirect(reverse('new_application_step1')) else: return redirect('/')
def submit_applications(form): form_details = {} i = 1 for i in range(1, int(form.get('fields_count')) + 1): if form.get(f'{i}_type') == "checkbox": answers = [] for j in range(1, int(form.get(f'{i}_answer_count')) + 1): if form.get(f'{i}_answer_{j}') is not None: answers.append(form.get(f'{i}_answer_{j}')) form_details[form.get(f'{i}_question')] = answers else: form_details[form.get(f'{i}_question')] = form.get(f'{i}_answer') data = { "name": form.get('name'), "templateId": form.get('id'), "form": form_details } print(data) url = daftar.settings.DAFTAR_HOST + "/applications" hed = {'Authorization': 'Bearer ' + User().token} response = requests.post(url, json=data, headers=hed) if response.status_code == requests.codes.ok: # if False: return True else: return False
def application_template(request): if verify_token(request): application_templates = get_application_templates() if application_templates is False: # TODO: Error handling print('Error Loading Documents') return render( request, 'application_template.html', { 'title': 'Daftar | Application Templates', 'user_type': User().type, 'first_name': User().first_name, 'application_templates': application_templates }) else: return redirect('/')
def form(request): if verify_token(request): forms = get_forms() if forms is False: # TODO: Error handling print('Error Loading Documents') return render( request, 'form.html', { 'title': 'Daftar | Forms', 'user_type': User().type, 'first_name': User().first_name, 'forms': forms }) else: return redirect('/')
def workflow(request): if verify_token(request): workflows = get_workflows() if workflows is False: # TODO: Error handling print('Error Loading Documents') return render( request, 'workflow.html', { 'title': 'Daftar | Workflows', 'user_type': User().type, 'first_name': User().first_name, 'workflows': workflows }) else: return redirect('/')
def new_workflow(request): if verify_token(request): auth_list = get_authorities() if auth_list is False: # TODO: Error handling print('Error Loading Documents') return return render( request, 'new_workflow.html', { 'title': 'Daftar | Workflow', 'user_type': User().type, 'first_name': User().first_name, 'authorities': auth_list }) else: return redirect('/')
def fetch_workflow(request): if verify_token(request): if request.method == "POST": url = daftar.settings.DAFTAR_HOST + "/workflow/" + str( request.POST.get('id')) hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) return HttpResponse(response, content_type=json)
def storage(request): if verify_token(request): docs = get_storage_documents() if docs is False: # TODO: Error handling print('Error Loading Documents') return return render( request, 'storage.html', { 'title': 'Daftar | Storage', 'user_type': User().type, 'first_name': User().first_name, 'docs': docs }) else: return redirect('/')
def save_changes(form): url = daftar.settings.DAFTAR_HOST + "/settings/costOfPaper/" + form.get( "costOfPaper") hed = {'Authorization': 'Bearer ' + User().token} response = requests.post(url, {}, headers=hed) if response.status_code == requests.codes.ok: return True else: return False
def application(request): if verify_token(request): applications = get_applications() pending_applications = [] rejected_applications = [] approved_applications = [] all_applications = [] for application in applications: if application.creatorId == User().id: if application.status == 0: pending_applications.append(application) elif application.status == 1: approved_applications.append(application) else: rejected_applications.append(application) all_applications.append(application) cost_per_paper = User().costOfPaper money_saved = cost_per_paper * len(all_applications) trees_saved = len(all_applications) / 8333 trees_saved = round(trees_saved, 5) if all_applications is False: # TODO: Error handling print('Error Loading Documents') return return render( request, 'application.html', { 'title': 'Daftar | Applications', 'user_type': User().type, 'first_name': User().first_name, 'user_id': User().id, 'all_applications': all_applications, 'pending_applications': pending_applications, 'approved_applications': approved_applications, 'rejected_applications': rejected_applications, 'cost_per_paper': cost_per_paper, 'money_saved': money_saved, 'trees_saved': trees_saved }) else: return redirect('/')
def logout(request): hed = {'Authorization': 'Bearer ' + User().token} r = requests.delete(daftar.settings.DAFTAR_HOST + "/auth/logout", headers=hed) if r.status_code == requests.codes.ok: request.session.flush() return redirect('/') else: return HttpResponse("Error: Logging out!")
def get_workflow(workflow_id): url = daftar.settings.DAFTAR_HOST + "/workflow/" + workflow_id hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) if response.status_code != 200: return False workflow = Workflow(response.json()) return workflow
def get_form(form_id): url = daftar.settings.DAFTAR_HOST + "/form/" + form_id hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) if response.status_code != 200: return False form = Form(response.json()) return form
def get_application_template(application_template_id): url = daftar.settings.DAFTAR_HOST + "/applications/templates/" + application_template_id hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) if response.status_code != 200: return False application_template = ApplicationTemplates(response.json()) return application_template
def index(request): if verify_token(request): docs = get_storage_documents(limit=4) applications = get_applications(limit=3) cost_per_paper = User().costOfPaper all_applications = get_applications() money_saved = cost_per_paper * len(all_applications) money_saved = round(money_saved, 5) trees_saved = len(all_applications) / 8333 trees_saved = round(trees_saved, 5) pending_applications = len(get_applications(filter='pending')) signed_applications = len(get_applications(filter='signed')) rejected_applications = len(get_applications(filter='rejected')) if docs is False: # TODO: Error handling print('Error Loading Documents') return if applications is False: # TODO: Error handling print('Error Loading Applications') return return render( request, 'dashboard.html', { 'title': 'Daftar | Dashboard', 'user_type': User().type, 'first_name': User().first_name, 'docs': docs, 'applications': applications, 'money_saved': money_saved, 'trees_saved': trees_saved, 'pending_applications': pending_applications, 'signed_applications': signed_applications, 'rejected_applications': rejected_applications }) else: return redirect('/')
def new_application_template(request): if verify_token(request): forms = get_forms() if forms is False: # TODO: Error handling print('Error Loading Forms') workflows = get_workflows() if workflows is False: # TODO: Error handling print('Error Loading Workflows') return render( request, 'new_application_template.html', { 'title': 'Daftar | New Application Template', 'user_type': User().type, 'first_name': User().first_name, 'forms': forms, 'workflows': workflows }) else: return redirect('/')
def add_application_templates(form): data = { "name": form.get('name'), "workflowId": form.get('workflow_id'), "formId": form.get('form_id') } url = daftar.settings.DAFTAR_HOST + "/applications/templates" hed = {'Authorization': 'Bearer ' + User().token} response = requests.post(url, json=data, headers=hed) if response.status_code == requests.codes.ok: return True else: return False
def fill_application(request): if verify_token(request): if request.method == 'GET': applications = get_application_templates() if applications is False: # TODO: Error handling print('Error Loading Documents') return render( request, 'fill_application.html', { 'title': 'Daftar | Fill Applications', 'user_type': User().type, 'first_name': User().first_name, 'applications': applications, 'isApplicationNotSelected': True }) else: application_template_id = request.POST.get( 'application_template_id') application_template = get_application_template( application_template_id) form = get_form(application_template.formId) workflow = get_workflow(application_template.workflowId) return render( request, 'fill_application.html', { 'title': 'Daftar | Fill Applications', 'user_type': User().type, 'first_name': User().first_name, 'application_template': application_template, 'form': form, 'workflow': workflow, 'fields': form.fields, 'isApplicationNotSelected': False }) else: return redirect('/')
def upload_new_document(fileName, fileDesc, file, fileExt): url = daftar.settings.DAFTAR_HOST + "/storage" hed = {'Authorization': 'Bearer ' + User().token} data = { "fileName": fileName, "fileDescription": fileDesc, "fileExt": fileExt } response = requests.post(url, data, headers=hed, files=dict(file=file)) if response.status_code == requests.codes.ok: return True else: print(response.json()) return False
def get_storage_documents(limit=None): url = daftar.settings.DAFTAR_HOST + "/storage" if limit is not None: url += '?limit=' + str(limit) hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) if response.status_code != 200: return False docs = [] for doc in response.json(): docs.append(StorageDocument(doc)) return docs
def get_authorities(filter=None, limit=None): url = daftar.settings.DAFTAR_HOST + "/users" if filter is not None: url += '?filter=' + str(filter) + '&' if limit is not None: url += '?limit=' + str(limit) hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) auth_list = [] for auth in response.json(): auth_list.append(Authority(auth)) return auth_list
def export(request): if verify_token(request): if request.method == "POST": url = daftar.settings.DAFTAR_HOST + "/" + request.POST.get( 'name') + "?excel" hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) file_name = default_storage.save('excel', ContentFile(response.content)) file_url = default_storage.url(file_name) f = open("media/" + file_name + ".xls", 'x') f.close() f = open("media/" + file_name + ".xls", 'wb+') f.write(response.content) f.close() return HttpResponse(file_url + ".xls")
def sign_applications(form): url = daftar.settings.DAFTAR_HOST + "/applications/" + form.get('id') if form.get('action') == "Sign": url += "/sign" else: url += "/reject" data = {"message": form.get('message')} hed = {'Authorization': 'Bearer ' + User().token} response = requests.post(url, json=data, headers=hed) print(response.text) print(response.status_code) if response.status_code == requests.codes.ok: # if True: return True else: return False
def get_workflows(filter=None, limit=None): url = daftar.settings.DAFTAR_HOST + "/workflow" if filter is not None: url += '?filter=' + str(filter) + '&' if limit is not None: url += '?limit=' + str(limit) hed = {'Authorization': 'Bearer ' + User().token} response = requests.get(url, headers=hed) if response.status_code != 200: return False workflows = [] for workflow in response.json(): workflows.append(Workflow(workflow)) return workflows