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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) form = ResourceForm(request.POST, i18n=context.translator) if request.method == 'POST' and form.validate(): url = forms.handle_upload(request, form.file, user) ckan_api('resource_create', user, { 'package_id': dataset_name, 'name': form.name.data, 'description': form.description.data, 'url': url or form.url.data, 'format': form.format.data, 'resource_type': form.resource_type.data.replace('.upload', ''), }) if 'add_another' in request.POST: return wsgihelpers.redirect(context, location=urls.get_url(lang, 'dataset/new_resource', dataset_name)) return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/resource-form.html', request, form=form, back_url=dataset_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) 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 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 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 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 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 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 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 ? dataset_name = request.urlvars.get('name') dataset_url = urls.get_url(lang, 'dataset', dataset_name) form = ResourceForm(request.POST, i18n=context.translator) if request.method == 'POST' and form.validate(): url = forms.handle_upload(request, form.file, user) ckan_api( 'resource_create', user, { 'package_id': dataset_name, 'name': form.name.data, 'description': form.description.data, 'url': url or form.url.data, 'format': form.format.data, 'resource_type': form.resource_type.data.replace( '.upload', ''), }) if 'add_another' in request.POST: return wsgihelpers.redirect(context, location=urls.get_url( lang, 'dataset/new_resource', dataset_name)) return wsgihelpers.redirect(context, location=dataset_url) return templates.render_site('forms/resource-form.html', request, form=form, back_url=dataset_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 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 render_site(name, request_or_context, **kwargs): ''' Render a template with a common site behavior. - handle language choice and fallback - inject user - inject sidebar items - fix CKAN translations ''' context = request_or_context if isinstance(request_or_context, contexts.Ctx) else contexts.Ctx(request_or_context) lang = context.req.urlvars.get('lang') # Locale-less location current_location = unicode(context.req.path_qs) base_location = current_location.replace('/{0}'.format(unicode(lang)), '') # Override browser language if not lang: lang = DEFAULT_LANG elif lang not in LANGUAGES: from weckan import wsgihelpers context.req.urlvars.pop('lang', None) return wsgihelpers.redirect(context, location=base_location) fix_pylons_translations(context) return render(context, name, full_url = context.req.url, current_location = current_location, current_base_location = base_location, query_string = context.req.query_string, user = auth.get_user_from_request(context.req), lang = lang, main_topics = main_topics(), piwik = get_piwik_context(), DOMAIN = conf['domain'], HOME_URL = conf['home_url'], WIKI_URL = conf['wiki_url'], WIKI_API_URL = conf['wiki_api_url'], QUESTIONS_URL = conf['questions_url'], CKAN_URL = conf['ckan_url'], COW_URL = conf['cow_url'], SSO_URL = conf['sso_url'], **kwargs )
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_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 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 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_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 to_logout(request): url = '{0}/logout/'.format(conf['sso_url']) return wsgihelpers.redirect(contexts.Ctx(request), location=url)
def redirect_old_dataset(request): context = contexts.Ctx(request) dataset_name = redirections.dataset_name_by_old_id.get(request.urlvars['id']) if dataset_name is None: return wsgihelpers.not_found(context) return wsgihelpers.redirect(context, location = urls.get_url(context, 'fr', 'dataset', dataset_name))
def to_profile(request): username = request.urlvars.get('username') profile_url = '{0}/u/{1}/'.format(conf['sso_url'], username) return wsgihelpers.redirect(contexts.Ctx(request), location=profile_url)
def to_home(request): return wsgihelpers.redirect(contexts.Ctx(request), location='/')
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)