How to use the ckan.model function in ckan

To help you get started, we’ve selected a few ckan examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ckan / ckanext-archiver / tests / test_archiver.py View on Github external
    @classmethod
    def setup_class(cls):
        reset_db()
        archiver_model.init_tables(model.meta.engine)

        cls.temp_dir = tempfile.mkdtemp()

        cls.config = config.__file__
github datagovuk / ckanext-os / ckanext / os / controllers / widgets.py View on Github external
url = request.params.get('url')

        # Check parameter
        if not (url):
            response.status_int = 400
            return 'Missing url parameter'

        # Check URL is in CKAN (otherwise we are an open proxy)
        base_url = url.split('?')[0] if '?' in url else url
        if base_url == urljoin(g.site_url, '/data/wfs'):
            # local WFS service
            return self._read_url(url, post_data=request.body,
                                  content_type='application/xml')
        else:
            # WMS
            query = model.Session.query(model.Resource) \
                         .filter(model.Resource.url.like(base_url + '%'))

            if query.count() == 0:
                response.status_int = 403
                return 'WMS URL not known: %s' % base_url

        # Correct basic errors in the WMS URL
        try:
            url = self.wms_url_correcter(url)
        except ValidationError, e:
            response.status_int = 400
            log.warning('WMS Preview proxy received invalid url: %r', url)
            return 'Invalid URL: %s' % str(e)

        content = self._read_url(url)
        if not content:
github NaturalHistoryMuseum / ckanext-nhm / ckanext / nhm / routes / record.py View on Github external
# assign title based on the title field
    toolkit.c.record_title = toolkit.c.record_dict.get(field_names[u'title'],
                                                       u'Record {}'.format(
                                                           toolkit.c.record_dict.get(
                                                               u'_id')))

    # sanity check: image field hasn't been set to _id
    if field_names[u'image'] and field_names[u'image'] != u'_id':
        default_copyright = u'<small>© The Trustees of the Natural History ' \
                            u'Museum, London</small>'
        licence_id = toolkit.c.resource.get(u'_image_licence') or u'cc-by'
        short_licence_id = licence_id[:5].lower()
        # try and overwrite default licence with more specific one
        for l_id in [licence_id, short_licence_id]:
            try:
                licence = model.Package.get_license_register()[l_id]
                break
            except KeyError:
                continue

        default_licence = u'Licence: {}'.format(toolkit.h.link_to(licence.title,
                                                                  licence.url,
                                                                  target=u'_blank'))

        # pop the image field so it isn't output as part of the
        # record_dict/field_data dict (see self.view())
        image_field_value = toolkit.c.record_dict.pop(field_names[u'image'], None)

        if image_field_value:
            # init the images list on the context var
            toolkit.c.images = []
github datadotworld / ckanext-datadotworld / ckanext / datadotworld / helpers.py View on Github external
def admin_in_orgs(name):
    user = model.User.get(name)
    if not user:
        return []
    return user.get_groups('organization', 'admin')
github ckan / ckanext-datastorer / ckanext / datastorer / plugin.py View on Github external
def notify(self, entity, operation=None):
        if not isinstance(entity, model.Resource):
            return
        
        if operation:
            if operation == model.DomainObjectOperation.new:
                self._create_datastorer_task(entity)
        else:
            # if operation is None, resource URL has been changed, as the
            # notify function in IResourceUrlChange only takes 1 parameter
            self._create_datastorer_task(entity)
github ckan / ckanext-issues / ckanext / issues / controller / controller.py View on Github external
return p.toolkit.redirect_to('issues_for_organization',
                                         org_id=org_id)
        return render("issues/organization_issues.html",
                      extra_vars=template_params)

        # TO DELETE
        c.org = model.Group.get(org_id)

        q = """
            SELECT table_id
            FROM member
            WHERE group_id='{gid}'
              AND table_name='package'
              AND state='active'
        """.format(gid=c.org.id)
        results = model.Session.execute(q)

        dataset_ids = [x['table_id'] for x in results]
        issues = model.Session.query(issuemodel.Issue)\
            .filter(issuemodel.Issue.dataset_id.in_(dataset_ids))\
            .order_by(issuemodel.Issue.created.desc())

        c.results = collections.defaultdict(list)
        for issue in issues:
            c.results[issue.package].append(issue)
        c.package_set = sorted(set(c.results.keys()), key=lambda x: x.title)
        return render("issues/organization_issues.html")
