Beispiel #1
0
    def new(self, data=None, errors=None, error_summary=None):
        context = {
            "model": model,
            "session": model.Session,
            "user": c.user or c.author,
            "extras_as_string": True,
            "schema": self._form_to_db_schema(),
            "save": "save" in request.params,
        }
        try:
            check_access("group_create", context)
        except NotAuthorized:
            abort(401, _("Unauthorized to create a group"))

        if context["save"] and not data:
            return self._save_new(context)

        data = data or {}
        errors = errors or {}
        error_summary = error_summary or {}
        vars = {"data": data, "errors": errors, "error_summary": error_summary}

        self._setup_template_variables(context)
        c.form = render(self.group_form, extra_vars=vars)
        return render("group/new.html")
Beispiel #2
0
    def new(self, data=None, errors=None, error_summary=None):
        
        group_type = request.path.strip('/').split('/')[0]
        if group_type == 'group':
            group_type = None
            if data:
                data['type'] = group_type
        
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'extras_as_string': True,
                   'schema': self._form_to_db_schema(),
                   'save': 'save' in request.params }
        try:
            check_access('group_create',context)
        except NotAuthorized:
            abort(401, _('Unauthorized to create a group'))

        if context['save'] and not data:
            return self._save_new(context, group_type)
        
        data = data or {}
        errors = errors or {}
        error_summary = error_summary or {}
        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}

        self._setup_template_variables(context,data)
        c.form = render(self._group_form(group_type=group_type), extra_vars=vars)
        return render('group/new.html')
Beispiel #3
0
    def config(self):

        items = self._get_config_form_items()
        data = request.POST
        if 'save' in data:
            try:
                # really?
                data_dict = logic.clean_dict(
                    dict_fns.unflatten(
                        logic.tuplize_dict(
                            logic.parse_params(
                                request.POST, ignore_keys=CACHE_PARAMETERS))))

                del data_dict['save']

                data = logic.get_action('config_option_update')(
                    {'user': c.user}, data_dict)
            except logic.ValidationError as e:
                errors = e.error_dict
                error_summary = e.error_summary
                vars = {'data': data, 'errors': errors,
                        'error_summary': error_summary, 'form_items': items}
                return base.render('admin/config.html', extra_vars=vars)

            h.redirect_to(controller='admin', action='config')

        schema = logic.schema.update_configuration_schema()
        data = {}
        for key in schema:
            data[key] = config.get(key)

        vars = {'data': data, 'errors': {}, 'form_items': items}
        return base.render('admin/config.html',
                           extra_vars=vars)
  def manage_assets(self, id, resource_id):
    # inits context
    self._init_context()

    if not c.userobj or not c.userobj.sysadmin:
      base.abort(404)
    try:
      toolkit.c.pkg_dict = toolkit.get_action('package_show')(None, {'id': id})
      toolkit.c.resource = toolkit.get_action('resource_show')(None, {'id': resource_id})
    except toolkit.ObjectNotFound:
      base.abort(404, _('Resource not found'))
    except toolkit.NotAuthorized:
      base.abort(401, _('Unauthorized to edit this resource'))

    page = int(request.params.get('page',1))
    assets = []
    try:
      result = toolkit.get_action('datastore_search')(self.context,{
        'id':resource_id,
        'limit':ASSETS_PER_PAGE,
        'offset':(page-1)*ASSETS_PER_PAGE,
        'sort':'_id asc'
      })
      assets.extend(result['records'])
    except toolkit.ObjectNotFound:
      return base.render('package/manage_assets.html')
    hidden_assets = []
    hidden = DFMPSearchQuery.run({
      'q':'id:{res_id}'.format(res_id=resource_id),
      'rows':100,
      'start':0,
      'fq':'+state:hidden',
    })['results']
    if hidden:
      for item in hidden:
        hidden_assets.append(json.loads(item['data_dict']))

    extra_vars = {
      'assets':assets,
      'hidden_assets':hidden_assets,
      'action_url':h.url_for('ajax_actions'),
    }



    def pager_url(q=None, page=None):
      params = [
        ('page', page),
      ]
      url = h.url_for('manage_assets', id=id, resource_id=resource_id)
      return url_with_params(url, params)
    c.page = h.Page(
        collection=assets,
        page=page,
        url=pager_url,#pager_url,
        item_count=result.get('total',0),
        items_per_page=ASSETS_PER_PAGE,
    )

    return base.render('package/manage_assets.html', extra_vars=extra_vars)
 def preselect(self):
     c.am_sysadmin = new_authz.is_sysadmin(c.user)
     c.organizations_available = helpers.hdx_organizations_available_with_roles()
     if c.organizations_available and len(c.organizations_available) > 0:
         return base.render('organization/organization_preselector.html')
     else:
         return base.render('organization/request_mem_or_org.html')
Beispiel #6
0
    def new(self, data=None, errors=None, error_summary=None):
        group_type = self._guess_group_type(True)
        if data:
            data['type'] = group_type

        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'extras_as_string': True,
                   'save': 'save' in request.params,
                   'parent': request.params.get('parent', None)}
        try:
            check_access('group_create', context)
        except NotAuthorized:
            abort(401, _('Unauthorized to create a group'))

        if context['save'] and not data:
            return self._save_new(context, group_type)

        data = data or {}
        errors = errors or {}
        error_summary = error_summary or {}
        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}

        self._setup_template_variables(context, data, group_type=group_type)
        c.form = render(self._group_form(group_type=group_type),
                        extra_vars=vars)
        return render(self._new_template(group_type))
Beispiel #7
0
    def new(self, data=None, errors=None, error_summary=None):
        group_type = self._guess_group_type(True)
        if data:
            data["type"] = group_type

        context = {
            "model": model,
            "session": model.Session,
            "user": c.user or c.author,
            "extras_as_string": True,
            "save": "save" in request.params,
            "parent": request.params.get("parent", None),
        }
        try:
            check_access("group_create", context)
        except NotAuthorized:
            abort(401, _("Unauthorized to create a group"))

        if context["save"] and not data:
            return self._save_new(context, group_type)

        data = data or {}
        errors = errors or {}
        error_summary = error_summary or {}
        vars = {"data": data, "errors": errors, "error_summary": error_summary}

        self._setup_template_variables(context, data, group_type=group_type)
        c.form = render(self._group_form(group_type=group_type), extra_vars=vars)
        return render(self._new_template(group_type))
Beispiel #8
0
    def edit(self, id, data = None,errors = None, error_summary = None):

        if ('save' in request.params) and not data:
            return self._save_edit(id)

        try:
            context = {'model':model, 'user':c.user, 'include_status':False}

            old_data = p.toolkit.get_action('harvest_source_show')(context, {'id':id})
        except p.toolkit.ObjectNotFound:
            abort(404, _('Harvest Source not found'))
        except p.toolkit.NotAuthorized:
            abort(401, self.not_auth_message)
        try:
            p.toolkit.check_access('harvest_source_update', context)
        except p.toolkit.NotAuthorized:
            abort(401, _('Unauthorized to update the harvest source'))

        data = data or old_data
        errors = errors or {}
        error_summary = error_summary or {}
        try:
            context = {'model': model, 'user': c.user}
            harvesters_info = p.toolkit.get_action('harvesters_info_show')(context, {})
        except p.toolkit.NotAuthorized:
            abort(401, self.not_auth_message)

        vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'harvesters': harvesters_info}

        c.source_title = old_data.get('title') if old_data else ''
        c.source_id = id
        c.groups = self._get_publishers()
        c.form = render('source/new_source_form.html', extra_vars=vars)
        return render('source/edit.html')
