示例#1
0
    def get_related_fields(self, model_class, field_name, path="", path_verbose=""):
        """ Get fields for a given model """
        if field_name:
            field = model_class._meta.get_field_by_name(field_name)
            if field[2]:
                # Direct field
                try:
                    new_model = field[0].related.parent_model()
                except AttributeError:
                    new_model = field[0].related.model
            else:
                # Indirect related field
                if hasattr(field[0], 'related_model'):  # Django>=1.8
                    new_model = field[0].related_model
                else:
                    new_model = field[0].model()

            if path_verbose:
                path_verbose += "::"
            path_verbose += field[0].name

            path += field_name
            path += '__'
        else:
            new_model = model_class

        new_fields = get_relation_fields_from_model(new_model)
        model_ct = ContentType.objects.get_for_model(new_model)

        return (new_fields, model_ct, path)
示例#2
0
    def get_related_fields(self,
                           model_class,
                           field_name,
                           path="",
                           path_verbose=""):
        """ Get fields for a given model """
        if field_name:
            field = model_class._meta.get_field_by_name(field_name)
            if field[2]:
                # Direct field
                try:
                    new_model = field[0].related.parent_model()
                except AttributeError:
                    new_model = field[0].related.model
            else:
                # Indirect related field
                if hasattr(field[0], 'related_model'):  # Django>=1.8
                    new_model = field[0].related_model
                else:
                    new_model = field[0].model()

            if path_verbose:
                path_verbose += "::"
            path_verbose += field[0].name

            path += field_name
            path += '__'
        else:
            new_model = model_class

        new_fields = get_relation_fields_from_model(new_model)
        model_ct = ContentType.objects.get_for_model(new_model)

        return (new_fields, model_ct, path)
示例#3
0
    def get_related_fields(self,
                           model_class,
                           field_name,
                           path="",
                           path_verbose=""):
        """ Get fields for a given model """
        field = model_class._meta.get_field_by_name(field_name)
        if field[2]:
            # Direct field
            new_model = field[0].related.parent_model()
        else:
            # Indirect related field
            new_model = field[0].model()

        new_fields = get_relation_fields_from_model(new_model)
        model_ct = ContentType.objects.get_for_model(new_model)

        if path_verbose:
            path_verbose += "::"
        path_verbose += field[0].name

        path += field_name
        path += '__'

        return (new_fields, model_ct, path)
示例#4
0
 def test_get_relation_fields_from_model(self):
     fields = get_relation_fields_from_model(Report)
     names = self.get_fields_names(fields)
     self.assertTrue('displayfield' in names or 'report_builder:displayfield' in names)
     self.assertTrue('filterfield' in names or 'report_builder:filterfield' in names)
     self.assertTrue('root_model' in names)
     self.assertEquals(len(names), 6)
示例#5
0
 def test_get_relation_fields_from_model(self):
     fields = get_relation_fields_from_model(Report)
     names = self.get_fields_names(fields)
     self.assertTrue('displayfield' in names or 'report_builder:displayfield' in names)
     self.assertTrue('filterfield' in names or 'report_builder:filterfield' in names)
     self.assertTrue('root_model' in names)
     self.assertEquals(len(names), 6)
示例#6
0
 def get_context_data(self, **kwargs):
     context = super(AdminExport, self).get_context_data(**kwargs)
     field_name = self.request.GET.get('field', '')
     model_class = self.get_model_class()
     queryset = self.get_queryset(model_class)
     path = self.request.GET.get('path', '')
     path_verbose = self.request.GET.get('path_verbose', '')
     context['opts'] = model_class._meta
     context['queryset'] = queryset
     context['model_ct'] = self.request.GET['ct']
     context['related_fields'] = get_relation_fields_from_model(model_class)
     context.update(self.get_fields(model_class, field_name, path, path_verbose))
     return context
示例#7
0
 def get_context_data(self, **kwargs):
     context = super(AdminExport, self).get_context_data(**kwargs)
     field_name = self.request.GET.get('field', '')
     model_class = self.get_model_class()
     queryset = self.get_queryset(model_class)
     path = self.request.GET.get('path', '')
     path_verbose = self.request.GET.get('path_verbose', '')
     context['opts'] = model_class._meta
     context['queryset'] = queryset
     context['model_ct'] = self.request.GET['ct']
     context['related_fields'] = get_relation_fields_from_model(model_class)
     context.update(
         self.get_fields(model_class, field_name, path, path_verbose))
     return context
