Ticket #28907: less_ifs.patch

File less_ifs.patch, 7.4 KB (added by Дилян Палаузов, 6 months ago)
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    a b class ChangeList: 
    104104                raise DisallowedModelAdminLookup("Filtering by %s not allowed" % key)
    105105
    106106        filter_specs = []
    107         if self.list_filter:
    108             for list_filter in self.list_filter:
    109                 if callable(list_filter):
    110                     # This is simply a custom list filter class.
    111                     spec = list_filter(request, lookup_params, self.model, self.model_admin)
     107        for list_filter in self.list_filter:
     108            if callable(list_filter):
     109                # This is simply a custom list filter class.
     110                spec = list_filter(request, lookup_params, self.model, self.model_admin)
     111            else:
     112                field_path = None
     113                if isinstance(list_filter, (tuple, list)):
     114                    # This is a custom FieldListFilter class for a given field.
     115                    field, field_list_filter_class = list_filter
    112116                else:
    113                     field_path = None
    114                     if isinstance(list_filter, (tuple, list)):
    115                         # This is a custom FieldListFilter class for a given field.
    116                         field, field_list_filter_class = list_filter
    117                     else:
    118                         # This is simply a field name, so use the default
    119                         # FieldListFilter class that has been registered for
    120                         # the type of the given field.
    121                         field, field_list_filter_class = list_filter, FieldListFilter.create
    122                     if not isinstance(field, models.Field):
    123                         field_path = field
    124                         field = get_fields_from_path(self.model, field_path)[-1]
    125 
    126                     lookup_params_count = len(lookup_params)
    127                     spec = field_list_filter_class(
    128                         field, request, lookup_params,
    129                         self.model, self.model_admin, field_path=field_path
    130                     )
    131                     # field_list_filter_class removes any lookup_params it
    132                     # processes. If that happened, check if distinct() is
    133                     # needed to remove duplicate results.
    134                     if lookup_params_count > len(lookup_params):
    135                         use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)
    136                 if spec and spec.has_output():
    137                     filter_specs.append(spec)
     117                    # This is simply a field name, so use the default
     118                    # FieldListFilter class that has been registered for
     119                    # the type of the given field.
     120                    field, field_list_filter_class = list_filter, FieldListFilter.create
     121                if not isinstance(field, models.Field):
     122                    field_path = field
     123                    field = get_fields_from_path(self.model, field_path)[-1]
     124
     125                lookup_params_count = len(lookup_params)
     126                spec = field_list_filter_class(
     127                    field, request, lookup_params,
     128                    self.model, self.model_admin, field_path=field_path
     129                )
     130                # field_list_filter_class removes any lookup_params it
     131                # processes. If that happened, check if distinct() is
     132                # needed to remove duplicate results.
     133                if lookup_params_count > len(lookup_params):
     134                    use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)
     135            if spec and spec.has_output():
     136                filter_specs.append(spec)
    138137
    139138        # At this point, all the parameters used by the various ListFilters
    140139        # have been removed from lookup_params, which now only contains other
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    a b class ModelIterable(BaseIterable): 
    6363        related_populators = get_related_populators(klass_info, select, db)
    6464        for row in compiler.results_iter(results):
    6565            obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
    66             if related_populators:
    67                 for rel_populator in related_populators:
    68                     rel_populator.populate(row, obj)
     66            for rel_populator in related_populators:
     67                rel_populator.populate(row, obj)
    6968            if annotation_col_map:
    7069                for attr_name, col_pos in annotation_col_map.items():
    7170                    setattr(obj, attr_name, row[col_pos])
    class RelatedPopulator: 
    17781777            obj = None
    17791778        else:
    17801779            obj = self.model_cls.from_db(self.db, self.init_list, obj_data)
    1781             if self.related_populators:
    1782                 for rel_iter in self.related_populators:
    1783                     rel_iter.populate(row, obj)
     1780            for rel_iter in self.related_populators:
     1781                rel_iter.populate(row, obj)
    17841782        self.local_setter(from_obj, obj)
    17851783        if obj is not None:
    17861784            self.remote_setter(obj, from_obj)
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    a b def url(parser, token): 
    13631363        asvar = bits[-1]
    13641364        bits = bits[:-2]
    13651365
    1366     if bits:
    1367         for bit in bits:
    1368             match = kwarg_re.match(bit)
    1369             if not match:
    1370                 raise TemplateSyntaxError("Malformed arguments to url tag")
    1371             name, value = match.groups()
    1372             if name:
    1373                 kwargs[name] = parser.compile_filter(value)
    1374             else:
    1375                 args.append(parser.compile_filter(value))
     1366    for bit in bits:
     1367        match = kwarg_re.match(bit)
     1368        if not match:
     1369            raise TemplateSyntaxError("Malformed arguments to url tag")
     1370        name, value = match.groups()
     1371        if name:
     1372            kwargs[name] = parser.compile_filter(value)
     1373        else:
     1374            args.append(parser.compile_filter(value))
    13761375
    13771376    return URLNode(viewname, args, kwargs, asvar)
    13781377
  • tests/admin_scripts/tests.py

    diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
    a b class AdminScriptTestCase(unittest.TestCase): 
    8282
    8383            settings_file.write("INSTALLED_APPS = %s\n" % apps)
    8484
    85             if sdict:
    86                 for k, v in sdict.items():
    87                     settings_file.write("%s = %s\n" % (k, v))
     85            for k, v in sdict.items():
     86                settings_file.write("%s = %s\n" % (k, v))
    8887
    8988    def remove_settings(self, filename, is_dir=False):
    9089        full_name = os.path.join(self.test_dir, filename)
  • tests/middleware/test_security.py

    diff --git a/tests/middleware/test_security.py b/tests/middleware/test_security.py
    a b class SecurityMiddlewareTest(SimpleTestCase): 
    1515
    1616    def response(self, *args, headers=None, **kwargs):
    1717        response = HttpResponse(*args, **kwargs)
    18         if headers:
    19             for k, v in headers.items():
    20                 response[k] = v
     18        for k, v in headers.items():
     19            response[k] = v
    2120        return response
    2221
    2322    def process_response(self, *args, secure=False, request=None, **kwargs):
Back to Top