From ffb9b4111a891fda9e9e1ddf19de936bdbd664f8 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Sat, 20 Jun 2020 00:28:16 +0200 Subject: Document support --- accounts/templates/accounts/index.html | 33 +++++++++ client/migrations/0007_auto_20200619_1508.py | 26 +++++++ client/migrations/0008_auto_20200619_1538.py | 74 ++++++++++++++++++++ client/migrations/0009_auto_20200619_1546.py | 32 +++++++++ client/migrations/0010_documenttype_enabled.py | 18 +++++ client/migrations/0011_keys.py | 22 ++++++ client/migrations/0012_document_group.py | 20 ++++++ client/models.py | 57 ++++++++++----- client/templates/client/doc_create.html | 42 +++++++++++ client/templates/client/index.html | 51 ++++++++++++++ client/urls.py | 3 +- client/views.py | 51 +++++++++++++- server/templates/server/doc_edit.html | 79 +++++++++++++++++++++ server/templates/server/doc_list.html | 70 +++++++++++++++++++ server/templates/server/doc_type.html | 70 +++++++++++++++++++ server/templates/server/index.html | 3 +- server/urls.py | 3 + server/views.py | 96 +++++++++++++++++++++++++- templates/registration/base.html | 4 ++ templates/registration/base_custom.html | 32 +-------- 20 files changed, 735 insertions(+), 51 deletions(-) create mode 100644 client/migrations/0007_auto_20200619_1508.py create mode 100644 client/migrations/0008_auto_20200619_1538.py create mode 100644 client/migrations/0009_auto_20200619_1546.py create mode 100644 client/migrations/0010_documenttype_enabled.py create mode 100644 client/migrations/0011_keys.py create mode 100644 client/migrations/0012_document_group.py create mode 100644 client/templates/client/doc_create.html create mode 100644 server/templates/server/doc_edit.html create mode 100644 server/templates/server/doc_list.html create mode 100644 server/templates/server/doc_type.html diff --git a/accounts/templates/accounts/index.html b/accounts/templates/accounts/index.html index c01878c..5ba2dc1 100644 --- a/accounts/templates/accounts/index.html +++ b/accounts/templates/accounts/index.html @@ -143,4 +143,37 @@
Tabella medica
+{% endblock %} + +{% block script %} + var elem = $('.tabs') + var options = { + yearRange:100, + format:'dd.mm.yyyy', + i18n: { + months: [ 'gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre' ], + monthsShort: [ 'gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic' ], + weekdays: [ 'domenica', 'lunedì', 'martedì', 'mercoledì', 'giovedì', 'venerdì', 'sabato' ], + weekdaysShort: [ 'dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab' ], + weekdaysAbbrev: [ 'D', 'L', 'M', 'M', 'G', 'V', 'S' ], + today: 'Oggi', + clear: 'Cancella', + close: 'Chiudi', + firstDay: 1, + format: 'dddd d mmmm yyyy', + formatSubmit: 'yyyy/mm/dd', + labelMonthNext: 'Mese successivo', + labelMonthPrev: 'Mese precedente', + labelMonthSelect: 'Seleziona un mese', + labelYearSelect: 'Seleziona un anno' + }} + var instance = M.Tabs.init(elem, options); + document.addEventListener('DOMContentLoaded', function() { + var elems = document.querySelectorAll('.datepicker'); + var instances = M.Datepicker.init(elems, options); + }); + document.addEventListener('DOMContentLoaded', function() { + var elems = document.querySelectorAll('select'); + var instances = M.FormSelect.init(elems, options); + }); {% endblock %} \ No newline at end of file diff --git a/client/migrations/0007_auto_20200619_1508.py b/client/migrations/0007_auto_20200619_1508.py new file mode 100644 index 0000000..a5f2669 --- /dev/null +++ b/client/migrations/0007_auto_20200619_1508.py @@ -0,0 +1,26 @@ +# Generated by Django 3.0.7 on 2020-06-19 13:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('client', '0006_auto_20200619_1049'), + ] + + operations = [ + migrations.RemoveField( + model_name='yearsubscription', + name='group', + ), + migrations.RemoveField( + model_name='yearsubscription', + name='school', + ), + migrations.AlterField( + model_name='yearsubscription', + name='year', + field=models.CharField(default='', max_length=250), + ), + ] diff --git a/client/migrations/0008_auto_20200619_1538.py b/client/migrations/0008_auto_20200619_1538.py new file mode 100644 index 0000000..c890cd1 --- /dev/null +++ b/client/migrations/0008_auto_20200619_1538.py @@ -0,0 +1,74 @@ +# Generated by Django 3.0.7 on 2020-06-19 13:38 + +import datetime +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('client', '0007_auto_20200619_1508'), + ] + + operations = [ + migrations.CreateModel( + name='KeyVal', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('key', models.CharField(db_index=True, max_length=240)), + ('value', models.CharField(db_index=True, max_length=240)), + ], + ), + migrations.CreateModel( + name='PersonalData', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('parent_name', models.CharField(default='', max_length=250)), + ('via', models.CharField(default='', max_length=250)), + ('cap', models.CharField(default='', max_length=250)), + ('country', models.CharField(default='', max_length=250)), + ('nationality', models.CharField(default='', max_length=250)), + ('born_date', models.DateField(default=datetime.datetime(1970, 1, 1, 1, 0), null=True)), + ('home_phone', models.CharField(default='', max_length=250)), + ('phone', models.CharField(default='', max_length=250)), + ], + ), + migrations.AddField( + model_name='document', + name='compilation_date', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + migrations.AddField( + model_name='document', + name='document_type', + field=models.CharField(default='', max_length=50), + ), + migrations.AddField( + model_name='document', + name='status', + field=models.CharField(default='', max_length=50), + ), + migrations.AddField( + model_name='document', + name='user', + field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + migrations.DeleteModel( + name='YearSubscription', + ), + migrations.AddField( + model_name='keyval', + name='container', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='client.Document'), + ), + migrations.AddField( + model_name='document', + name='personal_data', + field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.PROTECT, to='client.PersonalData'), + ), + ] diff --git a/client/migrations/0009_auto_20200619_1546.py b/client/migrations/0009_auto_20200619_1546.py new file mode 100644 index 0000000..88961d2 --- /dev/null +++ b/client/migrations/0009_auto_20200619_1546.py @@ -0,0 +1,32 @@ +# Generated by Django 3.0.7 on 2020-06-19 13:46 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0011_update_proxy_permissions'), + ('client', '0008_auto_20200619_1538'), + ] + + operations = [ + migrations.CreateModel( + name='DocumentType', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('group_private', models.BooleanField(default=False)), + ('personal_data', models.BooleanField(default=False)), + ('medical_data', models.BooleanField(default=False)), + ('custom_data', models.BooleanField(default=False)), + ('name', models.CharField(default='', max_length=250)), + ('group', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='auth.Group')), + ], + ), + migrations.AlterField( + model_name='document', + name='document_type', + field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.PROTECT, to='client.DocumentType'), + ), + ] diff --git a/client/migrations/0010_documenttype_enabled.py b/client/migrations/0010_documenttype_enabled.py new file mode 100644 index 0000000..24928b0 --- /dev/null +++ b/client/migrations/0010_documenttype_enabled.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-06-19 16:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('client', '0009_auto_20200619_1546'), + ] + + operations = [ + migrations.AddField( + model_name='documenttype', + name='enabled', + field=models.BooleanField(default=False), + ), + ] diff --git a/client/migrations/0011_keys.py b/client/migrations/0011_keys.py new file mode 100644 index 0000000..8739908 --- /dev/null +++ b/client/migrations/0011_keys.py @@ -0,0 +1,22 @@ +# Generated by Django 3.0.7 on 2020-06-19 20:36 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('client', '0010_documenttype_enabled'), + ] + + operations = [ + migrations.CreateModel( + name='Keys', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('key', models.CharField(db_index=True, max_length=240)), + ('container', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='client.DocumentType')), + ], + ), + ] diff --git a/client/migrations/0012_document_group.py b/client/migrations/0012_document_group.py new file mode 100644 index 0000000..a1fbabc --- /dev/null +++ b/client/migrations/0012_document_group.py @@ -0,0 +1,20 @@ +# Generated by Django 3.0.7 on 2020-06-19 21:05 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0011_update_proxy_permissions'), + ('client', '0011_keys'), + ] + + operations = [ + migrations.AddField( + model_name='document', + name='group', + field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='auth.Group'), + ), + ] diff --git a/client/models.py b/client/models.py index d6e8578..01d5f4c 100644 --- a/client/models.py +++ b/client/models.py @@ -1,25 +1,21 @@ from django.db import models -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from datetime import datetime # Create your models here. -class Document(models.Model): - code = models.IntegerField(default=0) - - class Meta: - permissions = [ - ("approved", "The user is approved") - ] +class DocumentType(models.Model): + enabled = models.BooleanField(default=False) + group_private = models.BooleanField(default=False) + group = models.ForeignKey(Group, default=None, on_delete=models.CASCADE) + personal_data = models.BooleanField(default=False) + medical_data = models.BooleanField(default=False) + custom_data = models.BooleanField(default=False) + name = models.CharField(default="", max_length=250) -class YearSubscription(models.Model): - user = models.ForeignKey(User, default=None, on_delete=models.CASCADE) - group = models.CharField(default="", max_length=50) - compilation_date = models.DateTimeField(auto_now_add=True) - status = models.CharField(default="", max_length=50) - code = models.IntegerField(default=0) +class PersonalData(models.Model): parent_name = models.CharField(default="", max_length=250) via = models.CharField(default="", max_length=250) cap = models.CharField(default="", max_length=250) @@ -28,8 +24,37 @@ class YearSubscription(models.Model): born_date = models.DateField(null=True, default=datetime.fromtimestamp(0)) home_phone = models.CharField(default="", max_length=250) phone = models.CharField(default="", max_length=250) - school = models.CharField(default="", max_length=250) - year = models.IntegerField(default=0) + + +class Document(models.Model): + user = models.ForeignKey(User, default=None, on_delete=models.CASCADE) + group = models.ForeignKey(Group, default=None, on_delete=models.CASCADE) + code = models.IntegerField(default=0) + compilation_date = models.DateTimeField(auto_now_add=True) + status = models.CharField(default="", max_length=50) + document_type = models.ForeignKey( + DocumentType, default=None, on_delete=models.PROTECT) + + personal_data = models.ForeignKey( + PersonalData, default=None, on_delete=models.PROTECT) + + class Meta: + permissions = [ + ("approved", "The user is approved") + ] + + +class KeyVal(models.Model): + container = models.ForeignKey( + Document, db_index=True, on_delete=models.CASCADE) + key = models.CharField(max_length=240, db_index=True) + value = models.CharField(max_length=240, db_index=True) + + +class Keys(models.Model): + container = models.ForeignKey( + DocumentType, db_index=True, on_delete=models.CASCADE) + key = models.CharField(max_length=240, db_index=True) class UserCode(models.Model): diff --git a/client/templates/client/doc_create.html b/client/templates/client/doc_create.html new file mode 100644 index 0000000..a52518f --- /dev/null +++ b/client/templates/client/doc_create.html @@ -0,0 +1,42 @@ +{% extends 'registration/base.html' %} + +{% block title %}Admin - Modifica Tipo{% endblock %} + +{% block nav %} + Home + Crea Doc +{% endblock %} + +{% block content %} +
+
+
+
+
+ {% csrf_token %} +
+
+ + +
+
+ +
+
+
+
+
+{% endblock %} + +{% block script %} +$(document).ready(function(){ + $('select').formSelect(); + }); +{% endblock %} \ No newline at end of file diff --git a/client/templates/client/index.html b/client/templates/client/index.html index 64b4d42..2b19f30 100644 --- a/client/templates/client/index.html +++ b/client/templates/client/index.html @@ -7,8 +7,45 @@ {% endblock%} {% block content %} +
+
+
Aggiungi un documento
+

