Пример #1
0
    def test_limit(self):
        for x in range(4):
            self._identifier()

        qu = self._db.query(Identifier)
        assert qu.count() == fast_query_count(qu)

        qu2 = qu.limit(2)
        assert qu2.count() == fast_query_count(qu2)

        qu3 = qu.limit(6)
        assert qu3.count() == fast_query_count(qu3)
Пример #2
0
    def generate_edition(self, identifier):
        """Utilizes an ISBN's equivalent identifiers (OCLC Number or Work IDs)
        to set an appropriate LicensePool presentation edition so a Work can
        later be created.
        """
        equivalent_ids = identifier.equivalent_identifier_ids()[identifier.id]

        # Get the editions of equivalent identifiers (OCLC Number or Work IDs)
        # to set as a presentation edition. These editions can be lower quality,
        # and it's important that they have a title.
        titled_equivalent_editions = self._db.query(Edition).\
            join(Edition.primary_identifier).\
            filter(Identifier.id.in_(equivalent_ids)).\
            filter(Edition.title!=None)

        # It's preferable that they have an author, too.
        authored_equivalent_editions = titled_equivalent_editions.filter(
            Edition.author != None, Edition.author != Edition.UNKNOWN_AUTHOR)

        if fast_query_count(authored_equivalent_editions):
            # Prioritize editions with both a title and an author if available.
            equivalent_editions = authored_equivalent_editions.all()
        else:
            equivalent_editions = titled_equivalent_editions.all()

        if equivalent_editions:
            # Set the presentation edition.
            pool = identifier.licensed_through[0]
            pool.set_presentation_edition(
                equivalent_editions=equivalent_editions)
    def generate_edition(self, identifier):
        """Utilizes an ISBN's equivalent identifiers (OCLC Number or Work IDs)
        to set an appropriate LicensePool presentation edition so a Work can
        later be created.
        """
        equivalent_ids = identifier.equivalent_identifier_ids()[identifier.id]

        # Get the editions of equivalent identifiers (OCLC Number or Work IDs)
        # to set as a presentation edition. These editions can be lower quality,
        # and it's important that they have a title.
        titled_equivalent_editions = self._db.query(Edition).\
            join(Edition.primary_identifier).\
            filter(Identifier.id.in_(equivalent_ids)).\
            filter(Edition.title!=None)

        # It's preferable that they have an author, too.
        authored_equivalent_editions = titled_equivalent_editions.filter(
            Edition.author!=None, Edition.author!=Edition.UNKNOWN_AUTHOR
        )

        if fast_query_count(authored_equivalent_editions):
            # Prioritize editions with both a title and an author if available.
            equivalent_editions = authored_equivalent_editions.all()
        else:
            equivalent_editions = titled_equivalent_editions.all()

        if equivalent_editions:
            # Set the presentation edition.
            pool = identifier.licensed_through[0]
            pool.set_presentation_edition(
                equivalent_editions=equivalent_editions
            )
Пример #4
0
    def test_distinct(self):
        e1 = self._edition(title="The title", authors="Author 1")
        e2 = self._edition(title="The title", authors="Author 1")
        e3 = self._edition(title="The title", authors="Author 2")
        e4 = self._edition(title="Another title", authors="Author 1")

        # Without the distinct clause, a query against Edition will
        # return four editions.
        qu = self._db.query(Edition)
        assert qu.count() == fast_query_count(qu)

        # If made distinct on Edition.author, the query will return only
        # two editions.
        qu2 = qu.distinct(Edition.author)
        assert qu2.count() == fast_query_count(qu2)

        # If made distinct on Edition.title _and_ Edition.author,
        # the query will return three editions.
        qu3 = qu.distinct(Edition.title, Edition.author)
        assert qu3.count() == fast_query_count(qu3)
    def add_pagination_links_to_feed(cls, pagination, query, feed, endpoint,
        collection, **url_param_kwargs
    ):
        """Adds links for pagination to a given collection's feed."""
        def href_for(page):
            return cls.collection_feed_url(
                endpoint, collection, page=page, **url_param_kwargs
            )

        if fast_query_count(query) > (pagination.size + pagination.offset):
            feed.add_link_to_feed(
                feed.feed, rel="next", href=href_for(pagination.next_page)
            )

        if pagination.offset > 0:
            feed.add_link_to_feed(
                feed.feed, rel="first", href=href_for(pagination.first_page)
            )

        previous_page = pagination.previous_page
        if previous_page:
            feed.add_link_to_feed(
                feed.feed, rel="previous", href=href_for(previous_page)
            )
