zenodo_accessrequests/views/requests.py
def access_request(pid, record, template, **kwargs):
    """Create an access request."""
    recid = int(pid.pid_value)
    datastore = LocalProxy(
        lambda: current_app.extensions['security'].datastore)

    # Record must be in restricted access mode.
    if record.get('access_right') != 'restricted' or \
       not record.get('access_conditions'):
        abort(404)

    # Record must have an owner and owner must still exists.
    owners = record.get('owners', [])
    record_owners = [datastore.find_user(id=owner_id) for owner_id in owners]
    if not record_owners:
        abort(404)

    sender = None
    initialdata = dict()

    # Prepare initial form data
    if current_user.is_authenticated:
        sender = current_user
        initialdata['email'] = current_user.email
        if current_user.profile:
            initialdata['full_name'] = current_user.profile.full_name

    # Normal form validation
    form = AccessRequestForm(formdata=request.form, **initialdata)

    if form.validate_on_submit():
        accreq = AccessRequest.create(
            recid=recid,
            receiver=record_owners[0],
            sender_full_name=form.data['full_name'],
            sender_email=form.data['email'],
            justification=form.data['justification'],
            sender=sender
        )
        db.session.commit()

        if accreq.status == RequestStatus.EMAIL_VALIDATION:
            flash(_(
                "Email confirmation needed: We have sent you an email to "
                "verify your address. Please check the email and follow the "
                "instructions to complete the access request."),
                category='info')
        else:
            flash(_("Access request submitted."), category='info')
        return redirect(url_for('invenio_records_ui.recid', pid_value=recid))

    return render_template(
        template,
        pid=pid,
        record=record,
        form=form,
        owners=record_owners,
    )
def access_request(pid, record, template, **kwargs):
    """Create an access request."""
    recid = int(pid.pid_value)
    datastore = LocalProxy(
        lambda: current_app.extensions['security'].datastore)

    # Record must be in restricted access mode.
    if record.get('access_right') != 'restricted' or \
       not record.get('access_conditions'):
        abort(404)

    # Record must have an owner and owner must still exists.
    owners = record.get('owners', [])
    record_owners = [datastore.find_user(id=owner_id) for owner_id in owners]
    if not record_owners:
        abort(404)

    sender = None
    initialdata = dict()

    # Prepare initial form data
    if current_user.is_authenticated:
        sender = current_user
        initialdata['email'] = current_user.email
        if current_user.profile:
            initialdata['full_name'] = current_user.profile.full_name

    # Normal form validation
    form = AccessRequestForm(formdata=request.form, **initialdata)

    if form.validate_on_submit():
        accreq = AccessRequest.create(
            recid=recid,
            receiver=record_owners[0],
            sender_full_name=form.data['full_name'],
            sender_email=form.data['email'],
            justification=form.data['justification'],
            sender=sender
        )
        db.session.commit()

        if accreq.status == RequestStatus.EMAIL_VALIDATION:
            flash(_(
                "Email confirmation needed: We have sent you an email to "
                "verify your address. Please check the email and follow the "
                "instructions to complete the access request."),
                category='info')
        else:
            flash(_("Access request submitted."), category='info')
        return redirect(url_for('invenio_records_ui.recid', pid_value=recid))

    return render_template(
        template,
        pid=pid,
        record=record,
        form=form,
        owners=record_owners,
    )
Create an access request.