예제 #1
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_delete_extra(request, is_org):
    context = contexts.Ctx(request)
    headers = wsgihelpers.handle_cross_origin_resource_sharing(context)

    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    extra_key = request.urlvars.get('key', '').strip().decode('utf8')
    if not extra_key in group.extras.keys():
        return wsgihelpers.not_found(context)

    extras = [{'key': key, 'value': value} for key, value in group.extras.items() if not key == extra_key]
    extras.append({'key': extra_key, 'value': group.extras.get(extra_key), 'deleted': True})
    data = ckan_api('organization_update' if is_org else 'group_update', user, {
        'id': group.id,
        'name': group.name,
        'title': group.title,
        'description': group.description,
        'image_url': group.image_url,
        'extras': extras,
        'users': _get_members(group)
    })
    if data['success']:
        return wsgihelpers.respond_json(context, {}, headers=headers, code=200)
    return wsgihelpers.respond_json(context, {}, headers=headers, code=400)
예제 #2
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_members(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(
            context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    if request.method == 'POST':
        form = GroupRoleForm(request.POST)
        headers = wsgihelpers.handle_cross_origin_resource_sharing(context)
        if form.validate():
            data = ckan_api(
                'member_create', user, {
                    'id': group.id,
                    'object': form.pk.data,
                    'object_type': 'user',
                    'capacity': form.value.data,
                })
            if data['success']:
                return wsgihelpers.respond_json(context, {},
                                                headers=headers,
                                                code=200)
        return wsgihelpers.respond_json(context, {}, headers=headers, code=400)

    members = DB.query(Member, User).filter(
        Member.group == group,
        Member.state == 'active',
        Member.table_id == User.id,
        Member.table_name == 'user',
    )

    roles = {
        'admin': context._('Administrator'),
        'editor': context._('Editor'),
        'member': context._('Member'),
    }

    group_base_url = urls.get_url(lang, 'organization' if is_org else 'group')
    back_url = urls.get_url(group, group.name)
    return templates.render_site('forms/group-members-form.html',
                                 request,
                                 is_org=is_org,
                                 members=members,
                                 group_base_url=group_base_url,
                                 back_url=back_url,
                                 group=group,
                                 roles=roles)
예제 #3
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_extras(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(
            context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    if request.method == 'POST':
        headers = wsgihelpers.handle_cross_origin_resource_sharing(context)
        form = GroupExtrasForm(request.POST)
        if form.validate():
            extras = [{
                'key': key,
                'value': value
            } for key, value in group.extras.items()
                      if not key == (form.old_key.data or form.key.data)]
            extras.append({'key': form.key.data, 'value': form.value.data})
            data = ckan_api(
                'organization_update' if is_org else 'group_update', user, {
                    'id': group.id,
                    'name': group.name,
                    'title': group.title,
                    'description': group.description,
                    'image_url': group.image_url,
                    'extras': extras,
                    'users': _get_members(group)
                })
            if data['success']:
                return wsgihelpers.respond_json(context, {
                    'key': form.key.data,
                    'value': form.value.data
                },
                                                headers=headers,
                                                code=200)
        return wsgihelpers.respond_json(context, {}, headers=headers, code=400)

    group_base_url = urls.get_url(lang, 'organization' if is_org else 'group')
    back_url = urls.get_url(lang, 'group', group.name)
    return templates.render_site('forms/group-extras-form.html',
                                 request,
                                 is_org=is_org,
                                 extras=group.extras.items(),
                                 group_base_url=group_base_url,
                                 back_url=back_url,
                                 group=group)
예제 #4
0
파일: group.py 프로젝트: etalab/weckan
def display(request):
    context = contexts.Ctx(request)
    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)
    page = parse_page(request)
    _, results = dataset.search('', request, page, SEARCH_PAGE_SIZE, group)
    main_groups = [t['name'] for t in templates.main_topics()]
    return templates.render_site('group.html', request,
        group=group,
        url_pattern=get_page_url_pattern(request),
        datasets=results,
        group_class='topic-{0}'.format(main_groups.index(group_name) + 1) if group_name in main_groups else None,
    )
예제 #5
0
파일: group.py 프로젝트: etalab/weckan
def edit_group_or_org(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(
            context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)
    form = GroupForm(request.POST, group, i18n=context.translator)

    if request.method == 'POST' and form.validate():
        name = strings.slugify(form.title.data)
        extras = [{
            'key': key,
            'value': value
        } for key, value in group.extras.items()]
        ckan_api(
            'organization_update' if is_org else 'group_update', user, {
                'id': group.id,
                'name': name,
                'title': form.title.data,
                'description': form.description.data,
                'image_url': form.image_url.data,
                'extras': extras,
                'users': _get_members(group),
            })

        redirect_url = urls.get_url(lang,
                                    'organization' if is_org else 'group',
                                    name)
        return wsgihelpers.redirect(context, location=redirect_url)

    group_type = 'organization' if is_org else 'group'
    group_base_url = urls.get_url(lang, group_type)
    back_url = urls.get_url(lang, group_type, group.name)
    delete_url = urls.get_url(lang, group_type, 'delete', group.name)
    return templates.render_site('forms/group-edit-form.html',
                                 request,
                                 is_org=is_org,
                                 form=form,
                                 group_base_url=group_base_url,
                                 group=group,
                                 back_url=back_url,
                                 delete_url=delete_url)
예제 #6
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_membership_requests(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)
    pending_requests = MembershipRequest.pending_for(group)

    group_base_url = urls.get_url(lang, 'organization' if is_org else 'group')
    back_url = urls.get_url(group, group.name)
    return templates.render_site('forms/group-membership-requests-form.html', request,
        is_org=is_org, pending_requests=pending_requests, group_base_url=group_base_url, back_url=back_url, group=group)
예제 #7
0
파일: group.py 프로젝트: etalab/weckan
def display(request):
    context = contexts.Ctx(request)
    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)
    page = parse_page(request)
    _, results = dataset.search('', request, page, SEARCH_PAGE_SIZE, group)
    main_groups = [t['name'] for t in templates.main_topics()]
    return templates.render_site(
        'group.html',
        request,
        group=group,
        url_pattern=get_page_url_pattern(request),
        datasets=results,
        group_class='topic-{0}'.format(main_groups.index(group_name) + 1)
        if group_name in main_groups else None,
    )
예제 #8
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_members(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    if request.method == 'POST':
        form = GroupRoleForm(request.POST)
        headers = wsgihelpers.handle_cross_origin_resource_sharing(context)
        if form.validate():
            data = ckan_api('member_create', user, {
                'id': group.id,
                'object': form.pk.data,
                'object_type': 'user',
                'capacity': form.value.data,
            })
            if data['success']:
                return wsgihelpers.respond_json(context, {}, headers=headers, code=200)
        return wsgihelpers.respond_json(context, {}, headers=headers, code=400)

    members = DB.query(Member, User).filter(
        Member.group == group,
        Member.state == 'active',
        Member.table_id == User.id,
        Member.table_name == 'user',
    )

    roles = {
        'admin': context._('Administrator'),
        'editor': context._('Editor'),
        'member': context._('Member'),
    }

    group_base_url = urls.get_url(lang, 'organization' if is_org else 'group')
    back_url = urls.get_url(group, group.name)
    return templates.render_site('forms/group-members-form.html', request,
        is_org=is_org, members=members, group_base_url=group_base_url, back_url=back_url, group=group, roles=roles)
예제 #9
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_delete_member(request, is_org):
    context = contexts.Ctx(request)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    username = request.urlvars.get('username')
    headers = wsgihelpers.handle_cross_origin_resource_sharing(context)
    data = ckan_api('member_delete', user, {
        'id': group.id,
        'object': username,
        'object_type': 'user',
    })
    if data['success']:
        return wsgihelpers.respond_json(context, {}, headers=headers, code=200)
    return wsgihelpers.respond_json(context, {}, headers=headers, code=400)
예제 #10
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_delete_extra(request, is_org):
    context = contexts.Ctx(request)
    headers = wsgihelpers.handle_cross_origin_resource_sharing(context)

    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(
            context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    extra_key = request.urlvars.get('key', '').strip().decode('utf8')
    if not extra_key in group.extras.keys():
        return wsgihelpers.not_found(context)

    extras = [{
        'key': key,
        'value': value
    } for key, value in group.extras.items() if not key == extra_key]
    extras.append({
        'key': extra_key,
        'value': group.extras.get(extra_key),
        'deleted': True
    })
    data = ckan_api(
        'organization_update' if is_org else 'group_update', user, {
            'id': group.id,
            'name': group.name,
            'title': group.title,
            'description': group.description,
            'image_url': group.image_url,
            'extras': extras,
            'users': _get_members(group)
        })
    if data['success']:
        return wsgihelpers.respond_json(context, {}, headers=headers, code=200)
    return wsgihelpers.respond_json(context, {}, headers=headers, code=400)
예제 #11
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_delete_member(request, is_org):
    context = contexts.Ctx(request)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(
            context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    username = request.urlvars.get('username')
    headers = wsgihelpers.handle_cross_origin_resource_sharing(context)
    data = ckan_api('member_delete', user, {
        'id': group.id,
        'object': username,
        'object_type': 'user',
    })
    if data['success']:
        return wsgihelpers.respond_json(context, {}, headers=headers, code=200)
    return wsgihelpers.respond_json(context, {}, headers=headers, code=400)
예제 #12
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_extras(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)

    if request.method == 'POST':
        headers = wsgihelpers.handle_cross_origin_resource_sharing(context)
        form = GroupExtrasForm(request.POST)
        if form.validate():
            extras = [
                {'key': key, 'value': value}
                for key, value in group.extras.items()
                if not key == (form.old_key.data or form.key.data)
            ]
            extras.append({'key': form.key.data, 'value': form.value.data})
            data = ckan_api('organization_update' if is_org else 'group_update', user, {
                'id': group.id,
                'name': group.name,
                'title': group.title,
                'description': group.description,
                'image_url': group.image_url,
                'extras': extras,
                'users': _get_members(group)
            })
            if data['success']:
                return wsgihelpers.respond_json(context, {'key': form.key.data, 'value': form.value.data}, headers=headers, code=200)
        return wsgihelpers.respond_json(context, {}, headers=headers, code=400)

    group_base_url = urls.get_url(lang, 'organization' if is_org else 'group')
    back_url = urls.get_url(lang, 'group', group.name)
    return templates.render_site('forms/group-extras-form.html', request,
        is_org=is_org, extras=group.extras.items(), group_base_url=group_base_url, back_url=back_url, group=group)
예제 #13
0
파일: group.py 프로젝트: etalab/weckan
def group_or_org_membership_requests(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(
            context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)
    pending_requests = MembershipRequest.pending_for(group)

    group_base_url = urls.get_url(lang, 'organization' if is_org else 'group')
    back_url = urls.get_url(group, group.name)
    return templates.render_site('forms/group-membership-requests-form.html',
                                 request,
                                 is_org=is_org,
                                 pending_requests=pending_requests,
                                 group_base_url=group_base_url,
                                 back_url=back_url,
                                 group=group)
예제 #14
0
파일: group.py 프로젝트: etalab/weckan
def edit_group_or_org(request, is_org):
    context = contexts.Ctx(request)
    lang = request.urlvars.get('lang', templates.DEFAULT_LANG)
    user = auth.get_user_from_request(request)
    if not user:
        return wsgihelpers.unauthorized(context)  # redirect to login/register ?

    group_name = request.urlvars.get('name')
    group = Group.by_name(group_name)
    if not group:
        return wsgihelpers.not_found(context)
    form = GroupForm(request.POST, group, i18n=context.translator)

    if request.method == 'POST' and form.validate():
        name = strings.slugify(form.title.data)
        extras = [{'key': key, 'value': value} for key, value in group.extras.items()]
        ckan_api('organization_update' if is_org else 'group_update', user, {
            'id': group.id,
            'name': name,
            'title': form.title.data,
            'description': form.description.data,
            'image_url': form.image_url.data,
            'extras': extras,
            'users': _get_members(group),
        })

        redirect_url = urls.get_url(lang, 'organization' if is_org else 'group', name)
        return wsgihelpers.redirect(context, location=redirect_url)

    group_type = 'organization' if is_org else 'group'
    group_base_url = urls.get_url(lang, group_type)
    back_url = urls.get_url(lang, group_type, group.name)
    delete_url = urls.get_url(lang, group_type, 'delete', group.name)
    return templates.render_site('forms/group-edit-form.html', request,
        is_org=is_org, form=form, group_base_url=group_base_url, group=group,
        back_url=back_url, delete_url=delete_url)