def edit(request): 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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) resource_id = request.urlvars.get('resource') resource = Resource.get(resource_id) delete_url = urls.get_url(lang, 'dataset', dataset_name, 'resource_delete', resource_id) form = ResourceForm(request.POST, resource, i18n=context.translator) if request.method == 'POST' and form.validate(): url = forms.handle_upload(request, form.file, user) ckan_api('resource_update', user, { 'id': resource_id, 'package_id': dataset_name, 'name': form.name.data, 'description': form.description.data, 'url': url or form.url.data, 'format': form.format.data, }) return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/resource-form.html', request, form=form, resource=resource, back_url=dataset_url, delete_url=delete_url)
def edit_community(request): 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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) resource_id = request.urlvars.get('resource') resource = CommunityResource.get(resource_id) if not resource: return wsgihelpers.not_found(context) delete_url = urls.get_url(lang, 'dataset', dataset_name, 'community/resource', resource_id, 'delete') form = CommunityResourceForm(request.POST, resource, i18n=context.translator) if request.method == 'POST' and form.validate(): form.populate_obj(resource) DB.add(resource) DB.commit() return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/resource-form.html', request, new=False, form=form, back_url=dataset_url, delete_url=delete_url)
def edit_community(request): 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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) resource_id = request.urlvars.get('resource') resource = CommunityResource.get(resource_id) if not resource: return wsgihelpers.not_found(context) delete_url = urls.get_url(lang, 'dataset', dataset_name, 'community/resource', resource_id, 'delete') form = CommunityResourceForm(request.POST, resource, i18n=context.translator) if request.method == 'POST' and form.validate(): form.populate_obj(resource) DB.add(resource) DB.commit() return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/resource-form.html', request, new=False, form=form, back_url=dataset_url, delete_url=delete_url)
def create(request): context = contexts.Ctx(request) lang = request.urlvars.get('lang', templates.DEFAULT_LANG) dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) user = auth.get_user_from_request(request) if not user: return wsgihelpers.unauthorized(context) # redirect to login/register ? form = ReuseForm(request.POST, i18n=context.translator) if request.method == 'POST' and form.validate(): data = ckan_api('related_create', user, { 'title': form.title.data, 'description': form.description.data, 'url': form.url.data, 'image_url': form.image_url.data, 'type': form.type.data, 'dataset_id': dataset_name, }) org_id = request.POST.get('publish_as') if org_id: reuse = Related.get(data['result']['id']) organization = Group.get(org_id) reuse_as_org = ReuseAsOrganization(reuse, organization) DB.add(reuse_as_org) DB.commit() return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/reuse-form.html', request, new=True, form=form, back_url=dataset_url)
def create_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 ? form = GroupForm(request.POST, i18n=context.translator) if request.method == 'POST' and form.validate(): name = strings.slugify(form.title.data) ckan_api( 'organization_create' if is_org else 'group_create', user, { 'name': name, 'title': form.title.data, 'description': form.description.data, 'image_url': form.image_url.data, }) redirect_url = urls.get_url(lang, 'organization' if is_org else 'group', name) return wsgihelpers.redirect(context, location=redirect_url) back_url = urls.get_url(lang, 'organizations' if is_org else 'groups') return templates.render_site('forms/group-create-form.html', request, is_new=True, is_org=is_org, form=form, back_url=back_url)
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)
def create(request): 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 ? form = DatasetForm(request.POST, i18n=context.translator) if request.method == 'POST' and form.validate(): name = build_slug(form.title.data) ckan_api('package_create', user, { 'name': name, 'title': form.title.data, 'notes': form.notes.data, 'owner_org': form.owner_org.data, 'private': form.private.data, 'license_id': form.license_id.data, 'extras': extras_from_form(form), 'tags': tags_from_form(form), }) dataset = Package.by_name(name) fix_groups(dataset, form.groups.data) redirect_url = urls.get_url(lang, 'dataset/new_resource', name) return wsgihelpers.redirect(context, location=redirect_url) back_url = urls.get_url(lang) return templates.render_site('forms/dataset-create-form.html', request, form=form, back_url=back_url)
def edit(request): 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 ? dataset_name = request.urlvars.get('name') dataset = Package.get(dataset_name) if not dataset: return wsgihelpers.not_found(context) form = DatasetForm(request.POST, dataset, frequency=dataset.extras.get('frequency'), territorial_coverage=dataset.extras.get('territorial_coverage', '').split(','), territorial_coverage_granularity=dataset.extras.get('territorial_coverage_granularity'), temporal_coverage_from=dataset.extras.get('temporal_coverage_from'), temporal_coverage_to=dataset.extras.get('temporal_coverage_to'), tags=[tag.name for tag in dataset.get_tags()], groups=[group.id for group in dataset.get_groups('group')], i18n=context.translator ) if request.method == 'POST' and form.validate(): name = build_slug(form.title.data, dataset.name) extras = [{'key': key, 'value': value} for key, value in dataset.extras.items() if key not in SPECIAL_EXTRAS] extras.extend(extras_from_form(form)) ckan_api('package_update', user, { 'id': dataset.id, 'name': name, 'title': form.title.data, 'notes': form.notes.data, 'owner_org': form.owner_org.data, 'private': form.private.data, 'license_id': form.license_id.data, 'extras': extras, 'tags': tags_from_form(form), 'resources': [{ 'id': resource.id, 'url': resource.url, 'description': resource.description, 'format': resource.format, 'name': resource.name, 'resource_type': resource.resource_type, } for resource in dataset.active_resources ], }) dataset = Package.by_name(name) fix_groups(dataset, form.groups.data) redirect_url = urls.get_url(lang, 'dataset', name) return wsgihelpers.redirect(context, location=redirect_url) delete_url = urls.get_url(lang, 'dataset/delete', dataset.name) back_url = urls.get_url(lang, 'dataset', dataset.name) return templates.render_site('forms/dataset-edit-form.html', request, dataset=dataset, form=form, back_url=back_url, delete_url=delete_url)
def to_account(request): context = contexts.Ctx(request) user = auth.get_user_from_request(request) username = request.urlvars.get('username') if not user or not username == user.name: return wsgihelpers.unauthorized(context) account_url = '{0}/my/profile/'.format(conf['sso_url']) return wsgihelpers.redirect(context, location=account_url)
def delete_community(request): 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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) resource_id = request.urlvars.get('resource') resource = CommunityResource.get(resource_id) if not user.sysadmin and not resource.owner_id == user.id: return wsgihelpers.unauthorized(context) # redirect to login/register ? DB.delete(resource) DB.commit() return wsgihelpers.redirect(context, location=dataset_url)
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)
def extras(request): 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 ? dataset_name = request.urlvars.get('name') dataset = Package.get(dataset_name) if not dataset: return wsgihelpers.not_found(context) if request.method == 'POST': headers = wsgihelpers.handle_cross_origin_resource_sharing(context) form = DatasetExtrasForm(request.POST) if form.validate(): extras = [ {'key': key, 'value': value} for key, value in dataset.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('package_update', user, { 'id': dataset.id, 'name': dataset.name, 'title': dataset.title, 'notes': dataset.notes, 'owner_org': dataset.owner_org, 'private': dataset.private, 'license_id': dataset.license_id, 'extras': extras, 'tags': [{'name': package_tag.tag.name} for package_tag in dataset.package_tag_all], 'resources': [{ 'id': resource.id, 'url': resource.url, 'description': resource.description, 'format': resource.format, 'name': resource.name, 'resource_type': resource.resource_type, } for resource in dataset.active_resources ], }) 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) redirect_url = urls.get_url(lang, 'dataset', dataset.name) return wsgihelpers.redirect(context, location=redirect_url) extras = [(key, value) for key, value in dataset.extras.items() if key not in SPECIAL_EXTRAS] back_url = urls.get_url(lang, 'dataset', dataset.name) return templates.render_site('forms/dataset-extras-form.html', request, dataset=dataset, extras=extras, back_url=back_url)
def delete_community(request): 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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) resource_id = request.urlvars.get('resource') resource = CommunityResource.get(resource_id) if not user.sysadmin and not resource.owner_id == user.id: return wsgihelpers.unauthorized( context) # redirect to login/register ? DB.delete(resource) DB.commit() return wsgihelpers.redirect(context, location=dataset_url)
def edit(request): context = contexts.Ctx(request) lang = request.urlvars.get('lang', templates.DEFAULT_LANG) dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) reuse_id = request.urlvars.get('reuse') reuse = Related.get(reuse_id) owner = User.get(reuse.owner_id) publish_as = ReuseAsOrganization.get(reuse) delete_url = urls.get_url(lang, 'dataset', dataset_name, 'related/delete', reuse.id) user = auth.get_user_from_request(request) if not user: return wsgihelpers.unauthorized(context) # redirect to login/register ? form = ReuseForm(request.POST, reuse, publish_as=publish_as.organization.id if publish_as else None, i18n=context.translator, ) if request.method == 'POST' and form.validate(): ckan_api('related_update', user, { 'id': reuse_id, 'title': form.title.data, 'description': form.description.data, 'url': form.url.data, 'image_url': form.image_url.data, 'type': form.type.data, 'owner_id': reuse.owner_id, 'dataset_id': dataset_name, }) org_id = request.POST.get('publish_as') if org_id: organization = Group.get(org_id) if publish_as: publish_as.organization = organization else: publish_as = ReuseAsOrganization(reuse, organization) DB.add(publish_as) DB.commit() elif publish_as: DB.delete(publish_as) DB.commit() return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/reuse-form.html', request, new=False, form=form, owner=owner, back_url=dataset_url, delete_url=delete_url)
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)
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)
def toggle_featured(request, value=None, url=None): user = auth.get_user_from_request(request) context = contexts.Ctx(request) if not user or not user.sysadmin: return wsgihelpers.unauthorized(context) # redirect to login/register ? reuse_id = request.urlvars.get('reuse') reuse = Related.get(reuse_id) reuse.featured = value if value is not None else (0 if reuse.featured else 1) DB.commit() if not url: dataset_name = request.urlvars.get('name') url = urls.get_url(request.urlvars.get('lang', templates.DEFAULT_LANG), 'dataset', dataset_name) return wsgihelpers.redirect(context, location=url)
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)
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)
def toggle_featured(request, value=None, url=None): user = auth.get_user_from_request(request) context = contexts.Ctx(request) if not user or not user.sysadmin: return wsgihelpers.unauthorized( context) # redirect to login/register ? reuse_id = request.urlvars.get('reuse') reuse = Related.get(reuse_id) reuse.featured = value if value is not None else ( 0 if reuse.featured else 1) DB.commit() if not url: dataset_name = request.urlvars.get('name') url = urls.get_url(request.urlvars.get('lang', templates.DEFAULT_LANG), 'dataset', dataset_name) return wsgihelpers.redirect(context, location=url)
def delete_extra(request): 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 ? dataset_name = request.urlvars.get('name') dataset = Package.get(dataset_name) if not dataset: return wsgihelpers.not_found(context) extra_key = request.urlvars.get('key', '').strip().decode('utf8') if not extra_key in dataset.extras.keys(): return wsgihelpers.not_found(context) extras = [{'key': key, 'value': value} for key, value in dataset.extras.items() if not key == extra_key] extras.append({'key': extra_key, 'value': dataset.extras.get(extra_key), 'deleted': True}) data = ckan_api('package_update', user, { 'id': dataset.id, 'name': dataset.name, 'title': dataset.title, 'notes': dataset.notes, 'owner_org': dataset.owner_org, 'private': dataset.private, 'license_id': dataset.license_id, 'extras': extras, 'tags': [{'name': package_tag.tag.name} for package_tag in dataset.package_tag_all], 'resources': [{ 'id': resource.id, 'url': resource.url, 'description': resource.description, 'format': resource.format, 'name': resource.name, 'resource_type': resource.resource_type, } for resource in dataset.active_resources ], }) if data['success']: return wsgihelpers.respond_json(context, {}, headers=headers, code=200) return wsgihelpers.respond_json(context, {}, headers=headers, code=400)
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)
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)
def create(request): context = contexts.Ctx(request) lang = request.urlvars.get('lang', templates.DEFAULT_LANG) dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) user = auth.get_user_from_request(request) if not user: return wsgihelpers.unauthorized( context) # redirect to login/register ? form = ReuseForm(request.POST, i18n=context.translator) if request.method == 'POST' and form.validate(): data = ckan_api( 'related_create', user, { 'title': form.title.data, 'description': form.description.data, 'url': form.url.data, 'image_url': form.image_url.data, 'type': form.type.data, 'dataset_id': dataset_name, }) org_id = request.POST.get('publish_as') if org_id: reuse = Related.get(data['result']['id']) organization = Group.get(org_id) reuse_as_org = ReuseAsOrganization(reuse, organization) DB.add(reuse_as_org) DB.commit() return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/reuse-form.html', request, new=True, form=form, back_url=dataset_url)
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)
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)
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)
def edit(request): 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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) resource_id = request.urlvars.get('resource') resource = Resource.get(resource_id) delete_url = urls.get_url(lang, 'dataset', dataset_name, 'resource_delete', resource_id) form = ResourceForm(request.POST, resource, i18n=context.translator) if request.method == 'POST' and form.validate(): url = forms.handle_upload(request, form.file, user) ckan_api( 'resource_update', user, { 'id': resource_id, 'package_id': dataset_name, 'name': form.name.data, 'description': form.description.data, 'url': url or form.url.data, 'format': form.format.data, }) return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/resource-form.html', request, form=form, resource=resource, back_url=dataset_url, delete_url=delete_url)
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)
def create_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 ? form = GroupForm(request.POST, i18n=context.translator) if request.method == 'POST' and form.validate(): name = strings.slugify(form.title.data) ckan_api('organization_create' if is_org else 'group_create', user, { 'name': name, 'title': form.title.data, 'description': form.description.data, 'image_url': form.image_url.data, }) redirect_url = urls.get_url(lang, 'organization' if is_org else 'group', name) return wsgihelpers.redirect(context, location=redirect_url) back_url = urls.get_url(lang, 'organizations' if is_org else 'groups') return templates.render_site('forms/group-create-form.html', request, is_new=True, is_org=is_org, form=form, back_url=back_url)
def edit(request): context = contexts.Ctx(request) lang = request.urlvars.get('lang', templates.DEFAULT_LANG) dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) reuse_id = request.urlvars.get('reuse') reuse = Related.get(reuse_id) owner = User.get(reuse.owner_id) publish_as = ReuseAsOrganization.get(reuse) delete_url = urls.get_url(lang, 'dataset', dataset_name, 'related/delete', reuse.id) user = auth.get_user_from_request(request) if not user: return wsgihelpers.unauthorized( context) # redirect to login/register ? form = ReuseForm( request.POST, reuse, publish_as=publish_as.organization.id if publish_as else None, i18n=context.translator, ) if request.method == 'POST' and form.validate(): ckan_api( 'related_update', user, { 'id': reuse_id, 'title': form.title.data, 'description': form.description.data, 'url': form.url.data, 'image_url': form.image_url.data, 'type': form.type.data, 'owner_id': reuse.owner_id, 'dataset_id': dataset_name, }) org_id = request.POST.get('publish_as') if org_id: organization = Group.get(org_id) if publish_as: publish_as.organization = organization else: publish_as = ReuseAsOrganization(reuse, organization) DB.add(publish_as) DB.commit() elif publish_as: DB.delete(publish_as) DB.commit() return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/reuse-form.html', request, new=False, form=form, owner=owner, back_url=dataset_url, delete_url=delete_url)