diff options
-rw-r--r-- | client/templates/client/approve_doc_pdf.html | 5 | ||||
-rw-r--r-- | server/templates/server/approve_doc.html | 84 | ||||
-rw-r--r-- | server/templates/server/index.html | 1 | ||||
-rw-r--r-- | server/views.py | 103 | ||||
-rw-r--r-- | version.txt | 2 |
5 files changed, 171 insertions, 24 deletions
diff --git a/client/templates/client/approve_doc_pdf.html b/client/templates/client/approve_doc_pdf.html index db1478e..244bc20 100644 --- a/client/templates/client/approve_doc_pdf.html +++ b/client/templates/client/approve_doc_pdf.html @@ -9,7 +9,10 @@ <body style="background: white;"> <main style="margin-left: 10px;margin-right: 10px;margin-top: 10px; background: white;"> <br><br><br><br> - <br><br><br><br> + <br><br> + <div class="row"> + <p class="center">{{doc.user.first_name}} {{doc.user.last_name}} ({{doc.user.username}})</p> + </div> <div class="row"> <h2 class="center">{{doc.code}}</h2> </div> diff --git a/server/templates/server/approve_doc.html b/server/templates/server/approve_doc.html index 23eef4f..f9985bb 100644 --- a/server/templates/server/approve_doc.html +++ b/server/templates/server/approve_doc.html @@ -29,6 +29,7 @@ <form id="form" action="{% url 'docapprove'%}" method="post"> {% csrf_token %} <div class="card-content"> + <span class="card-title">Approvazione multipla</span> <p>Inserire un codice per riga</p> <textarea name="codes" class="materialize-textarea"></textarea> </div> @@ -39,4 +40,87 @@ </div> </div> </div> + +<form target="_blank" action="{% url 'docpreview' %}" method="post" id="preview_form"> + {% csrf_token %} + <input type="hidden" name="preview" id="code_submit"> +</form> + +<div class="row"> + <div class="col l4 offset-l4 m8 offset-m2 s12"> + <div class="card"> + <form id="form2" action="{% url 'docapprove'%}" method="post" enctype="multipart/form-data"> + {% csrf_token %} + <div class="card-content"> + <span class="card-title">Approvazione singola</span> + <p>Possibilità di caricare foto della firma e visualizzare il documento prima di approvarlo</p> + {{error_text}} + <div class="row"> + <div class="input-field col s12"> + <input name="code" id="code" type="text"> + <label for="code">Codice documento</label> + </div> + </div> + <div class="row"> + <div class="file-field input-field col s12"> + <div class="btn {{color}}"> + <span><i class="material-icons left">file_upload</i>File</span> + <input type="file" name="doc_sign" id="doc_sign" onchange="loadFile(event)"> + </div> + <div class="file-path-wrapper"> + <input id="doc_sign_name" class="file-path" type="text" placeholder="Documento firmato"> + </div> + </div> + </div> + </div> + <div class="card-action"> + <a id="send_button" class="waves-effect waves-light btn {{color}}" href="#" onclick="confirm()">Approva</a> + <a class="right waves-effect waves-light btn {{color}}" onclick="send()">Anteprima documento</a> + </div> + </form> + </div> + </div> +</div> + +<div class="row"> + <div class="col l4 offset-l4 m8 offset-m2 s12"> + <div class="card"> + <div class="card-image"> + <img id="preview"/> + </div> + </div> + </div> +</div> +{% endblock %} + +{% block script %} +function confirm() { + var button = document.getElementById('send_button') + button.innerHTML = "Sicuro?" + button.setAttribute('onclick', "document.getElementById('form2').submit()") + button.setAttribute('class', "waves-effect waves-light btn green") +} + +function send() { + var form = document.getElementById('preview_form') + var action = document.getElementById('code_submit') + var text = document.getElementById('code').value + action.setAttribute('value', text); + form.submit() +} + +var loadFile = function(event) { + var output = document.getElementById('preview'); + output.src = URL.createObjectURL(event.target.files[0]); + output.onload = function() { + URL.revokeObjectURL(output.src) // free memory + } +}; +$(document).ready(function(){ + {% if error %} + M.toast({html: '{{ error_text }}', classes: 'orange'}) + {% elif success %} + M.toast({html: '{{ success_text }}', classes: 'green'}) + {% endif %} +}); {% endblock %}
\ No newline at end of file diff --git a/server/templates/server/index.html b/server/templates/server/index.html index 8c60b48..1d78a74 100644 --- a/server/templates/server/index.html +++ b/server/templates/server/index.html @@ -89,7 +89,6 @@ <a class="waves-effect waves-light btn {{color}}" href="{% url 'doctype' %}">Tipi</a> <a class="waves-effect waves-light btn {{color}}" href="{% url 'doclist' %}">Documenti</a> <a class="waves-effect waves-light btn {{color}}" href="{% url 'docapprove' %}">Approva documento</a> - <a class="waves-effect waves-light btn {{color}}" href="{% url 'docupload' %}">Carica firma</a> </div> <div class="hide-on-large-only"> diff --git a/server/views.py b/server/views.py index 35fb232..96b8d9f 100644 --- a/server/views.py +++ b/server/views.py @@ -180,40 +180,101 @@ def docapprove(request): context = {}
data = []
- # if user not staff of primary has only controll of non primary groups
+ # if user not staff of primary has only control of non primary groups
if request.user.is_staff:
groups = request.user.groups.values_list('name', flat=True)
else:
groups = request.user.groups.values_list('name', flat=True)[1:]
+ # setup variables for error text and success text
+ error = False
+ success = False
+ error_text = ""
+ success_text = ""
+
+ document = None
+ messages = []
+
if request.method == "POST":
- # parse text in array
- data = request.POST["codes"]
- data = split_codes(data)
- # check if code valid
- for i in range(len(data)):
- if not data[i].isdigit():
- data[i] = data[i] + " - Formato errato"
- elif int(data[i]) < 100000 or int(data[i]) > 999999:
- data[i] = data[i] + " - Formato errato"
- elif Document.objects.filter(code=data[i]).count() == 0:
- data[i] = data[i] + " - Invalido"
- elif Document.objects.filter(code=data[i])[0].group.name not in groups:
- # check if user has permission to approve document
- data[i] = data[i] + " - Invalido"
+ # check if bulk approve or single
+ if "codes" in request.POST:
+ # parse text in array
+ data = request.POST["codes"]
+ data = split_codes(data)
+ # check if code valid
+ for i in range(len(data)):
+ if not data[i].isdigit():
+ messages.append(data[i] + " - Formato errato")
+ elif int(data[i]) < 100000 or int(data[i]) > 999999:
+ messages.append(data[i] + " - Formato errato")
+ elif Document.objects.filter(code=data[i]).count() == 0:
+ messages.append(data[i] + " - Invalido")
+ elif Document.objects.filter(code=data[i])[0].group.name not in groups:
+ # check if user has permission to approve document
+ messages.append(data[i] + " - Invalido")
+ else:
+ document = Document.objects.filter(code=data[i])[0]
+ if document.status == 'ok':
+ # do nothing document already approved
+ messages.append(data[i] + " - Già approvato")
+ else:
+ document.status = 'ok'
+ document.save()
+ messages.append(data[i] + " - Ok")
+
+ elif "code" in request.POST:
+ print("doing this")
+ data = request.POST["code"]
+ if not data.isdigit():
+ error_text = "Formato codice errato"
+ error = True
+ elif int(data) < 100000 or int(data) > 999999:
+ error_text = "Formato codice errato"
+ error = True
+ elif Document.objects.filter(code=data).count() == 0:
+ error_text = "Codice invalido"
+ error = True
+ elif Document.objects.filter(code=data)[0].group.name not in groups:
+ error_text = "Codice invalido"
+ error = True
else:
- document = Document.objects.filter(code=data[i])[0]
+ # get document
+ document = Document.objects.filter(code=data)[0]
+
+ # prepare success message
if document.status == 'ok':
- # do nothing document already approved
- data[i] = data[i] + " - Già approvato"
+ success_text = "File caricato"
+ success = True
else:
document.status = 'ok'
document.save()
- data[i] = data[i] + " - Ok"
+ success_text = "Documento approvato e file caricato"
+ success = True
+
+ # check for errors and upload files
+ if "doc_sign" in request.FILES and not error:
+ myfile = request.FILES['doc_sign']
+ try:
+ im = Image.open(myfile)
+ im_io = BytesIO()
+ # compress image in WEBP
+ im.save(im_io, 'WEBP', quality=50)
+ document.signed_doc.save(data+"_"+myfile.name, im_io)
+ document.save()
+ except UnidentifiedImageError:
+ error = True
+ error_text = "Il file non è un immagine valida"
+ else:
+ error = True
+ error_text = "Prego caricare un file"
context = {
- 'messages': data,
- 'empty': len(data) == 0,
+ 'messages': messages,
+ 'empty': len(messages) == 0,
+ "error": error,
+ "error_text": error_text,
+ "success": success,
+ "success_text": success_text,
}
return render(request, 'server/approve_doc.html', context)
diff --git a/version.txt b/version.txt index 9ea1174..f8c6175 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ version=0.5 -rev=37 +rev=38 |