github ckan / ckanext-issues / ckanext / issues / controller / show.py View on Github external
def _get_assigned_user(assignee_id, session):
    context = {'session': session, 'model': cmodel}
    data_dict = {'id': assignee_id}
    # we only need the basic properties of the user, not its datasets etc
    if toolkit.check_ckan_version(min_version='2.3'):
        # these are the defaults, but just in case...
        data_dict.update({'include_datasets': False,
                          'include_num_followers': False})
    else:
        context = {'return_minimal': True}
    try:
        return toolkit.get_action('user_show')(context, data_dict)
    except toolkit.ObjectNotFound:
        return None
    except toolkit.NotAuthorized:
        return None
github ckan / ckanext-qa / ckanext / qa / tasks.py View on Github external
def _update_search_index(package_id):
    '''
    Tells CKAN to update its search index for a given package.
    '''
    from ckan import model
    from ckan.lib.search.index import PackageSearchIndex
    package_index = PackageSearchIndex()
    context_ = {'model': model, 'ignore_auth': True, 'session': model.Session,
                'use_cache': False, 'validate': False}
    package = toolkit.get_action('package_show')(context_, {'id': package_id})
    package_index.index_package(package, defer_commit=False)
    log.info('Search indexed %s', package['name'])
github ckan / ckanext-archiver / ckanext / archiver / commands.py View on Github external
When is_pkg=True:
               pkg_or_res - package object
               num_resources_for_pkg - number of resources it has
               pkg_for_res - None
           When is_pkg=False:
               pkg_or_res - resource object
               num_resources_for_pkg - None
               pkg_for_res - package object relating to the given resource
        '''
        from ckan import model
        packages = []
        resources = []
        if args:
            for arg in args:
                # try arg as a group id/name
                group = model.Group.get(arg)
                if group:
                    if group.is_organization:
                        packages.extend(
                            model.Session.query(model.Package)
                                 .filter_by(owner_org=group.id))
                    else:
                        packages.extend(group.packages(with_private=True))
                    if not self.options.queue:
                        self.options.queue = 'bulk'
                    continue
                # try arg as a package id/name
                pkg = model.Package.get(arg)
                if pkg:
                    packages.append(pkg)
                    if not self.options.queue:
                        self.options.queue = 'priority'
github vrk-kpa / api-catalog / ckanext / ckanext-apicatalog_routes / ckanext / apicatalog_routes / health.py View on Github external
def check(self):
        result = True
        site_url = config.get('ckan.site_url')

        for url in self.check_site_urls:
            if not check_url("%s/%s" % (site_url, url)):
                log.warn(SITE_URL_FAILURE_LOGMESSAGE % url)
                result = False
        harvest_source_list = get_action('harvest_source_list')

        data_dict = {'return_last_job_status': True}
        context = {'model': model,
                   'ignore_auth': True}
        for harvest_source in harvest_source_list(context, data_dict):
            last_job_status = harvest_source.get('last_job_status')
            if last_job_status is not None:
                num_errors = last_job_status.get('stats', {}) .get('errored', 0)
                if num_errors > 0:
                    log.warn(HARVEST_FAILURE_LOGMESSAGE % (
                        harvest_source.get('title', ''),
                        pformat(harvest_source)))
                    result = False
                elif not last_job_status.get('finished'):
                    harvest_job_created = last_job_status.get('created')
                    created = datetime.datetime.strptime(harvest_job_created, HARVEST_JOB_TIMESTAMP_FORMAT)
                    now = datetime.datetime.now()

                    if now - created > HARVEST_JOB_TIMEOUT: