# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Invenio module that adds userprofiles to the platform."""
from __future__ import absolute_import, print_function
from flask import Blueprint, current_app, flash, render_template, request
from flask_babelex import lazy_gettext as _
from flask_breadcrumbs import register_breadcrumb
from flask_login import current_user, login_required
from flask_menu import register_menu
from flask_security.confirmable import send_confirmation_instructions
from invenio_db import db
from .api import current_userprofile
from .forms import EmailProfileForm, ProfileForm, VerificationForm, \
confirm_register_form_factory, register_form_factory
from .models import UserProfile
blueprint = Blueprint(
'invenio_userprofiles',
__name__,
template_folder='templates',
)
blueprint_api_init = Blueprint(
'invenio_userprofiles_api_init',
__name__,
template_folder='templates',
)
blueprint_ui_init = Blueprint(
'invenio_userprofiles_ui_init',
__name__,
)
[docs]def init_common(app):
"""Post initialization."""
if app.config['USERPROFILES_EXTEND_SECURITY_FORMS']:
security_ext = app.extensions['security']
security_ext.confirm_register_form = confirm_register_form_factory(
security_ext.confirm_register_form)
security_ext.register_form = register_form_factory(
security_ext.register_form)
@blueprint_ui_init.record_once
def init_ui(state):
"""Post initialization for UI application."""
app = state.app
init_common(app)
# Register blueprint for templates
app.register_blueprint(
blueprint, url_prefix=app.config['USERPROFILES_PROFILE_URL'])
@blueprint_api_init.record_once
def init_api(state):
"""Post initialization for API application."""
init_common(state.app)
@blueprint.app_template_filter()
[docs]def userprofile(value):
"""Retrieve user profile for a given user id."""
return UserProfile.get_by_userid(int(value))
@blueprint.route('/', methods=['GET', 'POST'])
@login_required
@register_menu(
blueprint, 'settings.profile',
# NOTE: Menu item text (icon replaced by a user icon).
_('%(icon)s Profile', icon='<i class="fa fa-user fa-fw"></i>'),
order=0)
@register_breadcrumb(
blueprint, 'breadcrumbs.settings.profile', _('Profile')
)
[docs]def profile():
"""View for editing profile."""
# Create forms
verification_form = VerificationForm(formdata=None, prefix="verification")
profile_form = profile_form_factory()
# Process forms
form = request.form.get('submit', None)
if form == 'profile':
handle_profile_form(profile_form)
elif form == 'verification':
handle_verification_form(verification_form)
return render_template(
current_app.config['USERPROFILES_PROFILE_TEMPLATE'],
profile_form=profile_form,
verification_form=verification_form,)