Beispiel #9
0
    def edit(self, id, data=None, errors=None, error_summary=None):
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'extras_as_string': True,
                   'preview': 'preview' in request.params,
                   'save': 'save' in request.params,
                   'moderated': config.get('moderated'),
                   'pending': True,
                   'schema': self._form_to_db_schema()}

        if (context['save'] or context['preview']) and not data:
            return self._save_edit(id, context)
        try:
            old_data = get.package_show(context, {'id':id})
            schema = self._db_to_form_schema()
            if schema:
                old_data, errors = validate(old_data, schema)
            data = data or old_data
        except NotAuthorized:
            abort(401, _('Unauthorized to read package %s') % '')
        except NotFound:
            abort(404, _('Package not found'))

        c.pkg = context.get("package")

        am_authz = self.authorizer.am_authorized(c, model.Action.EDIT, c.pkg)
        if not am_authz:
            abort(401, _('User %r not authorized to edit %s') % (c.user, id))

        errors = errors or {}
        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}

        self._setup_template_variables(context, {'id':'id'})
        c.form = render(self.package_form, extra_vars=vars)
        return render('package/edit.html')
    def register(self, data=None, errors=None, error_summary=None):
        '''GET to display a form for registering a new user.
           or POST the form data to actually do the user registration.

           The bulk of this code is pulled directly from ckan/controlllers/user.py
        '''
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'schema': schema.user_new_form_schema(),
                   'save': 'save' in request.params}

        try:
            check_access('user_create', context)
        except NotAuthorized:
            abort(401, _('Unauthorized to create a user'))

        if context['save'] and not data:
            uc = UserController()
            return uc._save_new(context)

        if c.user and not data:
            # #1799 Don't offer the registration form if already logged in
            return render('user/logout_first.html')

        data = data or {}
        errors = errors or {}
        error_summary = error_summary or {}

        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
        c.is_sysadmin = new_authz.is_sysadmin(c.user)
        c.form = render('user/new_user_form.html', extra_vars=vars)
        return render('user/new.html')
Beispiel #11
0
    def view(self, url):
        """By default, the final controller tried to fulfill the request
        when no other routes match. It may be used to display a template
        when all else fails, e.g.::

            def view(self, url):
                return render('/%s' % url)

        Or if you're using Mako and want to explicitly send a 404 (Not
        Found) response code when the requested template doesn't exist::

            import mako.exceptions

            def view(self, url):
                try:
                    return render('/%s' % url)
                except mako.exceptions.TopLevelLookupException:
                    abort(404)

        By default this controller aborts the request with a 404 (Not
        Found)
        """
        try:
            return base.render(url)
        except ckan.lib.render.TemplateNotFound:
            if url.endswith('.html'):
                base.abort(404)
            url += '.html'
            try:
                return base.render(url)
            except ckan.lib.render.TemplateNotFound:
                base.abort(404)
Beispiel #12
0
    def new (self, data=None, errors=None, error_summary=None):
        #q = model.Session.query(model.User).filter(model.User.sysadmin==True)
        #c.sysadmins = [a.name for a in q.all()]

        '''GET to display a form for registering a new user.
           or POST the form data to actually do the user registration.
        '''
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'auth_user_obj': c.userobj,
                   'schema': self._new_form_to_db_schema(),
                   'save': 'save' in request.params}
        c.is_sysadmin = new_authz.is_sysadmin(c.user)
        if not c.user or not c.is_sysadmin:
            return base.render('user/logout_first.html')

        try:
            logic.check_access('user_create', context)
        except logic.NotAuthorized:
            base.abort(401, _('Unauthorized to create a user'))

        if context['save'] and not data:
            return self._save_new(context)

        c.data = data or {}
        c.errors = errors or {}
        c.error_summary = error_summary or {}
        #vars = {'data': data, 'errors': errors, 'error_summary': error_summary}


        #c.form = render(self.new_user_form, extra_vars=vars)
        #return render('user/new.html')

        return base.render('muser/new.html')
Beispiel #13
0
    def get(self, group_type, is_organization,
            data=None, errors=None, error_summary=None):
        extra_vars = {}
        set_org(is_organization)
        context = self._prepare()
        data = data or {}
        if not data.get(u'image_url', u'').startswith(u'http'):
            data.pop(u'image_url', None)
        errors = errors or {}
        error_summary = error_summary or {}
        extra_vars = {
            u'data': data,
            u'errors': errors,
            u'error_summary': error_summary,
            u'action': u'new',
            u'group_type': group_type
        }
        _setup_template_variables(
            context, data, group_type=group_type)
        form = base.render(
            _group_form(group_type=group_type), extra_vars)

        # TODO: Remove
        # ckan 2.9: Adding variables that were removed from c object for
        # compatibility with templates in existing extensions
        g.form = form

        extra_vars["form"] = form
        return base.render(_new_template(group_type), extra_vars)
Beispiel #14
0
    def new(self, data=None, errors=None, error_summary=None):
        package_type = self._guess_package_type(True)

        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'extras_as_string': True,
                   'save': 'save' in request.params,}

        # Package needs to have a organization group in the call to check_access
        # and also to save it
        try:
            check_access('package_create',context)
        except NotAuthorized:
            abort(401, _('Unauthorized to create a package'))

        if context['save'] and not data:
            return self._save_new(context)

        data = data or clean_dict(unflatten(tuplize_dict(parse_params(
            request.params, ignore_keys=[CACHE_PARAMETER]))))
        c.resources_json = json.dumps(data.get('resources',[]))

        errors = errors or {}
        error_summary = error_summary or {}
        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
        c.errors_json = json.dumps(errors)

        self._setup_template_variables(context, {'id': id})

        # TODO: This check is to maintain backwards compatibility with the old way of creating
        # custom forms. This behaviour is now deprecated.
        if hasattr(self, 'package_form'):
            c.form = render(self.package_form, extra_vars=vars)
        else:
            c.form = render(self._package_form(package_type=package_type), extra_vars=vars)
        return render( self._new_template(package_type))
Beispiel #15
0
    def edit(self, id=None, data=None, errors=None, error_summary=None):
        context = {'save': 'save' in request.params,
                   'schema': self._edit_form_to_db_schema(),
                   'model': model, 'session': model.Session,
                   'user': c.user, 'auth_user_obj': c.userobj,
                   'keep_apikey': True, 'keep_email': True
                   }
        if id is None:
            if c.userobj:
                id = c.userobj.id
            else:
                abort(400, _('No user specified'))
        data_dict = {'id': id}

        try:
            check_access('user_update', context, data_dict)
        except NotAuthorized:
            abort(401, _('Unauthorized to edit a user.'))

        if (context['save']) and not data:
            return self._save_edit(id, context)

        try:
            old_data = get_action('user_show')(context, data_dict)

            schema = self._db_to_edit_form_schema()
            if schema:
                old_data, errors = validate(old_data, schema)

            c.display_name = old_data.get('display_name')
            c.user_name = old_data.get('name')

            data = data or old_data

        except NotAuthorized:
            abort(401, _('Unauthorized to edit user %s') % '')
        except NotFound:
            abort(404, _('User not found'))

        user_obj = context.get('user_obj')

        if not (new_authz.is_sysadmin(c.user) or c.user == user_obj.name):
            abort(401, _('User %s not authorized to edit %s') %
                  (str(c.user), id))

        errors = errors or {}
        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}

        self._setup_template_variables({'model': model,
                                        'session': model.Session,
                                        'user': c.user or c.author},
                                       data_dict)

        c.is_myself = True
        c.show_email_notifications = h.asbool(
            config.get('ckan.activity_streams_email_notifications'))
        c.form = render(self.edit_user_form, extra_vars=vars)

        return render('user/edit.html')
Beispiel #16
0
 def broken_resource_links(self, id=None):
     if id is None:
         c.organisations = organisations_with_broken_resource_links_by_name()
         return render('ckanext/qa/organisation/broken_resource_links/index.html')
     else:
         c.id = id
         c.organisation = broken_resource_links_by_dataset_for_organisation(organisation_id=id)
         return render('ckanext/qa/organisation/broken_resource_links/organisation.html')
