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 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 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 autocomplete(request): query = request.params.get('q', '') num = int(request.params.get('num', SEARCH_MAX_ORGANIZATIONS)) _, results = search(query, 1, num) context = contexts.Ctx(request) headers = wsgihelpers.handle_cross_origin_resource_sharing(context) data = [{ 'name': organization.name, 'title': organization.display_name, 'image_url': organization.image_url or templates.static('/img/placeholder_producer.png'), } for organization, _, _ in results['results']] return wsgihelpers.respond_json(context, data, headers=headers)
def autocomplete(request): context = contexts.Ctx(request) pattern = '{0}%'.format(request.params.get('q', '')) num = int(request.params.get('num', 0)) query = DB.query(distinct(func.lower(Tag.name)).label('name'), func.count(PackageTag.package_id).label('total')) query = query.join(PackageTag) query = query.filter(Tag.name.ilike(pattern)) query = query.order_by('total desc', 'name').group_by('name') if num: query = query.limit(num) data = [row[0] for row in query] headers = wsgihelpers.handle_cross_origin_resource_sharing(context) return wsgihelpers.respond_json(context, data, headers=headers)
def autocomplete(request): context = contexts.Ctx(request) pattern = '{0}%'.format(request.params.get('q', '')) num = int(request.params.get('num', 0)) query = DB.query( distinct(func.lower(Tag.name)).label('name'), func.count(PackageTag.package_id).label('total')) query = query.join(PackageTag) query = query.filter(Tag.name.ilike(pattern)) query = query.order_by('total desc', 'name').group_by('name') if num: query = query.limit(num) data = [row[0] for row in query] headers = wsgihelpers.handle_cross_origin_resource_sharing(context) return wsgihelpers.respond_json(context, data, headers=headers)
def autocomplete(request): query = request.params.get('q', '') num = int(request.params.get('num', NB_DATASETS)) _, results = search(query, request, 1, num) context = contexts.Ctx(request) headers = wsgihelpers.handle_cross_origin_resource_sharing(context) data = [{ 'name': dataset['name'], 'title': dataset['display_name'], 'image_url': ( (dataset['organization'].image_url if dataset['organization'] else None) or templates.static('/img/placeholder_producer.png') ), } for dataset in results['results']] return wsgihelpers.respond_json(context, data, headers=headers)
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 autocomplete_formats(request): context = contexts.Ctx(request) pattern = request.params.get('q', '') headers = wsgihelpers.handle_cross_origin_resource_sharing(context) if not pattern: return wsgihelpers.respond_json(context, [], headers=headers) pattern = '{0}%'.format(pattern) num = int(request.params.get('num', 0)) query = DB.query(distinct(func.lower(Resource.format)).label('format'), func.count(Resource.id).label('count')) query = query.filter(Resource.format.ilike(pattern)) query = query.order_by('count', 'format').group_by('format') if num: query = query.limit(num) data = [row[0] for row in query] return wsgihelpers.respond_json(context, data, headers=headers)
def autocomplete(request): query = request.params.get('q', '') num = int(request.params.get('num', SEARCH_MAX_ORGANIZATIONS)) _, results = search(query, 1, num) context = contexts.Ctx(request) headers = wsgihelpers.handle_cross_origin_resource_sharing(context) data = [{ 'name': organization.name, 'title': organization.display_name, 'image_url': organization.image_url or templates.static('/img/placeholder_producer.png'), } for organization, _, _ in results['results']] return wsgihelpers.respond_json(context, data, headers=headers)
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 autocomplete_formats(request): context = contexts.Ctx(request) pattern = request.params.get('q', '') headers = wsgihelpers.handle_cross_origin_resource_sharing(context) if not pattern: return wsgihelpers.respond_json(context, [], headers=headers) pattern = '{0}%'.format(pattern) num = int(request.params.get('num', 0)) query = DB.query( distinct(func.lower(Resource.format)).label('format'), func.count(Resource.id).label('count')) query = query.filter(Resource.format.ilike(pattern)) query = query.order_by('count', 'format').group_by('format') if num: query = query.limit(num) data = [row[0] for row in query] return wsgihelpers.respond_json(context, data, headers=headers)
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 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)