Skip to content

Crud

DOIQuery

Source code in ckanext/doi/model/crud.py
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class DOIQuery:
    # convenience properties
    m = DOI
    cols = [c.name for c in doi_table.c]

    @classmethod
    def create(cls, identifier, package_id, published=None):
        """
        Create a new record in the DOI table.

        :param identifier: a new DOI string
        :param package_id: the id of the package this DOI represents
        :param published: when this DOI was published (datetime, nullable)
        :return: the newly created record object
        """
        new_record = DOI(
            identifier=identifier, package_id=package_id, published=published
        )
        Session.add(new_record)
        Session.commit()
        return new_record

    @classmethod
    def read_doi(cls, identifier):
        """
        Retrieve a record with a given DOI.

        :param identifier: the DOI string
        :return: the record object
        """
        return Session.query(DOI).get(identifier)

    @classmethod
    def read_package(cls, package_id, create_if_none=False):
        """
        Retrieve a record associated with a given package.

        :param package_id: the id of the package
        :param create_if_none: generate a new DOI and add a record if no record is found for the
                               given package
        :return: the record object
        """
        from ckanext.doi.lib.api import DataciteClient

        record = Session.query(DOI).filter(DOI.package_id == package_id).first()
        if record is None and create_if_none:
            client = DataciteClient()
            new_doi = client.generate_doi()
            record = cls.create(new_doi, package_id)
        return record

    @classmethod
    def update_doi(cls, identifier, **kwargs):
        """
        Update the package_id and/or published fields of a record with a given DOI.

        :param identifier: the DOI string
        :param kwargs: the values to be updated
        :return: the updated record object
        """
        update_dict = {k: v for k, v in kwargs.items() if k in cls.cols}
        Session.query(DOI).filter(DOI.identifier == identifier).update(update_dict)
        Session.commit()
        return cls.read_doi(identifier)

    @classmethod
    def update_package(cls, package_id, **kwargs):
        """
        Update the package_id and/or published fields of a record associated with a
        given package.

        :param package_id: the id of the package
        :param kwargs: the values to be updated
        :return: the updated record object
        """
        update_dict = {k: v for k, v in kwargs.items() if k in cls.cols}
        Session.query(DOI).filter(DOI.package_id == package_id).update(update_dict)
        Session.commit()
        return cls.read_package(package_id)

    @classmethod
    def delete_doi(cls, identifier):
        """
        Delete the record with a given DOI.

        :param identifier: the DOI string
        :return: True if a record was deleted, False if not
        """
        to_delete = cls.read_doi(identifier)
        if to_delete is not None:
            Session.delete(to_delete)
            Session.commit()
            return True
        else:
            return False

    @classmethod
    def delete_package(cls, package_id):
        """
        Delete the record associated with a given package.

        :param package_id: the id of the package
        :return: True if a record was deleted, False if not
        """
        to_delete = cls.read_package(package_id)
        if to_delete is not None:
            Session.delete(to_delete)
            Session.commit()
            return True
        else:
            return False

create(identifier, package_id, published=None) classmethod

Create a new record in the DOI table.

Parameters:

Name Type Description Default
identifier

a new DOI string

required
package_id

the id of the package this DOI represents

required
published

when this DOI was published (datetime, nullable)

None

Returns:

Type Description

the newly created record object

Source code in ckanext/doi/model/crud.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@classmethod
def create(cls, identifier, package_id, published=None):
    """
    Create a new record in the DOI table.

    :param identifier: a new DOI string
    :param package_id: the id of the package this DOI represents
    :param published: when this DOI was published (datetime, nullable)
    :return: the newly created record object
    """
    new_record = DOI(
        identifier=identifier, package_id=package_id, published=published
    )
    Session.add(new_record)
    Session.commit()
    return new_record

delete_doi(identifier) classmethod

Delete the record with a given DOI.

Parameters:

Name Type Description Default
identifier

the DOI string

required

