def test_recordid_provider_v2(app, db):
    """Test RecordIdProviderV2."""
    with app.app_context():
        provider = RecordIdProviderV2.create()
        assert provider.pid
        assert provider.pid.pid_type == 'recid'
        pid_value = provider.pid.pid_value
        part1, part2 = pid_value.split('-')
        assert len(part1) == 5
        assert len(part2) == 5
        assert provider.pid.pid_provider is None
        assert provider.pid.status == PIDStatus.RESERVED
        assert provider.pid.object_type is None
        assert provider.pid.object_uuid is None

        # Assign to object immediately
        rec_uuid = uuid.uuid4()
        provider = RecordIdProviderV2.create(object_type='rec',
                                             object_uuid=rec_uuid)
        assert provider.pid
        assert provider.pid.pid_type == 'recid'
        pid_value = provider.pid.pid_value
        part1, part2 = pid_value.split('-')
        assert len(part1) == 5
        assert len(part2) == 5
        assert provider.pid.pid_provider is None
        assert provider.pid.status == PIDStatus.REGISTERED
        assert provider.pid.object_type == 'rec'
        assert provider.pid.object_uuid == rec_uuid

        pytest.raises(AssertionError, RecordIdProviderV2.create, pid_value='3')

        # Options
        provider = RecordIdProviderV2.create(options={
            'length': 3,
            'checksum': True,
            'split_every': 1
        })
        pid_value = provider.pid.pid_value
        part1, part2, part3 = pid_value.split('-')
        assert len(part1) == 1
        assert len(part2) == 1
        assert len(part3) == 1
Exemplo n.º 2
0
def location():
    """Create library location."""
    click.echo("Creating locations...")
    location = {
        "pid": RecordIdProviderV2.create().pid.pid_value,
        "name": "CERN Central Library",
        "address": "Rue de Meyrin",
        "email": "*****@*****.**"
    }
    record = Location.create(location)
    minter(LOCATION_PID_TYPE, "pid", record)
    record.commit()
    db.session.commit()
    indexer = LocationIndexer()
    indexer.index(record)
Exemplo n.º 3
0
def test_create_no_provider(base_app, db):
    """Test creation without a provider."""
    class Record(RecordBase):
        model_cls = RecordMetadata
        pid = PIDField()

    record = Record.create({})
    assert record.pid is None

    record.pid = RecordIdProviderV2.create(object_type='rec',
                                           object_uuid=record.id).pid

    assert record['id'] == record.pid.pid_value
    assert record['pid']['pk'] == record.pid.id
    assert record['pid']['status'] == record.pid.status
    assert record['pid']['obj_type'] == record.pid.object_type
    assert record['pid']['pid_type'] == record.pid.pid_type
    assert record.id == record.pid.object_uuid
Exemplo n.º 4
0
def location():
    """Create library location."""
    click.echo("Creating locations...")
    weekdays = [
        "monday",
        "tuesday",
        "wednesday",
        "thursday",
        "friday",
        "saturday",
        "sunday",
    ]
    closed = ["saturday", "sunday"]
    times = [
        {
            "start_time": "08:30",
            "end_time": "18:00"
        },
    ]
    opening_weekdays = []
    for weekday in weekdays:
        is_open = weekday not in closed
        opening_weekdays.append({
            "weekday": weekday,
            "is_open": weekday not in closed,
            **({
                "times": times
            } if is_open else {}),
        })
    location = {
        "pid": RecordIdProviderV2.create().pid.pid_value,
        "name": "CERN Central Library",
        "address": "Rue de Meyrin",
        "email": "*****@*****.**",
        "opening_weekdays": opening_weekdays,
        "opening_exceptions": [],
    }
    record = current_app_ils.location_record_cls.create(location)
    minter(LOCATION_PID_TYPE, "pid", record)
    db.session.commit()
    current_app_ils.location_indexer.index(record)
Exemplo n.º 5
0
 def create_pid(self):
     """Create a new persistent identifier."""
     return RecordIdProviderV2.create().pid.pid_value
Exemplo n.º 6
0
def create_loan(user_email, is_past_loan):
    """Create a loan."""
    # hardcode doc/item pids from the demo_data jsons
    ongoing_loan_item_pid = "vgrh9-jvj8E"
    past_loan_item_pid = "678e3-an678A"
    ongoing_loan_doc_pid = "67186-5rs9E"
    past_loan_doc_pid = "qaywb-gfe4B"

    active_loan = (get_active_loan_by_item_pid({
        "type": "pitmid",
        "value": ongoing_loan_item_pid
    }).execute().hits)

    lt_es7 = ES_VERSION[0] < 7
    total = active_loan.total if lt_es7 else active_loan.total.value

    if total > 0 and not is_past_loan:
        click.secho(
            "Item for ongoing loan is already loaned by patron with email {0}."
            .format(active_loan[0].patron.email),
            fg="red",
        )
        return

    patron = User.query.filter_by(email=user_email).one()
    patron_pid = patron.get_id()

    loc_pid, _ = current_app_ils.get_default_location_pid

    delivery = list(
        current_app.config["ILS_CIRCULATION_DELIVERY_METHODS"].keys())[randint(
            0, 1)]

    loan_dict = {
        "pid": RecordIdProviderV2.create().pid.pid_value,
        "patron_pid": "{}".format(patron_pid),
        "pickup_location_pid": "{}".format(loc_pid),
        "transaction_location_pid": "{}".format(loc_pid),
        "transaction_user_pid": "{}".format(patron_pid),
        "delivery": {
            "method": delivery
        },
    }

    if is_past_loan:
        loan_dict["state"] = "ITEM_RETURNED"
        loan_dict["document_pid"] = past_loan_doc_pid
        transaction_date = start_date = arrow.utcnow() - timedelta(days=365)
        end_date = start_date + timedelta(weeks=4)
        item_pid = past_loan_item_pid
    else:
        loan_dict["state"] = "ITEM_ON_LOAN"
        loan_dict["document_pid"] = ongoing_loan_doc_pid
        transaction_date = start_date = arrow.utcnow(
        ) - (timedelta(weeks=4) - timedelta(
            days=current_app.config["ILS_CIRCULATION_LOAN_WILL_EXPIRE_DAYS"]))
        end_date = start_date + timedelta(weeks=4)
        item_pid = ongoing_loan_item_pid

    loan_dict["transaction_date"] = transaction_date.isoformat()
    loan_dict["start_date"] = start_date.date().isoformat()
    loan_dict["end_date"] = end_date.date().isoformat()
    loan_dict["extension_count"] = randint(0, 3)
    loan_dict["item_pid"] = {"type": ITEM_PID_TYPE, "value": item_pid}

    loan = current_circulation.loan_record_cls.create(loan_dict)
    minter(CIRCULATION_LOAN_PID_TYPE, "pid", loan)
    db.session.commit()
    current_circulation.loan_indexer().index(loan)
    item = current_app_ils.item_record_cls.get_record_by_pid(item_pid)
    current_app_ils.item_indexer.index(item)

    current_search.flush_and_refresh(index="*")

    doc = current_app_ils.document_record_cls.get_record_by_pid(
        loan_dict["document_pid"])

    click.secho(
        "Loan with pid '{0}' on document '{1}' was created.".format(
            loan_dict["pid"], doc["title"]),
        fg="blue",
    )