Ejemplo n.º 1
0
    def collect_event(self,
                      library,
                      license_pool,
                      event_type,
                      time,
                      old_value=None,
                      new_value=None,
                      **kwargs):
        if not library and not license_pool:
            raise ValueError(
                "Either library or license_pool must be provided.")
        if library:
            _db = Session.object_session(library)
        else:
            _db = Session.object_session(license_pool)
        if library and self.library_id and library.id != self.library_id:
            return

        neighborhood = None
        if self.location_source == self.LOCATION_SOURCE_NEIGHBORHOOD:
            neighborhood = kwargs.pop("neighborhood", None)

        return CirculationEvent.log(_db,
                                    license_pool,
                                    event_type,
                                    old_value,
                                    new_value,
                                    start=time,
                                    library=library,
                                    location=neighborhood)
Ejemplo n.º 2
0
    def create_record(cls,
                      work,
                      annotator,
                      force_create=False,
                      integration=None):
        """Build a complete MARC record for a given work."""
        if callable(annotator):
            annotator = annotator()

        if isinstance(work, BaseMaterializedWork):
            pool = work.license_pool
        else:
            pool = work.active_license_pool()
        if not pool:
            return None

        edition = pool.presentation_edition
        identifier = pool.identifier

        _db = Session.object_session(work)

        record = None
        existing_record = getattr(work, annotator.marc_cache_field)
        if existing_record and not force_create:
            record = Record(data=existing_record.encode('utf-8'),
                            force_utf8=True)

        if not record:
            record = Record(leader=annotator.leader(work), force_utf8=True)
            annotator.add_control_fields(record, identifier, pool, edition)
            annotator.add_isbn(record, identifier)

            # TODO: The 240 and 130 fields are for translated works, so they can be grouped even
            # though they have different titles. We do not group editions of the same work in
            # different languages, so we can't use those yet.

            annotator.add_title(record, edition)
            annotator.add_contributors(record, edition)
            annotator.add_publisher(record, edition)
            annotator.add_physical_description(record, edition)
            annotator.add_audience(record, work)
            annotator.add_series(record, edition)
            annotator.add_system_details(record)
            annotator.add_ebooks_subject(record)

            data = record.as_marc()
            if isinstance(work, BaseMaterializedWork):
                setattr(pool.work, annotator.marc_cache_field, data)
            else:
                setattr(work, annotator.marc_cache_field, data)

        # Add additional fields that should not be cached.
        annotator.annotate_work_record(work, pool, edition, identifier, record,
                                       integration)

        return record
Ejemplo n.º 3
0
 def from_config(cls, library):
     _db = Session.object_session(library)
     integration = ExternalIntegration.lookup(
         _db,
         ExternalIntegration.MARC_EXPORT,
         ExternalIntegration.CATALOG_GOAL,
         library=library)
     if not integration:
         raise CannotLoadConfiguration(
             "No MARC export service is configured for this library")
     return cls(_db, library, integration)
Ejemplo n.º 4
0
 def from_config(cls, library):
     _db = Session.object_session(library)
     integration = ExternalIntegration.lookup(
         _db, ExternalIntegration.MARC_EXPORT,
         ExternalIntegration.CATALOG_GOAL, library=library
     )
     if not integration:
         raise CannotLoadConfiguration(
             "No MARC export service is configured for this library"
         )
     return cls(_db, library, integration)
Ejemplo n.º 5
0
    def create_record(cls, work, annotator, force_create=False, integration=None):
        """Build a complete MARC record for a given work."""
        if callable(annotator):
            annotator = annotator()

        if isinstance(work, BaseMaterializedWork):
            pool = work.license_pool
        else:
            pool = work.active_license_pool()
        if not pool:
            return None

        edition = pool.presentation_edition
        identifier = pool.identifier

        _db = Session.object_session(work)

        record = None
        existing_record = getattr(work, annotator.marc_cache_field)
        if existing_record and not force_create:
            record = Record(data=existing_record.encode('utf-8'), force_utf8=True)

        if not record:
            record = Record(leader=annotator.leader(work), force_utf8=True)
            annotator.add_control_fields(record, identifier, pool, edition)
            annotator.add_isbn(record, identifier)

            # TODO: The 240 and 130 fields are for translated works, so they can be grouped even
            # though they have different titles. We do not group editions of the same work in
            # different languages, so we can't use those yet.

            annotator.add_title(record, edition)
            annotator.add_contributors(record, edition)
            annotator.add_publisher(record, edition)
            annotator.add_physical_description(record, edition)
            annotator.add_audience(record, work)
            annotator.add_series(record, edition)
            annotator.add_system_details(record)
            annotator.add_ebooks_subject(record)

            data = record.as_marc()
            if isinstance(work, BaseMaterializedWork):
                setattr(pool.work, annotator.marc_cache_field, data)
            else:
                setattr(work, annotator.marc_cache_field, data)

        # Add additional fields that should not be cached.
        annotator.annotate_work_record(work, pool, edition, identifier, record, integration)

        return record
    def collect_event(self,
                      library,
                      license_pool,
                      event_type,
                      time,
                      old_value=None,
                      new_value=None,
                      **kwargs):
        if not library and not license_pool:
            raise ValueError(
                "Either library or license_pool must be provided.")
        if library:
            _db = Session.object_session(library)
        else:
            _db = Session.object_session(license_pool)
        if library and self.library_id and library.id != self.library_id:
            return

        CirculationEvent.log(_db,
                             license_pool,
                             event_type,
                             old_value,
                             new_value,
                             start=time)
