def test_checkbox(): """A CheckBox has not a value and is not checked by default""" w = widgets.CheckBox("foo") output = w.render(format='xhtml') assert 'name="foo"' in output assert 'value' not in output assert 'checked' not in output output = w.render(value=True, format='xhtml') assert 'checked' in output w = widgets.CheckBox("foo", default=True) output = w.render(format='xhtml') assert 'checked' in output output = w.render(value=False, format='xhtml') assert 'checked' not in output #CheckBox should accept alternate validators value = w.validator.to_python('checked') assert value == True w = widgets.CheckBox("foo", validator=validators.NotEmpty()) value = w.validator.to_python('checked') assert value == 'checked'
class GroupForm(HorizontalForm): fields = [ widgets.HiddenField(name='group_id'), widgets.TextField(name='group_name', label=_(u'Group Name')), widgets.TextField(name='display_name', label=_(u'Display Name')), widgets.PasswordField(name='root_password', label=_(u'Root Password'), validator=StrongPassword()), widgets.CheckBox(name='ldap', label=_(u'LDAP'), help_text=_(u'Populate group membership from LDAP?')), ] name = 'Group' action = 'save_data' submit_text = _(u'Save') validator = GroupFormSchema() def update_params(self, d): if not identity.current.user.is_admin() or \ not config.get('identity.ldap.enabled', False): d['disabled_fields'] = ['ldap'] super(GroupForm, self).update_params(d)
class KeyTypes(AdminPage): # For XMLRPC methods in this class. exposed = False id = widgets.HiddenField(name='id') key_name = widgets.TextField(name='key_name', label=_(u'Name')) numeric = widgets.CheckBox(name='numeric', label=_(u'Numeric')) form = HorizontalForm( 'keytypes', fields = [id, key_name, numeric], action = 'save_data', submit_text = _(u'Submit Data'), ) def __init__(self,*args,**kw): kw['search_url'] = url("/keytypes/by_name?anywhere=1"), kw['search_name'] = 'key' super(KeyTypes,self).__init__(*args,**kw) self.search_col = Key.key_name self.search_mapper = Key @expose(template='bkr.server.templates.form') def new(self, **kw): return dict( title=_(u'New Key Type'), form = self.form, action = './save', options = {}, value = kw, ) @expose(template='bkr.server.templates.form') def edit(self,**kw): values = [] if kw.get('id'): key = Key.by_id(kw['id']) values = dict( id = key.id, key_name = key.key_name, numeric = key.numeric ) return dict( form = self.form, action = './save', options = {}, value = values, ) @expose() @error_handler(edit) def save(self, **kw): if kw['id']: key = Key.by_id(kw['id']) key.key_name = kw['key_name'] else: key = Key(key_name=kw['key_name']) session.add(key) if 'numeric' in kw: key.numeric = kw['numeric'] flash( _(u"OK") ) redirect(".") @expose(template="bkr.server.templates.admin_grid") @paginate('list') def index(self,*args,**kw): keytypes = session.query(Key) list_by_letters = set([elem.key_name[0].capitalize() for elem in keytypes]) results = self.process_search(**kw) if results: keytypes = results.order_by(Key.key_name) keytypes_grid = myPaginateDataGrid(fields=[ ('Key', lambda x: make_edit_link(x.key_name, x.id)), ('Numeric', lambda x: x.numeric), (' ', lambda x: make_remove_link(x.id)), ], add_action='./new') return dict(title="Key Types", grid = keytypes_grid, search_widget = self.search_widget_form, alpha_nav_bar = AlphaNavBar(list_by_letters,self.search_name), list = keytypes) @expose() def remove(self, **kw): remove = Key.by_id(kw['id']) session.delete(remove) flash( _(u"%s Deleted") % remove.key_name ) raise redirect(".") @expose(format='json') def by_name(self,input,*args,**kw): if 'anywhere' in kw: search = Key.list_by_name(input,find_anywhere=True) else: search = Key.list_by_name(input) keys = [elem.key_name for elem in search] return dict(matches=keys)
class Users(AdminPage): # For XMLRPC methods in this class. exposed = True user_id = widgets.HiddenField(name='user_id') user_name = widgets.TextField(name='user_name', label=_(u'Login')) display_name = widgets.TextField(name='display_name', label=_(u'Display Name')) email_address = widgets.TextField(name='email_address', label=_(u'Email Address')) password = widgets.PasswordField(name='password', label=_(u'Password')) disabled = widgets.CheckBox(name='disabled', label=_(u'Disabled')) user_form = HorizontalForm( 'User', fields=[ user_id, user_name, display_name, email_address, password, disabled ], action='save_data', submit_text=_(u'Save'), ) def __init__(self, *args, **kw): kw['search_url'] = url("/users/by_name?anywhere=1&ldap=0") kw['search_name'] = 'user' super(Users, self).__init__(*args, **kw) self.search_col = User.user_name self.search_mapper = User @identity.require(identity.in_group("admin")) @expose(template='bkr.server.templates.form') def new(self, **kw): return dict( form=self.user_form, action='./save', options={}, value=kw, ) @identity.require(identity.in_group("admin")) @expose(template='bkr.server.templates.user_edit_form') def edit(self, id=None, **kw): if id: user = User.by_id(id) title = _(u'User %s') % user.user_name value = user else: user = None title = _(u'New user') value = kw return_vals = dict(form=self.user_form, action='./save', title=title, options={}, value=value) if id: return_vals['groupsgrid'] = self.show_groups() else: return_vals['groupsgrid'] = None return return_vals @identity.require(identity.in_group("admin")) @expose() @validate(user_form, validators=UserFormSchema()) @error_handler(edit) def save(self, **kw): if kw.get('user_id'): user = User.by_id(kw['user_id']) else: user = User() session.add(user) user.display_name = kw['display_name'] user.user_name = kw['user_name'] user.email_address = kw['email_address'] if kw.get('disabled') != user.disabled: user.disabled = kw.get('disabled') if user.disabled: self._disable(user, method="WEBUI") if kw['password'] != user.password: user.password = kw['password'] flash(_(u"%s saved" % user.display_name)) redirect(".") def make_remove_link(self, user): if user.removed is not None: return XML('<a class="btn" href="unremove?id=%s">' '<i class="fa fa-plus"/> Re-Add</a>' % user.user_id) else: return XML('<a class="btn" href="remove?id=%s">' '<i class="fa fa-times"/> Remove</a>' % user.user_id) @expose(template="bkr.server.templates.admin_grid") @paginate('list', default_order='user_name', limit=20) def index(self, *args, **kw): users = session.query(User) list_by_letters = set( [elem.user_name[0].capitalize() for elem in users]) result = self.process_search(**kw) if result: users = result users_grid = myPaginateDataGrid(fields=[ ('Login', lambda x: make_edit_link(x.user_name, x.user_id)), ('Display Name', lambda x: x.display_name), ('Disabled', lambda x: x.disabled), ('', lambda x: self.make_remove_link(x)), ], add_action='./new') return dict(title="Users", grid=users_grid, alpha_nav_bar=AlphaNavBar(list_by_letters, 'user'), search_widget=self.search_widget_form, list=users) @identity.require(identity.in_group("admin")) @expose() def remove(self, id, **kw): try: user = User.by_id(id) except InvalidRequestError: flash(_(u'Invalid user id %s' % id)) raise redirect('.') try: self._remove(user=user, method='WEBUI') except BX, e: flash( _(u'Failed to remove User %s, due to %s' % (user.user_name, e))) raise redirect('.') else:
class RetentionTag(AdminPage): exposed = False tag = widgets.TextField(name='tag', label=_(u'Tag')) default = widgets.SingleSelectField(name='default', label=(u'Default'), options=[(0,'False'),(1,'True')]) id = widgets.HiddenField(name='id') expire_in_days = widgets.TextField(name='expire_in_days', label=_(u'Expire In Days'), help_text=_(u'Number of days after which jobs will expire')) needs_product = widgets.CheckBox('needs_product', label=u'Needs Product') tag_form = HorizontalForm( 'Retention Tag', fields = [tag, default, expire_in_days, needs_product, id], action = 'save_data', submit_text = _(u'Save'), ) def __init__(self,*args,**kw): kw['search_url'] = url("/retentiontag/by_tag") kw['search_name'] = 'tag' kw['widget_action'] = './admin' super(RetentionTag,self).__init__(*args,**kw) self.search_col = Tag.tag self.search_mapper = Tag @identity.require(identity.in_group("admin")) @expose(template='bkr.server.templates.form') def new(self, **kw): return dict( form = self.tag_form, action = './save', options = {}, value = kw, ) @identity.require(identity.in_group("admin")) @expose() @validate(form=tag_form, validators=TagFormSchema()) @error_handler(new) def save(self, id=None, **kw): retention_tag = Tag(kw['tag'], kw['default'], kw['needs_product']) retention_tag.expire_in_days = kw['expire_in_days'] session.add(retention_tag) flash(_(u"OK")) redirect("./admin") @expose(format='json') def by_tag(self, input, *args, **kw): input = input.lower() search = Tag.list_by_tag(input) tags = [match.tag for match in search] return dict(matches=tags) @expose(template="bkr.server.templates.admin_grid") @identity.require(identity.in_group('admin')) @paginate('list', default_order='tag', limit=20) def admin(self, *args, **kw): tags = self.process_search(*args, **kw) alpha_nav_data = set([elem.tag[0].capitalize() for elem in tags]) nav_bar = self._build_nav_bar(alpha_nav_data,'tag') template_data = self.tags(tags, identity.current.user, *args, **kw) template_data['alpha_nav_bar'] = nav_bar template_data['addable'] = True return template_data @identity.require(identity.in_group('admin')) @expose() def delete(self, id): tag = Tag.by_id(id) if not tag.can_delete(): # Trying to be funny... flash(u'%s is not applicable for deletion' % tag.tag) redirect('/retentiontag/admin') session.delete(tag) flash(u'Successfully deleted %s' % tag.tag) redirect('/retentiontag/admin') @identity.require(identity.in_group("admin")) @expose(template='bkr.server.templates.form') def edit(self, id, **kw): tag = Tag.by_id(id) return dict( form = self.tag_form, title=_(u'Retention tag %s' % tag.tag), action = './save_edit', options = {}, value = tag, disabled_fields = ['tag'] ) @identity.require(identity.in_group("admin")) @expose() @validate(form=tag_form, validators=TagFormSchema()) @error_handler(edit) def save_edit(self, id=None, **kw): retention_tag = Tag.by_id(id) retention_tag.tag = kw['tag'] retention_tag.default = kw['default'] retention_tag.expire_in_days = kw['expire_in_days'] retention_tag.needs_product = kw['needs_product'] flash(_(u"OK")) redirect("./admin") @expose(template="bkr.server.templates.grid") @paginate('list', default_order='tag', limit=20) def index(self, *args, **kw): return self.tags() def tags(self, tags=None, user=None, *args, **kw): if tags is None: tags = Tag.get_all() def show_delete(x): if x.can_delete(): return XML('<a class="btn" href="./delete/%s">' '<i class="fa fa-times"/> Delete</a>' % x.id) else: return None def show_tag(x): if x.is_default: #If we are the default, we can't change to not default return x.tag elif user and user.is_admin(): return make_edit_link(x.tag,x.id) else: #no perms to edit return x.tag my_fields = [myPaginateDataGrid.Column(name='tag', title='Tags', getter=lambda x: show_tag(x),options=dict(sortable=True)), myPaginateDataGrid.Column(name='default', title='Default', getter=lambda x: x.default,options=dict(sortable=True)), myPaginateDataGrid.Column(name='delete', title='Delete', getter=lambda x: show_delete(x))] tag_grid = myPaginateDataGrid(fields=my_fields, add_action='./new') return_dict = dict(title='Tags', grid = tag_grid, search_bar = None, search_widget = self.search_widget_form, list = tags) return return_dict
class TestFormFields(widgets.WidgetsList): foo = widgets.TextField() bar = widgets.CheckBox()