django/django

View on GitHub
django/contrib/admin/utils.py

Summary

Maintainability
F
3 days
Test Coverage

File utils.py has 442 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import datetime
import decimal
import re
from collections import defaultdict

Severity: Minor
Found in django/contrib/admin/utils.py - About 6 hrs to fix

    Function label_for_field has a Cognitive Complexity of 35 (exceeds 5 allowed). Consider refactoring.
    Open

    def label_for_field(name, model, model_admin=None, return_attr=False, form=None):
        """
        Return a sensible label for a field name. The name can be a callable,
        property (but not created with @property decorator), or the name of an
        object's attribute, as well as a model field. If return_attr is True, also
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 5 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function construct_change_message has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
    Open

    def construct_change_message(form, formsets, add):
        """
        Construct a JSON structure describing changes from a changed object.
        Translations are deactivated so that strings are stored untranslated.
        Translation happens later on LogEntry access.
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function lookup_needs_distinct has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

    def lookup_needs_distinct(opts, lookup_path):
        """
        Return True if 'distinct()' should be used to query the given lookup path.
        """
        lookup_fields = lookup_path.split(LOOKUP_SEP)
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function get_deleted_objects has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

    def get_deleted_objects(objs, request, admin_site):
        """
        Find all objects related to ``objs`` that should also be deleted. ``objs``
        must be a homogeneous iterable of objects (e.g. a QuerySet).
    
    
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function lookup_field has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

    def lookup_field(name, obj, model_admin=None):
        opts = obj._meta
        try:
            f = _get_non_gfk_field(opts, name)
        except (FieldDoesNotExist, FieldIsAForeignKeyColumnName):
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function reverse_field_path has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

    def reverse_field_path(model, path):
        """ Create a reversed field path.
    
        E.g. Given (Order, "user__groups"),
        return (Group, "user__order").
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function display_for_field has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

    def display_for_field(value, field, empty_value_display):
        from django.contrib.admin.templatetags.admin_list import _boolean_icon
    
        if getattr(field, 'flatchoices', None):
            return dict(field.flatchoices).get(value, empty_value_display)
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 55 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function display_for_value has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

    def display_for_value(value, empty_value_display, boolean=False):
        from django.contrib.admin.templatetags.admin_list import _boolean_icon
    
        if boolean:
            return _boolean_icon(value)
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 45 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function label_for_field has 5 arguments (exceeds 4 allowed). Consider refactoring.
    Open

    def label_for_field(name, model, model_admin=None, return_attr=False, form=None):
    Severity: Minor
    Found in django/contrib/admin/utils.py - About 35 mins to fix

      Avoid too many return statements within this function.
      Open

              return formats.number_format(value, field.decimal_places)
      Severity: Major
      Found in django/contrib/admin/utils.py - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

                return formats.number_format(value)
        Severity: Major
        Found in django/contrib/admin/utils.py - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

                  return formats.localize(value)
          Severity: Major
          Found in django/contrib/admin/utils.py - About 30 mins to fix

            Avoid too many return statements within this function.
            Open

                    return format_html('<a href="{}">{}</a>', value.url, value)
            Severity: Major
            Found in django/contrib/admin/utils.py - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                      return formats.number_format(value)
              Severity: Major
              Found in django/contrib/admin/utils.py - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                        return display_for_value(value, empty_value_display)
                Severity: Major
                Found in django/contrib/admin/utils.py - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                          return ', '.join(str(v) for v in value)
                  Severity: Major
                  Found in django/contrib/admin/utils.py - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                            return formats.localize(value)
                    Severity: Major
                    Found in django/contrib/admin/utils.py - About 30 mins to fix

                      Avoid too many return statements within this function.
                      Open

                              return str(value)
                      Severity: Major
                      Found in django/contrib/admin/utils.py - About 30 mins to fix

                        Function collect has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def collect(self, objs, source=None, source_attr=None, **kwargs):
                                for obj in objs:
                                    if source_attr and not source_attr.endswith('+'):
                                        related_name = source_attr % {
                                            'class': source._meta.model_name,
                        Severity: Minor
                        Found in django/contrib/admin/utils.py - About 25 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Function _get_non_gfk_field has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                        Open

                        def _get_non_gfk_field(opts, name):
                            """
                            For historical reasons, the admin app relies on GenericForeignKeys as being
                            "not found" by get_field(). This could likely be cleaned up.
                        
                        
                        Severity: Minor
                        Found in django/contrib/admin/utils.py - About 25 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Similar blocks of code found in 2 locations. Consider refactoring.
                        Open

                                        for added_object in formset.new_objects:
                                            change_message.append({
                                                'added': {
                                                    'name': str(added_object._meta.verbose_name),
                                                    'object': str(added_object),
                        Severity: Major
                        Found in django/contrib/admin/utils.py and 1 other location - About 1 hr to fix
                        django/contrib/admin/utils.py on lines 525..529

                        Duplicated Code

                        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                        Tuning

                        This issue has a mass of 39.

                        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                        Refactorings

                        Further Reading

                        Similar blocks of code found in 2 locations. Consider refactoring.
                        Open

                                        for deleted_object in formset.deleted_objects:
                                            change_message.append({
                                                'deleted': {
                                                    'name': str(deleted_object._meta.verbose_name),
                                                    'object': str(deleted_object),
                        Severity: Major
                        Found in django/contrib/admin/utils.py and 1 other location - About 1 hr to fix
                        django/contrib/admin/utils.py on lines 510..514

                        Duplicated Code

                        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                        Tuning

                        This issue has a mass of 39.

                        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                        Refactorings

                        Further Reading

                        Similar blocks of code found in 2 locations. Consider refactoring.
                        Open

                                for child in self.edges.get(obj, ()):
                                    children.extend(self._nested(child, seen, format_callback))
                        Severity: Minor
                        Found in django/contrib/admin/utils.py and 1 other location - About 30 mins to fix
                        django/contrib/admin/utils.py on lines 210..211

                        Duplicated Code

                        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                        Tuning

                        This issue has a mass of 32.

                        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                        Refactorings

                        Further Reading

                        Similar blocks of code found in 2 locations. Consider refactoring.
                        Open

                                for root in self.edges.get(None, ()):
                                    roots.extend(self._nested(root, seen, format_callback))
                        Severity: Minor
                        Found in django/contrib/admin/utils.py and 1 other location - About 30 mins to fix
                        django/contrib/admin/utils.py on lines 194..195

                        Duplicated Code

                        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                        Tuning

                        This issue has a mass of 32.

                        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                        Refactorings

                        Further Reading

                        There are no issues that match your filters.

                        Category
                        Status