Skip to content

Commit 28ba2f3

Browse files
lauren-drerowep
authored andcommitted
users: make user profiles read-only
Co-Authored-by: Lauren-D <laurent.dubois@itld-solutions.be>
1 parent 9339a8c commit 28ba2f3

File tree

5 files changed

+87
-47
lines changed

5 files changed

+87
-47
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ target/
6464
# Example generated
6565
examples/static/
6666
examples/instance/
67+
68+
# VSCode
69+
.vscode

invenio_userprofiles/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,14 @@
2929
USERPROFILES_SETTINGS_TEMPLATE = None
3030
"""Settings base templates for user profile module."""
3131

32+
USERPROFILES_DEFAULT_COUNTRY = None
33+
"""Default country marc21 code for the user profile."""
34+
35+
USERPROFILES_COUNTRIES = lambda: [('ch', 'Switzerland')]
36+
"""Function to return the list of label, value for contries."""
37+
38+
USERPROFILES_READONLY_FIELDS = lambda: []
39+
"""Function to return readonly fields."""
40+
3241
USERPROFILES_READ_ONLY = False
3342
"""Make the user profiles read-only."""

invenio_userprofiles/templates/invenio_userprofiles/settings/_macros.html

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
under the terms of the MIT License; see LICENSE file for more details.
88
#}
99

10-
{% macro render_field(field, icon="", placeholder='', autofocus=False, enabled=True, field_class="form-control") %}
10+
{% macro render_field(field, icon="", placeholder='', autofocus=False, enabled=True) %}
1111
<div class="form-group {% if icon %} has-feedback{% endif %}{% if field.errors %} has-error{% endif %}">
1212
{{ field.label }}
1313
{%- set extras = dict(autofocus="") if autofocus else dict() %}
14-
{{field(class_=field_class, disabled=not enabled, placeholder=placeholder, **extras)}}
14+
{{field(class_="form-control", disabled=not enabled, placeholder=placeholder, **extras)}}
15+
16+
1517
{%- if icon %}
1618
<i class="{{icon}} form-control-feedback" aria-hidden="true" ></i>
1719
{%- endif %}
@@ -28,3 +30,26 @@
2830
{%- endif %}
2931
</div>
3032
{% endmacro %}
33+
34+
{% macro render_checkbox_field(field, icon="", autofocus=False, enabled=True) %}
35+
<div class="form-group form-check {% if icon %} has-feedback{% endif %}{% if field.errors %} has-error{% endif %}">
36+
{%- set extras = dict(autofocus="") if autofocus else dict() %}
37+
{{field(class_="form-check-input", type="checkbox", disabled=not enabled, **extras)}}
38+
{{ field.label }}
39+
40+
{%- if icon %}
41+
<i class="{{icon}} form-control-feedback" aria-hidden="true" ></i>
42+
{%- endif %}
43+
{%- if field.description %}
44+
<div class="help-block"><small>{{ field.description }}</small></div>
45+
{%- endif %}
46+
{%- if field.errors %}
47+
<div class="alert alert-danger alert-dismissible text-left" role="alert">
48+
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
49+
{%- for error in field.errors %}
50+
<p>{{error}}</p>
51+
{% endfor %}
52+
</div>
53+
{%- endif %}
54+
</div>
55+
{% endmacro %}

