mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-11 08:30:41 +00:00
Change directory structure (move ./nopaque/* to ./)
This commit is contained in:
5
app/settings/__init__.py
Normal file
5
app/settings/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
from flask import Blueprint
|
||||
|
||||
|
||||
settings = Blueprint('settings', __name__)
|
||||
from . import views # noqa
|
78
app/settings/forms.py
Normal file
78
app/settings/forms.py
Normal file
@ -0,0 +1,78 @@
|
||||
from flask import current_app
|
||||
from flask_login import current_user
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import (BooleanField, PasswordField, SelectField, StringField,
|
||||
SubmitField, ValidationError)
|
||||
from wtforms.validators import DataRequired, Email, EqualTo, Length, Regexp
|
||||
from ..models import User
|
||||
|
||||
|
||||
class ChangePasswordForm(FlaskForm):
|
||||
password = PasswordField('Old password', validators=[DataRequired()])
|
||||
new_password = PasswordField(
|
||||
'New password',
|
||||
validators=[DataRequired(), EqualTo('password_confirmation',
|
||||
message='Passwords must match.')]
|
||||
)
|
||||
new_password2 = PasswordField(
|
||||
'Confirm new password', validators=[DataRequired()])
|
||||
submit = SubmitField('Change password')
|
||||
|
||||
def __init__(self, user=current_user, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.user = user
|
||||
|
||||
def validate_current_password(self, field):
|
||||
if not self.user.verify_password(field.data):
|
||||
raise ValidationError('Invalid password.')
|
||||
|
||||
|
||||
class EditGeneralSettingsForm(FlaskForm):
|
||||
dark_mode = BooleanField('Dark mode')
|
||||
email = StringField('E-Mail',
|
||||
validators=[DataRequired(), Length(1, 254), Email()])
|
||||
username = StringField(
|
||||
'Benutzername',
|
||||
validators=[DataRequired(),
|
||||
Length(1, 64),
|
||||
Regexp(current_app.config['NOPAQUE_USERNAME_REGEX'],
|
||||
message='Usernames must have only letters, numbers,'
|
||||
' dots or underscores')]
|
||||
)
|
||||
submit = SubmitField('Submit')
|
||||
|
||||
def __init__(self, user=current_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.')
|
||||
|
||||
|
||||
class EditNotificationSettingsForm(FlaskForm):
|
||||
job_status_mail_notifications = SelectField(
|
||||
'Job status mail notifications',
|
||||
choices=[('', 'Choose your option'),
|
||||
('all', 'Notify on all status changes'),
|
||||
('end', 'Notify only when a job ended'),
|
||||
('none', 'No status update notifications')],
|
||||
validators=[DataRequired()])
|
||||
job_status_site_notifications = SelectField(
|
||||
'Job status site notifications',
|
||||
choices=[('', 'Choose your option'),
|
||||
('all', 'Notify on all status changes'),
|
||||
('end', 'Notify only when a job ended'),
|
||||
('none', 'No status update notifications')],
|
||||
validators=[DataRequired()])
|
||||
submit = SubmitField('Save settings')
|
||||
|
||||
def __init__(self, user=current_user, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.user = user
|
13
app/settings/tasks.py
Normal file
13
app/settings/tasks.py
Normal file
@ -0,0 +1,13 @@
|
||||
from .. import db
|
||||
from ..decorators import background
|
||||
from ..models import User
|
||||
|
||||
|
||||
@background
|
||||
def delete_user(user_id, *args, **kwargs):
|
||||
with kwargs['app'].app_context():
|
||||
user = User.query.get(user_id)
|
||||
if user is None:
|
||||
raise Exception('User {} not found'.format(user_id))
|
||||
user.delete()
|
||||
db.session.commit()
|
75
app/settings/views.py
Normal file
75
app/settings/views.py
Normal file
@ -0,0 +1,75 @@
|
||||
from flask import flash, redirect, render_template, url_for
|
||||
from flask_login import current_user, login_required, logout_user
|
||||
from . import settings, tasks
|
||||
from .forms import (ChangePasswordForm, EditGeneralSettingsForm,
|
||||
EditNotificationSettingsForm)
|
||||
from .. import db
|
||||
|
||||
|
||||
@settings.route('/')
|
||||
@login_required
|
||||
def index():
|
||||
return redirect(url_for('.edit_general_settings'))
|
||||
|
||||
|
||||
@settings.route('/change_password', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def change_password():
|
||||
form = ChangePasswordForm()
|
||||
if form.validate_on_submit():
|
||||
current_user.password = form.new_password.data
|
||||
db.session.commit()
|
||||
flash('Your password has been updated.')
|
||||
return redirect(url_for('.change_password'))
|
||||
return render_template('settings/change_password.html.j2',
|
||||
form=form, title='Change password')
|
||||
|
||||
|
||||
@settings.route('/edit_general_settings', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_general_settings():
|
||||
form = EditGeneralSettingsForm()
|
||||
if form.validate_on_submit():
|
||||
current_user.email = form.email.data
|
||||
current_user.setting_dark_mode = form.dark_mode.data
|
||||
current_user.username = form.username.data
|
||||
db.session.commit()
|
||||
flash('Your changes have been saved.')
|
||||
return redirect(url_for('.edit_general_settings'))
|
||||
form.dark_mode.data = current_user.setting_dark_mode
|
||||
form.email.data = current_user.email
|
||||
form.username.data = current_user.username
|
||||
return render_template('settings/edit_general_settings.html.j2',
|
||||
form=form, title='General settings')
|
||||
|
||||
|
||||
@settings.route('/edit_notification_settings', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_notification_settings():
|
||||
form = EditNotificationSettingsForm()
|
||||
if form.validate_on_submit():
|
||||
current_user.setting_job_status_mail_notifications = \
|
||||
form.job_status_mail_notifications.data
|
||||
current_user.setting_job_status_site_notifications = \
|
||||
form.job_status_site_notifications.data
|
||||
db.session.commit()
|
||||
flash('Your changes have been saved.')
|
||||
return redirect(url_for('.edit_notification_settings'))
|
||||
form.job_status_mail_notifications.data = \
|
||||
current_user.setting_job_status_mail_notifications
|
||||
form.job_status_site_notifications.data = \
|
||||
current_user.setting_job_status_site_notifications
|
||||
return render_template('settings/edit_notification_settings.html.j2',
|
||||
form=form, title='Notification settings')
|
||||
|
||||
|
||||
@settings.route('/delete')
|
||||
@login_required
|
||||
def delete():
|
||||
"""
|
||||
View to delete current_user and all associated data.
|
||||
"""
|
||||
tasks.delete_user(current_user.id)
|
||||
logout_user()
|
||||
flash('Your account has been marked for deletion!')
|
||||
return redirect(url_for('main.index'))
|
Reference in New Issue
Block a user