NEMO-custom-forms 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. nemo_custom_forms-1.0.1/LICENSE +6 -0
  2. nemo_custom_forms-1.0.1/NEMO_custom_forms/__init__.py +0 -0
  3. nemo_custom_forms-1.0.1/NEMO_custom_forms/admin.py +259 -0
  4. nemo_custom_forms-1.0.1/NEMO_custom_forms/apps.py +24 -0
  5. nemo_custom_forms-1.0.1/NEMO_custom_forms/migrations/0001_initial.py +431 -0
  6. nemo_custom_forms-1.0.1/NEMO_custom_forms/migrations/0002_alter_customformdocuments_display_order.py +21 -0
  7. nemo_custom_forms-1.0.1/NEMO_custom_forms/migrations/__init__.py +0 -0
  8. nemo_custom_forms-1.0.1/NEMO_custom_forms/models.py +775 -0
  9. nemo_custom_forms-1.0.1/NEMO_custom_forms/notifications.py +25 -0
  10. nemo_custom_forms-1.0.1/NEMO_custom_forms/pdf_utils.py +356 -0
  11. nemo_custom_forms-1.0.1/NEMO_custom_forms/static/NEMO_custom_forms/custom-forms.css +7 -0
  12. nemo_custom_forms-1.0.1/NEMO_custom_forms/static/NEMO_custom_forms/fonts/dancing_script.ttf +0 -0
  13. nemo_custom_forms-1.0.1/NEMO_custom_forms/templates/NEMO_custom_forms/choose_template.html +23 -0
  14. nemo_custom_forms-1.0.1/NEMO_custom_forms/templates/NEMO_custom_forms/custom_form.html +222 -0
  15. nemo_custom_forms-1.0.1/NEMO_custom_forms/templates/NEMO_custom_forms/custom_form_templates.html +44 -0
  16. nemo_custom_forms-1.0.1/NEMO_custom_forms/templates/NEMO_custom_forms/custom_forms.html +98 -0
  17. nemo_custom_forms-1.0.1/NEMO_custom_forms/templates/NEMO_custom_forms/custom_forms_base.html +117 -0
  18. nemo_custom_forms-1.0.1/NEMO_custom_forms/templatetags/__init__.py +0 -0
  19. nemo_custom_forms-1.0.1/NEMO_custom_forms/templatetags/custom_form_tags.py +24 -0
  20. nemo_custom_forms-1.0.1/NEMO_custom_forms/tests/__init__.py +0 -0
  21. nemo_custom_forms-1.0.1/NEMO_custom_forms/tests/test_custom_forms.py +234 -0
  22. nemo_custom_forms-1.0.1/NEMO_custom_forms/tests/test_settings.py +87 -0
  23. nemo_custom_forms-1.0.1/NEMO_custom_forms/urls.py +52 -0
  24. nemo_custom_forms-1.0.1/NEMO_custom_forms/utilities.py +84 -0
  25. nemo_custom_forms-1.0.1/NEMO_custom_forms/views/__init__.py +0 -0
  26. nemo_custom_forms-1.0.1/NEMO_custom_forms/views/custom_forms.py +480 -0
  27. nemo_custom_forms-1.0.1/NEMO_custom_forms.egg-info/PKG-INFO +78 -0
  28. nemo_custom_forms-1.0.1/NEMO_custom_forms.egg-info/SOURCES.txt +33 -0
  29. nemo_custom_forms-1.0.1/NEMO_custom_forms.egg-info/dependency_links.txt +1 -0
  30. nemo_custom_forms-1.0.1/NEMO_custom_forms.egg-info/requires.txt +13 -0
  31. nemo_custom_forms-1.0.1/NEMO_custom_forms.egg-info/top_level.txt +1 -0
  32. nemo_custom_forms-1.0.1/PKG-INFO +78 -0
  33. nemo_custom_forms-1.0.1/README.md +35 -0
  34. nemo_custom_forms-1.0.1/pyproject.toml +58 -0
  35. nemo_custom_forms-1.0.1/setup.cfg +4 -0
