<commit_before>"""
byceps.blueprints.terms.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime

from flask import abort, g, request

from ...services.consent import consent_service
from ...services.terms import version_service as terms_version_service
from ...services.verification_token import service as verification_token_service
from ...util.framework.blueprint import create_blueprint
from ...util.framework.flash import flash_error, flash_success
from ...util.framework.templating import templated
from ...util.views import redirect_to

from .forms import ConsentForm


blueprint = create_blueprint('terms', __name__)


@blueprint.route('/')
@templated
def view_current():
    """Show the current version of this brand's terms and conditions."""
    version = terms_version_service.get_current_version(g.brand_id)

    return {
        'version': version,
    }


@blueprint.route('/consent/<uuid:token>')
@templated
def consent_form(token, *, erroneous_form=None):
    """Show that version of the terms, and a form to consent to it."""
    terms_version = terms_version_service.find_current_version(g.brand_id)

    verification_token = verification_token_service \
        .find_for_terms_consent_by_token(token)

    if verification_token is None:
        flash_error('Unbekannter Bestätigungscode.')
        abort(404)

    form = erroneous_form if erroneous_form \
        else ConsentForm(
            terms_consent_subject_id=terms_version.consent_subject_id)

    return {
        'terms_version': terms_version,
        'token': token,
        'form': form,
    }


@blueprint.route('/consent/<uuid:token>', methods=['POST'])
def consent(token):
    """Consent to that version of the terms."""
    verification_token = verification_token_service \
        .find_for_terms_consent_by_token(token)

    if verification_token is None:
        flash_error('Unbekannter Bestätigungscode.')
        abort(404)

    form = ConsentForm(request.form)
    if not form.validate():
        return consent_form(token, erroneous_form=form)

    terms_consent_subject_id = form.terms_consent_subject_id.data
    terms_version = terms_version_service \
        .find_version_for_consent_subject_id(terms_consent_subject_id)<commit_msg>Fetch terms version *after* validation of verification token in consent form<commit_after>"""
byceps.blueprints.terms.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime

from flask import abort, g, request

from ...services.consent import consent_service
from ...services.terms import version_service as terms_version_service
from ...services.verification_token import service as verification_token_service
from ...util.framework.blueprint import create_blueprint
from ...util.framework.flash import flash_error, flash_success
from ...util.framework.templating import templated
from ...util.views import redirect_to

from .forms import ConsentForm


blueprint = create_blueprint('terms', __name__)


@blueprint.route('/')
@templated
def view_current():
    """Show the current version of this brand's terms and conditions."""
    version = terms_version_service.get_current_version(g.brand_id)

    return {
        'version': version,
    }


@blueprint.route('/consent/<uuid:token>')
@templated
def consent_form(token, *, erroneous_form=None):
    """Show that version of the terms, and a form to consent to it."""
    verification_token = verification_token_service \
        .find_for_terms_consent_by_token(token)

    if verification_token is None:
        flash_error('Unbekannter Bestätigungscode.')
        abort(404)

    terms_version = terms_version_service.find_current_version(g.brand_id)

    form = erroneous_form if erroneous_form \
        else ConsentForm(
            terms_consent_subject_id=terms_version.consent_subject_id)

    return {
        'terms_version': terms_version,
        'token': token,
        'form': form,
    }


@blueprint.route('/consent/<uuid:token>', methods=['POST'])
def consent(token):
    """Consent to that version of the terms."""
    verification_token = verification_token_service \
        .find_for_terms_consent_by_token(token)

    if verification_token is None:
        flash_error('Unbekannter Bestätigungscode.')
        abort(404)

    form = ConsentForm(request.form)
    if not form.validate():
        return consent_form(token, erroneous_form=form)

    terms_consent_subject_id = form.terms_consent_subject_id.data
    terms_version = terms_version_service \
        .find_version_for_consent_subject_id(terms_consent_subject_id)
{'fraction_non_alphanumeric': 0.10450602919399196, 'fraction_numerical': 0.005923418658768775, 'mean_word_length': 4.155943293347874, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7198618', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1369, 'n_words': 327}