Example #1
0
    def test_should_generate_title_from_fieldname_if_not_specified(self):
        _, _, _, _, title = get_model_related_field(
            CustomPage,
            'image__caption'
        )

        self.assertEqual('Caption', title)
Example #2
0
    def test_should_resolve_related_field_name(self):
        field, related, rel_fieldname, rel_model, title = get_model_related_field(
            CustomPage,
            'image__caption',
            'Image Caption'
        )

        self.assertEqual('image', field.name)
        self.assertEqual('image', related)
        self.assertEqual('caption', rel_fieldname)
        self.assertEqual(Media, rel_model)
        self.assertEqual('Image Caption', title)
Example #3
0
 def test_should_fail_if_rel_field_does_not_exist(self):
     field, _, _, _, _ = get_model_related_field(CustomPage, 'image__foo')
     self.assertIsNone(field)
Example #4
0
 def test_should_fail_if_not_matching_pattern(self):
     field, _, _, _, _ = get_model_related_field(CustomPage, 'image')
     self.assertIsNone(field)
Example #5
0
 def test_should_fail_on_many_to_many_field(self):
     field, _, _, _, _ = get_model_related_field(CustomPage, 'gallery_images__caption')
     self.assertIsNone(field)
Example #6
0
    def get_fieldnames(self):
        """
        Return a list of exportable field names. This generally are all fields
        in the model. However it may be overridden by the model. Also the model
        may define a list of fields that are ignored. In addition, it may
        contain related fields.
        """
        # get list of field names
        pk_name = self.get_data_id_field()
        _fieldnames = get_listing_option(self.model, 'data_columns')
        if _fieldnames is not None:
            fieldnames = _fieldnames
        else:
            fieldnames = [field.name for field in self.model._meta.fields]

            # if we have a custom primary key used to refer to entries,
            # remove the regular primary key
            if pk_name != self.model._meta.pk.name and self.model._meta.pk.name in fieldnames and pk_name in fieldnames:
                fieldnames.remove(self.model._meta.pk.name)

        # id field should always be the first column
        pk_selector = None
        for selector in fieldnames:
            if selector == pk_name or selector.startswith('%s:as' % pk_name):
                pk_selector = selector
                break
        if pk_selector:
            fieldnames.remove(pk_selector)
            fieldnames.insert(0, pk_selector)

        # get list of ignored fields
        ignore = get_listing_option(self.model, 'data_ignore', [])

        # process fields
        names = []
        for fieldname in fieldnames:
            # ignored?
            if fieldname in ignore:
                continue

            # fieldname mapping
            column_name = None
            m = re.match(
                r'(?P<fieldname>[_\w\d]+)(:as\((?P<columnname>[-\._\w\d\s]+)\))?$',
                fieldname)
            if m:
                fieldname = m.group('fieldname')
                column_name = m.group('columnname')

            if column_name == None:
                column_name = fieldname

            try:
                # test for database field
                field = self.model._meta.get_field(fieldname)
                names.append((fieldname, column_name))
            except FieldDoesNotExist:
                if self._is_property_or_function(fieldname):
                    # property or function
                    names.append((fieldname, column_name))
                else:
                    # related, e.g. foo__bar
                    field, related, rel_fieldname, rel_model, title = get_model_related_field(
                        self.model, fieldname)
                    if rel_model:
                        names.append((fieldname, column_name))

        return names