Usa questo bottone per creare un nuovo documento

+
+
{% if user.is_authenticated %} {% if user.is_staff or perms.client.approved %} +
+ {% csrf_token %} + +
+
+ + add + +
{% else %}
@@ -34,4 +71,18 @@
{% endif %} +{% endblock %} + +{% block script %} +$(document).ready(function(){ + $('.tap-target').tapTarget(); + {% if empty %} + $('.tap-target').tapTarget('open'); + {% endif %} +}); +$('*').click(function(event) { + if (this === event.target) { + $('.tap-target').tapTarget('close'); + } +}); {% endblock %} \ No newline at end of file diff --git a/client/urls.py b/client/urls.py index 4e9f08e..b58433e 100644 --- a/client/urls.py +++ b/client/urls.py @@ -4,5 +4,6 @@ from . import views urlpatterns = [ path('', views.index, name='index'), - path('approve', views.approve, name='approve') + path('approve', views.approve, name='approve'), + path('create', views.create, name='create'), ] diff --git a/client/views.py b/client/views.py index 96eb977..78ca7d1 100644 --- a/client/views.py +++ b/client/views.py @@ -1,9 +1,10 @@ from random import randint +from django.contrib.auth.models import Group, Permission, User +from client.models import UserCode, Keys, DocumentType, Document, PersonalData +from django.db.models import Q from django.shortcuts import render -from .models import UserCode - # Create your views here. @@ -19,6 +20,15 @@ def index(request): break userCode = UserCode(user=request.user, code=code) userCode.save() + + documents = Document.objects.filter(user=request.user) + out = [] + for i in documents: + out.append(i) + context = { + "docs": out, + "empty": len(out) == 0, + } return render(request, 'client/index.html', context) @@ -33,3 +43,40 @@ def approve(request): return render(request, 'client/approve.html', context) else: return render(request, 'client/index.html', context) + + +def create(request): + context = {} + if request.user.is_authenticated: + 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)) + out = [] + for doc in public_types: + 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() + + 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() + + return render(request, 'client/doc_create.html', context) + else: + return render(request, 'client/index.html', context) diff --git a/server/templates/server/doc_edit.html b/server/templates/server/doc_edit.html new file mode 100644 index 0000000..cca0607 --- /dev/null +++ b/server/templates/server/doc_edit.html @@ -0,0 +1,79 @@ +{% extends 'registration/base.html' %} + +{% block title %}Admin - Modifica Tipo{% endblock %} + +{% block nav %} + Home + Admin + Tipo Doc + Modifica Doc +{% endblock %} + +{% block content %} +
+
+
+
+
+ {% csrf_token %} +
+
+ + +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ + +
+
+ +
+
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/server/templates/server/doc_list.html b/server/templates/server/doc_list.html new file mode 100644 index 0000000..cf98010 --- /dev/null +++ b/server/templates/server/doc_list.html @@ -0,0 +1,70 @@ +{% extends 'registration/base_custom.html' %} + +{% block title %}Admin - Documenti{% endblock %} + +{% block nav %} + +{% endblock %} + +{% block content %} +
+{% csrf_token %} + +
+
+ + add + +
+{% endblock %} + +{%block script%} +$(document).ready(function(){ + $('.collapsible').collapsible(); +}); +{% endblock %} \ No newline at end of file diff --git a/server/templates/server/doc_type.html b/server/templates/server/doc_type.html new file mode 100644 index 0000000..75bb79a --- /dev/null +++ b/server/templates/server/doc_type.html @@ -0,0 +1,70 @@ +{% extends 'registration/base_custom.html' %} + +{% block title %}Admin - Tipo Documenti{% endblock %} + +{% block nav %} + +{% endblock %} + +{% block content %} +
+{% csrf_token %} + +
+
+ + add + +
+{% endblock %} + +{%block script%} +$(document).ready(function(){ + $('.collapsible').collapsible(); +}); +{% endblock %} \ No newline at end of file diff --git a/server/templates/server/index.html b/server/templates/server/index.html index 37a553b..4095d4a 100644 --- a/server/templates/server/index.html +++ b/server/templates/server/index.html @@ -44,7 +44,8 @@ I am convenient because I require little markup to use effectively.

