1
1
# -*- coding: utf-8 -*-
2
2
import copy
3
3
from django .core .paginator import Paginator
4
- from django .utils . datastructures import SortedDict
4
+ from django .db . models . query import QuerySet
5
5
from django .http import Http404
6
- from django .template . loader import get_template
6
+ from django .utils . datastructures import SortedDict
7
7
from django .template import Context
8
+ from django .template .loader import get_template
8
9
from django .utils .encoding import StrAndUnicode
9
- from django .db .models .query import QuerySet
10
- from itertools import chain
11
- from .utils import OrderBy , OrderByTuple , Accessor , AttributeDict
10
+ from .utils import Accessor , AttributeDict , OrderBy , OrderByTuple , Sequence
12
11
from .rows import BoundRows , BoundRow
13
12
from .columns import BoundColumns , Column
14
13
15
14
16
15
QUERYSET_ACCESSOR_SEPARATOR = '__'
17
16
18
17
19
- class Sequence (list ):
20
- """
21
- Represents a column sequence, e.g. ("first_name", "...", "last_name")
22
-
23
- This is used to represent ``Table.Meta.sequence`` or the Table
24
- constructors's ``sequence`` keyword argument.
25
-
26
- The sequence must be a list of column names and is used to specify the
27
- order of the columns on a table. Optionally a "..." item can be inserted,
28
- which is treated as a *catch-all* for column names that aren't explicitly
29
- specified.
30
- """
31
- def expand (self , columns ):
32
- """
33
- Expands the "..." item in the sequence into the appropriate column
34
- names that should be placed there.
35
-
36
- :raises: ``ValueError`` if the sequence is invalid for the columns.
37
- """
38
- # validation
39
- if self .count ("..." ) > 1 :
40
- raise ValueError ("'...' must be used at most once in a sequence." )
41
- elif "..." in self :
42
- # Check for columns in the sequence that don't exist in *columns*
43
- extra = (set (self ) - set (("..." , ))).difference (columns )
44
- if extra :
45
- raise ValueError (u"sequence contains columns that do not exist"
46
- u" in the table. Remove '%s'."
47
- % "', '" .join (extra ))
48
- else :
49
- diff = set (self ) ^ set (columns )
50
- if diff :
51
- raise ValueError (u"sequence does not match columns. Fix '%s' "
52
- u"or possibly add '...'." % "', '" .join (diff ))
53
- # everything looks good, let's expand the "..." item
54
- columns = columns [:] # don't modify
55
- head = []
56
- tail = []
57
- target = head # start by adding things to the head
58
- for name in self :
59
- if name == "..." :
60
- # now we'll start adding elements to the tail
61
- target = tail
62
- continue
63
- else :
64
- target .append (columns .pop (columns .index (name )))
65
- self [:] = list (chain (head , columns , tail ))
66
-
67
-
68
18
class TableData (object ):
69
19
"""
70
20
Exposes a consistent API for :term:`table data`. It currently supports a
@@ -284,9 +234,9 @@ def obj_list(request):
284
234
def __init__ (self , data , order_by = None , sortable = None , empty_text = None ,
285
235
exclude = None , attrs = None , sequence = None , prefix = None ,
286
236
order_by_field = None , page_field = None , per_page_field = None ):
287
- self ._rows = BoundRows (self )
288
- self ._columns = BoundColumns (self )
289
- self ._data = self .TableDataClass (data = data , table = self )
237
+ self .rows = BoundRows (self )
238
+ self .columns = BoundColumns (self )
239
+ self .data = self .TableDataClass (data = data , table = self )
290
240
self .attrs = attrs
291
241
self .empty_text = empty_text
292
242
self .sortable = sortable
@@ -299,9 +249,6 @@ def __init__(self, data, order_by=None, sortable=None, empty_text=None,
299
249
# copy is made available in a ``fields`` attribute.
300
250
self .base_columns = copy .deepcopy (self .__class__ .base_columns )
301
251
self .exclude = exclude or ()
302
- for ex in self .exclude :
303
- if ex in self .base_columns :
304
- self .base_columns .pop (ex )
305
252
self .sequence = sequence
306
253
if order_by is None :
307
254
self .order_by = self ._meta .order_by
@@ -337,14 +284,6 @@ def attrs(self):
337
284
def attrs (self , value ):
338
285
self ._attrs = value
339
286
340
- @property
341
- def columns (self ):
342
- return self ._columns
343
-
344
- @property
345
- def data (self ):
346
- return self ._data
347
-
348
287
@property
349
288
def empty_text (self ):
350
289
return (self ._empty_text if self ._empty_text is not None
@@ -377,7 +316,7 @@ def order_by(self, value):
377
316
new .append (ob )
378
317
order_by = OrderByTuple (new )
379
318
self ._order_by = order_by
380
- self ._data .order_by (order_by )
319
+ self .data .order_by (order_by )
381
320
382
321
@property
383
322
def order_by_field (self ):
@@ -447,10 +386,6 @@ def prefixed_page_field(self):
447
386
def prefixed_per_page_field (self ):
448
387
return u"%s%s" % (self .prefix , self .per_page_field )
449
388
450
- @property
451
- def rows (self ):
452
- return self ._rows
453
-
454
389
@property
455
390
def sequence (self ):
456
391
return (self ._sequence if self ._sequence is not None
0 commit comments