def __init__(self, entity, *args, **kwargs): super().__init__(*args, **kwargs) self.ml = entity ct = ContentType.objects.get_for_model(self.person_model) self.fields['filters'].queryset = EntityFilter.get_for_user( self.user, ct)
def __init__(self, entity, *args, **kwargs): # super(ObjectiveForm, self).__init__(*args, **kwargs) super().__init__(*args, **kwargs) self.act = entity instance = self.instance if instance.pk: # Edition fields = self.fields efilter = instance.filter # TODO: add a method EntityFilter.can_list(self.user) to avoid a query # if efilter and not efilter.can_view(self.user)[0]: if efilter and not EntityFilter.get_for_user(self.user, content_type=instance.ctype)\ .filter(id=efilter.id).exists(): fields['ec_label'] = CharField( label=fields['entity_counting'].label, required=False, widget=Label, initial=_( 'The filter cannot be changed because it is private.'), ) del fields['entity_counting'] else: fields[ 'entity_counting'].initial = instance.ctype_id, instance.filter_id
def __init__(self, model, field, user, entities, is_bulk=False, **kwargs): super().__init__(model, field, user, entities, is_bulk=is_bulk, **kwargs) filter_field = self.fields['field_value'] filter_field.empty_label = _('All') first_ct = entities[0].ct if entities else None self._has_same_report_ct = all(e.ct == first_ct for e in entities) self._uneditable_ids = set() if self._has_same_report_ct: user = self.user filter_field.queryset = EntityFilter.get_for_user(user, first_ct) self._uneditable_ids = uneditable_ids = { e.id for e in entities if e.filter and not e.filter.can_view(user)[0] } if uneditable_ids: length = len(uneditable_ids) if length == len(entities): self.fields['field_value'] = CharField( label=filter_field.label, required=False, widget=Label, initial=ngettext( 'The filter cannot be changed because it is private.', 'The filters cannot be changed because they are private.', length), ) else: self.fields['beware'] = CharField( label=_('Beware !'), required=False, widget=Label, initial=ngettext( 'The filter of {count} report cannot be changed because it is private.', 'The filters of {count} reports cannot be changed because they are private.', length).format(count=length), ) else: filter_field.help_text = _( 'Filter field can only be updated when ' 'reports target the same type of entities (e.g: only contacts).' ) filter_field.widget = Label(empty_label='') filter_field.value = None
def get_filter_choices(self, user, *models): choices = [] get_ct = ContentType.objects.get_for_model ctypes = [get_ct(model) for model in models] efilters_per_ctid = defaultdict(list) for efilter in EntityFilter.get_for_user(user, ctypes): efilters_per_ctid[efilter.entity_type_id].append(efilter) for ct in ctypes: efilters = efilters_per_ctid[ct.id] if efilters: title = str(ct.model_class()._meta.verbose_name_plural) choices.append( (title, [(ef.id, u'{} - {}'.format(title, ef.name)) for ef in efilters])) return choices