def notify_ckan_user_create(email, fullname, username, phoneno, dept):
    """
    Send an e-mail notification about new users that register on the site to
    the configured recipient and to the new user
    """
    import ckan.lib.mailer

    try:
        if 'canada.notification_new_user_email' in config:
            xtra_vars = {
                'email': email,
                'fullname': fullname,
                'username': username,
                'phoneno': phoneno,
                'dept': dept
            }
            ckan.lib.mailer.mail_recipient(
                config.get(
                    'canada.notification_new_user_name',
                    config['canada.notification_new_user_email']
                ),
                config['canada.notification_new_user_email'],
                (
                    u'New data.gc.ca Registry Account Created / Nouveau compte'
                    u' cr\u00e9\u00e9 dans le registre de Gouvernement ouvert'
                ),
                render(
                    'user/new_user_email.html',
                    extra_vars=xtra_vars
                )
            )
    except ckan.lib.mailer.MailerException as m:
        log = getLogger('ckanext')
        log.error(m.message)

    try:
        xtra_vars = {
            'email': email,
            'fullname': fullname,
            'username': username,
            'phoneno': phoneno,
            'dept': dept
        }
        ckan.lib.mailer.mail_recipient(
            fullname or email,
            email,
            (
                u'Welcome to the Open Government Registry / '
                u'Bienvenue au Registre de Gouvernement Ouvert'
            ),
            render(
                'user/user_welcome_email.html',
                extra_vars=xtra_vars
            )
        )
    except (ckan.lib.mailer.MailerException, socket.error) as m:
        log = getLogger('ckanext')
        log.error(m.message)
Beispiel #18
0
    def edit(self, id=None): # allow id=None to allow posting
        c.error = ''
        group = model.Group.get(id)
        if group is None:
            abort(404, '404 Not Found')

        context = {'model': model, 'user': c.user or c.author, 'group':group}
        try:
            check_access('group_update',context)
        except NotAuthorized:
            abort(401, _('User %r not authorized to edit %s') % (c.user, group.id))
        try:
            check_access('group_change_state',context)
            auth_for_change_state = True
        except NotAuthorized:
            auth_for_change_state = False
        
        if not 'save' in request.params:
            c.group = group
            c.groupname = group.name
            c.grouptitle = group.title
            
            fs = forms.get_group_fieldset(is_admin=auth_for_change_state).bind(c.group)
            c.form = self._render_edit_form(fs)
            return render('group/edit.html')
        else:
            rev = model.repo.new_revision()
            rev.author = c.author
            # id is the name (pre-edited state)
            c.groupname = id
            # needed because request is nested
            # multidict which is read only
            params = dict(request.params)
            fs = ckan.forms.get_group_fieldset(is_admin=auth_for_change_state)
            c.fs = fs.bind(group, data=params or None)
            try:
                self._update(c.fs, id, group.id)
                # do not use groupname from id as may have changed
                c.groupname = c.fs.name.value
                c.grouptitle = c.fs.title.value
            except ValidationException, error:
                fs = error.args[0]
                c.form = self._render_edit_form(fs)
                return render('group/edit.html')
            pkgs = [model.Package.by_name(name) for name in request.params.getall('Group-packages-current')]
            group.packages = pkgs
            pkgnames = request.params.getall('PackageGroup--package_name')
            for pkgname in pkgnames:
                if pkgname:
                    package = model.Package.by_name(pkgname)
                    if package and package not in group.packages:
                        group.packages.append(package)
            for extension in self.extensions: 
                extension.edit(group)
            model.repo.commit_and_remove()
            h.redirect_to(controller='group', action='read', id=c.groupname)
Beispiel #19
0
    def error_handler(e):
        if isinstance(e, HTTPException):
            extra_vars = {u'code': [e.code], u'content': e.description}
            # TODO: Remove
            g.code = [e.code]

            return base.render(
                u'error_document_template.html', extra_vars), e.code
        extra_vars = {u'code': [500], u'content': u'Internal server error'}
        return base.render(u'error_document_template.html', extra_vars), 500
    def static_content(self, path):
        from ckan.lib.render import TemplateNotFound

        try:
            return render("static-content/" + path + "/index.html")
        except TemplateNotFound:
            from pylons import tmpl_context as c

            c.code = ["404"]
            return render("error_document_template.html")
 def render_ingestion_template(self, resource_id):
     c.resource_id = resource_id
     c.task_result = resource_ingestion.get_result(resource_id)['result']
     res_identify_obj = model.Session.query(ResourceIngest).filter(
         ResourceIngest.resource_id == resource_id).first()
     
     # Fixme adapt
     if res_identify_obj.storer_type == ResourceStorerType.VECTOR:
         c.layers = [{k: layer[k] for k in layer if k != 'sample_data'} 
             for layer in c.task_result['layers']]
         return render('user/snippets/ingest_templates/vector/vector.html')
     elif res_identify_obj.storer_type == ResourceStorerType.RASTER:
         return render('user/snippets/ingest_templates/raster/raster.html')
    def edit(self, id=None):  # allow id=None to allow posting
        c.error = ""
        group = model.Group.get(id)
        if group is None:
            abort(404, "404 Not Found")
        am_authz = self.authorizer.am_authorized(c, model.Action.EDIT, group)
        if not am_authz:
            abort(401, _("User %r not authorized to edit %r") % (c.user, id))

        auth_for_change_state = self.authorizer.am_authorized(c, model.Action.CHANGE_STATE, group)

        if not "save" in request.params:
            c.group = group
            c.groupname = group.name
            c.grouptitle = group.title

            fs = forms.get_group_fieldset(is_admin=auth_for_change_state).bind(c.group)
            c.form = self._render_edit_form(fs)
            return render("group/edit.html")
        else:
            rev = model.repo.new_revision()
            rev.author = c.author
            # id is the name (pre-edited state)
            c.groupname = id
            # needed because request is nested
            # multidict which is read only
            params = dict(request.params)
            fs = ckan.forms.get_group_fieldset(is_admin=auth_for_change_state)
            c.fs = fs.bind(group, data=params or None)
            try:
                self._update(c.fs, id, group.id)
                # do not use groupname from id as may have changed
                c.groupname = c.fs.name.value
                c.grouptitle = c.fs.title.value
            except ValidationException, error:
                fs = error.args[0]
                c.form = self._render_edit_form(fs)
                return render("group/edit.html")
            pkgs = [model.Package.by_name(name) for name in request.params.getall("Group-packages-current")]
            group.packages = pkgs
            pkgnames = request.params.getall("PackageGroup--package_name")
            for pkgname in pkgnames:
                if pkgname:
                    package = model.Package.by_name(pkgname)
                    if package and package not in group.packages:
                        group.packages.append(package)
            for extension in self.extensions:
                extension.edit(group)
            model.repo.commit_and_remove()
            h.redirect_to(controller="group", action="read", id=c.groupname)
Beispiel #23
0
    def report_action(self, id, data=None, errors=None, error_summary=None):
        """
        Reports action page
        """
        # Setup dataset information
        package_type = self._get_package_type(id.split("@")[0])
        # context = {'model': model, 'session': model.Session,
        #           'user': c.user or c.author, 'extras_as_string': True,
        #           'schema': self._form_to_db_schema(package_type=package_type)}
        context = {
            "model": model,
            "session": model.Session,
            "save": "save" in request.params,
            "user": c.user or c.author,
            "extras_as_string": True,
        }
        data_dict = {"id": id}

        # check if package exists
        try:
            c.pkg_dict = get_action("package_update")(context, data_dict)
            c.pkg = context["package"]
            c.pkg_json = json.dumps(c.pkg_dict)
        except NotFound:
            abort(404, _("Dataset not found"))
        except NotAuthorized:
            abort(401, _("Unauthorized to edit package %s") % id)

        data = data or clean_dict(unflatten(tuplize_dict(parse_params(request.params, ignore_keys=[CACHE_PARAMETER]))))

        errors = errors or {}
        error_summary = error_summary or {}
        vars = {"data": data, "errors": errors, "error_summary": error_summary}

        # Save data if requested
        if context.get("save") and data:
            result = self._save_report_action_new(data)
            # Return error message
            if not result:
                errors["name"] = "Insertion error"
                error_summary[_(u"Ação")] = _(u"Erro inserindo elemento na base de dados")

        # Add actual reports actions information
        data["actions"] = model.Session.query(lightbaseDatasetActions).all()

        self._setup_template_variables(context, {"id": id})
        c.form = render("package/reports/actions_new_form.html", extra_vars=vars)

        return render("package/reports/actions_new.html")
