~homeworkprod/byceps

3cf502460f3f3efa7c76e1a063ef143254ea377e — Jochen Kupperschmidt 1 year, 3 months ago 4c245aa
Rename "site mode" to "app mode"
M byceps/application.py => byceps/application.py +4 -4
@@ 79,7 79,7 @@ def _get_blueprints(app: Flask) -> Iterator[BlueprintReg]:
    """Yield blueprints to register on the application."""
    yield from _get_blueprints_common()

    current_mode = config.get_site_mode(app)
    current_mode = config.get_app_mode(app)
    if current_mode.is_public():
        yield from _get_blueprints_site()
    elif current_mode.is_admin():


@@ 212,8 212,8 @@ def init_app(app: Flask) -> None:
    with app.app_context():
        _set_url_root_path(app)

        site_mode = config.get_site_mode()
        if site_mode.is_public():
        app_mode = config.get_app_mode()
        if app_mode.is_public():
            # Incorporate site-specific template overrides.
            app.jinja_loader = SiteTemplateOverridesLoader()



@@ 223,7 223,7 @@ def init_app(app: Flask) -> None:

        _load_announce_signal_handlers()

        if site_mode.is_admin() and app.config['RQ_DASHBOARD_ENABLED']:
        if app_mode.is_admin() and app.config['RQ_DASHBOARD_ENABLED']:
            import rq_dashboard
            app.register_blueprint(
                rq_dashboard.blueprint, url_prefix='/admin/rq'

M byceps/blueprints/authentication/views.py => byceps/blueprints/authentication/views.py +4 -4
@@ 10,7 10,7 @@ from typing import Optional

from flask import abort, g, request, url_for

from ...config import get_site_mode
from ...config import get_app_mode
from ...services.authentication.exceptions import AuthenticationFailed
from ...services.authentication import service as authentication_service
from ...services.authentication.password import service as password_service


@@ 62,7 62,7 @@ def login_form():
            'angemeldet.'
        )

    in_admin_mode = get_site_mode().is_admin()
    in_admin_mode = get_app_mode().is_admin()

    if not _is_login_enabled(in_admin_mode):
        return {


@@ 87,7 87,7 @@ def login():
    if g.current_user.is_active:
        return

    in_admin_mode = get_site_mode().is_admin()
    in_admin_mode = get_app_mode().is_admin()

    if not _is_login_enabled(in_admin_mode):
        abort(403, 'Log in to this site is generally disabled.')


@@ 271,7 271,7 @@ def request_password_reset():


def _get_sender() -> Optional[Sender]:
    if not get_site_mode().is_public():
    if not get_app_mode().is_public():
        return None

    site = site_service.get_site(g.site_id)

M byceps/blueprints/authorization/registry.py => byceps/blueprints/authorization/registry.py +1 -1
@@ 32,7 32,7 @@ class PermissionRegistry:
        if enum is None:
            # No enum found for that key. This happens if the blueprint
            # which contains the authorization enum is not registered in
            # the current site mode (public/admin).
            # the current app mode (public/admin).
            return None

        try:

M byceps/blueprints/core/templates/layout/base_auto.html => byceps/blueprints/core/templates/layout/base_auto.html +1 -1
@@ 1,1 1,1 @@
{% extends ('layout/admin/base.html' if g.site_mode.is_admin() else 'layout/base.html') %}
{% extends ('layout/admin/base.html' if g.app_mode.is_admin() else 'layout/base.html') %}

M byceps/blueprints/core/views.py => byceps/blueprints/core/views.py +7 -7
@@ 74,19 74,19 @@ def is_current_page(nav_item_path, current_page=None):


@blueprint.before_app_request
def provide_site_mode():
    # site mode
    site_mode = config.get_site_mode()
    g.site_mode = site_mode
def provide_app_mode():
    # app mode
    app_mode = config.get_app_mode()
    g.app_mode = app_mode

    # site ID
    if site_mode.is_public():
    if app_mode.is_public():
        site_id = config.get_current_site_id()
        g.site_id = site_id

    # current party and brand
    party_id = None
    if site_mode.is_public():
    if app_mode.is_public():
        site = site_service.get_site(site_id)

        party_id = site.party_id


@@ 101,7 101,7 @@ def provide_site_mode():
            g.brand_id = None

    # current user
    is_admin_mode = site_mode.is_admin()
    is_admin_mode = app_mode.is_admin()
    g.current_user = authentication_blueprint_service.get_current_user(
        is_admin_mode, party_id=party_id
    )

M byceps/blueprints/user/creation/views.py => byceps/blueprints/user/creation/views.py +2 -2
@@ 11,7 11,7 @@ from typing import Optional

from flask import abort, g, request

from ....config import get_site_mode
from ....config import get_app_mode
from ....services.brand import settings_service as brand_settings_service
from ....services.consent.transfer.models import Consent, SubjectID
from ....services.newsletter.transfer.models import (


@@ 182,7 182,7 @@ def _abort_if_user_account_creation_disabled():


def _is_user_account_creation_enabled():
    if get_site_mode().is_admin():
    if get_app_mode().is_admin():
        return False

    site = site_service.get_site(g.site_id)

M byceps/blueprints/user/current/templates/user/current/view.html => byceps/blueprints/user/current/templates/user/current/view.html +1 -1
@@ 21,7 21,7 @@
  {%- with label_column_width = '8rem', data_column_min_width = '12rem' %}
{%- include 'user/current/_account.html' %}
{%- include 'user/current/_details_personal.html' %}
    {%- if g.site_mode.is_public() and newsletter_offered %}
    {%- if g.app_mode.is_public() and newsletter_offered %}
{%- include 'user/current/_newsletter.html' %}
    {%- endif %}
  {%- endwith %}

M byceps/blueprints/user/current/views.py => byceps/blueprints/user/current/views.py +3 -3
@@ 8,7 8,7 @@ byceps.blueprints.user.current.views

from flask import abort, g, jsonify, request, Response

from ....config import get_site_mode
from ....config import get_app_mode
from ....services.country import service as country_service
from ....services.newsletter import service as newsletter_service
from ....services.user import command_service as user_command_service


@@ 40,7 40,7 @@ def view():
    if user is None:
        abort(404)

    if get_site_mode().is_public():
    if get_app_mode().is_public():
        newsletter_list_id = _find_newsletter_list_for_brand()
        newsletter_offered = (newsletter_list_id is not None)



@@ 63,7 63,7 @@ def view():
@blueprint.route('/me.json')
def view_as_json():
    """Show selected attributes of the current user's profile as JSON."""
    if get_site_mode().is_admin():
    if get_app_mode().is_admin():
        abort(404)

    user = g.current_user

M byceps/config.py => byceps/config.py +15 -15
@@ 15,13 15,13 @@ from .services.site.transfer.models import SiteID


EXTENSION_KEY = 'byceps_config'
KEY_SITE_MODE = 'site_mode'
KEY_APP_MODE = 'app_mode'
KEY_SITE_ID = 'site_id'


SiteMode = Enum('SiteMode', ['public', 'admin'])
SiteMode.is_admin = lambda self: self == SiteMode.admin
SiteMode.is_public = lambda self: self == SiteMode.public
AppMode = Enum('AppMode', ['public', 'admin'])
AppMode.is_admin = lambda self: self == AppMode.admin
AppMode.is_public = lambda self: self == AppMode.public


class ConfigurationError(Exception):


@@ 31,10 31,10 @@ class ConfigurationError(Exception):
def init_app(app: Flask) -> None:
    app.extensions[EXTENSION_KEY] = {}

    site_mode = _determine_site_mode(app)
    set_extension_value(KEY_SITE_MODE, site_mode, app)
    app_mode = _determine_app_mode(app)
    set_extension_value(KEY_APP_MODE, app_mode, app)

    if site_mode.is_public():
    if app_mode.is_public():
        site_id = _determine_site_id(app)
        set_extension_value(KEY_SITE_ID, site_id, app)



@@ 62,23 62,23 @@ def set_extension_value(key: str, value: Any, app: Flask) -> None:


# -------------------------------------------------------------------- #
# site mode
# app mode


def _determine_site_mode(app: Flask) -> SiteMode:
    value = app.config.get('SITE_MODE')
def _determine_app_mode(app: Flask) -> AppMode:
    value = app.config.get('APP_MODE')
    if value is None:
        raise ConfigurationError('No site mode configured.')
        raise ConfigurationError('No app mode configured.')

    try:
        return SiteMode[value]
        return AppMode[value]
    except KeyError:
        raise ConfigurationError(f'Invalid site mode "{value}" configured.')
        raise ConfigurationError(f'Invalid app mode "{value}" configured.')


def get_site_mode(app: Optional[Flask]=None) -> SiteMode:
def get_app_mode(app: Optional[Flask]=None) -> AppMode:
    """Return the mode the site should run in."""
    return get_extension_value(KEY_SITE_MODE, app)
    return get_extension_value(KEY_APP_MODE, app)


# -------------------------------------------------------------------- #

M config/development_admin.py => config/development_admin.py +1 -1
@@ 7,7 7,7 @@ REDIS_URL = 'redis://127.0.0.1:6379/0'

RQ_DASHBOARD_ENABLED = True

SITE_MODE = 'admin'
APP_MODE = 'admin'

MAIL_DEBUG = True
MAIL_DEFAULT_SENDER = 'BYCEPS <noreply@example.com>'

M config/development_party.py => config/development_party.py +1 -1
@@ 8,7 8,7 @@ SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://byceps:boioioing@127.0.0.1/byce

REDIS_URL = 'redis://127.0.0.1:6379/0'

SITE_MODE = 'public'
APP_MODE = 'public'
SITE_ID = 'example-dev'

MAIL_DEBUG = True

M config/production_party.py => config/production_party.py +1 -1
@@ 15,7 15,7 @@ SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://byceps:boioioing@127.0.0.1/byce

REDIS_URL = 'unix:///var/run/redis/redis.sock?db=0'

SITE_MODE = 'public'
APP_MODE = 'public'
SITE_ID = 'example-prod'

MAIL_DEBUG = False

M config/test_admin.py => config/test_admin.py +1 -1
@@ 9,7 9,7 @@ SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://byceps_test:test@127.0.0.1/byce

REDIS_URL = 'redis://127.0.0.1:6379/0'

SITE_MODE = 'admin'
APP_MODE = 'admin'

MAIL_DEBUG = False
MAIL_DEFAULT_SENDER = 'BYCEPS <noreply@example.com>'

M config/test_party.py => config/test_party.py +1 -1
@@ 9,7 9,7 @@ SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://byceps_test:test@127.0.0.1/byce

REDIS_URL = 'redis://127.0.0.1:6379/0'

SITE_MODE = 'public'
APP_MODE = 'public'
SITE_ID = 'acmecon-2014-website'

MAIL_DEBUG = False