Ejemplo n.º 1
0
    def __call__(self, context, request):
        if self.api is None:
            self.api = TemplateAPIEdit(context, request)

        result = self._handle_form(context, request)
        if is_response(result):
            return result
        else:
            return render_to_response(
                self.renderer,
                {'form': result, 'api': self.api},
                request=request,
                )
Ejemplo n.º 2
0
def generic_edit(context, request, schema, **kwargs):
    api = template_api(context, request)
    api.first_heading=u'<h1>Edit <em>%s</em></h1>' % context.title
    api.page_title = u'Edit %s - %s' % (context.title, api.site_title)

    form = Form(schema, buttons=('save', 'cancel'))
    rendered = FormController(form, **kwargs)(context, request)
    if is_response(rendered):
        return rendered

    return {
        'api': api,
        'form': rendered,
        }
Ejemplo n.º 3
0
def preferences(context, request):
    api = template_api(context, request)
    api.page_title=u"My preferences - %s" % api.site_title
    user = api.user
    uschema = user_schema(PrincipalBasic())
    user_form = Form(uschema, buttons=('save', 'cancel'))
    user_fc = FormController(user_form)
    form = user_fc(user, request)
    if is_response(form):
        return form

    return {
        'api': api,
        'form': form,
        }
Ejemplo n.º 4
0
def user_manage(context, request):
    username = request.params['name']
    principal = get_principals()[username]

    api = template_api(
        context, request,
        page_title=u"Edit User - %s" % context.title,
        cp_links=CONTROL_PANEL_LINKS,
        principal=principal,
        )

    def edit_principal(context, request, appstruct):
        _massage_groups_in(appstruct)
        for key, value in appstruct.items():
            setattr(context, key, value)
        request.session.flash(u"Your changes have been saved.", 'success')
        return HTTPFound(location=request.url)

    uschema = user_schema()
    del uschema['name']
    del uschema['password']
    user_form = Form(
        uschema,
        buttons=('save', 'cancel'),
        )
    user_fc = FormController(
        user_form,
        appstruct=lambda p: _massage_groups_out(p.__dict__.copy()),
        edit_item=edit_principal,
        )
    form = user_fc(principal, request)
    if is_response(form):
        return form

    return {
        'api': api,
        'form': form,
        }
Ejemplo n.º 5
0
 def _callFUT(self, *arg, **kw):
     from pyramid.view import is_response
     return is_response(*arg, **kw)
Ejemplo n.º 6
0
def users_manage(context, request):
    api = template_api(
        context, request,
        cp_links=CONTROL_PANEL_LINKS,
        )
    api.page_title = u"User Management - %s" % api.site_title

    principals = get_principals()

    def groups_lister(principal_name, context):
        return principals[principal_name].groups

    # Handling the user/roles matrix:
    changed = roles_form_handler(
        context, request, USER_MANAGEMENT_ROLES, groups_lister)
    if changed:
        changed_names = []
        for (principal_name, context, groups) in changed:
            principal = principals[principal_name]
            principal.groups = list(groups)
            changed_names.append(principal_name)
        location = request.url.split('?')[0] + '?' + urlencode(
            {'extra': ','.join(changed_names)})
        return HTTPFound(location=location)

    extra = request.params.get('extra') or ()
    if extra:
        extra = extra.split(',')
    search_entries = search_principals(request, extra=extra)
    available_roles = [ROLES[role_name] for role_name in USER_MANAGEMENT_ROLES]

    # Add forms:

    # These are callbacks that we pass to the form controller.  They
    # take control as soon as the data validates and are responsible
    # for adding the actual principals and redirect:
    def add_user(context, request, appstruct):
        _massage_groups_in(appstruct)
        name = appstruct['name'].lower()
        send_email = appstruct.pop('send_email', False)
        get_principals()[name] = appstruct
        if send_email:
            send_set_password(get_principals()[name], request)
        request.session.flash(u'%s added.' % appstruct['title'], 'success')
        location = request.url.split('?')[0] + '?' + urlencode({'extra': name})
        return HTTPFound(location=location)
        
    def add_group(context, request, appstruct):
        appstruct['name'] = u'group:%s' % appstruct['name']
        return add_user(context, request, appstruct)

    # The actual add forms:
    uschema = user_schema()
    del uschema['active']
    uschema.add(colander.SchemaNode(
        colander.Boolean(),
        name=u'send_email',
        title=u'Send password registration link',
        default=True,
        ))
    user_form = Form(
        uschema,
        buttons=(Button('add-user', u'Add User'), 'cancel'),
        )
    user_fc = FormController(
        user_form,
        add=True,
        add_item=add_user,
        post_key='add-user',
        )
    user_addform = user_fc(context, request)
    if is_response(user_addform):
        return user_addform

    gschema = group_schema()
    del gschema['active']
    group_form = Form(
        gschema,
        buttons=(Button('add-group', u'Add Group'), 'cancel'),
        )
    group_fc = FormController(
        group_form,
        add=True,
        add_item=add_group,
        post_key='add-group',
        )
    group_addform = group_fc(context, request)
    if is_response(group_addform):
        return group_addform

    return {
        'api': api,
        'entries': search_entries,
        'available_roles': available_roles,
        'user_addform': user_addform,
        'group_addform': group_addform,
        }