Beispiel #24
0
def login():
    # Do any plugin login stuff
    for item in plugins.PluginImplementations(plugins.IAuthenticator):
        item.login()

    extra_vars = {}
    if g.user:
        return base.render(u'user/logout_first.html', extra_vars)

    came_from = request.params.get(u'came_from')
    if not came_from:
        came_from = h.url_for(u'user.logged_in')
    g.login_handler = h.url_for(
        _get_repoze_handler(u'login_handler_path'), came_from=came_from)
    return base.render(u'user/login.html', extra_vars)
def notify(context,issue,email_template):

  notify_admin = toolkit.asbool(config.get("ckanext.issues.notify_admin", False))
  notify_owner = toolkit.asbool(config.get("ckanext.issues.notify_owner", False))
  if not notify_admin and not notify_owner:
      return

  user_obj = model.User.get(issue.user_id)
  dataset = model.Package.get(issue.dataset_id)

  extra_vars = {
      'issue': issue,
      'title': dataset.title,
      'username': user_obj.name,
      'email': user_obj.email,
      'site_url': h.url_for(
          controller='ckanext.issues.controller:IssueController',
          action='show',
          id=issue.id,
          package_id=issue.dataset_id,
          qualified=True
      )
  }

  if notify_owner:
    contact_name = dataset.author or dataset.maintainer
    contact_email =  dataset.author_email or dataset.maintainer_email

    email_msg = render(email_template,extra_vars=extra_vars)
    send_email(contact_name,contact_email,email_msg)

  if notify_admin:

      # retrieve organization's admins (or the role specified on ckanext.issues.minimun_role_required) to notify
      organization = action.get.organization_show(context,data_dict={'id':dataset.owner_org})
      minimun_role_required = config.get("ckanext.issues.minimun_role_required", "Anonymous")

      for user in organization['users']:

        if user['capacity'] == role_mapper[minimun_role_required] and user['activity_streams_email_notifications']:

          admin_user = model.User.get(user['id'])
          admin_name = admin_user.name
          admin_email = admin_user.email

          if admin_email != contact_email:
            email_msg = render(email_template,extra_vars=extra_vars)
            send_email(admin_name,admin_email,email_msg)
Beispiel #26
0
    def index(self):
        # Avoid duplicate URLs for the same WMS service
        # (Only if it has not been checked before)
        if not request.params.get('deduped', False):
            urls = request.params.getall('url')
            deduped_urls = set(urls)

            if len(deduped_urls) < len(urls):
                # Redirect to the same location, but with the deduplicated
                # URLs.
                offset = url_for(controller='ckanext.os.controllers.widgets:PreviewWidget',action='index')

                query_string = urlencode([('url', u) for u in deduped_urls])

                for key,value in request.params.iteritems():
                    if key != 'url':
                        query_string += '&' + urlencode([(key, value)])
                query_string += '&deduped=true'
                new_url = offset + '?' + query_string

                redirect(new_url)

        # Render the page
        c.libraries_base_url = 'http://%s/libraries' % LIBRARIES_HOST
        c.tiles_url_ckan = TILES_URL_CKAN
        c.wms_url_ckan = WMS_URL_CKAN
        c.wfs_url_ckan = WFS_URL_CKAN

        return render('os/map_preview.html')
 def report_view(self, id):
     """
     """    
     page = int(tk.request.params.get('page', 0))
     perpage = int(tk.request.params.get('perpage', 10))
         
     report, results, total, show_org = self._report_view(id, page, perpage)
     
     tk.c.sub_title = _(report['name'])
     
     tk.c.total = total
     tk.c.page = page
     tk.c.perpage = perpage
     tk.c.start_record = (page * perpage) + 1
     tk.c.end_record = min((page * perpage) + perpage, total)
     tk.c.lastpage = int(math.ceil(total / perpage))
     tk.c.report = report
         
     if report['report_on'] == "activities":          
         tk.c.activities = results
     elif report['report_on'] == "details":
         tk.c.users = results
         tk.c.show_org = show_org
     else:
         tk.abort(404, tk._('Report not found'))
         
     return render('vdojstats-report-view.html')
Beispiel #28
0
 def _render_edit_form(self, fs, params={}, clear_session=False):
     # errors arrive in c.error and fs.errors
     c.log_message = params.get('log_message', '')
     # rgrp: expunge everything from session before dealing with
     # validation errors) so we don't have any problematic saves
     # when the fs.render causes a flush.
     # seb: If the session is *expunged*, then the form can't be
     # rendered; I've settled with a rollback for now, which isn't
     # necessarily what's wanted here.
     # dread: I think this only happened with tags because until
     # this changeset, Tag objects were created in the Renderer
     # every time you hit preview. So I don't believe we need to
     # clear the session any more. Just in case I'm leaving it in
     # with the log comments to find out.
     if clear_session:
         # log to see if clearing the session is ever required
         if model.Session.new or model.Session.dirty or model.Session.deleted:
             log.warn('Expunging session changes which were not expected: '
                      '%r %r %r', (model.Session.new, model.Session.dirty,
                                   model.Session.deleted))
         try:
             model.Session.rollback()
         except AttributeError: # older SQLAlchemy versions
             model.Session.clear()
     edit_form_html = fs.render()
     c.form = h.literal(edit_form_html)
     return h.literal(render('package/edit_form.html'))
Beispiel #29
0
    def organization_index(self):
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'for_view': True,
                   'with_private': False}

        data_dict = {'all_fields': True}

        try:
            check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))

        # pass user info to context as needed to view private datasets of
        # orgs correctly
        if c.userobj:
            context['user_id'] = c.userobj.id
            context['user_is_admin'] = c.userobj.sysadmin

        results = get_action('organization_list')(context, data_dict)

        def org_key(org):
            title = org['title'].split(' | ')[-1 if c.language == 'fr' else 0]
            return normalize_strip_accents(title)

        results.sort(key=org_key)

        c.page = Page(
            collection=results,
            page=request.params.get('page', 1),
            url=h.pager_url,
            items_per_page=1000
        )
        return render('organization/index.html')
Beispiel #30
0
    def index(self):
        c.countries = json.dumps(self.get_countries())
        c.organizations, c.organization_count = self.get_organizations()
        c.topics = self.get_topics()
        c.topic_icons = self.get_topic_icons()

        return base.render('browse/browse.html')
Beispiel #31
0
 def tou(self):
     return base.render('content/tou.html')
Beispiel #32
0
 def helper_as_attribute(self):
     return base.render('tests/helper_as_attribute.html')
Beispiel #33
0
 def accessibility(self):
     return base.render('content/accessibility.html')
