Ejemplo n.º 1
0
    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,
        }
Ejemplo n.º 2
0
    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,
        }
Ejemplo n.º 3
0
 def test_get_properties_from_model(self):
     properties = get_properties_from_model(DisplayField)
     self.assertEquals(properties[0]['label'], 'choices')
     self.assertEquals(properties[1]['label'], 'choices_dict')
Ejemplo n.º 4
0
 def test_get_properties_from_model(self):
     properties = get_properties_from_model(DisplayField)
     self.assertEquals(properties[0]['label'], 'choices')
     self.assertEquals(properties[1]['label'], 'choices_dict')
Ejemplo n.º 5
0
 def test_get_properties_from_model(self):
     properties = get_properties_from_model(DisplayField)
     self.assertEquals(properties[0]["label"], "choices")
     self.assertEquals(properties[1]["label"], "choices_dict")
Ejemplo n.º 6
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)