Example #1
0
def test_item_type_exist_name_and_organisation_pid(
        item_type_standard_martigny):
    """Test item type name uniqueness."""
    item_type = item_type_standard_martigny
    itty = item_type.replace_refs()
    assert ItemType.exist_name_and_organisation_pid(
        itty.get('name'), itty.get('organisation', {}).get('pid'))
    assert not ItemType.exist_name_and_organisation_pid(
        'not exists yet', itty.get('organisation', {}).get('pid'))
Example #2
0
def test_item_type_exist_name_and_organisation_pid(item_type):
    """."""
    itty = item_type.replace_refs()
    assert ItemType.exist_name_and_organisation_pid(
        itty.get('name'),
        itty.get('organisation', {}).get('pid'))
    assert not ItemType.exist_name_and_organisation_pid(
        'not exists yet',
        itty.get('organisation', {}).get('pid'))
Example #3
0
def test_item_type_es_mapping(es_clear, db, organisation, item_type_data_tmp):
    """."""
    search = ItemTypesSearch()
    mapping = get_mapping(search.Meta.index)
    assert mapping
    ItemType.create(item_type_data_tmp,
                    dbcommit=True,
                    reindex=True,
                    delete_pid=True)
    assert mapping == get_mapping(search.Meta.index)
def test_item_type_es_mapping(es_clear, db, org_martigny, item_type_data_tmp):
    """Test item type elasticsearch mapping."""
    search = ItemTypesSearch()
    mapping = get_mapping(search.Meta.index)
    assert mapping
    ItemType.create(item_type_data_tmp,
                    dbcommit=True,
                    reindex=True,
                    delete_pid=True)
    assert mapping == get_mapping(search.Meta.index)
Example #5
0
def test_item_type_create(db, item_type_data_tmp):
    """Test item type record creation."""
    itty = ItemType.create(item_type_data_tmp, delete_pid=True)
    assert itty == item_type_data_tmp
    assert itty.get('pid') == '1'

    itty = ItemType.get_record_by_pid('1')
    assert itty == item_type_data_tmp

    fetched_pid = item_type_id_fetcher(itty.id, itty)
    assert fetched_pid.pid_value == '1'
    assert fetched_pid.pid_type == 'itty'
    assert not ItemType.get_pid_by_name('no exists')