Beispiel #34
0
def search(package_type):
    extra_vars = {}

    try:
        context = {
            u'model': model,
            u'user': g.user,
            u'auth_user_obj': g.userobj
        }
        check_access(u'site_read', context)
    except NotAuthorized:
        base.abort(403, _(u'Not authorized to see this page'))

    # unicode format (decoded from utf8)
    extra_vars[u'q'] = q = request.args.get(u'q', u'')

    extra_vars['query_error'] = False
    page = h.get_page_number(request.args)

    limit = int(config.get(u'ckan.datasets_per_page', 20))

    # most search operations should reset the page counter:
    params_nopage = [(k, v) for k, v in request.args.items() if k != u'page']

    extra_vars[u'drill_down_url'] = drill_down_url
    extra_vars[u'remove_field'] = partial(remove_field, package_type)

    sort_by = request.args.get(u'sort', None)
    params_nosort = [(k, v) for k, v in params_nopage if k != u'sort']

    extra_vars[u'sort_by'] = partial(_sort_by, params_nosort, package_type)

    if not sort_by:
        sort_by_fields = []
    else:
        sort_by_fields = [field.split()[0] for field in sort_by.split(u',')]
    extra_vars[u'sort_by_fields'] = sort_by_fields

    pager_url = partial(_pager_url, params_nopage, package_type)

    search_url_params = urlencode(_encode_params(params_nopage))
    extra_vars[u'search_url_params'] = search_url_params

    try:
        # fields_grouped will contain a dict of params containing
        # a list of values eg {u'tags':[u'tag1', u'tag2']}

        extra_vars[u'fields'] = fields = []
        extra_vars[u'fields_grouped'] = fields_grouped = {}
        search_extras = {}
        fq = u''
        for (param, value) in request.args.items(multi=True):
            if param not in [u'q', u'page', u'sort'] \
                    and len(value) and not param.startswith(u'_'):
                if not param.startswith(u'ext_'):
                    fields.append((param, value))
                    fq += u' %s:"%s"' % (param, value)
                    if param not in fields_grouped:
                        fields_grouped[param] = [value]
                    else:
                        fields_grouped[param].append(value)
                else:
                    search_extras[param] = value

        context = {
            u'model': model,
            u'session': model.Session,
            u'user': g.user,
            u'for_view': True,
            u'auth_user_obj': g.userobj
        }

        # Unless changed via config options, don't show other dataset
        # types any search page. Potential alternatives are do show them
        # on the default search page (dataset) or on one other search page
        search_all_type = config.get(u'ckan.search.show_all_types', u'dataset')
        search_all = False

        try:
            # If the "type" is set to True or False, convert to bool
            # and we know that no type was specified, so use traditional
            # behaviour of applying this only to dataset type
            search_all = asbool(search_all_type)
            search_all_type = u'dataset'
        # Otherwise we treat as a string representing a type
        except ValueError:
            search_all = True

        if not search_all or package_type != search_all_type:
            # Only show datasets of this particular type
            fq += u' +dataset_type:{type}'.format(type=package_type)

        facets = OrderedDict()

        default_facet_titles = {
            u'organization': _(u'Organizations'),
            u'groups': _(u'Groups'),
            u'tags': _(u'Tags'),
            u'res_format': _(u'Formats'),
            u'license_id': _(u'Licenses'),
        }

        for facet in h.facets():
            if facet in default_facet_titles:
                facets[facet] = default_facet_titles[facet]
            else:
                facets[facet] = facet

        # Facet titles
        for plugin in plugins.PluginImplementations(plugins.IFacets):
            facets = plugin.dataset_facets(facets, package_type)

        extra_vars[u'facet_titles'] = facets
        data_dict = {
            u'q': q,
            u'fq': fq.strip(),
            u'facet.field': facets.keys(),
            u'rows': limit,
            u'start': (page - 1) * limit,
            u'sort': sort_by,
            u'extras': search_extras,
            u'include_private': asbool(
                config.get(u'ckan.search.default_include_private', True)
            ),
        }

        query = get_action(u'package_search')(context, data_dict)

        extra_vars[u'sort_by_selected'] = query[u'sort']

        extra_vars[u'page'] = h.Page(
            collection=query[u'results'],
            page=page,
            url=pager_url,
            item_count=query[u'count'],
            items_per_page=limit
        )
        extra_vars[u'search_facets'] = query[u'search_facets']
        extra_vars[u'page'].items = query[u'results']
    except SearchQueryError as se:
        # User's search parameters are invalid, in such a way that is not
        # achievable with the web interface, so return a proper error to
        # discourage spiders which are the main cause of this.
        log.info(u'Dataset search query rejected: %r', se.args)
        base.abort(
            400,
            _(u'Invalid search query: {error_message}')
            .format(error_message=str(se))
        )
    except SearchError as se:
        # May be bad input from the user, but may also be more serious like
        # bad code causing a SOLR syntax error, or a problem connecting to
        # SOLR
        log.error(u'Dataset search error: %r', se.args)
        extra_vars[u'query_error'] = True
        extra_vars[u'search_facets'] = {}
        extra_vars[u'page'] = h.Page(collection=[])

    # FIXME: try to avoid using global variables
    g.search_facets_limits = {}
    for facet in extra_vars[u'search_facets'].keys():
        try:
            limit = int(
                request.args.get(
                    u'_%s_limit' % facet,
                    int(config.get(u'search.facets.default', 10))
                )
            )
        except ValueError:
            base.abort(
                400,
                _(u'Parameter u"{parameter_name}" is not '
                  u'an integer').format(parameter_name=u'_%s_limit' % facet)
            )

        g.search_facets_limits[facet] = limit

    _setup_template_variables(context, {}, package_type=package_type)

    extra_vars[u'dataset_type'] = package_type

    # TODO: remove
    for key, value in extra_vars.iteritems():
        setattr(g, key, value)

    return base.render(
        _get_pkg_template(u'search_template', package_type), extra_vars
    )
 def index (ctrl, slug):
     c.wp_page = wp.page(slug)
     return render('paginas/index.html')
Beispiel #36
0
def logged_out_page():
    return base.render(u'user/logout.html', {})
Beispiel #37
0
    def get(self, package_type, data=None, errors=None, error_summary=None):
        context = self._prepare(data)
        if data and u'type' in data:
            package_type = data[u'type']

        data = data or clean_dict(
            dict_fns.unflatten(
                tuplize_dict(
                    parse_params(request.args, ignore_keys=CACHE_PARAMETERS)
                )
            )
        )
        resources_json = h.json.dumps(data.get(u'resources', []))
        # convert tags if not supplied in data
        if data and not data.get(u'tag_string'):
            data[u'tag_string'] = u', '.join(
                h.dict_list_reduce(data.get(u'tags', {}), u'name')
            )

        errors = errors or {}
        error_summary = error_summary or {}
        # in the phased add dataset we need to know that
        # we have already completed stage 1
        stage = [u'active']
        if data.get(u'state', u'').startswith(u'draft'):
            stage = [u'active', u'complete']

        # if we are creating from a group then this allows the group to be
        # set automatically
        data[
            u'group_id'
        ] = request.args.get(u'group') or request.args.get(u'groups__0__id')

        form_snippet = _get_pkg_template(
            u'package_form', package_type=package_type
        )
        form_vars = {
            u'data': data,
            u'errors': errors,
            u'error_summary': error_summary,
            u'action': u'new',
            u'stage': stage,
            u'dataset_type': package_type,
            u'form_style': u'new'
        }
        errors_json = h.json.dumps(errors)

        # TODO: remove
        g.resources_json = resources_json
        g.errors_json = errors_json

        _setup_template_variables(context, {}, package_type=package_type)

        new_template = _get_pkg_template(u'new_template', package_type)
        return base.render(
            new_template,
            extra_vars={
                u'form_vars': form_vars,
                u'form_snippet': form_snippet,
                u'dataset_type': package_type,
                u'resources_json': resources_json,
                u'form_snippet': form_snippet,
                u'errors_json': errors_json
            }
        )
Beispiel #38
0
 def get(self):
     if u'cancel' in request.args:
         return h.redirect_to(u'admin.config')
     return base.render(u'admin/confirm_reset.html', extra_vars={})
