author | Todd Larsen <tlarsen@google.com> |
Wed, 21 Jan 2009 18:25:37 +0000 | |
changeset 880 | d67557c612de |
parent 835 | 1590625ead55 |
child 935 | 09f47e08f805 |
permissions | -rw-r--r-- |
38
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
2 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
4 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
8 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
10 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
16 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
17 |
"""A Django template tag library containing forms helpers. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
18 |
""" |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
19 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
21 |
'"Todd Larsen" <tlarsen@google.com>', |
99
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
22 |
'"Pawel Solyga" <pawel.solyga@gmail.com>', |
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
23 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
38
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
24 |
] |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
25 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
26 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
27 |
from django import template |
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
28 |
from django.forms import forms as forms_in |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
29 |
from django.utils.encoding import force_unicode |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
30 |
from django.utils.html import escape |
316
9efdc7bc3565
Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
273
diff
changeset
|
31 |
|
322
6641e941ef1e
Fixed imports sorting based on comments for r751.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
316
diff
changeset
|
32 |
|
38
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
33 |
register = template.Library() |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
34 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
35 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
36 |
@register.inclusion_tag('soc/templatetags/_field_as_table_row.html') |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
37 |
def field_as_table_row(field): |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
38 |
"""Prints a newforms field as a table row. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
39 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
40 |
This function actually does very little, simply passing the supplied |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
41 |
form field instance in a simple context used by the _field_as_table_row.html |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
42 |
template (which is actually doing all of the work). |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
43 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
44 |
See soc/templates/soc/templatetags/_field_as_table_row.html for the CSS |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
45 |
styles used by this template tag. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
46 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
47 |
Usage: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
48 |
{% load forms_helpers %} |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
49 |
... |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
50 |
<table> |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
51 |
{% field_as_table_row form.fieldname %} |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
52 |
... |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
53 |
</table> |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
54 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
55 |
Args: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
56 |
field: a Django newforms field instance |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
57 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
58 |
Returns: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
59 |
a simple context containing the supplied newforms field instance: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
60 |
{ 'field': field } |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
61 |
""" |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
62 |
return {'field': field} |
99
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
63 |
|
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
64 |
|
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
65 |
@register.inclusion_tag('soc/templatetags/_readonly_field_as_table_row.html') |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
66 |
def readonly_field_as_table_row(field_label, field_value): |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
67 |
"""Prints a field value and it's verbose name as a table row. |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
68 |
|
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
69 |
This function actually does very little, simply passing the |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
70 |
supplied field_label and field_value in a simple context used by the |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
71 |
_readonly_field_as_table_row.html template (which is actually |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
72 |
doing all of the work). |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
73 |
|
744
cd9bf163473c
Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
741
diff
changeset
|
74 |
See soc/templates/soc/templatetags/_readonly_field_as_table_row.html for |
cd9bf163473c
Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
741
diff
changeset
|
75 |
the CSS styles used by this template tag. |
99
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
76 |
|
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
77 |
Usage: |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
78 |
{% load forms_helpers %} |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
79 |
... |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
80 |
<table> |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
81 |
{% readonly_field_as_table_row field_label field_value %} |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
82 |
... |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
83 |
</table> |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
84 |
|
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
85 |
Args: |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
86 |
field_label: label of the field to render |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
87 |
field_value: value of the field to render |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
88 |
|
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
89 |
Returns: |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
90 |
a simple context containing the supplied newforms field instance: |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
91 |
{ 'field_label': field_label', |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
92 |
'field_value': field_value'} |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
93 |
""" |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
94 |
return {'field_label': field_label, |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
95 |
'field_value': field_value} |
8c38b546a3cf
Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
54
diff
changeset
|
96 |
|
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
97 |
|
794
19c24508c398
Added _readonly_field_as_twoline_table_row .
Lennard de Rijk <ljvderijk@gmail.com>
parents:
785
diff
changeset
|
98 |
@register.inclusion_tag('soc/templatetags/_readonly_field_as_twoline_table_row.html') |
19c24508c398
Added _readonly_field_as_twoline_table_row .
Lennard de Rijk <ljvderijk@gmail.com>
parents:
785
diff
changeset
|
99 |
def readonly_field_as_twoline_table_row(field_label, field_value): |
19c24508c398
Added _readonly_field_as_twoline_table_row .
Lennard de Rijk <ljvderijk@gmail.com>
parents:
785
diff
changeset
|
100 |
"""See readonly_field_as_table_row(). |
19c24508c398
Added _readonly_field_as_twoline_table_row .
Lennard de Rijk <ljvderijk@gmail.com>
parents:
785
diff
changeset
|
101 |
""" |
19c24508c398
Added _readonly_field_as_twoline_table_row .
Lennard de Rijk <ljvderijk@gmail.com>
parents:
785
diff
changeset
|
102 |
return {'field_label': field_label, |
19c24508c398
Added _readonly_field_as_twoline_table_row .
Lennard de Rijk <ljvderijk@gmail.com>
parents:
785
diff
changeset
|
103 |
'field_value': field_value} |
19c24508c398
Added _readonly_field_as_twoline_table_row .
Lennard de Rijk <ljvderijk@gmail.com>
parents:
785
diff
changeset
|
104 |
|
728
602c2b2f4d8b
Added a readonly_multiline_field_as_table_row tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
322
diff
changeset
|
105 |
|
744
cd9bf163473c
Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
741
diff
changeset
|
106 |
@register.inclusion_tag( |
cd9bf163473c
Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
741
diff
changeset
|
107 |
'soc/templatetags/_readonly_multiline_field_as_table_row.html') |
728
602c2b2f4d8b
Added a readonly_multiline_field_as_table_row tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
322
diff
changeset
|
108 |
def readonly_multiline_field_as_table_row(field_label, field_value): |
744
cd9bf163473c
Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
741
diff
changeset
|
109 |
"""See readonly_field_as_table_row, but with a different template tag. |
728
602c2b2f4d8b
Added a readonly_multiline_field_as_table_row tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
322
diff
changeset
|
110 |
""" |
602c2b2f4d8b
Added a readonly_multiline_field_as_table_row tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
322
diff
changeset
|
111 |
return {'field_label': field_label, |
602c2b2f4d8b
Added a readonly_multiline_field_as_table_row tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
322
diff
changeset
|
112 |
'field_value': field_value} |
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
113 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
114 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
115 |
@register.inclusion_tag('soc/templatetags/_as_table.html') |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
116 |
def as_table(form): |
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
117 |
"""Outputs a form as a properly formatted html table. |
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
118 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
119 |
Args: |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
120 |
form: the form that should be converted to a table |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
121 |
""" |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
122 |
|
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
123 |
return as_table_helper(form) |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
124 |
|
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
125 |
|
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
126 |
@register.inclusion_tag('soc/templatetags/_as_twoline_table.html') |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
127 |
def as_twoline_table(form): |
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
128 |
"""Outputs a form as a properly formatted html table. |
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
129 |
|
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
130 |
Args: |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
131 |
form: the form that should be converted to a table |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
132 |
""" |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
133 |
|
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
134 |
return as_table_helper(form) |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
135 |
|
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
136 |
|
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
137 |
def as_table_helper(form): |
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
138 |
fields = [] |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
139 |
hidden_fields = [] |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
140 |
hidden_fields_errors = [] |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
141 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
142 |
# Iterate over all fields and prepare it for adding |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
143 |
for name, field in form.fields.items(): |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
144 |
bf = forms_in.BoundField(form, field, name) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
145 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
146 |
# If the field is hidden we display it elsewhere |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
147 |
if not bf.is_hidden: |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
148 |
example_text = '' |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
149 |
if hasattr(field, 'example_text'): |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
150 |
example_text = force_unicode(field.example_text) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
151 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
152 |
item = (bf, field.required, example_text) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
153 |
fields.append(item) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
154 |
else: |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
155 |
hidden_fields.append(unicode(bf)) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
156 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
157 |
for e in bf.errors: |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
158 |
item = (name, force_unicode(e)) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
159 |
hidden_fields_errors.append(item) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
160 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
161 |
return { |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
162 |
'top_errors': form.non_field_errors() or '', |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
163 |
'hidden_field_errors': hidden_fields_errors or '', |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
164 |
'form': form, |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
165 |
'fields': fields or '', |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
166 |
'hidden_fields': hidden_fields or '', |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
167 |
} |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
168 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
169 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
170 |
@register.inclusion_tag('soc/templatetags/_as_table_row.html') |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
171 |
def as_table_row(form, field, required, example_text): |
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
172 |
"""Outputs a field as a properly formatted html row. |
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
173 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
174 |
Args: |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
175 |
form: the form that the row belongs to |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
176 |
field: the field that should be converted to a row |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
177 |
required: whether the field is required |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
178 |
example_text: the example_text for this row |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
179 |
""" |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
180 |
|
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
181 |
return as_table_row_helper(form, field, required, example_text) |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
182 |
|
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
183 |
|
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
184 |
@register.inclusion_tag('soc/templatetags/_as_twoline_table_row.html') |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
185 |
def as_twoline_table_row(form, field, required, example_text): |
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
186 |
"""Outputs a field as a properly formatted html row. |
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
187 |
|
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
188 |
Args: |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
189 |
form: the form that the row belongs to |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
190 |
field: the field that should be converted to a row |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
191 |
required: whether the field is required |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
192 |
example_text: the example_text for this row |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
193 |
""" |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
194 |
|
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
195 |
return as_table_row_helper(form, field, required, example_text) |
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
196 |
|
835
1590625ead55
Add missing blank lines and dots in soc.views.helper.templatetags.forms_helpers module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
794
diff
changeset
|
197 |
|
785
c740d0129cce
Added a twoline_edit.html page
Sverre Rabbelier <srabbelier@gmail.com>
parents:
754
diff
changeset
|
198 |
def as_table_row_helper(form, field, required, example_text): |
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
199 |
# Escape and cache in local variable. |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
200 |
errors = [force_unicode(escape(error)) for error in field.errors] |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
201 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
202 |
if field.label: |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
203 |
label = escape(force_unicode(field.label)) |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
204 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
205 |
# Only add the suffix if the label does not end in punctuation. |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
206 |
if form.label_suffix and (label[-1] not in ':?.!'): |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
207 |
label += form.label_suffix |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
208 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
209 |
label = field.label_tag(label) or '' |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
210 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
211 |
field_class_type = 'formfield%slabel' % ('error' if errors else '') |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
212 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
213 |
help_text = field.help_text |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
214 |
|
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
215 |
return { |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
216 |
'help_text': force_unicode(help_text) if help_text else '', |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
217 |
'field_class_type': field_class_type, |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
218 |
'label': force_unicode(label) if field.label else '', |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
219 |
'field': unicode(field), |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
220 |
'required': required, |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
221 |
'example_text': example_text, |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
222 |
'errors': errors, |
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
728
diff
changeset
|
223 |
} |