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

NaturalHistoryMuseum / ckanext-query-dois / #154

05 Oct 2023 04:24PM UTC coverage: 36.063% (-0.2%) from 36.248%
#154

push

coveralls-python

web-flow
merge: #45 from patch

11 of 11 new or added lines in 1 file covered. (100.0%)

229 of 635 relevant lines covered (36.06%)

0.36 hits per line

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

60.61
/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 json
1✔
8
import logging
1✔
9
from contextlib import suppress
1✔
10

11
from ckan import plugins
1✔
12

13
from . import helpers, routes, cli
1✔
14
from .lib.doi import mint_doi, mint_multisearch_doi, find_existing_doi
1✔
15
from .lib.query import DatastoreQuery
1✔
16
from .lib.stats import DOWNLOAD_ACTION, record_stat
1✔
17
from .logic import auth, action
1✔
18
from .logic.utils import extract_resource_ids_and_versions
1✔
19

20

21
log = logging.getLogger(__name__)
1✔
22

23

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

35
        plugins.implements(IVersionedDatastoreDownloads, inherit=True)
×
36
        versioned_datastore_available = True
×
37
    except ImportError:
1✔
38
        versioned_datastore_available = False
1✔
39

40
    # IBlueprint
41
    def get_blueprint(self):
1✔
42
        return routes.blueprints
×
43

44
    # IClick
45
    def get_commands(self):
1✔
46
        return cli.get_commands()
×
47

48
    # IAuthFunctions
49
    def get_auth_functions(self):
1✔
50
        return {
1✔
51
            'create_doi': auth.create_doi,
52
        }
53

54
    # IActions
55
    def get_actions(self):
1✔
56
        return {
×
57
            'create_doi': action.create_doi,
58
        }
59

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

67
    # IVersionedDatastoreDownloads
68
    def download_after_init(self, request):
1✔
69
        try:
×
70
            # check to see if the download is something we can stick a DOI on (this will
71
            # throw a validation error if any of the resources aren't valid for DOI-ing
72
            extract_resource_ids_and_versions(
×
73
                req_resource_ids_and_versions=request.core_record.resource_ids_and_versions
74
            )
75

76
            # mint the DOI on datacite if necessary
77
            created, doi = mint_multisearch_doi(
×
78
                request.core_record.query,
79
                request.core_record.query_version,
80
                request.core_record.resource_ids_and_versions,
81
            )
82
        except:
×
83
            # if anything goes wrong we don't want to stop the download from completing;
84
            # just log the error and move on
85
            log.error('Failed to mint/retrieve DOI', exc_info=True)
×
86

87
    def download_modify_notifier_template_context(self, request, context):
1✔
88
        try:
1✔
89
            # if a DOI can be created it should already have been created in download_after_init
90
            doi = find_existing_doi(
1✔
91
                request.core_record.resource_ids_and_versions,
92
                request.core_record.query_hash,
93
                request.core_record.query_version,
94
            )
95

96
            if doi:
1✔
97
                # update the context with the doi
98
                context['doi'] = doi.doi
1✔
99
        except:
1✔
100
            # if anything goes wrong we don't want to stop the download; just log the
101
            # error and move on
102
            log.error('Failed to retrieve DOI', exc_info=True)
1✔
103

104
        # always return the context
105
        return context
1✔
106

107
    def download_modify_manifest(self, manifest, request):
1✔
108
        try:
×
109
            # if a DOI can be created it should already have been created in download_after_init
110
            doi = find_existing_doi(
×
111
                request.core_record.resource_ids_and_versions,
112
                request.core_record.query_hash,
113
                request.core_record.query_version,
114
            )
115

116
            if doi:
×
117
                # add the doi to the manifest
118
                manifest['query-doi'] = doi.doi
×
119
        except:
×
120
            # if anything goes wrong we don't want to stop the download from completing;
121
            # just log the error and move on
122
            log.error('Failed to retrieve DOI', exc_info=True)
×
123

124
        # always return the manifest
125
        return manifest
×
126

127
    def download_after_run(self, request):
1✔
128
        try:
×
129
            # if a DOI can be created it should already have been created in
130
            # download_modify_manifest
131
            doi = find_existing_doi(
×
132
                request.core_record.resource_ids_and_versions,
133
                request.core_record.query_hash,
134
                request.core_record.query_version,
135
            )
136

137
            if doi and request.state == 'complete':
×
138
                # record a download stat against the DOI
139
                record_stat(doi, DOWNLOAD_ACTION, identifier=request.id)
×
140
        except:
×
141
            # just log the error and move on
142
            log.error('Failed to retrieve DOI and/or create stats', exc_info=True)
×
143

144
    # ITemplateHelpers
145
    def get_helpers(self):
1✔
146
        return {
×
147
            'render_filter_value': helpers.render_filter_value,
148
            'get_most_recent_dois': helpers.get_most_recent_dois,
149
            'get_time_ago_description': helpers.get_time_ago_description,
150
            'get_landing_page_url': helpers.get_landing_page_url,
151
            'create_citation_text': helpers.create_citation_text,
152
            'create_multisearch_citation_text': helpers.create_multisearch_citation_text,
153
            'pretty_print_query': helpers.pretty_print_query,
154
            'get_doi_count': helpers.get_doi_count,
155
            'versioned_datastore_available': self.versioned_datastore_available,
156
        }
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