Beispiel #39
0
    def index(self):
        #now pass the list of sysadmins
        c.sysadmins = [a.name for a in get_sysadmins()]

        return base.render('admin/index.html')
    def edit(self, id):
        def validate(data):
            error_dict = {}
            has_owner = data.get('owner')
            has_key = data.get('key')

            if tk.asbool(data.get('integration', 'False')):
                if not has_owner:
                    error_dict['owner'] = ['Required']
                if not has_key:
                    error_dict['key'] = ['Required']
            if tk.asbool(data.get('show_links', 'False')):
                if not has_owner or not has_key:
                    error_dict['show_links'] = [
                        'This option available only '
                        'if credentials are provided'
                    ]
            if not error_dict:
                api = API(has_owner, has_key)
                check = api.check_credentials()
                if not check:
                    error_dict['key'] = ['Incorrect key']
            if error_dict:
                raise logic.ValidationError(error_dict)

        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'auth_user_obj': c.userobj
        }
        data_dict = {'id': id}
        stats = {}
        extra = {'errors': {}, 'error_summary': None, 'stats': stats}
        try:
            if not h.check_access('organization_update', data_dict):
                raise logic.NotAuthorized
            c.group_dict = logic.get_action('organization_show')(context,
                                                                 data_dict)
            c.group = context['group']
            c.credentials = c.group.datadotworld_credentials
            if c.credentials is None:
                c.credentials = Credentials(organization=c.group)
                model.Session.add(c.credentials)
        except logic.NotFound:
            base.abort(404, _('Organization not found'))
        except logic.NotAuthorized:
            base.abort(401,
                       _('User %r not authorized to edit %s') % (c.user, id))
        if request.method == 'POST':
            data = dict(request.POST)
            c.credentials.update(data)
            try:
                validate(data)
            except logic.ValidationError as e:
                extra['errors'] = e.error_dict
                extra['error_summary'] = e.error_summary
            else:

                query = model.Session.query(Extras).join(model.Package).join(
                    model.Group,
                    model.Package.owner_org == model.Group.id).filter(
                        model.Group.id == c.group.id)
                for item in query:
                    item.state = 'pending'

                model.Session.commit()
                h.flash_success('Saved')
                if tk.asbool(c.credentials.integration):
                    syncronize_org(c.group.id)
                return base.redirect_to('organization_dataworld', id=id)

        query = model.Session.query(
            func.count(model.Package.id).label('total'),
            Extras.state).join(model.Group,
                               model.Package.owner_org == model.Group.id).join(
                                   Extras).group_by(Extras.state).filter(
                                       model.Package.owner_org == c.group.id)

        for amount, state in query:
            stats[state] = amount
        return base.render('organization/edit_credentials.html',
                           extra_vars=extra)
Beispiel #41
0
def index():
    data = dict(sysadmins=[a.name for a in _get_sysadmins()])
    return base.render(u'admin/index.html', extra_vars=data)
Beispiel #42
0
    def get(self,
            package_type,
            id,
            resource_id,
            view_id=None,
            post_extra=None):
        context, extra_vars = self._prepare(id, resource_id)
        to_preview = extra_vars[u'to_preview']
        if post_extra:
            extra_vars.update(post_extra)

        package_type = _get_package_type(id)
        data = extra_vars[u'data']
        view_type = None
        # view_id exists only when updating
        if view_id:
            if not data:
                try:
                    data = get_action(u'resource_view_show')(context, {
                        u'id': view_id
                    })
                except (NotFound, NotAuthorized):
                    return base.abort(404, _(u'View not found'))

            view_type = data.get(u'view_type')
            # might as well preview when loading good existing view
            if not extra_vars[u'errors']:
                to_preview = True

        view_type = view_type or request.args.get(u'view_type')
        data[u'view_type'] = view_type
        view_plugin = lib_datapreview.get_view_plugin(view_type)
        if not view_plugin:
            return base.abort(404, _(u'View Type Not found'))

        _setup_template_variables(context, {u'id': id},
                                  package_type=package_type)

        data_dict = {
            u'package': extra_vars[u'pkg_dict'],
            u'resource': extra_vars[u'resource'],
            u'resource_view': data
        }

        view_template = view_plugin.view_template(context, data_dict)
        form_template = view_plugin.form_template(context, data_dict)

        extra_vars.update({
            u'form_template':
            form_template,
            u'view_template':
            view_template,
            u'data':
            data,
            u'to_preview':
            to_preview,
            u'datastore_available':
            plugins.plugin_loaded(u'datastore')
        })
        extra_vars.update(
            view_plugin.setup_template_variables(context, data_dict) or {})
        extra_vars.update(data_dict)

        if view_id:
            return base.render(u'package/edit_view.html', extra_vars)

        return base.render(u'package/new_view.html', extra_vars)
Beispiel #43
0
def read(package_type, id, resource_id):
    context = {
        u'model': model,
        u'session': model.Session,
        u'user': g.user,
        u'auth_user_obj': g.userobj,
        u'for_view': True
    }

    try:
        package = get_action(u'package_show')(context, {u'id': id})
    except (NotFound, NotAuthorized):
        return base.abort(404, _(u'Dataset not found'))
    activity_id = request.params.get(u'activity_id')
    if activity_id:
        # view an 'old' version of the package, as recorded in the
        # activity stream
        current_pkg = package
        try:
            package = context['session'].query(
                model.Activity).get(activity_id).data['package']
        except AttributeError:
            base.abort(404, _(u'Dataset not found'))

        if package['id'] != current_pkg['id']:
            log.info(
                u'Mismatch between pkg id in activity and URL {} {}'.format(
                    package['id'], current_pkg['id']))
            # the activity is not for the package in the URL - don't allow
            # misleading URLs as could be malicious
            base.abort(404, _(u'Activity not found'))
        # The name is used lots in the template for links, so fix it to be
        # the current one. It's not displayed to the user anyway.
        package['name'] = current_pkg['name']

        # Don't crash on old (unmigrated) activity records, which do not
        # include resources or extras.
        package.setdefault(u'resources', [])

    resource = None
    for res in package.get(u'resources', []):
        if res[u'id'] == resource_id:
            resource = res
            break
    if not resource:
        return base.abort(404, _(u'Resource not found'))

    # get package license info
    license_id = package.get(u'license_id')
    try:
        package[u'isopen'] = model.Package.get_license_register(
        )[license_id].isopen()
    except KeyError:
        package[u'isopen'] = False

    resource_views = get_action(u'resource_view_list')(context, {
        u'id': resource_id
    })
    resource[u'has_views'] = len(resource_views) > 0

    current_resource_view = None
    view_id = request.args.get(u'view_id')
    if resource[u'has_views']:
        if view_id:
            current_resource_view = [
                rv for rv in resource_views if rv[u'id'] == view_id
            ]
            if len(current_resource_view) == 1:
                current_resource_view = current_resource_view[0]
            else:
                return base.abort(404, _(u'Resource view not found'))
        else:
            current_resource_view = resource_views[0]

    # required for nav menu
    pkg = context[u'package']
    dataset_type = pkg.type or package_type

    # TODO: remove
    g.package = package
    g.resource = resource
    g.pkg = pkg
    g.pkg_dict = package

    extra_vars = {
        u'resource_views': resource_views,
        u'current_resource_view': current_resource_view,
        u'dataset_type': dataset_type,
        u'pkg_dict': package,
        u'package': package,
        u'resource': resource,
        u'pkg': pkg,  # NB it is the current version of the dataset, so ignores
        # activity_id. Still used though in resource views for
        # backward compatibility
        u'is_activity_archive': bool(activity_id),
    }

    template = _get_pkg_template(u'resource_template', dataset_type)
    return base.render(template, extra_vars)
Beispiel #44
0
    def follow(self):

        return base.render('follow/read.html')
Beispiel #45
0
 def faq(self):
     return base.render('content/faq.html')
Beispiel #46
0
 def license(self):
     return base.render('home/license.html')
Beispiel #47
0
 def get(self):
     context, data_dict = self._prepare()
     return base.render(u'user/request_reset.html', {})
Beispiel #48
0
 def about(self):
     return base.render('home/about.html')