Пример #6
0
    def add_pagination_links_to_feed(cls, pagination, query, feed, endpoint,
                                     collection, **url_param_kwargs):
        """Adds links for pagination to a given collection's feed."""
        def href_for(page):
            return cls.collection_feed_url(endpoint,
                                           collection,
                                           page=page,
                                           **url_param_kwargs)

        if fast_query_count(query) > (pagination.size + pagination.offset):
            feed.add_link_to_feed(feed.feed,
                                  rel="next",
                                  href=href_for(pagination.next_page))

        if pagination.offset > 0:
            feed.add_link_to_feed(feed.feed,
                                  rel="first",
                                  href=href_for(pagination.first_page))

        previous_page = pagination.previous_page
        if previous_page:
            feed.add_link_to_feed(feed.feed,
                                  rel="previous",
                                  href=href_for(previous_page))
    single_collection_sources = [
        DataSource.PLYMPTON, DataSource.ELIB, DataSource.UNGLUE_IT,
        DataSource.STANDARD_EBOOKS, DataSource.GUTENBERG
    ]
    for data_source_name in single_collection_sources:
        # Get the Collection.
        collection = Collection.by_datasource(_db, data_source_name).one()

        # Find LicensePools with the matching DataSource.
        source = DataSource.lookup(_db, data_source_name)
        qu = base_query.filter(LicensePool.data_source==source)
        qu.update({LicensePool.collection_id : collection.id})

        logging.info('UPDATED: %d LicensePools given Collection %r' % (
            int(fast_query_count(qu)), collection))
        _db.commit()

    # Now update the FeedBooks LicensePools, which have to take language
    # into account.
    feedbooks = DataSource.lookup(_db, DataSource.FEEDBOOKS)
    base_query = _db.query(LicensePool.id)\
        .filter(LicensePool.data_source==feedbooks)\
        .join(LicensePool.presentation_edition)

    for lang in ['en', 'es', 'fr', 'it', 'de']:
        # Get the Collection for each language.
        language = LanguageCodes.english_names.get(lang)[0]
        name = FeedbooksOPDSImporter.BASE_COLLECTION_NAME + language
        collection, ignore = Collection.by_name_and_protocol(
            _db, name, ExternalIntegration.OPDS_IMPORT
log = logging.getLogger('Migration [20170714]')

def log_change(count, collection):
    log.info('UPDATED: %d LicensePools given Collection %r' % (
        int(count), collection))

try:
    collections = _db.query(Collection).all()
    collections_by_data_source = dict([(collection.data_source, collection) for collection in collections])

    base_query = _db.query(LicensePool).filter(LicensePool.collection_id==None)
    for data_source, collection in collections_by_data_source.items():
        # Find LicensePools with the matching DataSource.
        qu = base_query.filter(LicensePool.data_source==data_source)
        qu.update({LicensePool.collection_id : collection.id})
        log_change(fast_query_count(qu), collection)
        _db.commit()

    # Some LicensePools may be associated with the a duplicate or
    # outdated Bibliotheca DataSource. Find them.
    bibliotheca = DataSource.lookup(_db, DataSource.BIBLIOTHECA)
    old_sources = _db.query(DataSource.id).filter(
        DataSource.name.in_([u'3M', u'Bibliotecha'])).subquery()
    threem_qu = base_query.filter(LicensePool.data_source_id.in_(old_sources))

    # Associate these LicensePools with the Bibliotheca Collection.
    bibliotheca_collection = collections_by_data_source.get(bibliotheca)
    if bibliotheca_collection:
        result = threem_qu.update(
            {LicensePool.collection_id : bibliotheca_collection.id},
            synchronize_session='fetch'
Пример #9
0
    log.info('UPDATED: %d LicensePools given Collection %r' %
             (int(count), collection))


try:
    collections = _db.query(Collection).all()
    collections_by_data_source = dict([(collection.data_source, collection)
                                       for collection in collections])

    base_query = _db.query(LicensePool).filter(
        LicensePool.collection_id == None)
    for data_source, collection in collections_by_data_source.items():
        # Find LicensePools with the matching DataSource.
        qu = base_query.filter(LicensePool.data_source == data_source)
        qu.update({LicensePool.collection_id: collection.id})
        log_change(fast_query_count(qu), collection)
        _db.commit()

    # Some LicensePools may be associated with the a duplicate or
    # outdated Bibliotheca DataSource. Find them.
    bibliotheca = DataSource.lookup(_db, DataSource.BIBLIOTHECA)
    old_sources = _db.query(DataSource.id).filter(
        DataSource.name.in_([u'3M', u'Bibliotecha'])).subquery()
    threem_qu = base_query.filter(LicensePool.data_source_id.in_(old_sources))

    # Associate these LicensePools with the Bibliotheca Collection.
    bibliotheca_collection = collections_by_data_source.get(bibliotheca)
    if bibliotheca_collection:
        result = threem_qu.update(
            {LicensePool.collection_id: bibliotheca_collection.id},
            synchronize_session='fetch')
Пример #10
0
 def test_no_distinct(self):
     identifier = self._identifier()
     qu = self._db.query(Identifier)
     assert 1 == fast_query_count(qu)