summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-03-02 11:23:23 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-03-02 12:44:59 +0100
commit4d49208aef1e0a0babae8c2ffd231f397f19d513 (patch)
tree98dcb4e9c96101b01b4fd17bcddbd12a1c127377
parentf8d43731beb03c1407f4f8991c0764d980494ef4 (diff)
downloadIshtar-4d49208aef1e0a0babae8c2ffd231f397f19d513.tar.bz2
Ishtar-4d49208aef1e0a0babae8c2ffd231f397f19d513.zip
Fix custom form -> use profile instead of person types
-rw-r--r--archaeological_operations/tests.py101
-rw-r--r--ishtar_common/admin.py7
-rw-r--r--ishtar_common/forms.py11
-rw-r--r--ishtar_common/migrations/0213_auto_20210302_0950.py25
-rw-r--r--ishtar_common/models.py5
5 files changed, 111 insertions, 38 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index 650a839aa..3eefe5d61 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -1834,56 +1834,97 @@ class CustomFormTest(TestCase, OperationInitTest):
'{}{}-current_step'.format(cls_wiz.url_name,
cls_wiz.wizard_name): [step],
}
+
+ MSG_FOUND = " - '{}' field found on the modification "\
+ "wizard. It should have been filtered."
+ MSG_NOT_FOUND = " - '{}' field not found on the modification "\
+ "wizard. It shouldn't have been filtered."
+
key_in_charge = "in_charge"
response = c.post(url, data)
- self.assertIn(
- key_in_charge, response.content.decode(),
- msg="filter all - 'in charge' field not found on the modification "
- "wizard")
+ content = response.content.decode()
+ res = key_in_charge in content
+ self.assertTrue(res,
+ msg="filter all" + MSG_NOT_FOUND.format(key_in_charge))
f = CustomForm.objects.create(
name="Test - all", form="operation-010-general",
available=True, apply_to_all=True)
ExcludedField.objects.create(custom_form=f, field="in_charge")
response = c.post(url, data)
- self.assertNotIn(
- key_in_charge, response.content.decode(),
- msg="filter all - 'in charge' field found on the modification "
- "wizard. It should have been filtered.")
+ content = response.content.decode()
+ res = key_in_charge not in content
+ self.assertTrue(res,
+ msg="filter all" + MSG_FOUND.format(key_in_charge))
# user type form prevail on "all"
f_scientist = CustomForm.objects.create(
- name="Test - user", form="operation-010-general",
+ name="Test - user type", form="operation-010-general",
available=True)
tpe = PersonType.objects.get(txt_idx='head_scientist')
key_address = "address"
f_scientist.user_types.add(tpe)
self.user.ishtaruser.person.person_types.add(tpe)
- ExcludedField.objects.create(custom_form=f_scientist, field="address")
+ ExcludedField.objects.create(custom_form=f_scientist, field=key_address)
+
response = c.post(url, data)
- self.assertIn(
- key_in_charge, response.content.decode(),
- msg="filter user type - 'in charge' field not found on the "
- "modification wizard. It should not have been filtered.")
- self.assertNotIn(
- key_address, response.content.decode(),
- msg="filter user type - 'address' field found on the "
- "modification wizard. It should have been filtered.")
-
- # user prevail on "all" and "user_types"
+ content = response.content.decode()
+ res = key_in_charge in content
+ self.assertTrue(
+ res,
+ msg="filter profile type" + MSG_NOT_FOUND.format(key_in_charge)
+ )
+ res = key_address not in content
+ self.assertTrue(
+ res,
+ msg="filter profile type" + MSG_FOUND.format(key_address))
+
+ # profile type form prevail on "all" and "user types"
+ f_scientist2 = CustomForm.objects.create(
+ name="Test - profile type", form="operation-010-general",
+ available=True)
+ key_scientific = "scientific_documentation_comment"
+ ExcludedField.objects.create(custom_form=f_scientist2,
+ field=key_scientific)
+
+ collaborator = ProfileType.objects.get(txt_idx='collaborator')
+ UserProfile.objects.create(
+ profile_type=collaborator,
+ person=self.user.ishtaruser.person,
+ current=True)
+ f_scientist2.profile_types.add(collaborator)
+
+ response = c.post(url, data)
+ content = response.content.decode()
+ res = key_in_charge in content
+ self.assertTrue(
+ res,
+ msg="filter profile type" + MSG_NOT_FOUND.format(key_in_charge))
+ res = key_address in content
+ self.assertTrue(
+ res,
+ msg="filter profile type" + MSG_NOT_FOUND.format(key_address))
+ res = key_scientific not in content
+ self.assertTrue(
+ res,
+ msg="filter profile type" + MSG_FOUND.format(key_scientific))
+
+ # user prevail on "all", "profile_type" and "user_types"
f_user = CustomForm.objects.create(
- name="Test", form="operation-010-general", available=True)
+ name="Test - user", form="operation-010-general", available=True)
f_user.users.add(self.user.ishtaruser)
- self.user.ishtaruser.person.person_types.add(tpe)
+
response = c.post(url, data)
- self.assertIn(
- key_in_charge, response.content.decode(),
- msg="filter user - 'in charge' field not found on the modification "
- "wizard. It should not have been filtered.")
- self.assertIn(
- key_address, response.content.decode(),
- msg="filter user - 'address' field not found on the modification "
- "wizard. It should not have been filtered.")
+ content = response.content.decode()
+ res = key_in_charge in content
+ self.assertTrue(res,
+ msg="filter user" + MSG_NOT_FOUND.format(key_in_charge))
+ res = key_scientific in content
+ self.assertTrue(res,
+ msg="filter user" + MSG_NOT_FOUND.format(key_scientific))
+ res = key_address in content
+ self.assertTrue(res,
+ msg="filter user" + MSG_FOUND.format(key_address))
def test_enabled(self):
c = Client()
diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py
index 2fe0c012a..4cae9e02c 100644
--- a/ishtar_common/admin.py
+++ b/ishtar_common/admin.py
@@ -1573,7 +1573,6 @@ def get_choices_form():
for slug in register.keys()
]
-
forms = sorted(forms, key=lambda x: x[1])
cache.set(cache_key, forms, settings.CACHE_TIMEOUT)
return forms
@@ -1652,7 +1651,7 @@ class CustomFormAdmin(admin.ModelAdmin):
list_display = ['name', 'form', 'available', 'enabled', 'apply_to_all',
'users_lbl', 'user_types_lbl']
fields = ('name', 'form', 'available', 'enabled', 'apply_to_all', 'users',
- 'user_types')
+ 'user_types', 'profile_types')
form = CustomFormForm
inlines = [ExcludeFieldInline, JsonFieldInline]
@@ -1665,8 +1664,8 @@ class CustomFormAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
if obj:
- return ('form',)
- return []
+ return ('form', "user_types")
+ return ("user_types",)
admin_site.register(models.CustomForm, CustomFormAdmin)
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py
index 5b6501b13..27741c576 100644
--- a/ishtar_common/forms.py
+++ b/ishtar_common/forms.py
@@ -243,9 +243,7 @@ class CustomForm(BSForm):
alt_key, alt_field = new_fields.pop(k)
alt_field.order_number = k
fields[alt_key] = alt_field
-
self.fields = fields
-
self._post_init()
def are_available(self, keys):
@@ -317,12 +315,19 @@ class CustomForm(BSForm):
if not current_user:
return True, [], []
base_q = {"form": cls.form_slug, 'available': True}
- # order is important : try for user, user type then all
+ # order is important : try for user, profile type, user type then all
query_dicts = []
if current_user:
dct = base_q.copy()
dct.update({'users__pk': current_user.pk})
query_dicts = [dct]
+ if current_user.current_profile:
+ dct = base_q.copy()
+ pt = current_user.current_profile.profile_type.pk
+ dct.update(
+ {'profile_types__pk': pt})
+ query_dicts.append(dct)
+
for user_type in current_user.person.person_types.all():
dct = base_q.copy()
dct.update({'user_types__pk': user_type.pk}),
diff --git a/ishtar_common/migrations/0213_auto_20210302_0950.py b/ishtar_common/migrations/0213_auto_20210302_0950.py
new file mode 100644
index 000000000..584ac7f62
--- /dev/null
+++ b/ishtar_common/migrations/0213_auto_20210302_0950.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.27 on 2021-03-02 09:50
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('ishtar_common', '0212_auto_20210222_1718'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='customform',
+ name='profile_types',
+ field=models.ManyToManyField(blank=True, to='ishtar_common.ProfileType'),
+ ),
+ migrations.AlterField(
+ model_name='customform',
+ name='user_types',
+ field=models.ManyToManyField(blank=True, help_text='Deprecated', to='ishtar_common.PersonType'),
+ ),
+ ]
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 4662bcc35..0e5a39c1f 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1098,7 +1098,10 @@ class CustomForm(models.Model):
help_text=_("Apply this form to all users. If set to True, selecting "
"user and user type is useless."))
users = models.ManyToManyField('IshtarUser', blank=True)
- user_types = models.ManyToManyField('PersonType', blank=True)
+ user_types = models.ManyToManyField(
+ 'PersonType', blank=True,
+ help_text=_("Deprecated - use profile types"))
+ profile_types = models.ManyToManyField("ProfileType", blank=True)
objects = CustomFormManager()
SERIALIZATION_EXCLUDE = ("users", )