• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalHistoryMuseum / ckanext-query-dois / #209

06 Jun 2025 01:21PM UTC coverage: 44.794% (-0.1%) from 44.902%
#209

push

coveralls-python

web-flow
merge: #58 from ginger/standardisation

23 of 36 new or added lines in 11 files covered. (63.89%)

1 existing line in 1 file now uncovered.

228 of 509 relevant lines covered (44.79%)

0.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.52
/ckanext/query_dois/plugin.py
1
#!/usr/bin/env python
2
# encoding: utf-8
3
#
4
# This file is part of ckanext-query-dois
5
# Created by the Natural History Museum in London, UK
6

7
import logging
1✔
8

9
from ckan import plugins
1✔
10
from ckan.plugins import toolkit
1✔
11

12
from . import cli, helpers, routes
1✔
13
from .lib.doi import find_existing_doi, mint_multisearch_doi
1✔
14
from .lib.query import Query
1✔
15
from .lib.stats import DOWNLOAD_ACTION, record_stat
1✔
16
from .logic import action, auth
1✔
17

18
log = logging.getLogger(__name__)
1✔
19

20

21
class QueryDOIsPlugin(plugins.SingletonPlugin):
1✔
22
    plugins.implements(plugins.IBlueprint, inherit=True)
1✔
23
    plugins.implements(plugins.IConfigurer, inherit=True)
1✔
24
    plugins.implements(plugins.ITemplateHelpers)
1✔
25
    plugins.implements(plugins.IActions)
1✔
26
    plugins.implements(plugins.IAuthFunctions)
1✔
27
    plugins.implements(plugins.IClick)
1✔
28
    # if the versioned datastore downloader is available, we have a hook for it
29
    try:
1✔
30
        from ckanext.versioned_datastore.interfaces import IVersionedDatastoreDownloads
1✔
31

32
        plugins.implements(IVersionedDatastoreDownloads, inherit=True)
×
33
        versioned_datastore_available = True
×
34
    except ImportError:
1✔
35
        versioned_datastore_available = False
1✔
36

37
    # IBlueprint
38
    def get_blueprint(self):
1✔
39
        return routes.blueprints
×
40

41
    # IClick
42
    def get_commands(self):
1✔
43
        return cli.get_commands()
×
44

45
    # IAuthFunctions
46
    def get_auth_functions(self):
1✔
47
        return {
1✔
48
            'create_doi': auth.create_doi,
49
        }
50

51
    # IActions
52
    def get_actions(self):
1✔
53
        return {
×
54
            'create_doi': action.create_doi,
55
        }
56

57
    # IConfigurer
58
    def update_config(self, config):
1✔
59
        # add templates
60
        plugins.toolkit.add_template_directory(config, 'theme/templates')
×
61
        # add the resource groups
62
        plugins.toolkit.add_resource('theme/assets', 'ckanext-query-dois')
×
63

64
    # IVersionedDatastoreDownloads
65
    def download_after_init(self, request):
1✔
66
        try:
×
67
            query = Query.create_from_download_request(request)
×
68
            # mint the DOI on datacite if necessary
69
            mint_multisearch_doi(query)
×
70
        except toolkit.ValidationError:
×
71
            log.warning(
×
72
                'Could not create DOI for download, it contains private resources'
73
            )
74
        except:
×
75
            # if anything unexpected goes wrong we don't want to stop the download from
76
            # completing; just log the error and move on
NEW
77
            log.error('Failed to mint/retrieve DOI', exc_info=True)
×
78

79
    def download_modify_notifier_template_context(self, request, context):
1✔
80
        try:
1✔
81
            query = Query.create_from_download_request(request)
1✔
82
            # if a DOI can be created it should already have been created in
83
            # download_after_init
84
            doi = find_existing_doi(query)
1✔
85
            if doi:
1✔
86
                # update the context with the doi
87
                context['doi'] = doi.doi
1✔
88
        except:
1✔
89
            # if anything goes wrong we don't want to stop the download; just log the
90
            # error and move on
91
            log.error('Failed to retrieve DOI', exc_info=True)
1✔
92

93
        # always return the context
94
        return context
1✔
95

96
    def download_modify_manifest(self, manifest, request):
1✔
97
        try:
×
98
            query = Query.create_from_download_request(request)
×
99
            # if a DOI can be created it should already have been created in
100
            # download_after_init
101
            doi = find_existing_doi(query)
×
102
            if doi:
×
103
                # add the doi to the manifest
NEW
104
                manifest['query-doi'] = doi.doi
×
105
        except:
×
106
            # if anything goes wrong we don't want to stop the download from completing;
107
            # just log the error and move on
NEW
108
            log.error('Failed to retrieve DOI', exc_info=True)
×
109

110
        # always return the manifest
111
        return manifest
×
112

113
    def download_after_run(self, request):
1✔
114
        try:
×
115
            query = Query.create_from_download_request(request)
×
116
            # if a DOI can be created it should already have been created in
117
            # download_modify_manifest
118
            doi = find_existing_doi(query)
×
NEW
119
            if doi and request.state == 'complete':
×
120
                # record a download stat against the DOI
121
                record_stat(doi, DOWNLOAD_ACTION, identifier=request.id)
×
122
        except:
×
123
            # just log the error and move on
NEW
124
            log.error('Failed to retrieve DOI and/or create stats', exc_info=True)
×
125

126
    # ITemplateHelpers
127
    def get_helpers(self):
1✔
128
        return {
×
129
            'render_filter_value': helpers.render_filter_value,
130
            'get_most_recent_dois': helpers.get_most_recent_dois,
131
            'get_time_ago_description': helpers.get_time_ago_description,
132
            'get_landing_page_url': helpers.get_landing_page_url,
133
            'create_citation_text': helpers.create_citation_text,
134
            'create_multisearch_citation_text': helpers.create_multisearch_citation_text,
135
            'pretty_print_query': helpers.pretty_print_query,
136
            'get_doi_count': helpers.get_doi_count,
137
            'versioned_datastore_available': self.versioned_datastore_available,
138
        }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc