<commit_before># -*- coding: utf-8 -*-
#
# license: AGPLv3, see LICENSE for details. In addition we strongly encourage
#          you to buy us beer if we meet and you like the software.
"""Email confirmation logic.

Needed when external users want to sign up for public events.
"""

from itsdangerous import Signer, BadSignature
from bson import ObjectId

from flask import current_app, Blueprint, redirect, url_for

from eve.methods.patch import patch_internal

from amivapi.utils import mail

confirmprint = Blueprint('confirm', __name__)


def send_confirmmail_to_unregistered_users(item):
    """Send a confirmation email for external signups(email only)

    Args:
        item: The item, which was just inserted into the database
    """
    if 'user' not in item:
        event = current_app.data.find_one(
            'events', None,
            **{current_app.config['ID_FIELD']: item['event']})

        if 'title_en' in event:
            title = event['title_en']
        else:
            title = event['title_de']

        token = Signer(current_app.config['TOKEN_SECRET']).sign(
            str(item['_id']))

        fields = {
            'link': url_for('confirm.on_confirm_email', token=token,
                            _external=True),
            'title': title
        }
        email_content = current_app.config['CONFIRM_EMAIL_TEXT'] % fields

        mail(current_app.config['API_MAIL'],  # from
             [item['email']],  # receivers list
             'Registration for AMIV event %s' % title,
             email_content)


def send_confirmmail_to_unregistered_users_bulk(items):
    for item in items:
        send_confirmmail_to_unregistered_users(item)


def add_confirmed_before_insert(item):
    """Add the confirmed field to a event signup before it is inserted to the
    database. We accept all registered users instantly, others need to click the
    confirmation link first"""
    if item.get('user', None) is None:
        item['confirmed'] = False
    else:
        item['confirmed'] = True


def add_confirmed_before_insert_bulk(items):
    for item in items:
        add_confirmed_before_insert(item)


@confirmprint.route('/confirm_email/<token>')
def on_confirm_email(token):
    """Email confirmation endpoint.

    We try to confirm the specified signup and redirect to a webpage.
    """
    try:
        s = Signer(current_app.config['TOKEN_SECRET'])
        signup_id = ObjectId(s.unsign(token))
    except BadSignature:
        return "Unknown token"

    patch_internal('eventsignups', {'confirmed': True},<commit_msg>python 3 fix in signup token handlin<commit_after># -*- coding: utf-8 -*-
#
# license: AGPLv3, see LICENSE for details. In addition we strongly encourage
#          you to buy us beer if we meet and you like the software.
"""Email confirmation logic.

Needed when external users want to sign up for public events.
"""

from itsdangerous import Signer, BadSignature
from bson import ObjectId

from flask import current_app, Blueprint, redirect, url_for

from eve.methods.patch import patch_internal

from amivapi.utils import mail

confirmprint = Blueprint('confirm', __name__)


def send_confirmmail_to_unregistered_users(item):
    """Send a confirmation email for external signups(email only)

    Args:
        item: The item, which was just inserted into the database
    """
    if 'user' not in item:
        event = current_app.data.find_one(
            'events', None,
            **{current_app.config['ID_FIELD']: item['event']})

        if 'title_en' in event:
            title = event['title_en']
        else:
            title = event['title_de']

        token = Signer(current_app.config['TOKEN_SECRET']).sign(
            str(item['_id']).encode('utf-8'))

        fields = {
            'link': url_for('confirm.on_confirm_email', token=token,
                            _external=True),
            'title': title
        }
        email_content = current_app.config['CONFIRM_EMAIL_TEXT'] % fields

        mail(current_app.config['API_MAIL'],  # from
             [item['email']],  # receivers list
             'Registration for AMIV event %s' % title,
             email_content)


def send_confirmmail_to_unregistered_users_bulk(items):
    for item in items:
        send_confirmmail_to_unregistered_users(item)


def add_confirmed_before_insert(item):
    """Add the confirmed field to a event signup before it is inserted to the
    database. We accept all registered users instantly, others need to click the
    confirmation link first"""
    if item.get('user', None) is None:
        item['confirmed'] = False
    else:
        item['confirmed'] = True


def add_confirmed_before_insert_bulk(items):
    for item in items:
        add_confirmed_before_insert(item)


@confirmprint.route('/confirm_email/<token>')
def on_confirm_email(token):
    """Email confirmation endpoint.

    We try to confirm the specified signup and redirect to a webpage.
    """
    try:
        s = Signer(current_app.config['TOKEN_SECRET'])
        signup_id = ObjectId(s.unsign(token).decode('utf-8'))
    except BadSignature:
        return "Unknown token"

    patch_internal('eventsignups', {'confirmed': True},
{'fraction_non_alphanumeric': 0.09802037286181049, 'fraction_numerical': 0.0013453776667307323, 'mean_word_length': 2.9454131918119786, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2348655', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1477, 'n_words': 445}