def managegroups(request): params = {'action': 'manage_groups', 'form': None} form, session = None, DBSession is_a_post = request.method == 'POST' adding = 'groupname' in request.POST deleting = request.params.get('group_action', '') == 'delete' settings = request.registry.settings default_groups_names = [a[0] for a in get_default_groups(settings)] if is_a_post and deleting: items = [a[1] for a in request.POST.items() if a[0] == 'delete'] todelete = session.query( AuthGroup).filter( se.and_( AuthGroup.id.in_(items), se.not_(AuthGroup.name.in_(default_groups_names)) )).all() noecho = [session.delete(i) for i in todelete] request.session.flash(_('Groups %s have been deleted') % ( ', '.join([a.name for a in todelete])), 'info') add_form = GroupForm(request.POST) if is_a_post and adding: if add_form.validate(): try: group = add_form.save() add_form = GroupForm() flash(_('Added group : %s' % group.name, 'info')) except Exception, e: flash(_('Problem adding group : %s' % e, 'error'))
def managegroups(request): params = {'action': 'manage_groups', 'form': None} form, session = None, DBSession is_a_post = request.method == 'POST' adding = 'groupname' in request.POST deleting = request.params.get('group_action', '') == 'delete' settings = request.registry.settings default_groups_names = [a[0] for a in get_default_groups(settings)] if is_a_post and deleting: items = [a[1] for a in request.POST.items() if a[0] == 'delete'] todelete = session.query(AuthGroup).filter( se.and_(AuthGroup.id.in_(items), se.not_(AuthGroup.name.in_(default_groups_names)))).all() noecho = [session.delete(i) for i in todelete] request.session.flash( _('Groups %s have been deleted') % (', '.join([a.name for a in todelete])), 'info') add_form = GroupForm(request.POST) if is_a_post and adding: if add_form.validate(): try: group = add_form.save() add_form = GroupForm() flash(_('Added group : %s' % group.name, 'info')) except Exception, e: flash(_('Problem adding group : %s' % e, 'error'))
def __call__(self): params = self.get_base_params() form, request = None, self.request is_a_post = request.method == 'POST' adding = request.params.get('__formid__', '') == 'add_group' default_groups = [a[0] for a in apexmodels.get_default_groups( request.registry.settings)] deleting = request.params.get('group_action', '') == 'delete' class GroupSH(colander.MappingSchema): name = colander.SchemaNode( colander.String(), title = _('Group name'), validator = colander.All( v.not_empty_string, v.existing_group, ) ) description = colander.SchemaNode(colander.String(), title=_('Group description'),) add_form = deform.Form( GroupSH(), buttons=(_('Send'),), formid = 'add_group') if is_a_post and deleting: items = [a[1] for a in request.POST.items() if a[0] == 'delete' and not a[1] in default_groups] todelete = session.query( user.Group).filter( se.and_( user.Group.id.in_(items), se.not_(user.Group.name.in_(default_groups)) ) ).all() noecho = [session.delete(i) for i in todelete] session.commit() request.session.flash(_('Items have been deleted'), 'info') if is_a_post and adding: controls = request.POST.items() try: data = add_form.validate(controls) group = session.query( user.Group).filter( user.Group.name == data['name'] ).first() if not group: rl = user.Group(name=data['name'], description=data['description']) session.add(rl) session.commit() request.session.flash(_('Group added: %s' % rl.name, 'info')) params['add_form'] = add_form.render() except Exception, e: params['add_form'] = e.render()