Beispiel #49
0
    def get(
        self, package_type, id, data=None, errors=None, error_summary=None
    ):
        context = self._prepare(id, data)
        package_type = _get_package_type(id) or package_type
        try:
            pkg_dict = get_action(u'package_show')(
                dict(context, for_view=True), {
                    u'id': id
                }
            )
            context[u'for_edit'] = True
            old_data = get_action(u'package_show')(context, {u'id': id})
            # old data is from the database and data is passed from the
            # user if there is a validation error. Use users data if there.
            if data:
                old_data.update(data)
            data = old_data
        except (NotFound, NotAuthorized):
            return base.abort(404, _(u'Dataset not found'))
        # are we doing a multiphase add?
        if data.get(u'state', u'').startswith(u'draft'):
            g.form_action = h.url_for(u'dataset.new')
            g.form_style = u'new'

            return CreateView().get(
                package_type,
                data=data,
                errors=errors,
                error_summary=error_summary
            )

        pkg = context.get(u"package")
        resources_json = h.json.dumps(data.get(u'resources', []))

        try:
            check_access(u'package_update', context)
        except NotAuthorized:
            return base.abort(
                403,
                _(u'User %r not authorized to edit %s') % (g.user, id)
            )
        # convert tags if not supplied in data
        if data and not data.get(u'tag_string'):
            data[u'tag_string'] = u', '.join(
                h.dict_list_reduce(pkg_dict.get(u'tags', {}), u'name')
            )
        errors = errors or {}
        form_snippet = _get_pkg_template(
            u'package_form', package_type=package_type
        )
        form_vars = {
            u'data': data,
            u'errors': errors,
            u'error_summary': error_summary,
            u'action': u'edit',
            u'dataset_type': package_type,
            u'form_style': u'edit'
        }
        errors_json = h.json.dumps(errors)

        # TODO: remove
        g.pkg = pkg
        g.resources_json = resources_json
        g.errors_json = errors_json

        _setup_template_variables(
            context, {u'id': id}, package_type=package_type
        )

        # we have already completed stage 1
        form_vars[u'stage'] = [u'active']
        if data.get(u'state', u'').startswith(u'draft'):
            form_vars[u'stage'] = [u'active', u'complete']

        edit_template = _get_pkg_template(u'edit_template', package_type)
        return base.render(
            edit_template,
            extra_vars={
                u'form_vars': form_vars,
                u'form_snippet': form_snippet,
                u'dataset_type': package_type,
                u'pkg_dict': pkg_dict,
                u'pkg': pkg,
                u'resources_json': resources_json,
                u'form_snippet': form_snippet,
                u'errors_json': errors_json
            }
        )
 def view(self):
     # get package objects corresponding to popular GA content
     c.top_resources = dbutil.get_top_resources(limit=10)
     return render('summary.html')
Beispiel #51
0
def read(package_type, id):
    context = {
        u'model': model,
        u'session': model.Session,
        u'user': g.user,
        u'for_view': True,
        u'auth_user_obj': g.userobj
    }
    data_dict = {u'id': id, u'include_tracking': True}
    activity_id = request.params.get(u'activity_id')

    # check if package exists
    try:
        pkg_dict = get_action(u'package_show')(context, data_dict)
        pkg = context[u'package']
    except (NotFound, NotAuthorized):
        return base.abort(404, _(u'Dataset not found'))

    g.pkg_dict = pkg_dict
    g.pkg = pkg
    # NB templates should not use g.pkg, because it takes no account of
    # activity_id

    if activity_id:
        # view an 'old' version of the package, as recorded in the
        # activity stream
        try:
            activity = get_action(u'activity_show')(
                context, {u'id': activity_id, u'include_data': True})
        except NotFound:
            base.abort(404, _(u'Activity not found'))
        except NotAuthorized:
            base.abort(403, _(u'Unauthorized to view activity data'))
        current_pkg = pkg_dict
        try:
            pkg_dict = activity[u'data'][u'package']
        except KeyError:
            base.abort(404, _(u'Dataset not found'))
        if u'id' not in pkg_dict or u'resources' not in pkg_dict:
            log.info(u'Attempt to view unmigrated or badly migrated dataset '
                     '{} {}'.format(id, activity_id))
            base.abort(404, _(u'The detail of this dataset activity is not '
                              'available'))
        if pkg_dict[u'id'] != current_pkg[u'id']:
            log.info(u'Mismatch between pkg id in activity and URL {} {}'
                     .format(pkg_dict[u'id'], current_pkg[u'id']))
            # the activity is not for the package in the URL - don't allow
            # misleading URLs as could be malicious
            base.abort(404, _(u'Activity not found'))
        # The name is used lots in the template for links, so fix it to be
        # the current one. It's not displayed to the user anyway.
        pkg_dict[u'name'] = current_pkg[u'name']

        # Earlier versions of CKAN only stored the package table in the
        # activity, so add a placeholder for resources, or the template
        # will crash.
        pkg_dict.setdefault(u'resources', [])

    # if the user specified a package id, redirect to the package name
    if data_dict['id'] == pkg_dict['id'] and \
            data_dict['id'] != pkg_dict['name']:
        return h.redirect_to(u'dataset.read',
                             id=pkg_dict['name'],
                             activity_id=activity_id)

    # can the resources be previewed?
    for resource in pkg_dict[u'resources']:
        resource_views = get_action(u'resource_view_list')(
            context, {
                u'id': resource[u'id']
            }
        )
        resource[u'has_views'] = len(resource_views) > 0

    package_type = pkg_dict[u'type'] or package_type
    _setup_template_variables(context, {u'id': id}, package_type=package_type)

    template = _get_pkg_template(u'read_template', package_type)
    try:
        return base.render(
            template, {
                u'dataset_type': package_type,
                u'pkg_dict': pkg_dict,
                u'pkg': pkg,  # NB deprecated - it is the current version of
                              # the dataset, so ignores activity_id
                u'is_activity_archive': bool(activity_id),
            }
        )
    except TemplateNotFound as e:
        msg = _(
            u"Viewing datasets of type \"{package_type}\" is "
            u"not supported ({file_!r}).".format(
                package_type=package_type, file_=e.message
            )
        )
        return base.abort(404, msg)

    assert False, u"We should never get here"
Beispiel #52
0
 def list(self):
     format = request.params.get('format', '')
     if format == 'atom':
         # Generate and return Atom 1.0 document.
         from webhelpers.feedgenerator import Atom1Feed
         feed = Atom1Feed(
             title=_(u'CKAN Repository Revision History'),
             link=h.url_for(controller='revision', action='list', id=''),
             description=_(u'Recent changes to the CKAN repository.'),
             language=text_type(get_lang()),
         )
         # TODO: make this configurable?
         # we do not want the system to fall over!
         maxresults = 200
         try:
             dayHorizon = int(request.params.get('days', 5))
         except:
             dayHorizon = 5
         ourtimedelta = timedelta(days=-dayHorizon)
         since_when = datetime.now() + ourtimedelta
         revision_query = model.repo.history()
         revision_query = revision_query.filter(
             model.Revision.timestamp >= since_when).filter(
                 model.Revision.id != None)
         revision_query = revision_query.limit(maxresults)
         for revision in revision_query:
             package_indications = []
             revision_changes = model.repo.list_changes(revision)
             resource_revisions = revision_changes[model.Resource]
             package_extra_revisions = revision_changes[model.PackageExtra]
             for package in revision.packages:
                 if not package:
                     # package is None sometimes - I don't know why,
                     # but in the meantime while that is fixed,
                     # avoid an exception here
                     continue
                 if package.private:
                     continue
                 number = len(package.all_revisions)
                 package_revision = None
                 count = 0
                 for pr in package.all_revisions:
                     count += 1
                     if pr.revision.id == revision.id:
                         package_revision = pr
                         break
                 if package_revision and package_revision.state == \
                         model.State.DELETED:
                     transition = 'deleted'
                 elif package_revision and count == number:
                     transition = 'created'
                 else:
                     transition = 'updated'
                     for resource_revision in resource_revisions:
                         if resource_revision.package_id == package.id:
                             transition += ':resources'
                             break
                     for package_extra_revision in package_extra_revisions:
                         if package_extra_revision.package_id == \
                                 package.id:
                             if package_extra_revision.key == \
                                     'date_updated':
                                 transition += ':date_updated'
                                 break
                 indication = "%s:%s" % (package.name, transition)
                 package_indications.append(indication)
             pkgs = u'[%s]' % ' '.join(package_indications)
             item_title = u'r%s ' % (revision.id)
             item_title += pkgs
             if revision.message:
                 item_title += ': %s' % (revision.message or '')
             item_link = h.url_for(controller='revision',
                                   action='read',
                                   id=revision.id)
             item_description = _('Datasets affected: %s.\n') % pkgs
             item_description += '%s' % (revision.message or '')
             item_author_name = revision.author
             item_pubdate = revision.timestamp
             feed.add_item(
                 title=item_title,
                 link=item_link,
                 description=item_description,
                 author_name=item_author_name,
                 pubdate=item_pubdate,
             )
         feed.content_type = 'application/atom+xml'
         return feed.writeString('utf-8')
     else:
         query = model.Session.query(model.Revision)
         c.page = h.Page(collection=query,
                         page=h.get_page_number(request.params),
                         url=h.pager_url,
                         items_per_page=20)
         return base.render('revision/list.html')
