~canonical-isd-hackers/canonical-identity-provider/sst-changes

« back to all changes in this revision

Viewing changes to identityprovider/widgets.py

  • Committer: Danny Tamez
  • Date: 2010-04-21 15:29:24 UTC
  • Revision ID: danny.tamez@canonical.com-20100421152924-lq1m92tstk2iz75a
Canonical SSO Provider (Open Source) - Initial Commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
from itertools import chain
 
5
from django.forms.widgets import TextInput, Select, SelectMultiple
 
6
from django.conf import settings
 
7
from django.utils.safestring import mark_safe
 
8
 
 
9
 
 
10
def read_only_markup(value):
 
11
    if value is None:
 
12
        value = ''
 
13
    markup = '<span class="rofield">%s</span>' % value
 
14
    return mark_safe(markup)
 
15
 
 
16
 
 
17
class ROAwareTextInput(TextInput):
 
18
    def render(self, name, value, attrs=None):
 
19
        if getattr(settings, 'READ_ONLY_MODE', False):
 
20
            return read_only_markup(value)
 
21
        else:
 
22
            return super(ROAwareTextInput, self).render(name, value, attrs)
 
23
 
 
24
 
 
25
class ROAwareSelect(Select):
 
26
    def render(self, name, value, attrs=None, choices=()):
 
27
        if getattr(settings, 'READ_ONLY_MODE', False):
 
28
            label = ''
 
29
            for option_value, option_label in chain(self.choices, choices):
 
30
                if option_value == value:
 
31
                    label = option_label
 
32
                    break
 
33
            return read_only_markup(label)
 
34
        else:
 
35
            return super(ROAwareSelect, self).render(name, value, attrs,
 
36
                                                     choices)
 
37
 
 
38
 
 
39
class CommaSeparatedWidget(SelectMultiple):
 
40
 
 
41
    def render(self, name, value, attrs=None, choices=()):
 
42
        if value is None:
 
43
            value = ''
 
44
        if isinstance(value, list):
 
45
            vals = value
 
46
        else:
 
47
            vals = value.split(',')
 
48
        return super(CommaSeparatedWidget, self).render(
 
49
            name, vals, attrs, choices)