Returns:

Type Description

True if a record was deleted, False if not

Source code in ckanext/doi/model/crud.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@classmethod
def delete_doi(cls, identifier):
    """
    Delete the record with a given DOI.

    :param identifier: the DOI string
    :return: True if a record was deleted, False if not
    """
    to_delete = cls.read_doi(identifier)
    if to_delete is not None:
        Session.delete(to_delete)
        Session.commit()
        return True
    else:
        return False

delete_package(package_id) classmethod

Delete the record associated with a given package.

Parameters:

Name Type Description Default
package_id

the id of the package

required

Returns:

Type Description

True if a record was deleted, False if not

Source code in ckanext/doi/model/crud.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@classmethod
def delete_package(cls, package_id):
    """
    Delete the record associated with a given package.

    :param package_id: the id of the package
    :return: True if a record was deleted, False if not
    """
    to_delete = cls.read_package(package_id)
    if to_delete is not None:
        Session.delete(to_delete)
        Session.commit()
        return True
    else:
        return False

read_doi(identifier) classmethod

Retrieve a record with a given DOI.

Parameters:

Name Type Description Default
identifier

the DOI string

required

Returns:

Type Description

the record object

Source code in ckanext/doi/model/crud.py
34
35
36
37
38
39
40
41
42
@classmethod
def read_doi(cls, identifier):
    """
    Retrieve a record with a given DOI.

    :param identifier: the DOI string
    :return: the record object
    """
    return Session.query(DOI).get(identifier)

read_package(package_id, create_if_none=False) classmethod

Retrieve a record associated with a given package.

Parameters:

Name Type Description Default
package_id

the id of the package

required
create_if_none

generate a new DOI and add a record if no record is found for the given package

False

Returns:

Type Description

the record object

Source code in ckanext/doi/model/crud.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def read_package(cls, package_id, create_if_none=False):
    """
    Retrieve a record associated with a given package.

    :param package_id: the id of the package
    :param create_if_none: generate a new DOI and add a record if no record is found for the
                           given package
    :return: the record object
    """
    from ckanext.doi.lib.api import DataciteClient

    record = Session.query(DOI).filter(DOI.package_id == package_id).first()
    if record is None and create_if_none:
        client = DataciteClient()
        new_doi = client.generate_doi()
        record = cls.create(new_doi, package_id)
    return record

update_doi(identifier, **kwargs) classmethod

Update the package_id and/or published fields of a record with a given DOI.

Parameters:

Name Type Description Default
identifier

the DOI string

required
kwargs

the values to be updated

{}

Returns:

Type Description

the updated record object

Source code in ckanext/doi/model/crud.py
63
64
65
66
67
68
69
70
71
72
73
74
75
@classmethod
def update_doi(cls, identifier, **kwargs):
    """
    Update the package_id and/or published fields of a record with a given DOI.

    :param identifier: the DOI string
    :param kwargs: the values to be updated
    :return: the updated record object
    """
    update_dict = {k: v for k, v in kwargs.items() if k in cls.cols}
    Session.query(DOI).filter(DOI.identifier == identifier).update(update_dict)
    Session.commit()
    return cls.read_doi(identifier)

update_package(package_id, **kwargs) classmethod

Update the package_id and/or published fields of a record associated with a given package.

Parameters:

Name Type Description Default
package_id

the id of the package

required
kwargs

the values to be updated

{}

Returns:

Type Description

the updated record object

Source code in ckanext/doi/model/crud.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@classmethod
def update_package(cls, package_id, **kwargs):
    """
    Update the package_id and/or published fields of a record associated with a
    given package.

    :param package_id: the id of the package
    :param kwargs: the values to be updated
    :return: the updated record object
    """
    update_dict = {k: v for k, v in kwargs.items() if k in cls.cols}
    Session.query(DOI).filter(DOI.package_id == package_id).update(update_dict)
    Session.commit()
    return cls.read_package(package_id)