Some changes for profile

This commit is contained in:
Patrick Jentsch 2022-12-19 15:35:06 +01:00
parent c0d015acd8
commit c00f5230a1
4 changed files with 60 additions and 66 deletions

View File

@ -271,7 +271,8 @@ class Avatar(HashidMixin, FileMixin, db.Model):
def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = {
'id': self.id
'id': self.id,
**self.file_mixin_to_json_serializeable()
}
return json_serializeable
@ -540,16 +541,7 @@ class User(HashidMixin, UserMixin, db.Model):
self.profile_privacy_settings = 0
#endregion Profile Privacy settings
def to_json_serializeable(
self, backrefs=False,
relationships=False,
filter_by_privacy_settings=False,
include_corpus_relationships=False,
include_job_relationships=False,
include_tesseract_ocr_pipeline_model_relationships=False,
include_spacy_nlp_pipeline_model_relationships=False,
include_avatar_relationship=False
):
def to_json_serializeable(self, backrefs=False, relationships=False, filter_by_privacy_settings=False):
json_serializeable = {
'id': self.hashid,
'confirmed': self.confirmed,
@ -572,34 +564,30 @@ class User(HashidMixin, UserMixin, db.Model):
'show_last_seen': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN),
'show_member_since': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE)
}
json_serializeable['avatar'] = (
None if self.avatar is None
else self.avatar.to_json_serializeable(relationships=True)
)
if backrefs:
json_serializeable['role'] = \
self.role.to_json_serializeable(backrefs=True)
if relationships or include_corpus_relationships:
if relationships:
json_serializeable['corpora'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.corpora
}
if relationships or include_job_relationships:
json_serializeable['jobs'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.jobs
}
if relationships or include_tesseract_ocr_pipeline_model_relationships:
json_serializeable['tesseract_ocr_pipeline_models'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.tesseract_ocr_pipeline_models
}
if relationships or include_spacy_nlp_pipeline_model_relationships:
json_serializeable['spacy_nlp_pipeline_models'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.spacy_nlp_pipeline_models
}
if relationships or include_avatar_relationship:
json_serializeable['avatar'] = (
None if self.avatar is None
else self.avatar.to_json_serializeable(relationships=True)
)
if filter_by_privacy_settings:
if not self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL):

View File

@ -29,41 +29,50 @@ def before_request():
@bp.route('/<hashid:user_id>')
def profile(user_id):
user = User.query.get_or_404(user_id)
user_data = user.to_json_serializeable()
if not user.is_public and user != current_user:
abort(403)
return render_template('profile/profile_page.html.j2',
user=user,
user_data=user_data)
@bp.route('/<hashid:user_id>/avatars/<hashid:avatar_id>')
def avatar_download(user_id, avatar_id):
avatar_file = Avatar.query.filter_by(user_id = user_id, id = avatar_id).first_or_404()
if not (avatar_file and avatar_file.filename):
abort(404)
return send_from_directory(
os.path.dirname(avatar_file.path),
os.path.basename(avatar_file.path),
as_attachment=True,
attachment_filename=avatar_file.filename,
mimetype=avatar_file.mimetype
return render_template(
'profile/profile.html.j2',
user=user.to_json_serializeable(),
)
@bp.route('/<hashid:user_id>/avatars/<hashid:avatar_id>', methods=['DELETE'])
def delete_avatar(avatar_id, user_id):
@bp.route('/<hashid:user_id>/avatar')
def profile_avatar(user_id):
user = User.query.get_or_404(user_id)
if user.avatar is None:
abort(404)
if not user.is_public and not (user == current_user or current_user.is_administrator()):
abort(403)
return send_from_directory(
os.path.dirname(user.avatar.path),
os.path.basename(user.avatar.path),
as_attachment=True,
attachment_filename=user.avatar.filename,
mimetype=user.avatar.mimetype
)
@bp.route('/<hashid:user_id>/avatar', methods=['DELETE'])
def delete_profile_avatar(user_id):
def _delete_avatar(app, avatar_id):
with app.app_context():
avatar_file = Avatar.query.get(avatar_id)
avatar_file.delete()
avatar = Avatar.query.get(avatar_id)
avatar.delete()
db.session.commit()
user = User.query.get_or_404(user_id)
if user.avatar is None:
abort(404)
thread = Thread(
target=_delete_avatar,
args=(current_app._get_current_object(), avatar_id)
args=(current_app._get_current_object(), user.avatar.id)
)
thread.start()
return {}, 202
@bp.route('/<hashid:user_id>/edit-profile', methods=['GET', 'POST'])
@bp.route('/<hashid:user_id>/edit', methods=['GET', 'POST'])
def edit_profile(user_id):
user = User.query.get_or_404(user_id)
edit_profile_settings_form = EditProfileSettingsForm(
@ -79,16 +88,13 @@ def edit_profile(user_id):
data=current_user.to_json_serializeable(),
prefix='edit-public-profile-information-form'
)
if edit_profile_settings_form.validate_on_submit():
current_user.email = edit_profile_settings_form.email.data
current_user.username = edit_profile_settings_form.username.data
db.session.commit()
message = Markup(f'Profile settings updated')
flash(message, 'success')
flash('Profile settings updated')
return redirect(url_for('.profile', user_id=user.id))
if (edit_privacy_settings_form.submit.data
and edit_privacy_settings_form.validate()):
if edit_privacy_settings_form.submit.data and edit_privacy_settings_form.validate():
current_user.is_public = edit_privacy_settings_form.is_public.data
if edit_privacy_settings_form.show_email.data:
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL)
@ -117,12 +123,13 @@ def edit_profile(user_id):
current_user.website = edit_public_profile_information_form.website.data
current_user.full_name = edit_public_profile_information_form.full_name.data
db.session.commit()
message = Markup(f'Profile settings updated')
flash(message, 'success')
flash('Profile settings updated')
return redirect(url_for('.profile', user_id=user.id))
return render_template('profile/edit_profile.html.j2',
return render_template(
'profile/edit_profile.html.j2',
edit_profile_settings_form=edit_profile_settings_form,
edit_privacy_settings_form=edit_privacy_settings_form,
edit_public_profile_information_form=edit_public_profile_information_form,
user=user,
title='Edit Profile')
title='Edit Profile'
)

