From 4f775a0a20c070a84f3b23f31c77c072a109670c Mon Sep 17 00:00:00 2001 From: Ben Mehlman Date: Thu, 5 Sep 2019 15:42:16 -0400 Subject: [PATCH 1/4] (#682) Fix row_counter behavior when mixing access methods of BoundRows. --- django_tables2/rows.py | 17 +++++++++++------ django_tables2/tables.py | 1 - 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/django_tables2/rows.py b/django_tables2/rows.py index c120a6d3..bb9af2b8 100644 --- a/django_tables2/rows.py +++ b/django_tables2/rows.py @@ -1,6 +1,8 @@ # coding: utf-8 from __future__ import absolute_import, unicode_literals +from itertools import count + from django.db import models from django.db.models.fields import FieldDoesNotExist from django.utils import six @@ -83,11 +85,12 @@ class BoundRow(object): """ - def __init__(self, record, table): + def __init__(self, record, table, boundrows=None, index=-1): self._record = record self._table = table + self._boundrows = boundrows - self.row_counter = next(table._counter) + self.row_counter = (boundrows is not None and next(boundrows._counter)) or (index + 1) # support accessing cells from a template: {{ row.cells.column_name }} self.cells = CellAccessor(self) @@ -299,10 +302,12 @@ class BoundRows(object): This is used for `~.Table.rows`. """ - def __init__(self, data, table, pinned_data=None): + def __init__(self, data, table, pinned_data=None, index=0): self.data = data self.table = table self.pinned_data = pinned_data or {} + self._index = index + self._counter = count(index+1) def generator_pinned_row(self, data): """ @@ -327,7 +332,7 @@ def __iter__(self): yield pinned_record for record in self.data: - yield BoundRow(record, table=self.table) + yield BoundRow(record, table=self.table, boundrows=self) # Bottom pinned rows for pinned_record in self.generator_pinned_row(self.pinned_data.get("bottom")): @@ -347,6 +352,6 @@ def __getitem__(self, key): `~.BoundRow` instance. """ if isinstance(key, slice): - return BoundRows(data=self.data[key], table=self.table, pinned_data=self.pinned_data) + return BoundRows(data=self.data[key], table=self.table, pinned_data=self.pinned_data, index=key.start) else: - return BoundRow(record=self.data[key], table=self.table) + return BoundRow(record=self.data[key], table=self.table, index=key+self._index) diff --git a/django_tables2/tables.py b/django_tables2/tables.py index 45a254e6..4acbd864 100644 --- a/django_tables2/tables.py +++ b/django_tables2/tables.py @@ -366,7 +366,6 @@ def __init__( if request: RequestConfig(request).configure(self) - self._counter = count() def get_top_pinned_data(self): """ From 27516252fb6ba3cd25f60e0f606def0741f10dbf Mon Sep 17 00:00:00 2001 From: Jan Pieter Waagmeester Date: Mon, 11 Nov 2019 13:49:57 +0100 Subject: [PATCH 2/4] remove future imports --- django_tables2/rows.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/django_tables2/rows.py b/django_tables2/rows.py index bb9af2b8..4f51294a 100644 --- a/django_tables2/rows.py +++ b/django_tables2/rows.py @@ -1,6 +1,3 @@ -# coding: utf-8 -from __future__ import absolute_import, unicode_literals - from itertools import count from django.db import models From d7fb470ea062c217d94dfbcd1be251ace1c20516 Mon Sep 17 00:00:00 2001 From: Jan Pieter Waagmeester Date: Mon, 11 Nov 2019 13:51:11 +0100 Subject: [PATCH 3/4] remove from itertools import counter too --- django_tables2/rows.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/django_tables2/rows.py b/django_tables2/rows.py index 4f51294a..ed730b3c 100644 --- a/django_tables2/rows.py +++ b/django_tables2/rows.py @@ -1,5 +1,3 @@ -from itertools import count - from django.db import models from django.db.models.fields import FieldDoesNotExist from django.utils import six From d6781f29ce2dbac7f0d615cc2dddd9f4d6e5dcb1 Mon Sep 17 00:00:00 2001 From: Jan Pieter Waagmeester Date: Mon, 11 Nov 2019 13:56:58 +0100 Subject: [PATCH 4/4] re-add count import --- django_tables2/rows.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django_tables2/rows.py b/django_tables2/rows.py index ed730b3c..4f51294a 100644 --- a/django_tables2/rows.py +++ b/django_tables2/rows.py @@ -1,3 +1,5 @@ +from itertools import count + from django.db import models from django.db.models.fields import FieldDoesNotExist from django.utils import six