# Path: amivapi/settings.py
# EMAIL_REGEX = '^.+@.+$'
# 
# Path: amivapi/events/authorization.py
# class EventAuth(AmivTokenAuth):
#     """Auth for events."""
# 
#     def has_item_write_permission(self, user_id, item):
#         """The group moderator is allowed to change things."""
#         # Return true if a moderator exists and it is equal to the current user
#         return item.get('moderator') and (
#                 user_id == str(get_id(item['moderator'])))
# 
# class EventSignupAuth(AmivTokenAuth):
#     def create_user_lookup_filter(self, user_id):
#         """Users can see own signups and signups for moderated events.
#         """
#         # Find events the user moderates
#         event_collection = current_app.data.driver.db['events']
#         events = event_collection.find({'moderator': ObjectId(user_id)},
#                                        {'_id': 1})
#         moderated_events = [event['_id'] for event in events]
# 
#         return {'$or': [
#             {'user': user_id},
#             {'event': {'$in': moderated_events}}
#         ]}
# 
#     def has_item_write_permission(self, user_id, item):
#         """Users can modify their signups within the registration window.
#             Moderators can not modify signups from other users.
#         """
#         if isinstance(item['event'], dict):
#             event = item['event']
#         else:
#             # Event is not embedded, get the event first
#             lookup = {current_app.config['ID_FIELD']: item['event']}
#             event = current_app.data.find_one('events', None, **lookup)
# 
#         # Remove tzinfo to compare to utcnow (API only accepts UTC anyways)
#         time_register_start = event['time_register_start'].replace(tzinfo=None)
#         time_register_end = event['time_register_end'].replace(tzinfo=None)
# 
#         # Only the user itself can modify the item (not moderators), and only
#         # within the signup window
#         return (('user' in item) and
#                 (user_id == str(get_id(item['user']))) and
#                 (time_register_start <= dt.utcnow() <= time_register_end))
# 
#     def has_resource_write_permission(self, user_id):
#         """Anyone can sign up. Further requirements are enforced with validators
#         to allow precise error messages.
# 
#         Users may only sign themselves up and anyone may POST with an email
#         address.
#         """
#         return True


from amivapi.settings import EMAIL_REGEX
from .authorization import EventAuth, EventSignupAuth
                               'or unconfirmed email signups.',

                'readonly': True,
                'type': 'integer'
            },
            'moderator': {
                'description': '`_id` of a user which will be the event '
                               'moderator, who can modify the event.',
                'example': 'ed1ac3fa99034762f7b55e5a',

                'type': 'objectid',
                'data_relation': {
                    'resource': 'users',
                    'embeddable': True,
                },
                'nullable': True,
                'default': None,
            },
        },
    },

    'eventsignups': {
        'resource_title': 'Event Signups',
        'item_title': 'Event Signup',

        'description': description_signups,

        'resource_methods': ['GET', 'POST'],
        'item_methods': ['GET', 'PATCH', 'DELETE'],


# Path: amivapi/settings.py
# EMAIL_REGEX = '^.+@.+$'
# 
# Path: amivapi/events/authorization.py
# class EventAuth(AmivTokenAuth):
#     """Auth for events."""
# 
#     def has_item_write_permission(self, user_id, item):
#         """The group moderator is allowed to change things."""
#         # Return true if a moderator exists and it is equal to the current user
#         return item.get('moderator') and (
#                 user_id == str(get_id(item['moderator'])))
# 
# class EventSignupAuth(AmivTokenAuth):
#     def create_user_lookup_filter(self, user_id):
#         """Users can see own signups and signups for moderated events.
#         """
#         # Find events the user moderates
#         event_collection = current_app.data.driver.db['events']
#         events = event_collection.find({'moderator': ObjectId(user_id)},
#                                        {'_id': 1})
#         moderated_events = [event['_id'] for event in events]
# 
#         return {'$or': [
#             {'user': user_id},
#             {'event': {'$in': moderated_events}}
#         ]}
# 
#     def has_item_write_permission(self, user_id, item):
#         """Users can modify their signups within the registration window.
#             Moderators can not modify signups from other users.
#         """
#         if isinstance(item['event'], dict):
#             event = item['event']
#         else:
#             # Event is not embedded, get the event first
#             lookup = {current_app.config['ID_FIELD']: item['event']}
#             event = current_app.data.find_one('events', None, **lookup)
# 
#         # Remove tzinfo to compare to utcnow (API only accepts UTC anyways)
#         time_register_start = event['time_register_start'].replace(tzinfo=None)
#         time_register_end = event['time_register_end'].replace(tzinfo=None)
# 
#         # Only the user itself can modify the item (not moderators), and only
#         # within the signup window
#         return (('user' in item) and
#                 (user_id == str(get_id(item['user']))) and
#                 (time_register_start <= dt.utcnow() <= time_register_end))
# 
#     def has_resource_write_permission(self, user_id):
#         """Anyone can sign up. Further requirements are enforced with validators
#         to allow precise error messages.
# 
#         Users may only sign themselves up and anyone may POST with an email
#         address.
#         """
#         return True


# Path: amivapi/events/model.py
from amivapi.settings import EMAIL_REGEX
from .authorization import EventAuth, EventSignupAuth

                               'or unconfirmed email signups.',

                'readonly': True,
                'type': 'integer'
            },
            'moderator': {
                'description': '`_id` of a user which will be the event '
                               'moderator, who can modify the event.',
                'example': 'ed1ac3fa99034762f7b55e5a',

                'type': 'objectid',
                'data_relation': {
                    'resource': 'users',
                    'embeddable': True,
                },
                'nullable': True,
                'default': None,
            },
        },
    },

    'eventsignups': {
        'resource_title': 'Event Signups',
        'item_title': 'Event Signup',

        'description': description_signups,

        'resource_methods': ['GET', 'POST'],
        'item_methods': ['GET', 'PATCH', 'DELETE'],


        'authentication': EventSignupAuth,