source: asadb/util/forms.py @ a484cbf

space-accessstablestagetest-hooks
Last change on this file since a484cbf was a484cbf, checked in by Alex Dehnert <adehnert@…>, 14 years ago

Limit which fields non-Exec can update

  • Property mode set to 100644
File size: 1.1 KB
Line 
1from django.forms.widgets import Widget, HiddenInput
2from django.utils.safestring import mark_safe
3from django.utils.html import conditional_escape
4
5class StaticWidget(Widget):
6    """Widget that just displays and supplies a specific value.
7
8    Useful for "freezing" form fields --- displaying them, but not
9    allowing editing.
10    """
11
12    def __init__(self, *args, **kwargs):
13        self.value = kwargs['value']
14        del kwargs['value']
15        super(StaticWidget, self).__init__(*args, **kwargs)
16        self.inner = HiddenInput()
17
18    def value_from_datadict(self, data, files, name, ):
19        return self.value
20
21    def render(self, name, value, attrs=None, ):
22        if value is None:
23            value = ''
24        # We have this inner hidden widget because that allows us to nicely
25        # handle a transition from StaticWidget to another widget type
26        # (e.g., the user loads a form while unauthenticated and submits while
27        # authenticated)
28        hidden_render = self.inner.render(name, value, attrs=attrs)
29        return mark_safe(conditional_escape(value) + hidden_render)
Note: See TracBrowser for help on using the repository browser.