Move account related views to a separate blueprint

This commit is contained in:
Gergely Polonkai 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 flask_security import SQLAlchemyUserDatastore, current_user, login_required
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from calsocial.account import AccountBlueprint
from calsocial.utils import RoutedMixin from calsocial.utils import RoutedMixin
@ -90,6 +91,8 @@ class CalendarSocialApp(Flask, RoutedMixin):
RoutedMixin.register_routes(self) RoutedMixin.register_routes(self)
AccountBlueprint().init_app(self, '/accounts/')
self.before_request(self.goto_first_steps) self.before_request(self.goto_first_steps)
@staticmethod @staticmethod
@ -100,7 +103,7 @@ class CalendarSocialApp(Flask, RoutedMixin):
if current_user.is_authenticated and \ if current_user.is_authenticated and \
not current_user.profile and \ not current_user.profile and \
request.endpoint != 'first_steps': request.endpoint != 'first_steps':
return redirect(url_for('first_steps')) return redirect(url_for('account.first_steps'))
return None return None
@ -181,36 +184,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
return render_template('index.html', calendar=calendar, user_only=True) 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 @staticmethod
@RoutedMixin.route('/new-event', methods=['GET', 'POST']) @RoutedMixin.route('/new-event', methods=['GET', 'POST'])
@login_required @login_required
@ -236,27 +209,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
return render_template('event-edit.html', form=form) 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 @staticmethod
@RoutedMixin.route('/event/<string:event_uuid>', methods=['GET', 'POST']) @RoutedMixin.route('/event/<string:event_uuid>', methods=['GET', 'POST'])
def event_details(event_uuid): def event_details(event_uuid):
@ -320,21 +272,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
return redirect(url_for('display_profile', username=username)) 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 @staticmethod
@RoutedMixin.route('/accept/<int:invite_id>') @RoutedMixin.route('/accept/<int:invite_id>')
def accept_invite(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)) 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 @staticmethod
@RoutedMixin.route('/all-events') @RoutedMixin.route('/all-events')
def all_events(): def all_events():
@ -430,45 +319,5 @@ class CalendarSocialApp(Flask, RoutedMixin):
return render_template('index.html', calendar=calendar, user_only=False) 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__) app = CalendarSocialApp(__name__)

199
calsocial/account.py Normal file
View File