invenio_userprofiles/templates/invenio_userprofiles/settings/profile.html

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,24 @@
2626
{%- set form = profile_form %}
2727
{%- set read_only = config.USERPROFILES_READ_ONLY %}
2828
<form method="POST" name="profile_form">
29-
{%- for field in form %}
30-
{%- if field.widget.input_type == 'hidden' %}
31-
{{ field() }}
32-
{%- elif not read_only or "repeat" not in field.id %}
33-
{{ render_field(field, autofocus=True, enabled=not read_only, placeholder=field.label.text) }}
34-
{%- endif %}
35-
{%- endfor %}
36-
{%- if not read_only %}
37-
<div class="form-actions">
38-
<a href="." class="btn btn-default"><i class="fa fa-times"></i> {{ _('Cancel') }}</a>
39-
<button type="submit" name="submit" value="profile" class="btn btn-primary"><i class="fa fa-check"></i> {{ _('Update profile') }}</button>
40-
</div>
41-
{%- endif %}
29+
{%- for field in form %}
30+
{%- if field.widget.input_type == 'hidden' %}
31+
{{ field() }}
32+
{%- else %}
33+
{% if field.type == "BooleanField" %}
34+
{{ render_checkbox_field(field, autofocus=True, enabled=not read_only) }}
35+
{%- else %}
36+
{{ render_field(field, autofocus=True, enabled=not read_only, placeholder=field.label.text) }}
37+
{%- endif %}
38+
39+
{%- endif %}
40+
{%- endfor %}
41+
{%- if not read_only %}
42+
<div class="form-actions">
43+
<a href="." class="btn btn-default"><i class="fa fa-times"></i> {{ _('Cancel') }}</a>
44+
<button type="submit" name="submit" value="profile" class="btn btn-primary"><i class="fa fa-check"></i>
45+
{{ _('Update profile') }}</button>
46+
</div>
47+
{%- endif %}
4248
</form>
4349
{%- endblock settings_form %}

invenio_userprofiles/views.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,13 @@ def profile():
112112
formdata=None, obj=current_user, prefix="preferences"
113113
)
114114

115-
# Pick form
115+
# Process forms
116116
is_read_only = current_app.config.get("USERPROFILES_READ_ONLY", False)
117-
form_name = request.form.get("submit", None)
118-
if form_name == "profile" and not is_read_only:
119-
handle_form = handle_profile_form
120-
form = profile_form
121-
elif form_name == "verification":
122-
handle_form = handle_verification_form
123-
form = verification_form
124-
elif form_name == "preferences":
125-
handle_form = handle_preferences_form
126-
form = preferences_form
127-
else:
128-
form = None
129-
130-
# Process form
131-
if form:
132-
form.process(formdata=request.form)
133-
if form.validate_on_submit():
134-
handle_form(form)
135-
return redirect(url_for(".profile"), code=303) # this endpoint
117+
form = request.form.get('submit', None)
118+
if form == 'profile' and not is_read_only:
119+
handle_profile_form(profile_form)
120+
elif form == 'verification':
121+
handle_verification_form(verification_form)
136122

137123
return render_template(
138124
current_app.config["USERPROFILES_PROFILE_TEMPLATE"],
@@ -167,18 +153,29 @@ def handle_verification_form(form):
167153

168154
def handle_profile_form(form):
169155
"""Handle profile update form."""
170-
email_changed = False
171-
datastore = current_app.extensions["security"].datastore
172-
with db.session.begin_nested():
173-
if (
174-
current_app.config["USERPROFILES_EMAIL_ENABLED"]
175-
and form.email.data != current_user.email
176-
):
177-
email_changed = True
178-
form.populate_obj(current_user)
179-
db.session.add(current_user)
180-
datastore.mark_changed(id(db.session), uid=current_user.id)
181-
datastore.commit()
156+
if current_app.config.get("USERPROFILES_READ_ONLY", False):
157+
return
158+
159+
form.process(formdata=request.form)
160+
if form.validate_on_submit():
161+
email_changed = False
162+
with db.session.begin_nested():
163+
# Update profile.
164+
current_userprofile.username = form.username.data
165+
current_userprofile.last_name=form.last_name.data,
166+
current_userprofile.first_name=form.first_name.data,
167+
current_userprofile.gender=form.gender.data,
168+
current_userprofile.birth_date=form.birth_date.data,
169+
current_userprofile.street=form.street.data,
170+
current_userprofile.postal_code=form.postal_code.data,
171+
current_userprofile.city=form.city.data,
172+
current_userprofile.country=form.country.data,
173+
current_userprofile.home_phone=form.home_phone.data,
174+
current_userprofile.business_phone=form.business_phone.data,
175+
current_userprofile.mobile_phone=form.mobile_phone.data,
176+
current_userprofile.other_phone=form.other_phone.data,
177+
current_userprofile.keep_history=form.keep_history.data
178+
db.session.add(current_userprofile)
182179

183180
if email_changed:
184181
send_confirmation_instructions(current_user)

0 commit comments

Comments
 (0)