- Lista documenti + Lista tipo doc + Lista documenti Approva documento
diff --git a/server/urls.py b/server/urls.py index a41b346..de94d50 100644 --- a/server/urls.py +++ b/server/urls.py @@ -6,4 +6,7 @@ urlpatterns = [ path('', views.index, name='server'), path('uapprove', views.uapprove, name='uapprove'), path('ulist', views.ulist, name='ulist'), + path('doctype', views.doctype, name='doctype'), + path('docedit', views.docedit, name='docedit'), + path('doclist', views.doclist, name='doclist'), ] diff --git a/server/views.py b/server/views.py index aa2dc3a..aed1fb6 100644 --- a/server/views.py +++ b/server/views.py @@ -1,6 +1,8 @@ from django.shortcuts import render -from client.models import UserCode +from client.models import UserCode, Keys, DocumentType, Document from django.contrib.auth.models import Group, Permission, User +from django.db.models import Q +from django.http import HttpResponseRedirect # Create your views here. @@ -136,3 +138,95 @@ def ulist(request): return render(request, 'server/user_list.html', context) else: return render(request, 'client/index.html', context) + + +def doctype(request): + context = {} + if request.user.is_staff: + if request.method == "POST": + selected = [] + for i in request.POST.keys(): + if i == "csrfmiddlewaretoken": + continue + selected.append(DocumentType.objects.get(id=i)) + + for i in selected: + i.delete() + + 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)) + out = [] + for doc in public_types: + out.append(doc) + + context = {'docs': out} + return render(request, 'server/doc_type.html', context) + else: + return render(request, 'client/index.html', context) + + +def docedit(request): + context = {} + if request.user.is_staff: + parent_group = request.user.groups.values_list('name', flat=True)[ + 0] + group = Group.objects.get(name=parent_group) + enabled = False + group_private = False + personal_data = False + medical_data = False + custom_data = False + name = "" + + enabled_check = "" + private_check = 'checked="checked"' + personal_check = 'checked="checked"' + medical_check = "" + custom_check = "" + context = { + "enabled_check": enabled_check, + "private_check": private_check, + "personal_check": personal_check, + "medical_check": medical_check, + "custom_check": custom_check, + } + if request.method == "POST": + enabled = "enabled" in request.POST.keys() + group_private = "group_private" in request.POST.keys() + personal_data = "personal_data" in request.POST.keys() + medical_data = "medical_data" in request.POST.keys() + custom_data = "custom_data" in request.POST.keys() + name = request.POST["name"] + custom = request.POST["custom"] + custom += " " + custom = custom.split("\n") + doctype = DocumentType( + name=request.POST["name"], enabled=enabled, group_private=group_private, group=group, personal_data=personal_data, medical_data=medical_data, custom_data=custom_data) + doctype.save() + for i in custom: + key = Keys(key=i[:-1], container=doctype) + key.save() + return HttpResponseRedirect('doctype') + + return render(request, 'server/doc_edit.html', context) + else: + return render(request, 'client/index.html', context) + + +def doclist(request): + context = {} + if request.user.is_staff: + parent_group = request.user.groups.values_list('name', flat=True)[ + 0] + group = Group.objects.get(name=parent_group) + documents = Document.objects.filter(group=group) + out = [] + for i in documents: + out.append(i) + context = {"docs": out} + return render(request, 'server/doc_list.html', context) + else: + return render(request, 'client/index.html', context) diff --git a/templates/registration/base.html b/templates/registration/base.html index f5a8a80..2e4f5b2 100644 --- a/templates/registration/base.html +++ b/templates/registration/base.html @@ -39,5 +39,9 @@ integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"> + \ No newline at end of file diff --git a/templates/registration/base_custom.html b/templates/registration/base_custom.html index d38be7a..56b9180 100644 --- a/templates/registration/base_custom.html +++ b/templates/registration/base_custom.html @@ -20,36 +20,8 @@ crossorigin="anonymous"> \ No newline at end of file -- cgit v1.2.1