@ -0,0 +1,199 @@
# Calendar.social
# Copyright (C) 2018 Gergely Polonkai
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Main module for the Calendar.social app
"""
from flask import Blueprint, abort, current_app, redirect, render_template, url_for
from flask_security import current_user, login_required
from sqlalchemy.orm.exc import NoResultFound
from calsocial.utils import RoutedMixin
class AccountBlueprint(Blueprint, RoutedMixin):
"""Blueprint for account management
"""
def __init__(self):
Blueprint.__init__(self, 'account', __name__)
self.app = None
RoutedMixin.register_routes(self)
def init_app(self, app, url_prefix=None):
"""Initialise the blueprint, registering it with ``app``.
"""
self.app = app
app.register_blueprint(self, url_prefix=url_prefix)
@staticmethod
@RoutedMixin.route('/register')
def register_account():
"""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('account/registration.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('account/user-settings.html', form=form)
@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('account/notifications.html', notifs=notifs)
@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('account/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('account.edit_profile'))
return render_template('account/profile-edit.html', form=form)
@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('account/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('account.follow_requests'))

View File

@ -7,7 +7,7 @@
{% for req in requests %} {% for req in requests %}
<li> <li>
{{ req.follower }} {{ req.follower }}
<a href="{{ url_for('accept_follow', follower_id=req.follower_id) }}" class="ui button">{% trans %}Accept{% endtrans %}</a> <a href="{{ url_for('account.accept_follow', follower_id=req.follower_id) }}" class="ui button">{% trans %}Accept{% endtrans %}</a>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>

View File

@ -1,4 +1,4 @@
{% extends 'settings-base.html' %} {% extends 'account/settings-base.html' %}
{% from '_macros.html' import field %} {% from '_macros.html' import field %}
{% block settings_content %} {% block settings_content %}

View File

@ -0,0 +1,15 @@
{% extends 'base.html' %}
{% block content %}
<div class="ui grid">
<div class="four wide column">
<div class="ui secondary pointing vertical menu">
<a class="item{% if request.endpoint == 'account.edit_profile' %} active{% endif %}" href="{{ url_for('account.edit_profile') }}">{% trans %}Edit profile{% endtrans %}</a>
<a class="item{% if request.endpoint == 'account.settings' %} active{% endif %}" href="{{ url_for('account.settings') }}">{% trans %}Settings{% endtrans %}</a>
</div>
</div>
<div class="twelve wide stretched column">
{% block settings_content %}{% endblock %}
</div>
</div>
{% endblock %}

View File

@ -1,4 +1,4 @@
{% extends 'settings-base.html' %} {% extends 'account/settings-base.html' %}
{% from '_macros.html' import field %} {% from '_macros.html' import field %}
{% block settings_content %} {% block settings_content %}

View File

@ -34,8 +34,8 @@
{%- endtrans %} {%- endtrans %}
</div> </div>
<a class="item" href="{{ url_for('hello') }}">{% trans %}Calendar view{% endtrans %}</a> <a class="item" href="{{ url_for('hello') }}">{% trans %}Calendar view{% endtrans %}</a>
<a class="item" href="{{ url_for('notifications') }}">{% trans %}Notifications{% endtrans %}</a> <a class="item" href="{{ url_for('account.notifications') }}">{% trans %}Notifications{% endtrans %}</a>
<a class="item" href="{{ url_for('settings') }}">{% trans %}Settings{% endtrans %}</a> <a class="item" href="{{ url_for('account.settings') }}">{% trans %}Settings{% endtrans %}</a>
<a class="item" href="{{ url_for('security.logout') }}">{% trans %}Logout{% endtrans %}</a> <a class="item" href="{{ url_for('security.logout') }}">{% trans %}Logout{% endtrans %}</a>
{% endif %} {% endif %}
</div> </div>
@ -50,6 +50,6 @@
</footer> </footer>
{% block scripts %}{% endblock %} {% block scripts %}{% endblock %}
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="{{ url_for('static', filename='semantic/semantic.min.js') }}"></script> <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" type="text/css"> <script src="{{ url_for('static', filename='semantic/semantic.min.js') }}"></script>
</body> </body>
</html> </html>

View File

@ -1,15 +0,0 @@
{% extends 'base.html' %}
{% block content %}
<div class="ui grid">
<div class="four wide column">
<div class="ui secondary pointing vertical menu">
<a class="item{% if request.endpoint == 'edit_profile' %} active{% endif %}" href="{{ url_for('edit_profile') }}">{% trans %}Edit profile{% endtrans %}</a>
<a class="item{% if request.endpoint == 'settings' %} active{% endif %}" href="{{ url_for('settings') }}">{% trans %}Settings{% endtrans %}</a>
</div>
</div>
<div class="twelve wide stretched column">
{% block settings_content %}{% endblock %}
</div>
</div>
{% endblock %}

View File

@ -18,7 +18,7 @@
<div class="ui horizontal divider"> <div class="ui horizontal divider">
{% trans %}Or{% endtrans %} {% trans %}Or{% endtrans %}
</div> </div>
<a href="{{ url_for('register' ) }}" class="fluid ui button">{% trans %}Register an account{% endtrans %}</a> <a href="{{ url_for('account.register_account' ) }}" class="fluid ui button">{% trans %}Register an account{% endtrans %}</a>
{% endif %} {% endif %}
<div class="ui horizontal divider"></div> <div class="ui horizontal divider"></div>
{% endif %} {% endif %}