Ejemplo n.º 7
0
    def for_collection(cls, collection, purpose):
        """Create a MirrorUploader for the given Collection.

        :param collection: Use the mirror configuration for this Collection.
        :param purpose: Use the purpose of the mirror configuration.

        :return: A MirrorUploader, or None if the Collection has no
            mirror integration.
        """
        from model import ExternalIntegration
        try:
            from model import Session
            _db = Session.object_session(collection)
            integration = ExternalIntegration.for_collection_and_purpose(_db, collection, purpose)
        except CannotLoadConfiguration, e:
            return None
Ejemplo n.º 8
0
    def collect_event(self,
                      library,
                      license_pool,
                      event_type,
                      time,
                      old_value=None,
                      new_value=None,
                      **kwargs):
        _db = Session.object_session(library)

        CirculationEvent.log(_db,
                             license_pool,
                             event_type,
                             old_value,
                             new_value,
                             start=time)
Ejemplo n.º 9
0
 def add_isbn(cls, record, identifier):
     # Add the ISBN if we have one.
     isbn = None
     if identifier.type == Identifier.ISBN:
         isbn = identifier
     if not isbn:
         _db = Session.object_session(identifier)
         identifier_ids = identifier.equivalent_identifier_ids()[identifier.id]
         isbn = _db.query(Identifier).filter(
             Identifier.type==Identifier.ISBN).filter(
             Identifier.id.in_(identifier_ids)).order_by(
             Identifier.id).first()
     if isbn:
         record.add_field(
             Field(
                 tag="020",
                 indicators=[" "," "],
                 subfields=[
                     "a", isbn.identifier,
                 ]))
Ejemplo n.º 10
0
 def add_isbn(cls, record, identifier):
     # Add the ISBN if we have one.
     isbn = None
     if identifier.type == Identifier.ISBN:
         isbn = identifier
     if not isbn:
         _db = Session.object_session(identifier)
         identifier_ids = identifier.equivalent_identifier_ids()[
             identifier.id]
         isbn = _db.query(Identifier).filter(
             Identifier.type == Identifier.ISBN).filter(
                 Identifier.id.in_(identifier_ids)).order_by(
                     Identifier.id).first()
     if isbn:
         record.add_field(
             Field(tag="020",
                   indicators=[" ", " "],
                   subfields=[
                       "a",
                       isbn.identifier,
                   ]))
Ejemplo n.º 11
0
    def for_collection(cls, collection, use_sitewide=False):
        """Create a MirrorUploader for the given Collection.

        :param collection: Use the mirror configuration for this Collection.

        :param use_sitewide: If there's no mirror for this specific Collection,
            should we return a sitewide mirror instead?

        :return: A MirrorUploader, or None if the Collection has no
            mirror integration.
        """
        integration = collection.mirror_integration
        if not integration:
            if use_sitewide:
                try:
                    from model import Session
                    _db = Session.object_session(collection)
                    return cls.sitewide(_db)
                except CannotLoadConfiguration, e:
                    return None
            else:
                return None
Ejemplo n.º 12
0
    def for_collection(cls, collection, use_sitewide=False):
        """Create a MirrorUploader for the given Collection.

        :param collection: Use the mirror configuration for this Collection.

        :param use_sitewide: If there's no mirror for this specific Collection,
            should we return a sitewide mirror instead?

        :return: A MirrorUploader, or None if the Collection has no
            mirror integration.
        """
        integration = collection.mirror_integration
        if not integration:
            if use_sitewide:
                try:
                    from model import Session
                    _db = Session.object_session(collection)
                    return cls.sitewide(_db)
                except CannotLoadConfiguration, e:
                    return None
            else:
                return None