示例#8
0
 def get_context_data(self, **kwargs):
     context = super(AdminExport, self).get_context_data(**kwargs)
     field_name = self.request.GET.get('field', '')
     model_class = ContentType.objects.get(id=self.request.GET['ct']).model_class()
     if self.request.GET['ids'] == "IN_SESSION":
         queryset = model_class.objects.filter(pk__in=self.request.session['selected_ids'])
     else:
         queryset = model_class.objects.filter(pk__in=self.request.GET['ids'].split(','))
     path = self.request.GET.get('path', '')
     path_verbose = self.request.GET.get('path_verbose', '')
     context['model_name'] = model_class.__name__.lower()
     context['queryset'] = queryset
     context['model_ct'] = self.request.GET['ct']
     field_data = self.get_fields(model_class, field_name, path, path_verbose)
     context['related_fields'] = get_relation_fields_from_model(model_class)
     return dict(context.items() + field_data.items())
示例#9
0
    def get_context_data(self, **kwargs):
        ctx = super(ReportUpdateView, self).get_context_data(**kwargs)
        model_class = self.object.root_model.model_class()
        model_ct = ContentType.objects.get_for_model(model_class)
        relation_fields = get_relation_fields_from_model(model_class)

        DisplayFieldFormset = inlineformset_factory(Report,
                                                    DisplayField,
                                                    extra=0,
                                                    can_delete=True,
                                                    form=DisplayFieldForm)

        FilterFieldFormset = inlineformset_factory(Report,
                                                   FilterField,
                                                   extra=0,
                                                   can_delete=True,
                                                   form=FilterFieldForm)

        if self.request.POST:
            ctx['field_list_formset'] = DisplayFieldFormset(
                self.request.POST, instance=self.object)
            ctx['field_filter_formset'] = FilterFieldFormset(
                self.request.POST, instance=self.object, prefix="fil")
        else:
            ctx['field_list_formset'] = DisplayFieldFormset(
                instance=self.object)
            ctx['field_filter_formset'] = FilterFieldFormset(
                instance=self.object, prefix="fil")

        ctx['related_fields'] = relation_fields
        ctx['fieldsets'] = get_fieldsets(model_class)
        ctx['model_ct'] = model_ct
        ctx['root_model'] = model_ct.model
        ctx['app_label'] = model_ct.app_label

        if getattr(settings, 'REPORT_BUILDER_ASYNC_REPORT', False):
            ctx['async_report'] = True

        field_context = self.get_fields(model_class)
        ctx = ctx.copy()
        ctx.update(field_context)
        return ctx
示例#10
0
    def get_related_fields(self, model_class, field_name, path="", path_verbose=""):
        """ Get fields for a given model """
        field = model_class._meta.get_field_by_name(field_name)
        if field[2]:
            # Direct field
            new_model = field[0].related.parent_model()
        else:
            # Indirect related field
            new_model = field[0].model()

        new_fields = get_relation_fields_from_model(new_model)
        model_ct = ContentType.objects.get_for_model(new_model)

        if path_verbose:
            path_verbose += "::"
        path_verbose += field[0].name

        path += field_name
        path += '__'

        return (new_fields, model_ct, path)
示例#11
0
    def get_context_data(self, **kwargs):
        ctx = super(ReportUpdateView, self).get_context_data(**kwargs)
        model_class = self.object.root_model.model_class()
        model_ct = ContentType.objects.get_for_model(model_class)
        relation_fields = get_relation_fields_from_model(model_class)

        DisplayFieldFormset = inlineformset_factory(
            Report,
            DisplayField,
            extra=0,
            can_delete=True,
            form=DisplayFieldForm)

        FilterFieldFormset = inlineformset_factory(
            Report,
            FilterField,
            extra=0,
            can_delete=True,
            form=FilterFieldForm)

        if self.request.POST:
            ctx['field_list_formset'] =  DisplayFieldFormset(self.request.POST, instance=self.object)
            ctx['field_filter_formset'] =  FilterFieldFormset(self.request.POST, instance=self.object, prefix="fil")
        else:
            ctx['field_list_formset'] =  DisplayFieldFormset(instance=self.object)
            ctx['field_filter_formset'] =  FilterFieldFormset(instance=self.object, prefix="fil")

        ctx['related_fields'] = relation_fields
        ctx['fieldsets'] = get_fieldsets(model_class)
        ctx['model_ct'] = model_ct
        ctx['root_model'] = model_ct.model
        ctx['app_label'] = model_ct.app_label

        if getattr(settings, 'REPORT_BUILDER_ASYNC_REPORT', False):
            ctx['async_report'] = True

        field_context = self.get_fields(model_class)
        ctx = ctx.copy()
        ctx.update(field_context)
        return ctx