Beispiel #53
0
 def get(self):
     self._prepare()
     return base.render(u'user/request_reset.html', {})
Beispiel #54
0
 def broken_helper_as_attribute(self):
     return base.render('tests/broken_helper_as_attribute.html')
Beispiel #55
0
 def get(self, id):
     context, user_dict = self._prepare(id)
     return base.render(u'user/perform_reset.html',
                        {u'user_dict': user_dict})
Beispiel #56
0
 def helper_as_item(self):
     return base.render('tests/helper_as_item.html')
Beispiel #57
0
class OrganizationController(BaseController):
    def _send_application(self, group, reason):
        from genshi.template.text import NewTextTemplate

        if not reason:
            h.flash_error(
                _("There was a problem with your submission, \
                             please correct it and try again"))
            errors = {"reason": ["No reason was supplied"]}
            return self.apply(group.id,
                              errors=errors,
                              error_summary=action.error_summary(errors))

        admins = group.members_of_type(model.User, 'admin').all()
        recipients = [(u.fullname,u.email) for u in admins] if admins else \
                     [(config.get('ckan.admin.name', "CKAN Administrator"),
                       config.get('ckan.admin.email', None), )]

        if not recipients:
            h.flash_error(
                _("There is a problem with the system configuration"))
            errors = {"reason": ["No group administrator exists"]}
            return self.apply(group.id,
                              data=data,
                              errors=errors,
                              error_summary=action.error_summary(errors))

        extra_vars = {'group': group, 'requester': c.userobj, 'reason': reason}
        email_msg = render("email/join_publisher_request.txt",
                           extra_vars=extra_vars,
                           loader_class=NewTextTemplate)

        try:
            for (name, recipient) in recipients:
                mailer.mail_recipient(name, recipient, "Publisher request",
                                      email_msg)
        except:
            h.flash_error(
                _("There is a problem with the system configuration"))
            errors = {"reason": ["No mail server was found"]}
            return self.apply(group.id,
                              errors=errors,
                              error_summary=action.error_summary(errors))

        h.flash_success(_("Your application has been submitted"))
        h.redirect_to('publisher_read', id=group.name)

    def apply(self, id=None, data=None, errors=None, error_summary=None):
        """
        A user has requested access to this publisher and so we will send an
        email to any admins within the publisher.
        """
        if 'parent' in request.params and not id:
            id = request.params['parent']

        if id:
            c.group = model.Group.get(id)
            if 'save' in request.params and not errors:
                return self._send_application(
                    c.group, request.params.get('reason', None))

        self._add_publisher_list()
        data = data or {}
        errors = errors or {}
        error_summary = error_summary or {}

        data.update(request.params)

        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
        c.form = render('organization_apply_form.html', extra_vars=vars)
        return render('organization_apply.html')

    def _add_users(self, group, parameters):
        if not group:
            h.flash_error(
                _("There was a problem with your submission, "
                  "please correct it and try again"))
            errors = {"reason": ["No reason was supplied"]}
            return self.apply(group.id,
                              errors=errors,
                              error_summary=action.error_summary(errors))

        data_dict = logic.clean_dict(
            dict_func.unflatten(
                logic.tuplize_dict(logic.parse_params(request.params))))
        data_dict['id'] = group.id

        # Temporary fix for strange caching during dev
        l = data_dict['users']
        for d in l:
            d['capacity'] = d.get('capacity', 'editor')

        context = {
            "group": group,
            "schema": schema.default_group_schema(),
            "model": model,
            "session": model.Session
        }

        # Temporary cleanup of a capacity being sent without a name
        users = [d for d in data_dict['users'] if len(d) == 2]
        data_dict['users'] = users

        model.repo.new_revision()
        model_save.group_member_save(context, data_dict, 'users')
        model.Session.commit()

        h.redirect_to(controller='group', action='edit', id=group.name)

    def users(self, id, data=None, errors=None, error_summary=None):
        c.group = model.Group.get(id)

        if not c.group:
            abort(404, _('Group not found'))

        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'group': c.group
        }

        try:
            logic.check_access('group_update', context)
        except logic.NotAuthorized, e:
            abort(401, _('User %r not authorized to edit %s') % (c.user, id))

        if 'save' in request.params and not errors:
            return self._add_users(c.group, request.params)

        data = data or {}
        errors = errors or {}
        error_summary = error_summary or {}

        data['users'] = []
        data['users'].extend({
            "name": user.name,
            "capacity": "admin"
        } for user in c.group.members_of_type(model.User, "admin").all())
        data['users'].extend({
            "name": user.name,
            "capacity": "editor"
        } for user in c.group.members_of_type(model.User, 'editor').all())

        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
        c.form = render('organization_users_form.html', extra_vars=vars)

        return render('organization_users.html')
Beispiel #58
0
 def privacy(self):
     return base.render('content/privacy.html')
Beispiel #59
0
def activity_list_to_html(context, activity_stream, extra_vars):
    activity_list = []  # These are the activity stream messages.
    for activity in activity_stream:
        detail = None
        activity_type = activity['activity_type']
        # Some activity types may have details.
        if activity_type in activity_streams.activity_stream_actions_with_detail:
            details = logic.get_action('activity_detail_list')(
                context=context, data_dict={
                    'id': activity['id']
                })
            # If an activity has just one activity detail then render the
            # detail instead of the activity.
            if len(details) == 1:
                detail = details[0]
                object_type = detail['object_type']

                if object_type == 'PackageExtra':
                    object_type = 'package_extra'

                new_activity_type = '%s %s' % (detail['activity_type'],
                                               object_type.lower())
                if new_activity_type in activity_streams.activity_stream_string_functions:
                    activity_type = new_activity_type

        if not activity_type in activity_streams.activity_stream_string_functions:
            raise NotImplementedError("No activity renderer for activity "
                                      "type '%s'" % activity_type)

        if activity_type in activity_streams.activity_stream_string_icons:
            activity_icon = activity_streams.activity_stream_string_icons[
                activity_type]
        else:
            activity_icon = activity_streams.activity_stream_string_icons[
                'undefined']

        activity_msg = activity_streams.activity_stream_string_functions[
            activity_type](context, activity)

        include_msg = True

        # Get the data needed to render the message.
        matches = re.findall('\{([^}]*)\}', activity_msg)
        data = {}
        for match in matches:
            snippet = activity_streams.activity_snippet_functions[match](
                activity, detail)
            if match == 'extra':
                if 'updateFrequency' in snippet:
                    data[str(match)] = 'Frecuencia de actualización'
                    activity_msg = u'{actor} actualizó {extra} del conjunto de datos {dataset}'
                else:
                    include_msg = False
            else:
                data[str(match)] = snippet

        reload(sys)
        sys.setdefaultencoding('utf-8')

        if include_msg:
            activity_list.append({
                'msg':
                activity_msg,
                'type':
                activity_type.replace(' ', '-').lower(),
                'icon':
                activity_icon,
                'data':
                data,
                'timestamp':
                activity['timestamp'],
                'is_new':
                activity.get('is_new', False)
            })

    extra_vars['activities'] = activity_list
    return literal(
        base.render('activity_streams/activity_stream_items.html',
                    extra_vars=extra_vars))
Beispiel #60
0
 def broken_helper_as_item(self):
     return base.render('tests/broken_helper_as_item.html')