stephrdev / django-readonlywidget (http://sjaekel.com/)
ReadOnly Widget for Django
Clone this repository (size: 12.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/stephrdev/django-readonlywidget/
| commit 7: | c507b559cd61 |
| parent 6: | 69fa47ca3b25 |
| branch: | default |
fixed bug when using manytomany fields, BACKWARDS INCOMPATIBLE
Changed (Δ577 bytes):
raw changeset »
django_readonlywidget/widgets.py (28 lines added, 14 lines removed)
Up to file-list django_readonlywidget/widgets.py:
| … | … | @@ -4,32 +4,38 @@ from django.utils.encoding import force_ |
4 |
4 |
from django.utils.translation import ugettext as _ |
5 |
5 |
from django.utils.html import escape, conditional_escape |
6 |
6 |
from django.contrib.admin.templatetags.admin_list import _boolean_icon |
7 |
from django.db.models.fields.related import ManyToManyField |
|
7 |
8 |
|
8 |
class ReadOnlyWidget |
|
9 |
class ReadOnlyWidgetMixin(object): |
|
9 |
10 |
def __init__(self, db_field, *args, **kwargs): |
10 |
11 |
self.db_field = db_field |
11 |
super(ReadOnlyWidget |
|
12 |
super(ReadOnlyWidgetMixin, self).__init__() |
|
12 |
13 |
|
13 |
14 |
def render(self, *args, **kwargs): |
14 |
15 |
field_name, value = args |
15 |
16 |
field_type = self.db_field.__class__.__name__ |
16 |
field_value = super(ReadOnlyWidget |
|
17 |
field_value = super(ReadOnlyWidgetMixin, self).render(*args, **kwargs) |
|
17 |
18 |
output = value |
18 |
19 |
|
19 |
|
|
20 |
treat_as_field_type = self.treat_as(field_type) |
|
21 |
if hasattr(self, 'get_%s_value' % treat_as_field_type.lower()): |
|
20 |
22 |
try: |
21 |
func = getattr(self, 'get_%s_value' % |
|
23 |
func = getattr(self, 'get_%s_value' % treat_as_field_type.lower()) |
|
22 |
24 |
output = func(field_name, value) |
23 |
25 |
except Exception,e: |
24 |
26 |
output = e |
25 |
27 |
else: |
26 |
raise AttributeError('%s is not supported by |
|
28 |
raise AttributeError('%s is not supported by %s.' % (field_type, type(self).__name__)) |
|
27 |
29 |
|
28 |
30 |
return self.render_output(field_name, field_value, output) |
29 |
31 |
|
32 |
def treat_as(self, field_type): |
|
33 |
return field_type |
|
34 |
||
30 |
35 |
def render_output(self, field_name, field_value, output): |
31 |
36 |
return mark_safe('%s %s' % (output, field_value)) |
32 |
37 |
|
38 |
class ReadOnlyWidget(ReadOnlyWidgetMixin, forms.HiddenInput): |
|
33 |
39 |
def get_textfield_value(self, field_name, value): |
34 |
40 |
return '<p style="clear:both;">%s</p>' % value |
35 |
41 |
|
| … | … | @@ -63,14 +69,6 @@ class ReadOnlyWidget(forms.HiddenInput): |
63 |
69 |
except: |
64 |
70 |
return '' |
65 |
71 |
|
66 |
def get_manytomanyfield_value(self, field_name, value): |
|
67 |
output = ['<ul class="m2m_list_%s">' % field_name,] |
|
68 |
for id in value: |
|
69 |
output.append('<li>%s</li>' % unicode(self.db_field.rel.to.objects.get(pk=id))) |
|
70 |
output.append('</ul>') |
|
71 |
||
72 |
return ''.join(output) |
|
73 |
||
74 |
72 |
def get_datetimefield_value(self, field_name, value): |
75 |
73 |
if value: |
76 |
74 |
return value.strftime('%x %X') |
| … | … | @@ -82,3 +80,19 @@ class ReadOnlyWidget(forms.HiddenInput): |
82 |
80 |
return value.strftime('%x') |
83 |
81 |
else: |
84 |
82 |
return '' |
83 |
||
84 |
class ReadOnlyMultiWidget(ReadOnlyWidgetMixin, forms.MultipleHiddenInput): |
|
85 |
||
86 |
def treat_as(self, field_type): |
|
87 |
if isinstance(field_type, ManyToManyField) and not hasattr(self, 'get_%s_value' % field_type.lower()): |
|
88 |
return ManyToManyField |
|
89 |
else: |
|
90 |
return field_type |
|
91 |
||
92 |
def get_manytomanyfield_value(self, field_name, value): |
|
93 |
output = ['<ul class="m2m_list_%s">' % field_name,] |
|
94 |
for id in value: |
|
95 |
output.append('<li>%s</li>' % unicode(self.db_field.rel.to.objects.get(pk=id))) |
|
96 |
output.append('</ul>') |
|
97 |
||
98 |
return ''.join(output) |
