• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

django-import-export / django-import-export / 15833405581

23 Jun 2025 07:33PM UTC coverage: 99.912%. Remained the same
15833405581

push

github

web-flow
Prepare release 4.3.8 (#2074)

* updated messages

* prepare release 4.3.8

2280 of 2282 relevant lines covered (99.91%)

4.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/import_export/command_utils.py
1
from django.apps import apps
5✔
2
from django.core.management.base import CommandError
5✔
3
from django.http.response import mimetypes
5✔
4
from django.utils.module_loading import import_string
5✔
5

6
from import_export.formats.base_formats import DEFAULT_FORMATS
5✔
7
from import_export.resources import modelresource_factory
5✔
8

9

10
def get_resource_class(model_or_resource_class):
5✔
11
    try:
5✔
12
        # First, try to load it as a resource class
13
        resource_class = import_string(model_or_resource_class)
5✔
14
        return resource_class
5✔
15
    except ImportError:
5✔
16
        pass
5✔
17

18
    try:
5✔
19
        if model_or_resource_class.count(".") == 1:
5✔
20
            app_label, model_name = model_or_resource_class.split(".")
5✔
21
            model = apps.get_model(app_label, model_name)
5✔
22
            if model:
5✔
23
                resource_class = modelresource_factory(model)
5✔
24
                return resource_class
5✔
25
    except LookupError:
5✔
26
        pass
5✔
27

28
    raise CommandError(
5✔
29
        f"Cannot import '{model_or_resource_class}' as a resource class or model."
30
    )
31

32

33
MIME_TYPE_FORMAT_MAPPING = {format.CONTENT_TYPE: format for format in DEFAULT_FORMATS}
5✔
34

35

36
def get_format_class(format_name, file_name, encoding=None):
5✔
37
    if format_name:
5✔
38
        try:
5✔
39
            # Direct import attempt
40
            format_class = import_string(format_name)
5✔
41
        except ImportError:
5✔
42
            # Fallback to base_formats
43
            fallback_format_name = f"import_export.formats.base_formats.{format_name}"
5✔
44
            try:
5✔
45
                format_class = import_string(fallback_format_name)
5✔
46
            except ImportError:
5✔
47
                # fallback to uppercase format name
48
                try:
5✔
49
                    format_class = import_string(
5✔
50
                        f"import_export.formats.base_formats.{format_name.upper()}"
51
                    )
52
                except ImportError:
5✔
53
                    raise CommandError(
5✔
54
                        f"Cannot import '{format_name}' or '{fallback_format_name}'"
55
                        " format class."
56
                    )
57
        return format_class(encoding=encoding)
5✔
58

59
    else:
60
        # Determine MIME type from file name
61
        mimetype, file_encoding = mimetypes.guess_type(file_name)
5✔
62

63
        if not mimetype:
5✔
64
            raise CommandError(
5✔
65
                f"Cannot determine MIME type for '{file_name}'. "
66
                " Please specify format with --format."
67
            )
68

69
        try:
5✔
70
            format_class = MIME_TYPE_FORMAT_MAPPING[mimetype]
5✔
71
            return format_class(encoding=encoding or file_encoding)
5✔
72
        except KeyError:
5✔
73
            raise CommandError(
5✔
74
                f"Cannot find format for MIME type '{mimetype}'."
75
                " Please specify format with --format."
76
            )
77

78

79
def get_default_format_names():
5✔
80
    return ", ".join([f.__name__ for f in DEFAULT_FORMATS])
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc