From adb087d58bbec664338c18ba609c2e9ea11d6362 Mon Sep 17 00:00:00 2001 From: sandeepsajan0 <sandeepsajan0@gmail.com> Date: Wed, 6 Jul 2022 18:32:37 +0530 Subject: [PATCH] Limit single file field block for PAF --- hypha/apply/projects/admin.py | 3 +++ hypha/apply/projects/admin_views.py | 37 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 hypha/apply/projects/admin_views.py diff --git a/hypha/apply/projects/admin.py b/hypha/apply/projects/admin.py index a5f69ceb1..2192a9050 100644 --- a/hypha/apply/projects/admin.py +++ b/hypha/apply/projects/admin.py @@ -1,5 +1,6 @@ from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup +from .admin_views import CreateProjectApprovalFormView, EditProjectApprovalFormView from .models import DocumentCategory, ProjectApprovalForm @@ -13,6 +14,8 @@ class ProjectApprovalFormAdmin(ModelAdmin): model = ProjectApprovalForm menu_icon = 'form' list_display = ('name', 'used_by',) + create_view_class = CreateProjectApprovalFormView + edit_view_class = EditProjectApprovalFormView def used_by(self, obj): rows = list() diff --git a/hypha/apply/projects/admin_views.py b/hypha/apply/projects/admin_views.py new file mode 100644 index 000000000..1356717a7 --- /dev/null +++ b/hypha/apply/projects/admin_views.py @@ -0,0 +1,37 @@ +from wagtail.contrib.modeladmin.views import CreateView, EditView + +from hypha.apply.utils.blocks import show_admin_form_error_messages + + +class CreateProjectApprovalFormView(CreateView): + + def get_form(self): + """ + Overriding this method to disable the single file block option from Project Approval Form. + Set 0 as max_number of single file can be added to make single file block option unavailable or disable. + """ + form = super(CreateProjectApprovalFormView, self).get_form() + form.fields['form_fields'].block.meta.block_counts = {'file': {'min_num': 0, 'max_num': 0}} + return form + + def form_invalid(self, form): + show_admin_form_error_messages(self.request, form) + return self.render_to_response(self.get_context_data(form=form)) + + +class EditProjectApprovalFormView(EditView): + + def get_form(self): + """ + Overriding this method to disable the single file block option from Project Approval Form. + Calculating the number of Single file blocks that exist in the instance already. + And set that count as max_number of single file block can be added to make single file option disable. + """ + form = super(EditProjectApprovalFormView, self).get_form() + single_file_count = sum(1 for block in self.get_instance().form_fields.raw_data if block['type'] == 'file') + form.fields['form_fields'].block.meta.block_counts = {'file': {'min_num': 0, 'max_num': single_file_count}} + return form + + def form_invalid(self, form): + show_admin_form_error_messages(self.request, form) + return self.render_to_response(self.get_context_data(form=form)) -- GitLab