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): def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = { json_serializeable = {
'id': self.id 'id': self.id,
**self.file_mixin_to_json_serializeable()
} }
return json_serializeable return json_serializeable
@ -540,16 +541,7 @@ class User(HashidMixin, UserMixin, db.Model):
self.profile_privacy_settings = 0 self.profile_privacy_settings = 0
#endregion Profile Privacy settings #endregion Profile Privacy settings
def to_json_serializeable( def to_json_serializeable(self, backrefs=False, relationships=False, filter_by_privacy_settings=False):
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
):
json_serializeable = { json_serializeable = {
'id': self.hashid, 'id': self.hashid,
'confirmed': self.confirmed, '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_last_seen': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN),
'show_member_since': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE) 'show_member_since': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE)
} }
if backrefs:
json_serializeable['role'] = \
self.role.to_json_serializeable(backrefs=True)
if relationships or include_corpus_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'] = ( json_serializeable['avatar'] = (
None if self.avatar is None None if self.avatar is None
else self.avatar.to_json_serializeable(relationships=True) else self.avatar.to_json_serializeable(relationships=True)
) )
if backrefs:
json_serializeable['role'] = \
self.role.to_json_serializeable(backrefs=True)
if relationships:
json_serializeable['corpora'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.corpora
}
json_serializeable['jobs'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.jobs
}
json_serializeable['tesseract_ocr_pipeline_models'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.tesseract_ocr_pipeline_models
}
json_serializeable['spacy_nlp_pipeline_models'] = {
x.hashid: x.to_json_serializeable(relationships=True)
for x in self.spacy_nlp_pipeline_models
}
if filter_by_privacy_settings: if filter_by_privacy_settings:
if not self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL): if not self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL):

View File

@ -29,41 +29,50 @@ def before_request():
@bp.route('/<hashid:user_id>') @bp.route('/<hashid:user_id>')
def profile(user_id): def profile(user_id):
user = User.query.get_or_404(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: if not user.is_public and user != current_user:
abort(403) abort(403)
return render_template('profile/profile_page.html.j2', return render_template(
user=user, 'profile/profile.html.j2',
user_data=user_data) user=user.to_json_serializeable(),
@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
) )
@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): def _delete_avatar(app, avatar_id):
with app.app_context(): with app.app_context():
avatar_file = Avatar.query.get(avatar_id) avatar = Avatar.query.get(avatar_id)
avatar_file.delete() avatar.delete()
db.session.commit() db.session.commit()
user = User.query.get_or_404(user_id)
if user.avatar is None:
abort(404)
thread = Thread( thread = Thread(
target=_delete_avatar, target=_delete_avatar,
args=(current_app._get_current_object(), avatar_id) args=(current_app._get_current_object(), user.avatar.id)
) )
thread.start() thread.start()
return {}, 202 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): def edit_profile(user_id):
user = User.query.get_or_404(user_id) user = User.query.get_or_404(user_id)
edit_profile_settings_form = EditProfileSettingsForm( edit_profile_settings_form = EditProfileSettingsForm(
@ -79,16 +88,13 @@ def edit_profile(user_id):
data=current_user.to_json_serializeable(), data=current_user.to_json_serializeable(),
prefix='edit-public-profile-information-form' prefix='edit-public-profile-information-form'
) )
if edit_profile_settings_form.validate_on_submit(): if edit_profile_settings_form.validate_on_submit():
current_user.email = edit_profile_settings_form.email.data current_user.email = edit_profile_settings_form.email.data
current_user.username = edit_profile_settings_form.username.data current_user.username = edit_profile_settings_form.username.data
db.session.commit() db.session.commit()
message = Markup(f'Profile settings updated') flash('Profile settings updated')
flash(message, 'success')
return redirect(url_for('.profile', user_id=user.id)) return redirect(url_for('.profile', user_id=user.id))
if (edit_privacy_settings_form.submit.data if edit_privacy_settings_form.submit.data and edit_privacy_settings_form.validate():
and edit_privacy_settings_form.validate()):
current_user.is_public = edit_privacy_settings_form.is_public.data current_user.is_public = edit_privacy_settings_form.is_public.data
if edit_privacy_settings_form.show_email.data: if edit_privacy_settings_form.show_email.data:
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL) 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.website = edit_public_profile_information_form.website.data
current_user.full_name = edit_public_profile_information_form.full_name.data current_user.full_name = edit_public_profile_information_form.full_name.data
db.session.commit() db.session.commit()
message = Markup(f'Profile settings updated') flash('Profile settings updated')
flash(message, 'success')
return redirect(url_for('.profile', user_id=user.id)) 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_profile_settings_form=edit_profile_settings_form,
edit_privacy_settings_form=edit_privacy_settings_form, edit_privacy_settings_form=edit_privacy_settings_form,
edit_public_profile_information_form=edit_public_profile_information_form, edit_public_profile_information_form=edit_public_profile_information_form,
user=user, user=user,
title='Edit Profile') title='Edit Profile'
)

