summaryrefslogtreecommitdiff
path: root/ishtar_common/forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r--ishtar_common/forms.py43
1 files changed, 32 insertions, 11 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py
index 0f1fa20f8..683726e67 100644
--- a/ishtar_common/forms.py
+++ b/ishtar_common/forms.py
@@ -301,20 +301,41 @@ class ManageOldType(object):
class CustomForm(object):
form_admin_name = ""
form_slug = ""
+ need_user_for_initialization = True
def __init__(self, *args, **kwargs):
+ current_user = None
+ if 'user' in kwargs:
+ try:
+ current_user = kwargs.pop('user').ishtaruser
+ except AttributeError:
+ pass
super(CustomForm, self).__init__(*args, **kwargs)
- # todo: filter by user / group...
- q = models.CustomForm.objects.filter(form=self.form_slug,
- available=True, apply_to_all=True)
- if not q.count():
- return
- # todo: prevent multiple result in database
- form = q.all()[0]
- for excluded in form.excluded_fields.all():
- # could have be filtered previously
- if excluded.field in self.fields:
- self.fields.pop(excluded.field)
+ base_q = {"form": self.form_slug, 'available': True}
+ # order is important : try for user, user type then all
+ query_dicts = []
+ if current_user:
+ dct = base_q.copy()
+ dct.update({'users__pk': current_user.pk})
+ query_dicts = [dct]
+ for user_type in current_user.person.person_types.all():
+ dct = base_q.copy()
+ dct.update({'user_types__pk': user_type.pk}),
+ query_dicts.append(dct)
+ dct = base_q.copy()
+ dct.update({'apply_to_all': True})
+ query_dicts.append(dct)
+ for query_dict in query_dicts:
+ q = models.CustomForm.objects.filter(**query_dict)
+ if not q.count():
+ continue
+ # todo: prevent multiple result in database
+ form = q.all()[0]
+ for excluded in form.excluded_fields.all():
+ # could have be filtered previously
+ if excluded.field in self.fields:
+ self.fields.pop(excluded.field)
+ break
@classmethod
def get_custom_fields(cls):