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

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

from datetime import datetime
from uuid import UUID

from flask import abort, g, request

from ...services.consent import consent_service, subject_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 create_consent_form, get_subject_field_name


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 = _get_verification_token_or_404(token)

    user_id = verification_token.user_id

    unconsented_subjects = _get_unconsented_subjects_for_user(user_id)

    ConsentForm = create_consent_form(unconsented_subjects)
    form = erroneous_form if erroneous_form else ConsentForm()

    subjects_and_fields = _get_subjects_and_fields(unconsented_subjects, form)

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


def _get_subjects_and_fields(subjects, form):
    field_names = [get_subject_field_name(subject) for subject in subjects]
    fields = [getattr(form, field_name) for field_name in field_names]
    return list(zip(subjects, fields))


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

    user_id = verification_token.user_id

    unconsented_subjects = _get_unconsented_subjects_for_user(user_id)

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

    subject_ids_from_form = [UUID(id_str)
                             for id_str in form.subject_ids.data.split(',')]

    for subject_id in subject_ids_from_form:
        subject = subject_service.find_subject(subject_id)
        if subject is None:
            flash_error('Unbekanntes Zustimmungsthema')
            return consent_form(token, erroneous_form=form)

    expressed_at = datetime.utcnow()
    consent_service.consent_to_subjects(subject_ids_from_form, expressed_at,
                                        verification_token)

    flash_success('Vielen Dank für deine Zustimmung. Bitte melde dich erneut an.')
    return redirect_to('authentication.login_form')


def _get_unconsented_subjects_for_user(user_id):
    terms_version = terms_version_service.find_current_version(g.brand_id)
    return [terms_version.consent_subject]


def _get_verification_token_or_404(token_str):
    verification_token = verification_token_service \
        .find_for_terms_consent_by_token(token_str)

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

    return verification_token<commit_msg>Include privacy policy 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 uuid import UUID

from flask import abort, g, request

from ...services.consent import consent_service, subject_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 ..authentication.views import _get_required_consent_subject_ids

from .forms import create_consent_form, get_subject_field_name


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 = _get_verification_token_or_404(token)

    user_id = verification_token.user_id

    unconsented_subjects = _get_unconsented_subjects_for_user(user_id)

    ConsentForm = create_consent_form(unconsented_subjects)
    form = erroneous_form if erroneous_form else ConsentForm()

    subjects_and_fields = _get_subjects_and_fields(unconsented_subjects, form)

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


def _get_subjects_and_fields(subjects, form):
    field_names = [get_subject_field_name(subject) for subject in subjects]
    fields = [getattr(form, field_name) for field_name in field_names]
    return list(zip(subjects, fields))


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

    user_id = verification_token.user_id

    unconsented_subjects = _get_unconsented_subjects_for_user(user_id)

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

    subject_ids_from_form = [UUID(id_str)
                             for id_str in form.subject_ids.data.split(',')]

    for subject_id in subject_ids_from_form:
        subject = subject_service.find_subject(subject_id)
        if subject is None:
            flash_error('Unbekanntes Zustimmungsthema')
            return consent_form(token, erroneous_form=form)

    expressed_at = datetime.utcnow()
    consent_service.consent_to_subjects(subject_ids_from_form, expressed_at,
                                        verification_token)

    flash_success('Vielen Dank für deine Zustimmung. Bitte melde dich erneut an.')
    return redirect_to('authentication.login_form')


def _get_unconsented_subjects_for_user(user_id):
    required_consent_subject_ids = _get_required_consent_subject_ids()

    unconsented_subject_ids = _get_unconsented_subject_ids(
        user_id, required_consent_subject_ids)

    return _get_subjects(unconsented_subject_ids)


def _get_unconsented_subject_ids(user_id, required_subject_ids):
    subject_ids = []

    for subject_id in required_subject_ids:
        if not consent_service.has_user_consented_to_subject(user_id, subject_id):
            subject_ids.append(subject_id)

    return subject_ids


def _get_subjects(subject_ids):
    return [subject_service.find_subject(subject_id)
            for subject_id in subject_ids]


def _get_verification_token_or_404(token_str):
    verification_token = verification_token_service \
        .find_for_terms_consent_by_token(token_str)

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

    return verification_token
{'fraction_non_alphanumeric': 0.08722421754746024, 'fraction_numerical': 0.00513083632632119, 'mean_word_length': 4.355082417582418, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5264129', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2351, 'n_words': 501}