View File

@ -68,8 +68,8 @@
<div class="row">
<div class="col s2"></div>
<div class="col s8">
{% if current_user.avatar %}
<img src="{{ url_for('profile.avatar_download', user_id=user.id, avatar_id=current_user.avatar.id) }}" alt="user-image" class="circle responsive-img" id="avatar">
{% if user.avatar %}
<img src="{{ url_for('.profile_avatar', user_id=user.id) }}" alt="user-image" class="circle responsive-img" id="avatar">
{% else %}
<img src="{{ url_for('static', filename='images/user_avatar.png') }}" alt="user-image" class="circle responsive-img" id="placeholder-avatar">
{% endif %}
@ -83,7 +83,7 @@
</div>
</div>
<div class="col s10">
{{wtf.render_field(edit_public_profile_information_form.avatar, accept='image/jpeg, image/png, image/gif', placeholder='Choose an image file', id='avatar-upload', data_action='disable')}}
{{ wtf.render_field(edit_public_profile_information_form.avatar, accept='image/jpeg, image/png, image/gif', placeholder='Choose an image file', id='avatar-upload', data_action='disable') }}
</div>
</div>
</div>
@ -165,15 +165,14 @@ avatarUpload.addEventListener('change', function() {
deleteButton.addEventListener('click', function() {
return new Promise((resolve, reject) => {
let user_id = "{{ current_user.hashid }}";
let avatar_id = "{{ current_user.avatar.hashid }}";
let user_id = {{ user.hashid }};
fetch(`/profile/${user_id}/avatars/${avatar_id}`, {method: 'DELETE', headers: {Accept: 'application/json'}})
fetch(`/profile/${user_id}/avatar`, {method: 'DELETE', headers: {Accept: 'application/json'}})
.then(
(response) => {
if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}
if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);}
app.flash(`Avatar marked for deletion`, 'corpus');
app.flash('Avatar marked for deletion');
resolve(response);
},
(response) => {

View File

@ -13,7 +13,7 @@
<br>
<br>
{% if user.avatar %}
<img src="{{ url_for('profile.avatar_download', user_id=user.id, avatar_id=user.avatar.id) }}" alt="user-image" class="circle responsive-img">
<img src="/profile/{{ user.id }}/avatar" alt="user-image" class="circle responsive-img">
{% else %}
<img src="{{ url_for('static', filename='images/user_avatar.png') }}" alt="user-image" class="circle responsive-img">
{% endif %}
@ -25,15 +25,15 @@
<h3 style="float:left">{{ user.username }}<span class="new badge green" id="public-information-badge" data-badge-caption="custom caption" style="margin-top:20px;"></span></h3>
</div>
<div class="col 12">
{% if user_data['show_last_seen'] %}
<div class="chip">Last seen: {{ user.last_seen.strftime('%Y-%m-%d %H:%M') }}</div>
{% if user.show_last_seen %}
<div class="chip">Last seen: {{ user.last_seen }}</div>
{% endif %}
{% if user.location %}
<p><span class="material-icons" style="margin-right:20px; margin-top:20px;">location_on</span><i>{{ user.location }}</i></p>
{% endif %}
<p></p>
<br>
{% if user.about_me%}
{% if user.about_me %}
<div style="border-left: solid 3px #E91E63; padding-left: 15px;">
<h5>About me</h5>
<p>{{ user.about_me }}</p>
@ -57,7 +57,7 @@
<td>{{ user.full_name }} </td>
</tr>
{% endif %}
{% if user_data['show_email'] %}
{% if user.show_email %}
<tr>
<td><span class="material-icons">email</span></td>
<td>{{ user.email }}</td>
@ -77,13 +77,13 @@
{% endif %}
</table>
<br>
{% if user_data['show_member_since'] %}
<p><i>Member since: {{ user.member_since.strftime('%Y-%m-%d') }}</i></p>
{% if user.show_member_since %}
<p><i>Member since: {{ user.member_since }}</i></p>
{% endif %}
<p></p>
<br>
{% if current_user.is_authenticated and current_user.id == user.id %}
<a class="waves-effect waves-light btn-small" href="{{ url_for('profile.edit_profile', user_id=user.id) }}">Edit profile</a>
<a class="waves-effect waves-light btn-small" href="{{ url_for('.edit_profile', user_id=user.id) }}">Edit profile</a>
{% endif %}
</div>
</div>