|
11 | 11 | register = template.Library() |
12 | 12 |
|
13 | 13 |
|
14 | | -def anchor(parser: Parser, token: Token) -> SortAnchorNode: |
15 | | - """ |
16 | | - Parses a tag that's supposed to be in this format '{% anchor field title %}' |
17 | | - Title may be a "string", _("trans string"), or variable |
18 | | - Optional - default sort direction to desc '{% anchor field title "desc" %}' |
19 | | - """ |
20 | | - bits = [b for b in token.split_contents()] |
21 | | - if len(bits) < 2: |
22 | | - raise template.TemplateSyntaxError("anchor tag takes at least 1 argument.") |
23 | | - |
24 | | - title_is_var = False |
25 | | - title_is_translatable = False |
26 | | - try: |
27 | | - title = bits[2] |
28 | | - if title[0] in ('"', "'"): |
29 | | - if title[0] == title[-1]: |
30 | | - title = title[1:-1] |
31 | | - else: |
32 | | - raise template.TemplateSyntaxError( |
33 | | - 'anchor tag title must be a "string", _("trans string"), or variable' |
34 | | - ) |
35 | | - elif title.startswith('_("') or title.startswith("_('"): |
36 | | - title_is_translatable = True |
37 | | - else: |
38 | | - title_is_var = True |
39 | | - except IndexError: |
40 | | - title = bits[1].capitalize() |
41 | | - |
42 | | - default_sort_order = "desc" if len(bits) >= 4 and bits[3].strip("'\"") == "desc" else "asc" |
43 | | - |
44 | | - return SortAnchorNode( |
45 | | - bits[1].strip(), |
46 | | - title.strip(), |
47 | | - title_is_var, |
48 | | - title_is_translatable, |
49 | | - default_sort_order, |
50 | | - ) |
51 | | - |
52 | | - |
53 | 14 | class SortAnchorNode(template.Node): |
54 | 15 | """ |
55 | 16 | Renders an <a> HTML tag with a link which href attribute |
@@ -90,30 +51,6 @@ def render(self, context: Context) -> str: |
90 | 51 | ) |
91 | 52 |
|
92 | 53 |
|
93 | | -def autosort(parser: Parser, token: Token) -> SortedDataNode: |
94 | | - """Parse the autosort template tag.""" |
95 | | - bits = [b.strip("\"'") for b in token.split_contents()] |
96 | | - help_msg = "autosort tag synopsis: {%% autosort queryset [as context_variable] %%}" |
97 | | - context_var: str | None = None |
98 | | - |
99 | | - # Check if their is some optional parameter (as new_context_var, nulls) |
100 | | - if 2 > len(bits) > 7: |
101 | | - raise template.TemplateSyntaxError(help_msg) |
102 | | - |
103 | | - context_var = None |
104 | | - null_ordering: str | None = None |
105 | | - |
106 | | - for index, bit in enumerate(bits): |
107 | | - if index > 1: |
108 | | - if bit == "as" and index + 1 < len(bits): |
109 | | - context_var = bits[index + 1] |
110 | | - del bits[index : index + 1] |
111 | | - if bit.startswith("nulls"): |
112 | | - null_ordering = bit[len("nulls=") :] |
113 | | - |
114 | | - return SortedDataNode(bits[1], null_ordering, context_var=context_var) |
115 | | - |
116 | | - |
117 | 54 | class SortedDataNode(template.Node): |
118 | 55 | """ |
119 | 56 | Automatically sort a queryset with {% autosort queryset %} |
@@ -154,5 +91,68 @@ def render(self, context: Context) -> str: |
154 | 91 | return "" |
155 | 92 |
|
156 | 93 |
|
| 94 | +def anchor(parser: Parser, token: Token) -> SortAnchorNode: |
| 95 | + """ |
| 96 | + Parses a tag that's supposed to be in this format '{% anchor field title %}' |
| 97 | + Title may be a "string", _("trans string"), or variable |
| 98 | + Optional - default sort direction to desc '{% anchor field title "desc" %}' |
| 99 | + """ |
| 100 | + bits = [b for b in token.split_contents()] |
| 101 | + if len(bits) < 2: |
| 102 | + raise template.TemplateSyntaxError("anchor tag takes at least 1 argument.") |
| 103 | + |
| 104 | + title_is_var = False |
| 105 | + title_is_translatable = False |
| 106 | + try: |
| 107 | + title = bits[2] |
| 108 | + if title[0] in ('"', "'"): |
| 109 | + if title[0] == title[-1]: |
| 110 | + title = title[1:-1] |
| 111 | + else: |
| 112 | + raise template.TemplateSyntaxError( |
| 113 | + 'anchor tag title must be a "string", _("trans string"), or variable' |
| 114 | + ) |
| 115 | + elif title.startswith('_("') or title.startswith("_('"): |
| 116 | + title_is_translatable = True |
| 117 | + else: |
| 118 | + title_is_var = True |
| 119 | + except IndexError: |
| 120 | + title = bits[1].capitalize() |
| 121 | + |
| 122 | + default_sort_order = "desc" if len(bits) >= 4 and bits[3].strip("'\"") == "desc" else "asc" |
| 123 | + |
| 124 | + return SortAnchorNode( |
| 125 | + bits[1].strip(), |
| 126 | + title.strip(), |
| 127 | + title_is_var, |
| 128 | + title_is_translatable, |
| 129 | + default_sort_order, |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +def autosort(parser: Parser, token: Token) -> SortedDataNode: |
| 134 | + """Parse the autosort template tag.""" |
| 135 | + bits = [b.strip("\"'") for b in token.split_contents()] |
| 136 | + help_msg = "autosort tag synopsis: {%% autosort queryset [as context_variable] %%}" |
| 137 | + context_var: str | None = None |
| 138 | + |
| 139 | + # Check if their is some optional parameter (as new_context_var, nulls) |
| 140 | + if 2 > len(bits) > 7: |
| 141 | + raise template.TemplateSyntaxError(help_msg) |
| 142 | + |
| 143 | + context_var = None |
| 144 | + null_ordering: str | None = None |
| 145 | + |
| 146 | + for index, bit in enumerate(bits): |
| 147 | + if index > 1: |
| 148 | + if bit == "as" and index + 1 < len(bits): |
| 149 | + context_var = bits[index + 1] |
| 150 | + del bits[index : index + 1] |
| 151 | + if bit.startswith("nulls"): |
| 152 | + null_ordering = bit[len("nulls=") :] |
| 153 | + |
| 154 | + return SortedDataNode(bits[1], null_ordering, context_var=context_var) |
| 155 | + |
| 156 | + |
157 | 157 | anchor = register.tag(anchor) |
158 | 158 | autosort = register.tag(autosort) |
0 commit comments