Пример #1
0
    def get_filter_form(self):
        form = forms.Form(self.request)
        form.title = 'Filtros'
        form.icon = 'fa-filter'
        form.partial = True
        self.filters = []
        for list_filter in self.get_list_filter():
            if type(list_filter) in (tuple, list):
                field_names = list_filter
            else:
                field_names = list_filter,
            for field_name in field_names:
                if self.relation and field_name == self.relation.hidden_field_name:
                    continue
                field = get_field(self.qs.model, field_name)
                form_field_name = '{}{}'.format(field_name, self.id)
                if hasattr(field, 'auto_now'):
                    initial = (self._get_from_request(field_name, None, '_0'), self._get_from_request(field_name, None, '_1'))
                    form.fields[form_field_name] = forms.DateFilterField(label=normalyze(field.verbose_name), initial=initial, required=False)
                else:
                    initial = self._get_from_request(field_name)
                    if type(field).__name__ in ['BooleanField', 'NullBooleanField']:
                        form.fields[form_field_name] = forms.ChoiceField(
                            choices=[['', ''], ['sim', 'Sim'], ['nao', 'Não'], ], label=normalyze(field.verbose_name), initial=initial, required=False)
                    elif hasattr(field, 'choices') and field.choices:
                        form.fields[form_field_name] = forms.ChoiceField(choices=[['', '']] + field.choices, label=normalyze(field.verbose_name), initial=initial, required=False)
                    else:
                        if hasattr(field.remote_field.model, 'unit_ptr_id') and self.request.user.role_set.filter(scope__unit__isnull=False).values_list('scope__unit', flat=True).count() == 1:
                            continue
                        if hasattr(field.remote_field.model, 'organization_ptr_id') and self.request.user.role_set.filter(scope__organization__isnull=False).values_list('scope__organization', flat=True).count() == 1:
                            continue
                        if field.remote_field and not should_filter_or_display(self.request, self.qs.model, field.remote_field.model):
                            continue
                        if initial:
                            if self.original_qs.query.can_filter():
                                pks = self.original_qs.order_by(field_name).values_list(field_name, flat=True).distinct()
                            else:
                                pks = self.original_qs.model.objects.all().order_by(field_name).values_list(field_name, flat=True).distinct()
                            qs = field.remote_field.model.objects.get_queryset().filter(pk__in=pks)
                            qs = qs | field.remote_field.model.objects.get_queryset().filter(pk=initial)
                        else:
                            if self.qs.query.can_filter():
                                pks = self.qs.order_by(field_name).values_list(field_name, flat=True).distinct()
                            else:
                                pks = self.qs.model.objects.all().order_by(field_name).values_list(field_name, flat=True).distinct()
                            qs = field.remote_field.model.objects.get_queryset().filter(pk__in=pks)
                        empty_label = ''

                        form.fields[form_field_name] = forms.ModelChoiceField(qs, label=normalyze(field.verbose_name), initial=initial, empty_label=empty_label, required=False, lazy=True, ignore_lookup=True, minimum_input_length=0)
                    form.fields[form_field_name].widget.attrs['data-placeholder'] = field.verbose_name
                    if initial:
                        label = form.fields[form_field_name].label
                        value = form.fields[form_field_name].clean(initial)
                        if type(form.fields[form_field_name]) == forms.ChoiceField:
                            for x, y in form.fields[form_field_name].choices:
                                if str(x) == str(value):
                                    value = y
                                    break
                        self.filters.append((label, value))
        return form
Пример #2
0
    def __init__(self, request, title, qs, list_display=(), list_filter=()):
        super(ModelReport, self).__init__(request)
        self.title = title
        self.qs = qs
        self.components = []

        if list_filter:
            form = forms.Form(request)
            form.icon = 'fa-file-text-o'
            form.title = u''
            form.submit_label = u'Gerar Relatório'
            for field_name in list_filter:
                field = get_field(qs.model, field_name)
                form.fields[field_name] = forms.ModelChoiceField(
                    field.rel.to.objects.all(),
                    label=field.verbose_name,
                    required=False)
            if form.is_valid():
                for field_name in list_filter:
                    value = form.cleaned_data[field_name]
                    if value:
                        qs = qs.filter(**{field_name: value})
            self.form = form

        order_by = get_metadata(qs.model, 'order_by', iterable=True)
        if order_by:
            qs = qs.order_by(*order_by)
        table_description = get_metadata(qs.model, 'verbose_name_plural')
        self.table = ModelTable(request, table_description, qs, list_display)