Example #6
0
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results.

        :param results: Elasticsearch search result.
        :param pid_fetcher: Persistent identifier fetcher.
        """
        records = results.get('hits', {}).get('hits', {})

        for record in records:
            metadata = record.get('metadata', {})
            document = search_document_by_pid(
                metadata.get('document').get('pid'))
            metadata['ui_title_text'] = title_format_text_head(
                document['title'], with_subtitle=True)

        # Add library name
        for lib_term in results.get('aggregations',
                                    {}).get('library', {}).get('buckets', []):
            lib = Library.get_record_by_pid(lib_term.get('key'))
            lib_term['name'] = lib.get('name')
        # Add location name
        for loc_term in results.get('aggregations',
                                    {}).get('location', {}).get('buckets', []):
            loc = Location.get_record_by_pid(loc_term.get('key'))
            loc_term['name'] = loc.get('name')

        # Add library name
        for item_type_term in results.get('aggregations',
                                          {}).get('item_type',
                                                  {}).get('buckets', []):
            item_type = ItemType.get_record_by_pid(item_type_term.get('key'))
            item_type_term['name'] = item_type.get('name')

        return super(ItemsJSONSerializer,
                     self).post_process_serialize_search(results, pid_fetcher)
Example #7
0
def item_type(app, organisation, item_type_data):
    """."""
    itty = ItemType.create(data=item_type_data,
                           delete_pid=False,
                           dbcommit=True,
                           reindex=True)
    flush_index(ItemTypesSearch.Meta.index)
    return itty
Example #8
0
def item_type_online_sion(app, org_sion, item_type_online_sion_data):
    """Create particular item type of sion."""
    itty = ItemType.create(data=item_type_online_sion_data,
                           delete_pid=False,
                           dbcommit=True,
                           reindex=True)
    flush_index(ItemTypesSearch.Meta.index)
    return itty
Example #9
0
def item_type_missing_martigny(app, org_martigny,
                               item_type_missing_martigny_data):
    """Create missing item type of martigny."""
    itty = ItemType.create(data=item_type_missing_martigny_data,
                           delete_pid=False,
                           dbcommit=True,
                           reindex=True)
    flush_index(ItemTypesSearch.Meta.index)
    return itty
def test_item_type_create(db, item_type_data_tmp, org_martigny,
                          item_type_online_martigny):
    """Test item type record creation."""
    item_type_data_tmp['type'] = 'online'
    with pytest.raises(RecordValidationError):
        itty = ItemType.create(item_type_data_tmp, delete_pid=True)

    item_type_data_tmp['type'] = 'standard'
    itty = ItemType.create(item_type_data_tmp, delete_pid=True)

    assert itty == item_type_data_tmp
    assert itty.get('pid') == '2'

    itty = ItemType.get_record_by_pid('2')
    assert itty == item_type_data_tmp

    fetched_pid = item_type_id_fetcher(itty.id, itty)
    assert fetched_pid.pid_value == '2'
    assert fetched_pid.pid_type == 'itty'
    assert not ItemType.get_pid_by_name('no exists')
Example #11
0
def test_item_type_create(db, item_type_data_tmp, org_martigny,
                          item_type_online_martigny):
    """Test item type record creation."""
    item_type_data_tmp['type'] = 'online'
    with pytest.raises(ValidationError):
        itty = ItemType.create(item_type_data_tmp, delete_pid=True)

    db.session.rollback()

    next_pid = ItemType.provider.identifier.next()
    item_type_data_tmp['type'] = 'standard'
    itty = ItemType.create(item_type_data_tmp, delete_pid=True)
    next_pid += 1

    assert itty == item_type_data_tmp
    assert itty.get('pid') == str(next_pid)

    itty = ItemType.get_record_by_pid(str(next_pid))
    assert itty == item_type_data_tmp

    fetched_pid = item_type_id_fetcher(itty.id, itty)
    assert fetched_pid.pid_value == str(next_pid)
    assert fetched_pid.pid_type == 'itty'
    assert not ItemType.get_pid_by_name('no exists')
Example #12
0
def _build_notification_email_context(loan, item, location):
    """Build the context used by the send_notification_to_location function.

    :param loan : the loan for which build context
    :param item : the item for which build context
    :param location : the item location
    """
    document_pid = Item.get_document_pid_by_item_pid(loan.item_pid)
    document = Document.get_record_by_pid(document_pid)
    pickup_location = Location.get_record_by_pid(
        loan.get('pickup_location_pid'))
    patron = Patron.get_record_by_pid(loan.patron_pid)

    # inherit holdings call number when possible
    issue_call_number = item.issue_inherited_first_call_number
    if issue_call_number:
        item['call_number'] = issue_call_number

    ctx = {
        'loan': loan.replace_refs().dumps(),
        'item': item.replace_refs().dumps(),
        'document': document.replace_refs().dumps(),
        'pickup_location': pickup_location,
        'item_location': location.dumps(),
        'patron': patron
    }
    library = pickup_location.get_library()
    ctx['pickup_location']['library'] = library
    ctx['item']['item_type'] = \
        ItemType.get_record_by_pid(item.item_type_circulation_category_pid)
    titles = [
        title for title in ctx['document'].get('title', [])
        if title['type'] == 'bf:Title'
    ]
    ctx['document']['title_text'] = \
        next(iter(titles or []), {}).get('_text')
    responsibility_statement = create_title_responsibilites(
        document.get('responsibilityStatement', []))
    ctx['document']['responsibility_statement'] = \
        next(iter(responsibility_statement or []), '')
    trans_date = ciso8601.parse_datetime(loan.get('transaction_date'))
    trans_date = trans_date\
        .replace(tzinfo=timezone.utc)\
        .astimezone(tz=library.get_timezone())
    ctx['loan']['transaction_date'] = \
        trans_date.strftime("%d.%m.%Y - %H:%M:%S")
    return ctx
Example #13
0
def negative_availability_changes(sender, record=None, *args, **kwargs):
    """Reindex related items if negative availability changes."""
    if isinstance(record, ItemType):
        ori_record = ItemType.get_record_by_pid(record.pid)
        record_availability = record.get('negative_availability', False)
        original_availability = ori_record.get('negative_availability', False)
        if record_availability != original_availability:
            # get all item uuid's related to the item type and mark them for
            # reindex into a asynchronous celery queue.
            item_uuids = []
            search = ItemsSearch()\
                .filter('bool', should=[
                    Q('match', item_type__pid=record.pid),
                    Q('match', temporary_item_type__pid=record.pid)
                ]) \
                .source().scan()
            for hit in search:
                item_uuids.append(hit.meta.id)
            ItemTypesIndexer().bulk_index(item_uuids)
            process_bulk_queue.apply_async()
Example #14
0
def test_item_type_can_delete(app, item_type_data_tmp):
    """Test item type can delete"""
    itty = ItemType.create(item_type_data_tmp, delete_pid=True)
    can, reasons = itty.can_delete
    assert can
    assert reasons == {}
Example #15
0
def test_item_type_can_delete(app, item_type_data_tmp):
    """Test item type can delete"""
    itty = ItemType.create(item_type_data_tmp, delete_pid=True)
    assert itty.get_links_to_me() == {}
    assert itty.can_delete
Example #16
0
def test_item_type_get_pid_by_name(item_type_standard_martigny):
    """Test item type retrieval by name."""
    assert not ItemType.get_pid_by_name('no exists')
    assert ItemType.get_pid_by_name('standard') == 'itty1'
Example #17
0
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results.

        :param results: Elasticsearch search result.
        :param pid_fetcher: Persistent identifier fetcher.
        """
        records = results.get('hits', {}).get('hits', {})
        orgs = {}
        libs = {}
        locs = {}
        for record in records:
            metadata = record.get('metadata', {})
            document = search_document_by_pid(
                metadata.get('document').get('pid')
            )
            metadata['ui_title_text'] = title_format_text_head(
                document['title'],
                with_subtitle=True
            )

            item = Item.get_record_by_pid(metadata.get('pid'))
            metadata['availability'] = {
                'available': item.available,
                'status': metadata['status'],
                'display_text': item.availability_text,
                'request': item.number_of_requests()
            }
            if not metadata['available']:
                if metadata['status'] == ItemStatus.ON_LOAN:
                    metadata['availability']['due_date'] =\
                        item.get_item_end_date(format='long', language='en')

            # Item in collection
            collection = item.in_collection()
            if collection:
                metadata['in_collection'] = collection
            # Organisation
            organisation = metadata['organisation']
            if organisation['pid'] not in orgs:
                orgs[organisation['pid']] = Organisation \
                    .get_record_by_pid(organisation['pid'])
            organisation['viewcode'] = orgs[organisation['pid']].get('code')
            # Library
            library = metadata['library']
            if library['pid'] not in libs:
                libs[library['pid']] = Library \
                    .get_record_by_pid(library['pid'])
            library['name'] = libs[library['pid']].get('name')
            # Location
            location = metadata['location']
            if location['pid'] not in locs:
                locs[location['pid']] = Location \
                    .get_record_by_pid(location['pid'])
            location['name'] = locs[location['pid']].get('name')

        # Add library name
        for lib_term in results.get('aggregations', {}).get(
                'library', {}).get('buckets', []):
            lib = Library.get_record_by_pid(lib_term.get('key'))
            lib_term['name'] = lib.get('name')
        # Add location name
        for loc_term in results.get('aggregations', {}).get(
                'location', {}).get('buckets', []):
            loc = Location.get_record_by_pid(loc_term.get('key'))
            loc_term['name'] = loc.get('name')

        # Add item type name
        for item_type_term in results.get('aggregations', {}).get(
                'item_type', {}).get('buckets', []):
            item_type = ItemType.get_record_by_pid(item_type_term.get('key'))
            item_type_term['name'] = item_type.get('name')

        # Add vendor name
        for vendor_term in results.get('aggregations', {}).get(
                'vendor', {}).get('buckets', []):
            vendor = Vendor.get_record_by_pid(vendor_term.get('key'))
            vendor_term['name'] = vendor.get('name')

        # Correct document type buckets
        buckets = results['aggregations']['document_type']['buckets']
        results['aggregations']['document_type']['buckets'] = \
            filter_document_type_buckets(buckets)

        return super().post_process_serialize_search(results, pid_fetcher)

        # Correct document type buckets
        buckets = results['aggregations']['document_type']['buckets']
        results['aggregations']['document_type']['buckets'] = \
            filter_document_type_buckets(buckets)
Example #18
0
def test_item_type_get_pid_by_name(item_type):
    """."""
    assert not ItemType.get_pid_by_name('no exists')
    assert ItemType.get_pid_by_name('standard') == 'itty1'