mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-15 02:20:40 +00:00
Profile page
This commit is contained in:
87
app/profile/forms.py
Normal file
87
app/profile/forms.py
Normal file
@ -0,0 +1,87 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import (
|
||||
FileField,
|
||||
StringField,
|
||||
SubmitField,
|
||||
TextAreaField,
|
||||
ValidationError
|
||||
)
|
||||
from wtforms.validators import (
|
||||
InputRequired,
|
||||
Email,
|
||||
Length,
|
||||
Regexp
|
||||
)
|
||||
from app.models import User
|
||||
from app.auth import USERNAME_REGEX
|
||||
|
||||
class EditProfileSettingsForm(FlaskForm):
|
||||
user_avatar = FileField(
|
||||
'Image File'
|
||||
)
|
||||
email = StringField(
|
||||
'E-Mail',
|
||||
validators=[InputRequired(), Length(max=254), Email()]
|
||||
)
|
||||
username = StringField(
|
||||
'Username',
|
||||
validators=[
|
||||
InputRequired(),
|
||||
Length(max=64),
|
||||
Regexp(
|
||||
USERNAME_REGEX,
|
||||
message=(
|
||||
'Usernames must have only letters, numbers, dots or '
|
||||
'underscores'
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
full_name = StringField(
|
||||
'Full name',
|
||||
validators=[Length(max=128)]
|
||||
)
|
||||
about_me = TextAreaField(
|
||||
'About me',
|
||||
validators=[
|
||||
Length(max=254)
|
||||
]
|
||||
)
|
||||
website = StringField(
|
||||
'Website',
|
||||
validators=[
|
||||
Length(max=254)
|
||||
]
|
||||
)
|
||||
organization = StringField(
|
||||
'Organization',
|
||||
validators=[
|
||||
Length(max=128)
|
||||
]
|
||||
)
|
||||
location = StringField(
|
||||
'Location',
|
||||
validators=[
|
||||
Length(max=128)
|
||||
]
|
||||
)
|
||||
|
||||
submit = SubmitField()
|
||||
|
||||
def __init__(self, user, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.user = user
|
||||
|
||||
def validate_email(self, field):
|
||||
if (field.data != self.user.email
|
||||
and User.query.filter_by(email=field.data).first()):
|
||||
raise ValidationError('Email already registered')
|
||||
|
||||
def validate_username(self, field):
|
||||
if (field.data != self.user.username
|
||||
and User.query.filter_by(username=field.data).first()):
|
||||
raise ValidationError('Username already in use')
|
||||
|
||||
def validate_image_file(self, field):
|
||||
if not field.data.filename.lower().endswith('.jpg' or '.png' or '.jpeg'):
|
||||
raise ValidationError('only .jpg, .png and .jpeg!')
|
@ -1,24 +1,61 @@
|
||||
from flask import render_template, url_for
|
||||
from flask import flash, redirect, render_template, url_for
|
||||
from flask_login import current_user, login_required
|
||||
from app import db
|
||||
from app.models import User
|
||||
from . import bp
|
||||
|
||||
from .forms import (
|
||||
EditProfileSettingsForm
|
||||
)
|
||||
|
||||
@bp.route('')
|
||||
@login_required
|
||||
def profile():
|
||||
user_image = 'static/images/user_avatar.png'
|
||||
user_name = current_user.username
|
||||
last_seen = current_user.last_seen
|
||||
member_since = current_user.member_since
|
||||
last_seen = f'{current_user.last_seen.strftime("%Y-%m-%d %H:%M")}'
|
||||
location = 'Bielefeld'
|
||||
about_me = '''Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
|
||||
sed diam nonumy eirmod tempor invidunt ut labore et dolore
|
||||
magna aliquyam erat, sed diam voluptua. At vero eos et accusam
|
||||
et justo duo dolores et ea rebum. Stet clita kasd gubergren,
|
||||
no sea takimat'''
|
||||
full_name = 'Inga Kirschnick'
|
||||
email = current_user.email
|
||||
role = current_user.role
|
||||
website = 'https://nopaque.uni-bielefeld.de'
|
||||
organization = 'Universität Bielefeld'
|
||||
member_since = f'{current_user.member_since.strftime("%Y-%m-%d")}'
|
||||
return render_template('profile/profile_page.html.j2',
|
||||
user_image=user_image,
|
||||
user_name=user_name,
|
||||
last_seen=last_seen,
|
||||
member_since=member_since,
|
||||
email=email,
|
||||
role=role)
|
||||
last_seen=last_seen,
|
||||
location=location,
|
||||
about_me=about_me,
|
||||
full_name=full_name,
|
||||
email=email,
|
||||
website=website,
|
||||
organization=organization,
|
||||
member_since=member_since)
|
||||
|
||||
@bp.route('/edit')
|
||||
@login_required
|
||||
def edit_profile():
|
||||
edit_profile_settings_form = EditProfileSettingsForm(
|
||||
current_user,
|
||||
data=current_user.to_json_serializeable(),
|
||||
prefix='edit-profile-settings-form'
|
||||
)
|
||||
if (edit_profile_settings_form.submit.data
|
||||
and edit_profile_settings_form.validate()):
|
||||
current_user.email = edit_profile_settings_form.email.data
|
||||
current_user.username = edit_profile_settings_form.username.data
|
||||
current_user.about_me = edit_profile_settings_form.about_me.data
|
||||
current_user.location = edit_profile_settings_form.location.data
|
||||
current_user.organization = edit_profile_settings_form.organization.data
|
||||
current_user.website = edit_profile_settings_form.website.data
|
||||
current_user.full_name = edit_profile_settings_form.full_name.data
|
||||
db.session.commit()
|
||||
flash('Your changes have been saved')
|
||||
return redirect(url_for('.profile.edit_profile'))
|
||||
return render_template('profile/edit_profile.html.j2',
|
||||
edit_profile_settings_form=edit_profile_settings_form,
|
||||
title='Edit Profile')
|
||||
|
Reference in New Issue
Block a user