Move account related views to a separate blueprint

This commit is contained in:
2018-07-17 15:21:12 +02:00
parent 8d45611e35
commit bcb7b524f3
12 changed files with 225 additions and 177 deletions

View File

@@ -25,6 +25,7 @@ from flask_babelex import Babel, get_locale as babel_get_locale
from flask_security import SQLAlchemyUserDatastore, current_user, login_required
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from calsocial.account import AccountBlueprint
from calsocial.utils import RoutedMixin
@@ -90,6 +91,8 @@ class CalendarSocialApp(Flask, RoutedMixin):
RoutedMixin.register_routes(self)
AccountBlueprint().init_app(self, '/accounts/')
self.before_request(self.goto_first_steps)
@staticmethod
@@ -100,7 +103,7 @@ class CalendarSocialApp(Flask, RoutedMixin):
if current_user.is_authenticated and \
not current_user.profile and \
request.endpoint != 'first_steps':
return redirect(url_for('first_steps'))
return redirect(url_for('account.first_steps'))
return None
@@ -181,36 +184,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
return render_template('index.html', calendar=calendar, user_only=True)
@staticmethod
@RoutedMixin.route('/register', methods=['POST', 'GET'])
def register():
"""View for user registration
If the ``REGISTRATION_FAILED`` configuration value is set to ``True`` it displays the
registration disabled template. Otherwise, it performs user registration.
"""
if not current_app.config['REGISTRATION_ENABLED']:
return render_template('registration-disabled.html')
from .forms import RegistrationForm
from .models import db, User
form = RegistrationForm()
if form.validate_on_submit():
# TODO: This might become False later, if we want registrations to be confirmed via
# e-mail
user = User(active=True)
form.populate_obj(user)
db.session.add(user)
db.session.commit()
return redirect(url_for('hello'))
return render_template('registration.html', form=form)
@staticmethod
@RoutedMixin.route('/new-event', methods=['GET', 'POST'])
@login_required
@@ -236,27 +209,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
return render_template('event-edit.html', form=form)
@staticmethod
@RoutedMixin.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
"""View for user settings
"""
from .forms import SettingsForm
from .models import db
form = SettingsForm(current_user)
if form.validate_on_submit():
form.populate_obj(current_user)
db.session.commit()
return redirect(url_for('hello'))
return render_template('user-settings.html', form=form)
@staticmethod
@RoutedMixin.route('/event/<string:event_uuid>', methods=['GET', 'POST'])
def event_details(event_uuid):
@@ -320,21 +272,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
return redirect(url_for('display_profile', username=username))
@staticmethod
@RoutedMixin.route('/notifications')
def notifications():
"""View to list the notifications for the current user
"""
from .models import Notification
if current_user.is_authenticated:
notifs = Notification.query.filter(Notification.profile == current_user.profile)
else:
notifs = []
return render_template('notifications.html', notifs=notifs)
@staticmethod
@RoutedMixin.route('/accept/<int:invite_id>')
def accept_invite(invite_id):
@@ -365,54 +302,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
return redirect(url_for('event_details', event_uuid=invitation.event.event_uuid))
@staticmethod
@RoutedMixin.route('/first-steps', methods=['GET', 'POST'])
@login_required
def first_steps():
"""View to set up a new registrants profile
"""
from .forms import FirstStepsForm
from .models import db, Profile
if current_user.profile:
return redirect(url_for('hello'))
form = FirstStepsForm()
if form.validate_on_submit():
profile = Profile(user=current_user, display_name=form.display_name.data)
db.session.add(profile)
current_user.settings['timezone'] = str(form.time_zone.data)
db.session.commit()
return redirect(url_for('hello'))
return render_template('first-steps.html', form=form)
@staticmethod
@RoutedMixin.route('/edit-profile', methods=['GET', 'POST'])
@login_required
def edit_profile():
"""View for editing ones profile
"""
from .forms import ProfileForm
from .models import db
form = ProfileForm(current_user.profile)
if form.validate_on_submit():
form.populate_obj(current_user.profile)
db.session.add(current_user.profile)
db.session.commit()
return redirect(url_for('edit_profile'))
return render_template('profile-edit.html', form=form)
@staticmethod
@RoutedMixin.route('/all-events')
def all_events():
@@ -430,45 +319,5 @@ class CalendarSocialApp(Flask, RoutedMixin):
return render_template('index.html', calendar=calendar, user_only=False)
@staticmethod
@RoutedMixin.route('/follow-requests')
@login_required
def follow_requests():
"""View for listing follow requests
"""
from .models import UserFollow
requests = UserFollow.query \
.filter(UserFollow.followed == current_user.profile) \
.filter(UserFollow.accepted_at.is_(None))
return render_template('follow-requests.html', requests=requests)
@staticmethod
@RoutedMixin.route('/follow-request/<int:follower_id>/accept')
@login_required
def accept_follow(follower_id):
"""View for accepting a follow request
"""
from .models import db, UserFollow
try:
req = UserFollow.query \
.filter(UserFollow.followed == current_user.profile) \
.filter(UserFollow.follower_id == follower_id) \
.one()
except NoResultFound:
abort(404)
if req.accepted_at is None:
req.accept()
db.session.add(req)
db.session.commit()
return redirect(url_for('follow_requests'))
app = CalendarSocialApp(__name__)