def __init__(self, current_user, *args, **kwargs): from users.models import TruffeUser from units.models import Unit super(GenericForm, self).__init__(*args, **kwargs) if 'user' in self.fields: if hasattr(self.Meta.model.MetaData, 'has_unit') and self.Meta.model.MetaData.has_unit: if hasattr(self.Meta.model, 'MetaEdit') and hasattr(self.Meta.model.MetaEdit, 'all_users') and self.Meta.model.MetaEdit.all_users: self.fields['user'].queryset = TruffeUser.objects.all() # Some classes allow creation of instances for any user (NdF, JdC) else: unit = get_property(self.instance, self.instance.MetaRights.linked_unit_property) if get_property(self.instance, 'MetaRights.linked_unit_property') else self.instance.unit self.fields['user'].queryset = TruffeUser.objects.filter(accreditation__unit=unit, accreditation__end_date=None).distinct().order_by('first_name', 'last_name') if 'unit' in self.fields: self.fields['unit'].queryset = Unit.objects.order_by('name') if hasattr(self.Meta.model, 'MetaEdit') and hasattr(self.Meta.model.MetaEdit, 'only_if'): for key, test in self.Meta.model.MetaEdit.only_if.iteritems(): if not test((self.instance, current_user)): if key in self.fields: del self.fields[key] if hasattr(self.instance, 'genericFormExtraInit'): self.instance.genericFormExtraInit(self, current_user, *args, **kwargs)
def rights_peoples_in_EDIT(self): if not get_property(self, self.MetaRights.linked_unit_property ): # Pas d'unité. L'user doit être l'user return [self.unit_blank_user] return self.people_in_linked_unit(self.MetaRightsUnit.access)
def people_in_linked_unit(self, access=None): if not self.MetaRights.linked_unit_property or not has_property(self, self.MetaRights.linked_unit_property): return False unit = get_property(self, self.MetaRights.linked_unit_property) return self.people_in_unit(unit, access)
def rights_can_EDIT(self, user): # Peut toujours afficher, de manière générique if not has_property(self, self.MetaRights.linked_unit_property): return True if not get_property(self, self.MetaRights.linked_unit_property): # Pas d'unité. L'user doit être l'user try: return not self.unit_blank_user or self.unit_blank_user == user or self.rights_in_root_unit(user, self.MetaRightsUnit.access) except: return True # Pas d'unité, ni d'users return self.rights_in_linked_unit(user, self.MetaRightsUnit.access)
def rights_can_EDIT(self, user): # Peut toujours afficher, de manière générique if not has_property(self, self.MetaRights.linked_unit_property): return True if not get_property(self, self.MetaRights.linked_unit_property): # Pas d'unité. L'user doit être l'user try: return not self.unit_blank_user or self.unit_blank_user == user except: return True # Pas d'unité, ni d'users return self.rights_in_linked_unit(user, self.MetaRightsUnit.access)
def rights_in_linked_unit(self, user, access=None): if not self.MetaRights.linked_unit_property or not has_property(self, self.MetaRights.linked_unit_property): return False unit = get_property(self, self.MetaRights.linked_unit_property) if not unit: e = Exception("Tried to test right in unit without an unit") raise e return False if type(access) is list: for acc in access: if self.rights_in_unit(user, unit, acc): return True return False return self.rights_in_unit(user, unit, access)
def rights_peoples_in_EDIT(self): if not get_property(self, self.MetaRights.linked_unit_property): # Pas d'unité. L'user doit être l'user return [self.unit_blank_user] return self.people_in_linked_unit(self.MetaRightsUnit.access)