View File

@ -68,8 +68,8 @@
<div class="row"> <div class="row">
<div class="col s2"></div> <div class="col s2"></div>
<div class="col s8"> <div class="col s8">
{% if current_user.avatar %} {% if 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"> <img src="{{ url_for('.profile_avatar', user_id=user.id) }}" alt="user-image" class="circle responsive-img" id="avatar">
{% else %} {% else %}
<img src="{{ url_for('static', filename='images/user_avatar.png') }}" alt="user-image" class="circle responsive-img" id="placeholder-avatar"> <img src="{{ url_for('static', filename='images/user_avatar.png') }}" alt="user-image" class="circle responsive-img" id="placeholder-avatar">
{% endif %} {% endif %}
@ -165,15 +165,14 @@ avatarUpload.addEventListener('change', function() {
deleteButton.addEventListener('click', function() { deleteButton.addEventListener('click', function() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let user_id = "{{ current_user.hashid }}"; let user_id = {{ user.hashid }};
let avatar_id = "{{ current_user.avatar.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( .then(
(response) => { (response) => {
if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}
if (response.status === 404) {app.flash('Not Found', '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); resolve(response);
}, },
(response) => { (response) => {

View File

@ -13,7 +13,7 @@
<br> <br>
<br> <br>
{% if user.avatar %} {% 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 %} {% else %}
<img src="{{ url_for('static', filename='images/user_avatar.png') }}" alt="user-image" class="circle responsive-img"> <img src="{{ url_for('static', filename='images/user_avatar.png') }}" alt="user-image" class="circle responsive-img">
{% endif %} {% endif %}
@ -25,8 +25,8 @@
<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> <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>
<div class="col 12"> <div class="col 12">
{% if user_data['show_last_seen'] %} {% if user.show_last_seen %}
<div class="chip">Last seen: {{ user.last_seen.strftime('%Y-%m-%d %H:%M') }}</div> <div class="chip">Last seen: {{ user.last_seen }}</div>
{% endif %} {% endif %}
{% if user.location %} {% if user.location %}
<p><span class="material-icons" style="margin-right:20px; margin-top:20px;">location_on</span><i>{{ user.location }}</i></p> <p><span class="material-icons" style="margin-right:20px; margin-top:20px;">location_on</span><i>{{ user.location }}</i></p>
@ -57,7 +57,7 @@
<td>{{ user.full_name }} </td> <td>{{ user.full_name }} </td>
</tr> </tr>
{% endif %} {% endif %}
{% if user_data['show_email'] %} {% if user.show_email %}
<tr> <tr>
<td><span class="material-icons">email</span></td> <td><span class="material-icons">email</span></td>
<td>{{ user.email }}</td> <td>{{ user.email }}</td>
@ -77,13 +77,13 @@
{% endif %} {% endif %}
</table> </table>
<br> <br>
{% if user_data['show_member_since'] %} {% if user.show_member_since %}
<p><i>Member since: {{ user.member_since.strftime('%Y-%m-%d') }}</i></p> <p><i>Member since: {{ user.member_since }}</i></p>
{% endif %} {% endif %}
<p></p> <p></p>
<br> <br>
{% if current_user.is_authenticated and current_user.id == user.id %} {% 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 %} {% endif %}
</div> </div>
</div> </div>