示例#12
0
        display_formats = {}
 for df in display_fields: if hasattr(df, 'display_format') and df.display_format:                display_formats[df.position] = df.display_format
 def formatter(value, style): # Convert value to Decimal to apply numeric formats. try:                value = Decimal(value) except Exception: pass
 try: return style.string.format(value) except ValueError: return value
 # Iterate rows and convert values by choice lists and field formats.
        final_list = []
 for row in values_and_properties_list:            row = list(row)
 for position, choice_list in choice_lists.items(): try:                    row[position] = text_type(choice_list[row[position]]) except Exception:                    row[position] = text_type(row[position])
 for pos, style in display_formats.items():                row[pos] = formatter(row[pos], style)
            final_list.append(row)
        values_and_properties_list = final_list
 if display_totals:            display_totals_row = []
            fields_and_properties = list(display_field_paths[0 if group else 1:])
 for position, value in property_list.items():                fields_and_properties.insert(position, value)
 for field in fields_and_properties:                display_totals_row.append(display_totals.get(field, ''))
 # Add formatting to display totals.
 for pos, style in display_formats.items():                display_totals_row[pos] = formatter(display_totals_row[pos], style)
            values_and_properties_list.append(                ['TOTALS'] + (len(fields_and_properties) - 1) * ['']            )            values_and_properties_list.append(display_totals_row)
 return values_and_properties_list, message
 def sort_helper(self, value, default): if value is None:            value = default if isinstance(value, string_types):            value = value.lower() return value
class GetFieldsMixin(object): def get_fields(self, model_class, field_name='', path='', path_verbose=''): """ Get fields and meta data from a model        :param model_class: A django model class        :param field_name: The field name to get sub fields from        :param path: path of our field in format            field_name__second_field_name__ect__        :param path_verbose: Human readable version of above        :returns: Returns fields and meta data about such fields            fields: Django model fields            custom_fields: fields from django-custom-field if installed            properties: Any properties the model has            path: Our new path            path_verbose: Our new human readable path        :rtype: dict """        fields = get_direct_fields_from_model(model_class)        properties = get_properties_from_model(model_class)        custom_fields = get_custom_fields_from_model(model_class)        app_label = model_class._meta.app_label
 if field_name != '':            field = model_class._meta.get_field_by_name(field_name) if path_verbose:                path_verbose += "::" # TODO: need actual model name to generate choice list (not pluralized field name) # - maybe store this as a separate value? if field[3] and hasattr(field[0], 'm2m_reverse_field_name'):                path_verbose += field[0].m2m_reverse_field_name() else:                path_verbose += field[0].name
            path += field_name            path += '__' if field[2]:  # Direct field try:                    new_model = field[0].related.parent_model except AttributeError:                    new_model = field[0].related.model                path_verbose = new_model.__name__.lower() else:  # Indirect related field try:                    new_model = field[0].related_model except AttributeError:  # Django 1.7                    new_model = field[0].model                path_verbose = new_model.__name__.lower()
            fields = get_direct_fields_from_model(new_model)
            custom_fields = get_custom_fields_from_model(new_model)            properties = get_properties_from_model(new_model)            app_label = new_model._meta.app_label
 return { 'fields': fields, 'custom_fields': custom_fields, 'properties': properties, 'path': path, 'path_verbose': path_verbose, 'app_label': app_label,        }
 def get_related_fields(self, model_class, field_name, path="", path_verbose=""): """ Get fields for a given model """ if field_name:            field = model_class._meta.get_field_by_name(field_name) if field[2]: # Direct field try:                    new_model = field[0].related.parent_model() except AttributeError:                    new_model = field[0].related.model else: # Indirect related field if hasattr(field[0], 'related_model'):  # Django>=1.8                    new_model = field[0].related_model else:                    new_model = field[0].model()
 if path_verbose:                path_verbose += "::"            path_verbose += field[0].name
            path += field_name            path += '__' else:            new_model = model_class
        new_fields = get_relation_fields_from_model(new_model)        model_ct = ContentType.objects.get_for_model(new_model)
 return (new_fields, model_ct, path)