Ejemplo n.º 1
0
class CircPolicyIdentifier(RecordIdentifier):
    """Sequence generator for circultion policies identifiers."""

    __tablename__ = 'circ_policy_id'
    __mapper_args__ = {'concrete': True}

    recid = db.Column(
        db.BigInteger().with_variant(db.Integer, 'sqlite'),
        primary_key=True, autoincrement=True,
    )
Ejemplo n.º 2
0
class HoldingIdentifier(RecordIdentifier):
    """Sequence generator for holdings identifiers."""

    __tablename__ = 'holding_id'
    __mapper_args__ = {'concrete': True}

    recid = db.Column(
        db.BigInteger().with_variant(db.Integer, 'sqlite'),
        primary_key=True, autoincrement=True,
    )
Ejemplo n.º 3
0
class AcqReceiptIdentifier(RecordIdentifier):
    """Sequence generator for acquisition receipt identifiers."""

    __tablename__ = 'acq_receipt_id'
    __mapper_args__ = {'concrete': True}

    recid = db.Column(
        db.BigInteger().with_variant(db.Integer, 'sqlite'),
        primary_key=True,
        autoincrement=True,
    )
Ejemplo n.º 4
0
class AgentGndIdentifier(RecordIdentifier):
    """Sequence generator for GND Authority identifiers."""

    __tablename__ = 'agent_gnd_id'
    __mapper_args__ = {'concrete': True}

    recid = db.Column(
        db.BigInteger().with_variant(db.Integer, 'sqlite'),
        primary_key=True,
        autoincrement=True,
    )
Ejemplo n.º 5
0
class PatronTransactionIdentifier(RecordIdentifier):
    """Sequence generator for Patron Transaction identifiers."""

    __tablename__ = 'patron_transaction_id'
    __mapper_args__ = {'concrete': True}

    recid = db.Column(
        db.BigInteger().with_variant(db.Integer, 'sqlite'),
        primary_key=True,
        autoincrement=True,
    )
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


"""Define relation between records and buckets."""

from __future__ import absolute_import

from invenio_db import db
from invenio_pidstore.models import RecordIdentifier


class {{ cookiecutter.class_name }}Identifier(RecordIdentifier):
    """Sequence generator for {{ cookiecutter.class_name }} identifiers."""

    __tablename__ = '{{ cookiecutter.name }}_id'
    __mapper_args__ = {'concrete': True}

    recid = db.Column(
        db.BigInteger().with_variant(db.Integer, 'sqlite'),
        primary_key=True, autoincrement=True,
    )
Ejemplo n.º 7
0
class RecordIdentifier(db.Model):
    """Sequence generator for integer record identifiers.

    The sole purpose of this model is to generate integer record identifiers in
    sequence using the underlying database's auto increment features in a
    transaction friendly manner. The feature is primarily provided to support
    legacy Invenio instances to continue their current record identifier
    scheme. For new instances we strong encourage to not use auto incrementing
    record identifiers, but instead use e.g. UUIDs as record identifiers.
    """

    __tablename__ = 'pidstore_recid'

    recid = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"),
                      primary_key=True,
                      autoincrement=True)

    @classmethod
    def next(cls):
        """Return next available record identifier."""
        try:
            with db.session.begin_nested():
                obj = cls()
                db.session.add(obj)
        except IntegrityError:  # pragma: no cover
            with db.session.begin_nested():
                # Someone has likely modified the table without using the
                # models API. Let's fix the problem.
                cls._set_sequence(cls.max())
                obj = cls()
                db.session.add(obj)
        return obj.recid

    @classmethod
    def max(cls):
        """Get max record identifier."""
        max_recid = db.session.query(func.max(cls.recid)).scalar()
        return max_recid if max_recid else 0

    @classmethod
    def _set_sequence(cls, val):
        """Internal function to reset sequence to specific value.

        Note: this function is for PostgreSQL compatibility.

        :param val: The value to be set.
        """
        if db.engine.dialect.name == 'postgresql':  # pragma: no cover
            db.session.execute(
                "SELECT setval(pg_get_serial_sequence("
                "'{0}', 'recid'), :newval)".format(cls.__tablename__),
                dict(newval=val))

    @classmethod
    def insert(cls, val):
        """Insert a record identifier.

        :param val: The `recid` column value to insert.
        """
        with db.session.begin_nested():
            obj = cls(recid=val)
            db.session.add(obj)
            cls._set_sequence(cls.max())
class Archive(db.Model, Timestamp):
    """Registers the status of a sip: archived or not.

    The status is a member of
    :py:class:`invenio_archivematica.models.ArchiveStatus`.

    A sip can have only one archive, and an archive applies to only
    one sip.
    """

    __tablename__ = 'archivematica_archive'
    __table_args__ = (db.Index('idx_ark_sip',
                               'sip_id'), db.Index('idx_ark_status', 'status'),
                      db.Index('idx_ark_accession_id', 'accession_id'))

    id = db.Column(db.BigInteger().with_variant(db.Integer, 'sqlite'),
                   primary_key=True)
    """ID of the Archive object."""

    sip_id = db.Column(UUIDType,
                       db.ForeignKey(SIP.id, name='fk_archivematica_sip_id'),
                       nullable=False)
    """SIP related with the Archive."""

    status = db.Column(ChoiceType(ArchiveStatus, impl=db.String(20)),
                       nullable=False)
    """Status of the archive."""

    accession_id = db.Column(db.String(255), nullable=True, unique=True)
    """Accessioned ID of the AIP in Archivematica."""

    archivematica_id = db.Column(UUIDType, nullable=True)
    """ID of the AIP in Archivematica."""

    # Relations
    sip = db.relationship(SIP)
    """Relationship with SIP."""

    #
    # Class methods
    #
    @classmethod
    def create(cls, sip, accession_id=None, archivematica_id=None):
        """Create a new Archive object and add it to the session.

        The new Archive object will have a NEW status

        :param sip: the sip attached to the archive
        :type sip: :py:class:`invenio_sipstore.models.SIP`
        :param str accession_id: the accession ID of the AIP
        :param str archivematica_id: The UUID of the AIP
        """
        ark = cls(sip=sip,
                  status=ArchiveStatus.NEW,
                  accession_id=accession_id,
                  archivematica_id=archivematica_id)
        db.session.add(ark)
        return ark

    @classmethod
    def get_from_sip(cls, uuid):
        """Return the Archive object associated to the given sip.

        It tries to get the Archive object associated to the sip. If it
        exists, it returns it, otherwise it returns None.

        :param str uuid: the uuid of the sip
        :rtype: :py:class:`invenio_archivematica.models.Archive` or None
        """
        return cls.query.filter_by(sip_id=uuid).one_or_none()

    @classmethod
    def get_from_accession_id(cls, accession_id):
        """Return the Archive object associated to the given accession_id.

        If the accession_id is not in the table, it returns None.

        :param str accession_id: the accession_id of the Archive object.
        :rtype: :py:class:`invenio_archivematica.models.Archive` or None
        """
        return cls.query.filter_by(accession_id=accession_id).one_or_none()