def get_user_selector(self): users = [user.name for user in model.Session.query(model.User)] for potential_default_user in self.default_users: if potential_default_user in users: default_user = potential_default_user break else: default_user = users[0] return literal('<label for="user_name">User:</label>') + \ select('user_name', default_user, users, 'user_name')
def render(self, **kwargs): # Get groups which are editable by the user. editable_groups = self._get_user_editable_groups() # Get groups which are already selected. selected_groups = self._get_value() # Make checkboxes HTML from selected groups. checkboxes_html = '' checkbox_action = '<input type="checkbox" name="%(name)s" checked="checked" value="%(id)s" />' checkbox_noaction = ' ' checkbox_template = ''' <dt> %(action)s </dt> <dd> <label for="%(name)s">%(title)s</label><br/> </dd> ''' for group in selected_groups: checkbox_context = { 'id': group.id, 'name': self.name + '-' + group.id, 'title': group.display_name } action = checkbox_noaction if group in editable_groups: context = { 'id': group.id, 'name': self.name + '-' + group.id } action = checkbox_action % context # Make checkbox HTML from a group. checkbox_context = { 'action': action, 'name': self.name + '-' + group.id, 'title': group.display_name } checkbox_html = checkbox_template % checkbox_context checkboxes_html += checkbox_html # Infer addable groups, subtract selected from editable groups. addable_groups = [] for group in editable_groups: if group not in selected_groups: addable_groups.append(group) # Construct addable options from addable groups. options = [] if len(addable_groups): if self.field.allow_empty or len(selected_groups): options.append(('', _('(None)'))) for group in addable_groups: options.append((group.id, group.display_name)) # Make select HTML. if len(options): new_name = self.name + '-new' select_html = h.select(new_name, None, options) else: # Todo: Translation call. select_html = _("Cannot add any groups.") # Make the field HTML. field_template = ''' <dl> %(checkboxes)s <dt> %(label)s </dt> <dd> %(select)s </dd> </dl> ''' field_context = { 'checkboxes': checkboxes_html, 'select': select_html, 'label': _("Group"), } field_html = field_template % field_context # Convert to literals. return h.literal(field_html)