From 05463489f459761e93b8921b5627626ee3adf420 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Tue, 25 Jul 2023 13:47:43 +0200 Subject: create docs from uc --- ..._alter_document_user_alter_document_usercode.py | 26 ++++++++++++++++ client/models.py | 4 +-- client/templates/client/approve_doc_pdf.html | 2 +- client/templates/client/doc_create.html | 8 ++++- client/templates/client/index.html | 8 ++--- client/urls.py | 2 +- client/views.py | 36 ++++++++++++---------- 7 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 client/migrations/0020_alter_document_user_alter_document_usercode.py (limited to 'client') diff --git a/client/migrations/0020_alter_document_user_alter_document_usercode.py b/client/migrations/0020_alter_document_user_alter_document_usercode.py new file mode 100644 index 0000000..8df069d --- /dev/null +++ b/client/migrations/0020_alter_document_user_alter_document_usercode.py @@ -0,0 +1,26 @@ +# Generated by Django 4.1.5 on 2023-07-25 10:46 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('client', '0019_alter_document_usercode'), + ] + + operations = [ + migrations.AlterField( + model_name='document', + name='user', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='document', + name='usercode', + field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='client.usercode'), + ), + ] diff --git a/client/models.py b/client/models.py index c39a5ea..c6cfdfd 100644 --- a/client/models.py +++ b/client/models.py @@ -97,8 +97,8 @@ class UserCode(models.Model): class Document(models.Model): - usercode = models.ForeignKey(UserCode, default=None, on_delete=models.CASCADE, null=True) - user = models.ForeignKey(User, default=None, on_delete=models.CASCADE) + usercode = models.ForeignKey(UserCode, default=None, on_delete=models.CASCADE) + user = models.ForeignKey(User, default=None, on_delete=models.CASCADE, null=True) group = models.ForeignKey(Group, default=None, on_delete=models.CASCADE) code = models.IntegerField(default=0) compilation_date = models.DateTimeField(auto_now_add=True) diff --git a/client/templates/client/approve_doc_pdf.html b/client/templates/client/approve_doc_pdf.html index d2ba29b..0a62886 100644 --- a/client/templates/client/approve_doc_pdf.html +++ b/client/templates/client/approve_doc_pdf.html @@ -16,7 +16,7 @@

-

{{doc.user.first_name}} {{doc.user.last_name}} ({{doc.user.username}})

+

{{doc.usercode.first_name}} {{doc.usercode.last_name}}

{{doc.code}}

diff --git a/client/templates/client/doc_create.html b/client/templates/client/doc_create.html index c42fa05..a6efae8 100644 --- a/client/templates/client/doc_create.html +++ b/client/templates/client/doc_create.html @@ -13,10 +13,16 @@
-
+ {% csrf_token %} {% if not next %} +
+
+ {{uc.first_name}} {{uc.last_name}} +
{{uc.born_date}}
+
+
+
- +
@@ -162,7 +162,7 @@
- +
diff --git a/client/urls.py b/client/urls.py index 4da5169..d76aa96 100644 --- a/client/urls.py +++ b/client/urls.py @@ -4,7 +4,7 @@ from . import views urlpatterns = [ path('', views.index, name='index'), - path('create', views.create, name='create'), + path('create/', views.create, name='create'), path('edit', views.edit, name='edit'), path('about', views.about, name='about'), ] diff --git a/client/views.py b/client/views.py index 5160927..2ed0c6b 100644 --- a/client/views.py +++ b/client/views.py @@ -46,7 +46,7 @@ def index(request): document = Document.objects.get(id=request.POST["action"][1:]) # check if document is valid to modify - if document.user != request.user: + if document.usercode not in ucs: return HttpResponseRedirect("/") if document.status == "ok" or document.status == "archive": @@ -124,26 +124,28 @@ def index(request): @login_required -def create(request): +def create(request, code): context = {} - usercode = UserCode.objects.filter(user=request.user)[0] - - if usercode.midata_id > 0: - if not copy_from_midata(request, usercode): - return HttpResponseRedirect(request.path_info) + usercode = UserCode.objects.filter(user=request.user, code=code) + if (len(usercode) == 0): + # the user has no person + return HttpResponseRedirect("/") - # group name and obj - parent_groups = request.user.groups.values_list('name', flat=True) + usercode = usercode[0] + + if usercode.branca == None: + return HttpResponseRedirect("/") # get available types for user - filter = (Q(group_private=False) | Q(group__name__in=parent_groups)) & Q(enabled=True) + filter = (Q(group_private=False) | Q(group__name=usercode.branca.name)) & Q(enabled=True) if not request.user.is_staff and "capi" not in request.user.groups.values_list('name',flat = True): filter = filter & Q(staff_only=False) # remove from the list documents from already used types - doctypes = DocumentType.objects.filter(filter).values_list("id", flat=True).difference(Document.objects.filter(Q(user=request.user) & ~Q(status="archive")).select_related("document_type").values_list("document_type", flat=True)) - doctypes = doctypes.difference(HideGroup.objects.filter(group__name__in=parent_groups).select_related("doc_type").values_list("doc_type", flat=True)) + doctypes = DocumentType.objects.filter(filter).values_list("id", flat=True).difference(Document.objects.filter(Q(usercode=usercode) & ~Q(status="archive")).select_related("document_type").values_list("document_type", flat=True)) + doctypes = doctypes.difference(HideGroup.objects.filter(group__name=usercode.branca.name).select_related("doc_type").values_list("doc_type", flat=True)) + context["uc"] = usercode context['docs'] = DocumentType.objects.filter(id__in=doctypes) if request.method == "POST": if request.POST["action"] == "details": @@ -190,12 +192,12 @@ def create(request): # user is cheating abort return HttpResponseRedirect("/") - if document_type.group_private and document_type.group.name not in request.user.groups.values_list('name', flat=True): + if document_type.group_private and document_type.group.name != usercode.branca.name: # user is cheating abort return HttpResponseRedirect("/") # get list of docs with that type - current_docs = Document.objects.filter(user=request.user).filter(Q(document_type=document_type) & ~Q(status="archive")) + current_docs = Document.objects.filter(usercode=usercode).filter(Q(document_type=document_type) & ~Q(status="archive")) if len(current_docs) > 0: # if there is already a document with that type abort (user is cheating) return HttpResponseRedirect("/") @@ -224,13 +226,13 @@ def create(request): # generate document code while (True): - code = randint(100000, 999999) - if len(Document.objects.filter(code=code)) == 0: + dcode = randint(100000, 999999) + if len(Document.objects.filter(code=dcode)) == 0: break # save document document = Document( - user=request.user, group=document_type.group, code=code, status=status, document_type=document_type, personal_data=personal_data, medical_data=medical_data) + usercode=usercode, group=document_type.group, code=dcode, status=status, document_type=document_type, personal_data=personal_data, medical_data=medical_data) document.save() # attach custom keys -- cgit v1.2.1