Пример #3
0
    def __init__(self,
                 request,
                 title,
                 qs,
                 list_display=(),
                 list_filter=(),
                 distinct=False):
        super(ModelReport, self).__init__(title, request)
        self.title = title
        self.qs = qs
        self.components = []
        self.filters = []

        if list_filter:
            form = forms.Form(request, method='GET')
            form.icon = 'fa-file-text-o'
            form.title = ''
            form.submit_label = 'Gerar Relatório'
            for field_name in list_filter:
                field = get_field(qs.model, field_name)
                if hasattr(field, 'choices') and field.choices:
                    form.fields[field_name] = forms.ChoiceField(
                        choices=[['', '']] + field.choices,
                        label=field.verbose_name,
                        required=False)
                else:
                    form.fields[field_name] = forms.ModelChoiceField(
                        field.remote_field.model.objects.all(),
                        label=field.verbose_name,
                        required=False)
            if form.is_valid():
                for field_name in list_filter:
                    value = form.cleaned_data[field_name]
                    if value:
                        qs = qs.filter(**{field_name: value})
                        self.filters.append(
                            (form.fields[field_name].label, value))
            self.form = form
        if distinct:
            pks = qs.values_list('pk', flat=True).order_by('pk').distinct()
            qs = qs.model.objects.filter(pk__in=pks)

        order_by = get_metadata(qs.model, 'order_by', iterable=True)
        if order_by:
            qs = qs.order_by(*order_by)
        table_description = get_metadata(qs.model, 'verbose_name_plural')
        self.table = ModelTable(request, table_description, qs, list_display)
Пример #4
0
    def get_filter_form(self):
        form = forms.Form(self.request)
        form.title = 'Filtros'
        form.icon = 'fa-filter'
        form.partial = True
        self.filters = []
        for list_filter in self.get_list_filter():
            if type(list_filter) in (tuple, list):
                field_names = list_filter
            else:
                field_names = list_filter,
            for field_name in field_names:
                if field_name == self.to:
                    continue
                field = get_field(self.qs.model, field_name)
                form_field_name = '%s%s' % (field_name, self.id)
                if type(field).__name__ in ['DateField', 'DateTimeField']:
                    initial = (self._get_from_request(field_name, None, '_0'),
                               self._get_from_request(field_name, None, '_1'))
                    form.fields[form_field_name] = forms.DateFilterField(
                        label=normalyze(field.verbose_name),
                        initial=initial,
                        required=False)
                else:
                    initial = self._get_from_request(field_name)
                    if type(field).__name__ in [
                            'BooleanField', 'NullBooleanField'
                    ]:
                        form.fields[form_field_name] = forms.ChoiceField(
                            choices=[
                                ['', ''],
                                ['sim', 'Sim'],
                                ['nao', 'Não'],
                            ],
                            label=normalyze(field.verbose_name),
                            initial=initial,
                            required=False)
                    elif hasattr(field, 'choices') and field.choices:
                        form.fields[form_field_name] = forms.ChoiceField(
                            choices=[['', '']] + field.choices,
                            label=normalyze(field.verbose_name),
                            initial=initial,
                            required=False)
                    else:
                        if self.request.user.unit_id and hasattr(
                                field.rel.to, 'unit_ptr_id'):
                            continue
                        if self.request.user.organization_id and hasattr(
                                field.rel.to, 'organization_ptr_id'):
                            continue
                        if not should_filter_or_display(
                                self.request, self.qs.model, field.rel.to):
                            continue

                        if self.original_qs.query.can_filter():
                            pks = self.original_qs.order_by(
                                field_name).values_list(field_name,
                                                        flat=True).distinct()
                        else:
                            pks = self.original_qs.model.objects.all(
                            ).order_by(field_name).values_list(
                                field_name, flat=True).distinct()
                        qs = field.rel.to.objects.get_queryset().filter(
                            pk__in=pks)
                        empty_label = ''

                        form.fields[form_field_name] = forms.ModelChoiceField(
                            qs,
                            label=normalyze(field.verbose_name),
                            initial=initial,
                            empty_label=empty_label,
                            required=False)
                    form.fields[form_field_name].widget.attrs[
                        'data-placeholder'] = field.verbose_name
                    if initial:
                        label = form.fields[form_field_name].label
                        value = form.fields[form_field_name].clean(initial)
                        if type(form.fields[form_field_name]
                                ) == forms.ChoiceField:
                            for x, y in form.fields[form_field_name].choices:
                                if unicode(x) == unicode(value):
                                    value = y
                                    break
                        self.filters.append((label, value))
        return form