@@ -0,0 +1,6 @@
1
+ # NIST Software Licensing Statement
2
+ NIST-developed software is provided by NIST as a public service. You may use, copy, and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify, and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.
3
+
4
+ NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.
5
+
6
+ You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.
File without changes
@@ -0,0 +1,259 @@
1
+ import json
2
+ import os
3
+
4
+ from NEMO.mixins import ModelAdminRedirectMixin
5
+ from NEMO.models import User
6
+ from NEMO.utilities import copy_media_file, new_model_copy
7
+ from NEMO.widgets.dynamic_form import DynamicForm
8
+ from django import forms
9
+ from django.contrib import admin, messages
10
+ from django.db import transaction
11
+ from django.urls import reverse
12
+ from django.utils.html import format_html
13
+ from django.utils.safestring import mark_safe
14
+
15
+ from NEMO_custom_forms.models import (
16
+ CustomForm,
17
+ CustomFormAction,
18
+ CustomFormActionRecord,
19
+ CustomFormAutomaticNumbering,
20
+ CustomFormDisplayColumn,
21
+ CustomFormDocumentType,
22
+ CustomFormDocuments,
23
+ CustomFormPDFTemplate,
24
+ CustomFormSpecialMapping,
25
+ )
26
+ from NEMO_custom_forms.utilities import custom_forms_current_numbers
27
+
28
+
29
+ @admin.action(description="Duplicate selected templates")
30
+ def duplicate_custom_form_template(modeladmin, request, queryset):
31
+ for template in queryset.all():
32
+ with transaction.atomic():
33
+ template: CustomFormPDFTemplate = template
34
+ template_copy = new_model_copy(template)
35
+ template_copy.name = "Copy of " + template.name
36
+ if CustomFormPDFTemplate.objects.filter(name=template_copy.name).exists():
37
+ messages.error(request, f"Template with name {template_copy.name} already exists. Skipping.")
38
+ else:
39
+ new_form_name = template_copy.get_filename_upload(os.path.basename(template.form.name))
40
+ copy_media_file(template.form.name, new_form_name)
41
+ template_copy.form.name = new_form_name
42
+ template_copy.save()
43
+ for display_column in template.customformdisplaycolumn_set.all():
44
+ display_column_copy = new_model_copy(display_column)
45
+ display_column_copy.template = template_copy
46
+ display_column_copy.save()
47
+ old_to_new_action_ids = {}
48
+ for action in template.customformaction_set.all():
49
+ action_copy = new_model_copy(action)
50
+ action_copy.template = template_copy
51
+ action_copy.save()
52
+ old_to_new_action_ids[action.id] = action_copy.id
53
+ for special_mapping in template.customformspecialmapping_set.all():
54
+ special_mapping_copy = new_model_copy(special_mapping)
55
+ special_mapping_copy.template = template_copy
56
+ if special_mapping.field_value_action_id:
57
+ special_mapping_copy.field_value_action_id = old_to_new_action_ids[
58
+ special_mapping.field_value_action_id
59
+ ]
60
+ special_mapping_copy.save()
61
+ messages.success(
62
+ request,
63
+ mark_safe(
64
+ f"Template {template.name} successfully duplicated to <a href='{reverse('admin:NEMO_custom_forms_customformpdftemplate_change', args=[template_copy.id])}'>{template_copy.name}</a>."
65
+ ),
66
+ )
67
+
68
+
69
+ class CustomFormActionFormset(forms.BaseInlineFormSet):
70
+ model = CustomFormAction
71
+
72
+
73
+ class CustomFormSpecialMappingFormset(forms.BaseInlineFormSet):
74
+ model = CustomFormSpecialMapping
75
+
76
+ def add_fields(self, form, index):
77
+ super().add_fields(form, index)
78
+ form.fields["field_value_action"].queryset = CustomFormAction.objects.filter(
79
+ template=self.instance
80
+ ).select_related("template")
81
+
82
+
83
+ class CustomFormActionAdminInline(admin.TabularInline):
84
+ model = CustomFormAction
85
+ formset = CustomFormActionFormset
86
+
87
+ def get_queryset(self, request):
88
+ queryset = super().get_queryset(request).select_related("template")
89
+ return queryset
90
+
91
+
92
+ class CustomFormSpecialMappingAdminInline(admin.TabularInline):
93
+ model = CustomFormSpecialMapping
94
+ formset = CustomFormSpecialMappingFormset
95
+
96
+ def get_queryset(self, request):
97
+ queryset = super().get_queryset(request).select_related("template", "field_value_action")
98
+ return queryset
99
+
100
+
101
+ class CustomFormDisplayColumnInline(admin.TabularInline):
102
+ model = CustomFormDisplayColumn
103
+
104
+ def get_queryset(self, request):
105
+ queryset = super().get_queryset(request).select_related("template")
106
+ return queryset
107
+
108
+
109
+ class CustomFormPDFTemplateForm(forms.ModelForm):
110
+
111
+ class Media:
112
+ js = ("admin/dynamic_form_preview/dynamic_form_preview.js",)
113
+ css = {"": ("admin/dynamic_form_preview/dynamic_form_preview.css",)}
114
+
115
+ class Meta:
116
+ widgets = {"filename_template": forms.Textarea(attrs={"rows": 4, "cols": 75})}
117
+
118
+ def clean_form_fields(self):
119
+ form_fields = self.cleaned_data["form_fields"]
120
+ try:
121
+ return json.dumps(json.loads(form_fields), indent=4)
122
+ except:
123
+ pass
124
+ return form_fields
125
+
126
+
127
+ @admin.register(CustomFormPDFTemplate)
128
+ class CustomFormPDFTemplateAdmin(ModelAdminRedirectMixin, admin.ModelAdmin):
129
+ form = CustomFormPDFTemplateForm
130
+ list_display = ["name", "enabled", "form", "get_create_permissions", "get_view_all_permissions"]
131
+ list_filter = ["enabled"]
132
+ readonly_fields = ["_pdf_form_fields", "_form_fields_preview"]
133
+ inlines = [CustomFormActionAdminInline, CustomFormSpecialMappingAdminInline, CustomFormDisplayColumnInline]
134
+ actions = [duplicate_custom_form_template]
135
+
136
+ def _pdf_form_fields(self, obj: CustomFormPDFTemplate):
137
+ if obj.form:
138
+ container_id = f"toggle-fields-{obj.pk}"
139
+ toggle_link = format_html(
140
+ f'<a href="javascript:void(0);" onclick="$(\'#{container_id}\').toggle()">Show/Hide Fields</a>',
141
+ container_id,
142
+ )
143
+ fields_list = "".join(
144
+ f"<li style='list-style: initial'>{field}{(': ' + str(obj.pdf_form_field_states(field))) if obj.pdf_form_field_states(field) else ''}</li>"
145
+ for field in obj.pdf_form_fields()
146
+ )
147
+ fields_container = format_html(
148
+ '<div id="{}" style="display:none;"><ul style="margin-left:10px;">{}</ul></div>',
149
+ container_id,
150
+ mark_safe(fields_list),
151
+ )
152
+ return mark_safe(f"{toggle_link}<br>{fields_container}")
153
+ return ""
154
+
155
+ def _form_fields_preview(self, obj: CustomFormPDFTemplate):
156
+ if obj.id:
157
+ form_validity_div = '<div id="form_validity"></div>' if obj.form_fields else ""
158
+ return mark_safe(
159
+ '<div class="dynamic_form_preview">{}{}</div><div class="help dynamic_form_preview_help">Save form to preview form fields</div>'.format(
160
+ DynamicForm(obj.form_fields).render("custom_form_fields_group", obj.id),
161
+ form_validity_div,
162
+ )
163
+ )
164
+ return ""
165
+
166
+ @admin.display(description="View all permissions", ordering="view_all_permissions")
167
+ def get_view_all_permissions(self, obj: CustomFormPDFTemplate):
168
+ return mark_safe(
169
+ "<br>".join(
170
+ str(obj.get_view_all_permissions_field().role_display(role_str, admin_display=True))
171
+ for role_str in obj.view_all_permissions
172
+ )
173
+ )
174
+
175
+ @admin.display(description="Create permissions", ordering="create_permissions")
176
+ def get_create_permissions(self, obj: CustomFormPDFTemplate):
177
+ return mark_safe(
178
+ "<br>".join(
179
+ str(obj.get_create_permissions_field().role_display(role_str, admin_display=True))
180
+ for role_str in obj.create_permissions
181
+ )
182
+ )
183
+
184
+
185
+ class CustomFormActionRecordInline(admin.TabularInline):
186
+ model = CustomFormActionRecord
187
+
188
+
189
+ class CustomFormDocumentsInline(admin.TabularInline):
190
+ model = CustomFormDocuments
191
+ extra = 1
192
+
193
+
194
+ @admin.register(CustomFormDocumentType)
195
+ class CustomFormDocumentTypeAdmin(admin.ModelAdmin):
196
+ list_display = ["name", "form_template", "display_order"]
197
+ list_filter = ["form_template"]
198
+
199
+
200
+ @admin.register(CustomForm)
201
+ class CustomFormAdmin(admin.ModelAdmin):
202
+ inlines = [CustomFormDocumentsInline, CustomFormActionRecordInline]
203
+ list_display = ["creation_time", "form_number", "status", "last_updated", "creator", "template", "cancelled"]
204
+ list_filter = [
205
+ ("creator", admin.RelatedOnlyFieldListFilter),
206
+ ("template", admin.RelatedOnlyFieldListFilter),
207
+ "status",
208
+ "cancelled",
209
+ ]
210
+ date_hierarchy = "last_updated"
211
+
212
+ def delete_queryset(self, request, queryset):
213
+ # This is needed so that the notifications for these forms are also deleted
214
+ for obj in queryset:
215
+ obj.delete()
216
+
217
+
218
+ class CustomFormAutomaticNumberingForm(forms.ModelForm):
219
+
220
+ class Meta:
221
+ model = CustomFormAutomaticNumbering
222
+ fields = "__all__"
223
+ widgets = {"numbering_template": forms.Textarea(attrs={"rows": 4, "cols": 75})}
224
+
225
+
226
+ @admin.register(CustomFormAutomaticNumbering)
227
+ class CustomFormAutomaticNumberingAdmin(admin.ModelAdmin):
228
+ list_display = ["template", "enabled", "numbering_group", "numbering_per_user", "get_role_display"]
229
+ list_filter = ["enabled", "numbering_per_user"]
230
+ readonly_fields = ["custom_form_numbers"]
231
+ form = CustomFormAutomaticNumberingForm
232
+
233
+ @admin.display(description="Allowed role/group", ordering="role")
234
+ def get_role_display(self, obj: CustomFormAutomaticNumbering):
235
+ return obj.get_role_display(admin_display=True)
236
+
237
+ def custom_form_numbers(self, obj: CustomFormAutomaticNumbering):
238
+ current_number = custom_forms_current_numbers(obj.template)
239
+ display_list = ""
240
+ if current_number:
241
+ if obj.numbering_group and obj.numbering_per_user:
242
+ for group, value in current_number.items():
243
+ display_list += f'<li style="list-style: inherit">{group}<ul style="margin-left: 20px">'
244
+ for user_id, number in value.items():
245
+ user = User.objects.filter(id=user_id).first()
246
+ user = user.username if user else user_id
247
+ display_list += f'<li style="list-style: inherit"><u>{user}</u>: {number}</li>'
248
+ display_list += "</ul></li>"
249
+ elif obj.numbering_group:
250
+ for group, number in current_number.items():
251
+ display_list += f'<li style="list-style: inherit"><u>{group}</u>: {number}</li>'
252
+ elif obj.numbering_per_user:
253
+ for user_id, number in current_number.items():
254
+ user = User.objects.filter(id=user_id).first()
255
+ user = user.username if user else user_id
256
+ display_list += f'<li style="list-style: inherit"><u>{user}</u>: {number}</li>'
257
+ return mark_safe(f'Current form numbers:<ul style="margin-left: 20px">{display_list}</ul>')
258
+ else:
259
+ return "No current form numbers recorded"
@@ -0,0 +1,24 @@
1
+ from django.apps import AppConfig
2
+
3
+
4
+ class CustomFormsConfig(AppConfig):
5
+ name = "NEMO_custom_forms"
6
+ verbose_name = "Custom Forms"
7
+ default_auto_field = "django.db.models.AutoField"
8
+ plugin_id = 1100 # Used to make EmailCategory and other IntegerChoices ranges unique
9
+
10
+ def ready(self):
11
+ from django.utils.translation import gettext_lazy as _
12
+ from NEMO.plugins.utils import (
13
+ add_dynamic_notification_types,
14
+ add_dynamic_email_categories,
15
+ check_extra_dependencies,
16
+ )
17
+ from NEMO_custom_forms.utilities import CUSTOM_FORM_NOTIFICATION, CUSTOM_FORM_EMAIL_CATEGORY
18
+
19
+ check_extra_dependencies(self.name, ["NEMO", "NEMO-CE"])
20
+
21
+ add_dynamic_notification_types(
22
+ [(CUSTOM_FORM_NOTIFICATION, _("Custom forms action - notifies next action candidates"))]
23
+ )
24
+ add_dynamic_email_categories([(CUSTOM_FORM_EMAIL_CATEGORY, _("Custom forms"))])