Skip to content

Commit

Permalink
users: make user profiles read-only
Browse files Browse the repository at this point in the history
Co-Authored-by: Lauren-D <[email protected]>
  • Loading branch information
lauren-d authored and rerowep committed Jun 1, 2023
1 parent 9339a8c commit 28ba2f3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ target/
# Example generated
examples/static/
examples/instance/

# VSCode
.vscode
9 changes: 9 additions & 0 deletions invenio_userprofiles/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,14 @@
USERPROFILES_SETTINGS_TEMPLATE = None
"""Settings base templates for user profile module."""

USERPROFILES_DEFAULT_COUNTRY = None
"""Default country marc21 code for the user profile."""

USERPROFILES_COUNTRIES = lambda: [('ch', 'Switzerland')]
"""Function to return the list of label, value for contries."""

USERPROFILES_READONLY_FIELDS = lambda: []
"""Function to return readonly fields."""

USERPROFILES_READ_ONLY = False
"""Make the user profiles read-only."""
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
under the terms of the MIT License; see LICENSE file for more details.
#}

{% macro render_field(field, icon="", placeholder='', autofocus=False, enabled=True, field_class="form-control") %}
{% macro render_field(field, icon="", placeholder='', autofocus=False, enabled=True) %}
<div class="form-group {% if icon %} has-feedback{% endif %}{% if field.errors %} has-error{% endif %}">
{{ field.label }}
{%- set extras = dict(autofocus="") if autofocus else dict() %}
{{field(class_=field_class, disabled=not enabled, placeholder=placeholder, **extras)}}
{{field(class_="form-control", disabled=not enabled, placeholder=placeholder, **extras)}}


{%- if icon %}
<i class="{{icon}} form-control-feedback" aria-hidden="true" ></i>
{%- endif %}
Expand All @@ -28,3 +30,26 @@
{%- endif %}
</div>
{% endmacro %}

{% macro render_checkbox_field(field, icon="", autofocus=False, enabled=True) %}
<div class="form-group form-check {% if icon %} has-feedback{% endif %}{% if field.errors %} has-error{% endif %}">
{%- set extras = dict(autofocus="") if autofocus else dict() %}
{{field(class_="form-check-input", type="checkbox", disabled=not enabled, **extras)}}
{{ field.label }}

{%- if icon %}
<i class="{{icon}} form-control-feedback" aria-hidden="true" ></i>
{%- endif %}
{%- if field.description %}
<div class="help-block"><small>{{ field.description }}</small></div>
{%- endif %}
{%- if field.errors %}
<div class="alert alert-danger alert-dismissible text-left" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{%- for error in field.errors %}
<p>{{error}}</p>
{% endfor %}
</div>
{%- endif %}
</div>
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,24 @@
{%- set form = profile_form %}
{%- set read_only = config.USERPROFILES_READ_ONLY %}
<form method="POST" name="profile_form">
{%- for field in form %}
{%- if field.widget.input_type == 'hidden' %}
{{ field() }}
{%- elif not read_only or "repeat" not in field.id %}
{{ render_field(field, autofocus=True, enabled=not read_only, placeholder=field.label.text) }}
{%- endif %}
{%- endfor %}
{%- if not read_only %}
<div class="form-actions">
<a href="." class="btn btn-default"><i class="fa fa-times"></i> {{ _('Cancel') }}</a>
<button type="submit" name="submit" value="profile" class="btn btn-primary"><i class="fa fa-check"></i> {{ _('Update profile') }}</button>
</div>
{%- endif %}
{%- for field in form %}
{%- if field.widget.input_type == 'hidden' %}
{{ field() }}
{%- else %}
{% if field.type == "BooleanField" %}
{{ render_checkbox_field(field, autofocus=True, enabled=not read_only) }}
{%- else %}
{{ render_field(field, autofocus=True, enabled=not read_only, placeholder=field.label.text) }}
{%- endif %}

{%- endif %}
{%- endfor %}
{%- if not read_only %}
<div class="form-actions">
<a href="." class="btn btn-default"><i class="fa fa-times"></i> {{ _('Cancel') }}</a>
<button type="submit" name="submit" value="profile" class="btn btn-primary"><i class="fa fa-check"></i>
{{ _('Update profile') }}</button>
</div>
{%- endif %}
</form>
{%- endblock settings_form %}
61 changes: 29 additions & 32 deletions invenio_userprofiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,13 @@ def profile():
formdata=None, obj=current_user, prefix="preferences"
)

# Pick form
# Process forms
is_read_only = current_app.config.get("USERPROFILES_READ_ONLY", False)
form_name = request.form.get("submit", None)
if form_name == "profile" and not is_read_only:
handle_form = handle_profile_form
form = profile_form
elif form_name == "verification":
handle_form = handle_verification_form
form = verification_form
elif form_name == "preferences":
handle_form = handle_preferences_form
form = preferences_form
else:
form = None

# Process form
if form:
form.process(formdata=request.form)
if form.validate_on_submit():
handle_form(form)
return redirect(url_for(".profile"), code=303) # this endpoint
form = request.form.get('submit', None)
if form == 'profile' and not is_read_only:
handle_profile_form(profile_form)
elif form == 'verification':
handle_verification_form(verification_form)

return render_template(
current_app.config["USERPROFILES_PROFILE_TEMPLATE"],
Expand Down Expand Up @@ -167,18 +153,29 @@ def handle_verification_form(form):

def handle_profile_form(form):
"""Handle profile update form."""
email_changed = False
datastore = current_app.extensions["security"].datastore
with db.session.begin_nested():
if (
current_app.config["USERPROFILES_EMAIL_ENABLED"]
and form.email.data != current_user.email
):
email_changed = True
form.populate_obj(current_user)
db.session.add(current_user)
datastore.mark_changed(id(db.session), uid=current_user.id)
datastore.commit()
if current_app.config.get("USERPROFILES_READ_ONLY", False):
return

form.process(formdata=request.form)
if form.validate_on_submit():
email_changed = False
with db.session.begin_nested():
# Update profile.
current_userprofile.username = form.username.data
current_userprofile.last_name=form.last_name.data,
current_userprofile.first_name=form.first_name.data,
current_userprofile.gender=form.gender.data,
current_userprofile.birth_date=form.birth_date.data,
current_userprofile.street=form.street.data,
current_userprofile.postal_code=form.postal_code.data,
current_userprofile.city=form.city.data,
current_userprofile.country=form.country.data,
current_userprofile.home_phone=form.home_phone.data,
current_userprofile.business_phone=form.business_phone.data,
current_userprofile.mobile_phone=form.mobile_phone.data,
current_userprofile.other_phone=form.other_phone.data,
current_userprofile.keep_history=form.keep_history.data
db.session.add(current_userprofile)

if email_changed:
send_confirmation_instructions(current_user)
Expand Down

0 comments on commit 28ba2f3

Please sign in to comment.