# Path: amivapi/events/emails.py
# def notify_signup_accepted(event, signup):
#     """Send an email to a user that his signup was accepted"""
#     id_field = current_app.config['ID_FIELD']
# 
#     if signup.get('user'):
#         lookup = {id_field: signup['user']}
#         user = current_app.data.find_one('users', None, **lookup)
#         name = user['firstname']
#         email = user['email']
#     else:
#         name = 'Guest of AMIV'
#         email = signup['email']
# 
#     s = URLSafeSerializer(get_token_secret())
#     token = s.dumps(str(signup[id_field]))
# 
#     if current_app.config.get('SERVER_NAME') is None:
#         current_app.logger.warning("SERVER_NAME is not set. E-Mail links "
#                                    "will not work!")
# 
#     deletion_link = url_for('emails.on_delete_signup', token=token,
#                             _external=True)
# 
#     mail([email],
#          'Eventsignup accepted',
#          current_app.config['ACCEPT_EMAIL_TEXT'].format(
#              name=name,
#              title=event.get('title_en') or event.get('title_de'),
#              link=deletion_link,
#              deadline=event['time_register_end'].strftime('%H.%M %d.%m.%Y')))


from flask import current_app, g
from pymongo import ASCENDING
from amivapi.events.emails import notify_signup_accepted
    id_field = current_app.config['ID_FIELD']
    lookup = {id_field: event_id}
    event = current_app.data.find_one('events', None, **lookup)

    accepted_ids = []

    if event['selection_strategy'] == 'fcfs':
        lookup = {'event': event_id, 'accepted': True}
        signup_count = (
            current_app.data.driver.db['eventsignups'].count_documents(lookup))

        # 0 spots == infinite spots
        if event['spots'] == 0 or signup_count < event['spots']:
            lookup = {'event': event_id, 'accepted': False, 'confirmed': True}
            new_list = current_app.data.driver.db['eventsignups'].find(
                lookup).sort('_created', ASCENDING)

            if event['spots'] > 0:
                to_accept = new_list.limit(event['spots'] - signup_count)
            else:
                # infinite spots, so just accept everyone
                to_accept = new_list

            for new_accepted in to_accept:
                accepted_ids.append(new_accepted['_id'])
                # Set accepted flag
                current_app.data.update('eventsignups', new_accepted[id_field],
                                        {'accepted': True}, new_accepted)

                # Notify user

# Path: amivapi/events/emails.py
# def notify_signup_accepted(event, signup):
#     """Send an email to a user that his signup was accepted"""
#     id_field = current_app.config['ID_FIELD']
# 
#     if signup.get('user'):
#         lookup = {id_field: signup['user']}
#         user = current_app.data.find_one('users', None, **lookup)
#         name = user['firstname']
#         email = user['email']
#     else:
#         name = 'Guest of AMIV'
#         email = signup['email']
# 
#     s = URLSafeSerializer(get_token_secret())
#     token = s.dumps(str(signup[id_field]))
# 
#     if current_app.config.get('SERVER_NAME') is None:
#         current_app.logger.warning("SERVER_NAME is not set. E-Mail links "
#                                    "will not work!")
# 
#     deletion_link = url_for('emails.on_delete_signup', token=token,
#                             _external=True)
# 
#     mail([email],
#          'Eventsignup accepted',
#          current_app.config['ACCEPT_EMAIL_TEXT'].format(
#              name=name,
#              title=event.get('title_en') or event.get('title_de'),
#              link=deletion_link,
#              deadline=event['time_register_end'].strftime('%H.%M %d.%m.%Y')))


# Path: amivapi/events/queue.py
from flask import current_app, g
from pymongo import ASCENDING
from amivapi.events.emails import notify_signup_accepted

    id_field = current_app.config['ID_FIELD']
    lookup = {id_field: event_id}
    event = current_app.data.find_one('events', None, **lookup)

    accepted_ids = []

    if event['selection_strategy'] == 'fcfs':
        lookup = {'event': event_id, 'accepted': True}
        signup_count = (
            current_app.data.driver.db['eventsignups'].count_documents(lookup))

        # 0 spots == infinite spots
        if event['spots'] == 0 or signup_count < event['spots']:
            lookup = {'event': event_id, 'accepted': False, 'confirmed': True}
            new_list = current_app.data.driver.db['eventsignups'].find(
                lookup).sort('_created', ASCENDING)

            if event['spots'] > 0:
                to_accept = new_list.limit(event['spots'] - signup_count)
            else:
                # infinite spots, so just accept everyone
                to_accept = new_list

            for new_accepted in to_accept:
                accepted_ids.append(new_accepted['_id'])
                # Set accepted flag
                current_app.data.update('eventsignups', new_accepted[id_field],
                                        {'accepted': True}, new_accepted)

                # Notify user

                notify_signup_accepted(event, new_accepted)