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

zopefoundation / z3c.table / 4524457608

pending completion
4524457608

push

github

GitHub
Config with pure python template (#13)

173 of 189 branches covered (91.53%)

Branch coverage included in aggregate %.

118 of 118 new or added lines in 8 files covered. (100.0%)

1004 of 1024 relevant lines covered (98.05%)

0.98 hits per line

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

86.67
/src/z3c/table/header.py
1
##############################################################################
2
#
3
# Copyright (c) 2008 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
__docformat__ = "reStructuredText"
1✔
15

16

17
from urllib.parse import urlencode
1✔
18

19
import zope.i18n
1✔
20
import zope.interface
1✔
21

22
import z3c.table.interfaces
1✔
23
from z3c.table.i18n import _
1✔
24

25

26
@zope.interface.implementer(z3c.table.interfaces.IColumnHeader)
1✔
27
class ColumnHeader:
1✔
28
    """ColumnHeader renderer provider"""
29

30
    _request_args = []
1✔
31

32
    def __init__(self, context, request, table, column):
1✔
33
        self.__parent__ = context
1✔
34
        self.context = context
1✔
35
        self.request = request
1✔
36
        self.table = table
1✔
37
        self.column = column
1✔
38

39
    def update(self):
1✔
40
        """Override this method in subclasses if required"""
41
        pass
1✔
42

43
    def render(self):
1✔
44
        """Override this method in subclasses"""
45
        return self.column.header
×
46

47
    def getQueryStringArgs(self):
1✔
48
        """
49
        Collect additional terms from the request and include in sorting column
50
        headers
51

52
        Perhaps this should be in separate interface only for sorting headers?
53

54
        """
55
        args = {}
1✔
56
        for key in self._request_args:
1!
57
            value = self.request.get(key, None)
×
58
            if value:
×
59
                args.update({key: value})
×
60
        return args
1✔
61

62

63
class SortingColumnHeader(ColumnHeader):
1✔
64
    """Sorting column header."""
65

66
    def render(self):
1✔
67
        table = self.table
1✔
68
        prefix = table.prefix
1✔
69
        colID = self.column.id
1✔
70

71
        # this may return a string 'id-name-idx' if coming from request,
72
        # otherwise in Table class it is intialised as a integer string
73
        currentSortID = table.getSortOn()
1✔
74
        try:
1✔
75
            currentSortID = int(currentSortID)
1✔
76
        except ValueError:
1✔
77
            currentSortID = currentSortID.rsplit("-", 1)[-1]
1✔
78

79
        currentSortOrder = table.getSortOrder()
1✔
80

81
        sortID = colID.rsplit("-", 1)[-1]
1✔
82

83
        sortOrder = table.sortOrder
1✔
84
        if int(sortID) == int(currentSortID):
1✔
85
            # ordering the same column so we want to reverse the order
86
            if currentSortOrder in table.reverseSortOrderNames:
1✔
87
                sortOrder = "ascending"
1✔
88
            elif currentSortOrder == "ascending":
1!
89
                sortOrder = table.reverseSortOrderNames[0]
1✔
90

91
        args = self.getQueryStringArgs()
1✔
92
        args.update(
1✔
93
            {"%s-sortOn" % prefix: colID, "%s-sortOrder" % prefix: sortOrder}
94
        )
95
        queryString = "?%s" % (urlencode(sorted(args.items())))
1✔
96

97
        return '<a href="{}" title="{}">{}</a>'.format(
1✔
98
            queryString,
99
            zope.i18n.translate(_("Sort"), context=self.request),
100
            zope.i18n.translate(self.column.header, context=self.request),
101
        )
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