<commit_before>#!/usr/bin/env python

"""
conference.py -- Udacity conference server-side Python App Engine API;
    uses Google Cloud Endpoints

$Id: conference.py,v 1.25 2014/05/24 23:42:19 wesc Exp wesc $

created by wesc on 2014 apr 21

"""

__author__ = '<EMAIL> (<NAME>)'

from datetime import datetime
from datetime import timedelta

import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote

from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.ext import ndb

from models import ConflictException
from models import Profile
from models import ProfileMiniForm
from models import ProfileForm
from models import StringMessage
from models import BooleanMessage
from models import Conference
from models import ConferenceForm
from models import ConferenceForms
from models import ConferenceQueryForms
from models import Session
from models import SessionForm
from models import SessionForms
from models import SessionType
from models import Wishlist
from models import WishlistForm
from models import Speaker
from models import SpeakerForm
from models import SpeakerForms
from models import TeeShirtSize

from settings import WEB_CLIENT_ID
from settings import ANDROID_CLIENT_ID
from settings import IOS_CLIENT_ID
from settings import ANDROID_AUDIENCE

from utils import getUserId

import logging


# Helper method during development, to log values of variables
def log_values(d={}):
    logging.debug('\n'.join(["{}: {}".format(i[0], i[1]) for i in d.items()]))


EMAIL_SCOPE = endpoints.EMAIL_SCOPE
API_EXPLORER_CLIENT_ID = endpoints.API_EXPLORER_CLIENT_ID
MEMCACHE_ANNOUNCEMENTS_KEY = "RECENT_ANNOUNCEMENTS"
ANNOUNCEMENT_TPL = ('Last chance to attend! The following conferences '
                    'are nearly sold out: %s')
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

DEFAULTS = {
    "city": "Default City",
    "maxAttendees": 0,
    "seatsAvailable": 0,
    "topics": ["Default", "Topic"],
}

SESSION_DEFAULTS = {
    "duration": 30,
    "speakers": [],
    "typeOfSession": "NOT_SPECIFIED"
}

OPERATORS = {
    'EQ':   '=',
    'GT':   '>',
    'GTEQ': '>=',
    'LT':   '<',
    'LTEQ': '<=',
    'NE':   '!='
}

FIELDS = {
    'CITY': 'city',
    'TOPIC': 'topics',
    'MONTH': 'month',
    'MAX_ATTENDEES': 'maxAttendees',
}

CONF_POST_REQUEST = endpoints.ResourceContainer(
    ConferenceForm,
    websafeConferenceKey=messages.StringField(1),
)

SESSION_GET_REQUEST_FILTERED = endpoints.ResourceContainer(
    message_types.VoidMessage,
    websafeConferenceKey=messages.StringField(1, required=True),
    typeOfSession=messages.StringField(2),  # XXX rename to filter? (generic)
)

SESSION_GET_REQUEST_SPEAKER = endpoints.ResourceContainer(
    message_types.VoidMessage,
    speaker=messages.StringField(1),
)

SESSION_POST_REQUEST = endpoints.ResourceContainer(
    SessionForm,
    websafeConferenceKey=messages.StringField(1),
)

GENERIC_WEBSAFEKEY_REQUEST = endpoints.ResourceContainer(
    message_types.VoidMessage,
    websafeKey=messages.StringField(1, required=True),
)

SESSION_POST_REQUEST_MODIFY_SPEAKERS = endpoints.ResourceContainer(
    websafeSessionKey=messages.StringField(1, required=True),
    websafeSpeakerKey=messages.StringField(2, required=True),
)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


@endpoints.api(name='conference', version='v1', audiences=[ANDROID_AUDIENCE],
               allowed_client_ids=[WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID,
                                   ANDROID_CLIENT_ID, IOS_CLIENT_ID],
               scopes=[EMAIL_SCOPE])
class ConferenceApi(remote.Service):
    """ Conference API v0.1
    """

    @staticmethod
    def get_authed_user():
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization Required')
        return user

# - - - Conference objects - - - - - - - - - - - - - - - - -

    def _copyConferenceToForm(self, conf, displayName):
        """ Copy relevant fields from Conference to ConferenceForm.
        """
        cf = ConferenceForm()
        for field in cf.all_fields():
            if hasattr(conf, field.name):
                # convert Date to date string; just copy others
                if field.name.endswith('Date'):
                    setattr(cf, field.name, str(getattr(conf, field.name)))
                else:
                    setattr(cf, field.name, getattr(conf, field.name))
            elif field.name == "websafeKey":
                setattr(cf, field.name, conf.key.urlsafe())
        if displayName:
            setattr(cf, 'organizerDisplayName', displayName)
        cf.check_initialized()
        return cf

    def _createConferenceObject(self, request):
        """ Create or update Conference object

        This method will return a ConferenceForm/request.
        """
        # preload necessary data items
        user = self.get_authed_user()
        user_id = getUserId(user)

        if not request.name:
            raise endpoints.BadRequestException(
                "Conference 'name' field required")

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name)
                for field in request.all_fields()}
        del data['websafeKey']
        del data['organizerDisplayName']

        # add default values for those missing
        # (both data model & outbound Message)
        for df in DEFAULTS:
            if data[df] in (None, []):
                data[df] = DEFAULTS[df]
                setattr(request, df, DEFAULTS[df])

        # convert dates from strings to Date objects; set month based on
        # start_date
        if data['startDate']:
            data['startDate'] = datetime.strptime(data['startDate'][:10],
                                                  "%Y-%m-%d").date()
            data['month'] = data['startDate'].month
        else:
            data['month'] = 0
        if data['endDate']:
            data['endDate'] = datetime.strptime(data['endDate'][:10],
                                                "%Y-%m-%d").date()

        # set seatsAvailable to be same as maxAttendees, if it's not
        # filled in on creation
        if (data["seatsAvailable"] is None) and (data["maxAttendees"] > 0):
            data["seatsAvailable"] = data["maxAttendees"]
        # generate Profile Key based on user ID and Conference
        # ID based on Profile key get Conference key from ID
        prof_key = ndb.Key(Profile, user_id)
        conf_id = Conference.allocate_ids(size=1, parent=prof_key)[0]
        conf_key = ndb.Key(Conference, conf_id, parent=prof_key)
        data['key'] = conf_key
        data['organizerUserId'] = request.organizerUserId = user_id

        # create Conference, send email to organizer confirming
        # creation of Conference & return (modified) ConferenceForm
        Conference(**data).put()
        taskqueue.add(params={'email': user.email(),
                              'conferenceInfo': repr(request)},
                      url='/tasks/send_confirmation_email')
        return request

    @ndb.transactional()
    def _updateConferenceObject(self, request):
        user = self.get_authed_user()
        user_id = getUserId(user)

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name)
                for field in request.all_fields()}

        # update existing conference
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()
        # check that conference exists
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: {}'.format(
                    request.websafeConferenceKey)
                )

        # check that user is owner
        if user_id != conf.organizerUserId:
            raise endpoints.ForbiddenException(
                'Only the owner can update the conference.')

        # Not getting all the fields, so don't create a new object; just
        # copy relevant fields from ConferenceForm to Conference object
        for field in request.all_fields():
            data = getattr(request, field.name)
            # only copy fields where we get data
            if data not in (None, []):
                # special handling for dates (convert string to Date)
                if field.name in ('startDate', 'endDate'):
                    data = datetime.strptime(data, "%Y-%m-%d").date()
                    if field.name == 'startDate':
                        conf.month = data.month
                # write to Conference object
                setattr(conf, field.name, data)
        conf.put()
        prof = ndb.Key(Profile, user_id).get()
        return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))

    @endpoints.method(ConferenceForm, ConferenceForm, path='conference',
                      http_method='POST', name='createConference')
    def createConference(self, request):
        """ Create new conference.
        """
        return self._createConferenceObject(request)

    @endpoints.method(CONF_POST_REQUEST, ConferenceForm,
                      path='conference/{websafeConferenceKey}',
                      http_method='PUT', name='updateConference')
    def updateConference(self, request):
        """ Update conference with provided fields

        This method will return a ConferenceForm with updated info.
        """
        return self._updateConferenceObject(request)

    @endpoints.method(GENERIC_WEBSAFEKEY_REQUEST, ConferenceForm,
                      path='conference/{websafeKey}',
                      http_method='GET', name='getConference')
    def getConference(self, request):
        """ Return requested conference (by websafeKey)
        """
        # get Conference object from request; bail if not found
        conf = ndb.Key(urlsafe=request.websafeKey).get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: {}'.format(
                    request.websafeKey)
                )
        prof = conf.key.parent().get()
        # return ConferenceForm
        return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))

    @endpoints.method(message_types.VoidMessage, ConferenceForms,
                      path='conferences/created',
                      http_method='POST', name='getConferencesCreated')
    def getConferencesCreated(self, request):
        """ Return conferences created by user
        """
        # make sure user is authed
        user = self.get_authed_user()
        user_id = getUserId(user)

        # create ancestor query for all key matches for this user
        confs = Conference.query(ancestor=ndb.Key(Profile, user_id))
        prof = ndb.Key(Profile, user_id).get()
        # return set of ConferenceForm objects per Conference
        return ConferenceForms(
            items=[
                self._copyConferenceToForm(conf, getattr(prof, 'displayName'))
                for conf in confs]
        )

    def _getQuery(self, request):
        """ Return formatted query from the submitted filters
        """
        q = Conference.query()
        inequality_filter, filters = self._formatFilters(request.filters)

        # If exists, sort on inequality filter first
        if not inequality_filter:
            q = q.order(Conference.name)
        else:
            q = q.order(ndb.GenericProperty(inequality_filter))
            q = q.order(Conference.name)

        for filtr in filters:
            if filtr["field"] in ["month", "maxAttendees"]:
                filtr["value"] = int(filtr["value"])
            formatted_query = ndb.query.FilterNode(
                filtr["field"],
                filtr["operator"],
                filtr["value"])
            q = q.filter(formatted_query)
        return q

    def _formatFilters(self, filters):
        """ Parse, check validity and format user supplied filters
        """
        formatted_filters = []
        inequality_field = None

        for f in filters:
            filtr = {field.name: getattr(f, field.name)
                     for field in f.all_fields()}

            try:
                filtr["field"] = FIELDS[filtr["field"]]
                filtr["operator"] = OPERATORS[filtr["operator"]]
            except KeyError:
                raise endpoints.BadRequestException(
                    "Filter contains invalid field or operator.")

            # Every operation except "=" is an inequality
            if filtr["operator"] != "=":
                # check if inequality operation has been used in previous
                # filters disallow the filter if inequality was performed on a
                # different field before track the field on which the
                # inequality operation is performed
                if inequality_field and inequality_field != filtr["field"]:
                    raise endpoints.BadRequestException(
                        "Inequality filter is allowed on only one field.")
                else:
                    inequality_field = filtr["field"]

            formatted_filters.append(filtr)
        return (inequality_field, formatted_filters)

    def _getConferenceOrganisers(self, conferences):
        """ Return a dict with organiser id's and names

        The dictionary has organiser id's as keys, and their names as values,
        based on queried conferences.
        """
        # need to fetch organiser displayName from profiles
        # get all keys and use get_multi for speed
        organisers = [(ndb.Key(Profile, conf.organizerUserId))
                      for conf in conferences]
        profiles = ndb.get_multi(organisers)

        # put display names in a dict for easier fetching
        names = {}
        for profile in profiles:
            names[profile.key.id()] = profile.displayName
        return names

    @endpoints.method(ConferenceQueryForms, ConferenceForms,
                      path='conferences/query',
                      http_method='POST',
                      name='queryConferences')
    def queryConferences(self, request):
        """ Query for conferences.
        """
        conferences = self._getQuery(request)

        names = self._getConferenceOrganisers(conferences)
        # return individual ConferenceForm object per Conference
        return ConferenceForms(
            items=[self._copyConferenceToForm(
                conf, names[conf.organizerUserId]) for conf in conferences]
        )

    # TASK 3
    def _intersectQueries(self, q1, q2):
        """ Return objects according to an intersection of two queries
        """
        return ndb.get_multi(
            set(q1.fetch(keys_only=True))
            & set(q2.fetch(keys_only=True)))

    # TASK 3
    @endpoints.method(message_types.VoidMessage, ConferenceForms,
                      path='conferences/upcoming',
                      http_method='POST',
                      name='getUpcomingConferences')
    def getUpcomingConferences(self, request):
        """ List all conferences that will be held in the upcoming three months
        """
        date_today = datetime.today().date()
        date_until = (date_today + timedelta(3*365/12))

        confs_from = Conference.query(
            Conference.endDate >= date_today)
        confs_till = Conference.query(
            Conference.startDate <= date_until
        )

        confs = self._intersectQueries(confs_from, confs_till)
        names = self._getConferenceOrganisers(confs)

        return ConferenceForms(
            items=[self._copyConferenceToForm(
                conf, names[conf.organizerUserId]) for conf in confs])

    # TASK 3
    @endpoints.method(message_types.VoidMessage, SessionForms,
                      path='sessions/nonworkshopbefore7',
                      http_method='GET', name='getNonWorkshopsBeforeSevenPM')
    def getNonWorkshopsBeforeSevenPM(self, request):
        """ Only show non-workshop sessions before 7PM
        """
        non_workshop = Session.query(
            Session.typeOfSession != 'WORKSHOP')
        before_seven = Session.query(
            # XXX replace startTime with endTime (which has to be
            # calculated first
            Session.startTime < datetime.strptime("19:00", "%H:%M").time())

        sessions = self._intersectQueries(non_workshop, before_seven)

        return SessionForms(
            items=[self._copySessionToForm(sess) for sess in sessions])

    # TASK 3
    @endpoints.method(message_types.VoidMessage, ConferenceForms,
                      path='conferences/not_sold_out_in_amsterdam',
                      http_method='GET',
                      name='getConferencesNotSoldOutInAmsterdam')
    def getConferencesNotSoldOutInAmsterdam(self, request):
        """ Only show conferences in Amsterdam that are not sold out
        """
        in_amsterdam = Conference.query(
            Conference.city == 'Amsterdam')
        not_sold_out = Conference.query(
            Conference.seatsAvailable > 0)

        confs = self._intersectQueries(in_amsterdam, not_sold_out)
        names = self._getConferenceOrganisers(confs)

        return ConferenceForms(
            items=[self._copyConferenceToForm(
                conf, names[conf.organizerUserId]) for conf in confs])

# - - - Session objects - - - - - - - - - - - - - - - - - - -

    def _copySessionToForm(self, session):
        """ Copy relevant fields from Session to SessionForm
        """
        sf = SessionForm()
        for field in sf.all_fields():
            if hasattr(session, field.name):
                # XXX Save an endTime when startTime and duration is
                # both given
                if field.name in ('date', 'startTime'):
                    # convert Date and Time to date and time string
                    setattr(sf, field.name, str(getattr(session, field.name)))
                elif field.name == 'typeOfSession':
                    # convert typeOfSession string to Enum
                    setattr(sf, field.name, getattr(SessionType,
                            getattr(session, field.name)))
                elif field.name == 'speakers':
                    setattr(sf, field.name, [str(speaker)
                            for speaker in getattr(session, field.name)])
                else:
                    # just copy the others
                    setattr(sf, field.name, getattr(session, field.name))
            elif field.name == "websafeKey":
                setattr(sf, field.name, session.key.urlsafe())
        sf.check_initialized()
        return sf

    def _createSessionObject(self, request):
        """ Create or update Session object, returning SessionForm/request
        """
        # preload necesarry data items
        user = self.get_authed_user()
        user_id = getUserId(user)

        conf_key = ndb.Key(urlsafe=request.websafeConferenceKey)
        conf = conf_key.get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key {}'.format(
                    request.websafeConferenceKey)
            )

        if user_id != conf.organizerUserId:
            raise endpoints.ForbiddenException(
                'Only the owner of the conference may create a session.'
            )

        if not request.name:
            raise endpoints.BadRequestException(
                "Session 'name' field required")
        if not request.websafeConferenceKey:
            raise endpoints.BadRequestException(
                "Session 'websafeConferenceKey' field required")

        # copy SessionForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name)
                for field in request.all_fields()}

        # add default values for those missing (both data model &
        # outbound Message)
        for sf in SESSION_DEFAULTS:
            if data[sf] in (None, []):
                data[sf] = SESSION_DEFAULTS[sf]
                setattr(request, sf, SESSION_DEFAULTS[sf])

        # convert date from string to Date object
        if data['date']:
            data['date'] = datetime.strptime(data['date'], "%Y-%m-%d").date()

        # convert startTime from string to Time
        if data['startTime']:
            data['startTime'] = datetime.strptime(data['startTime'][:5],
                                                  "%H:%M").time()

        data['typeOfSession'] = str(data['typeOfSession'])
        if data['speakers']:
            spkr_keys = [ndb.Key(
                urlsafe=speaker) for speaker in data['speakers']]

            # Update session if there already is a websafeKey
            if data['websafeKey']:
                for speaker in spkr_keys:
                    logging.debug('UPDATING SESSION')
                    self._updateSpeakerForSession(
                        websafeSpeakerKey=speaker.urlsafe(),
                        websafeSessionKey=data['websafeKey'],
                        add=True)
            data['speakers'] = spkr_keys

        # generate Conference Key based on websafeConferenceKey and
        # Session ID based on Conference Key and get Session websafe key
        # from ID.
        sess_id = Session.allocate_ids(size=1, parent=conf_key)[0]
        sess_key = ndb.Key(Session, sess_id, parent=conf_key)
        data['key'] = sess_key

        del data['websafeKey']
        del data['websafeConferenceKey']

        # create Session, send email to organizer confirming creation of
        # Session and return (modified) SessionForm
        session = Session(**data).put()

        # Check if there is more than one session by speakers of this
        # session.
        if data['speakers']:
            for speaker in data['speakers']:
                in_sessions = self._checkFeaturedSpeaker(
                    speaker, session.key.parent())
                if len(in_sessions) > 1:
                    self._setFeaturedSpeaker(speaker, in_sessions)

        taskqueue.add(params={'email': user.email(),
                      'sessionInfo': repr(request)},
                      url='/tasks/send_confirmation_email')
        return self._copySessionToForm(session.get())

    def _updateSpeakerForSession(self, websafeSpeakerKey, websafeSessionKey,
                                 add):
        """ Based on the calling endpoint, add or remove a Speaker
        """
        session = ndb.Key(urlsafe=websafeSessionKey).get()
        if not session:
            raise endpoints.NotFoundException(
                'No session found with key: {}'.format(
                    websafeSessionKey
                )
            )

        spkr_key = ndb.Key(urlsafe=websafeSpeakerKey)
        if not spkr_key:
            raise endpoints.NotFoundException(
                'No speaker found with key: {}'.format(
                    websafeSpeakerKey
                )
            )

        conf_key = session.key.parent()
        if add:
            if spkr_key not in session.speakers:
                session.speakers.append(spkr_key)
                session.put()

                self._checkFeaturedSpeaker(spkr_key, conf_key)
        else:
            if spkr_key in session.speakers:
                session.speakers.remove(spkr_key)
                session.put()

        in_sessions = self._checkFeaturedSpeaker(spkr_key, session.key)
        if in_sessions and len(in_sessions) > 1:
            self._setFeaturedSpeaker(spkr_key, in_sessions)

        return self._copySessionToForm(session)

    def _checkFeaturedSpeaker(self, speaker, conf):
        # TODO: This should return an mount of sessions for this speaker
        # at this conference, so it can be used to determine if the
        # speaker is a featured speaker, both when creating and updating
        # a session
        all_sess_by_spkr = Session.query(Session.speakers == speaker)
        all_sess_of_conf = Session.query(ancestor=conf)
        intersection = self._intersectQueries(
            all_sess_by_spkr, all_sess_of_conf)

        if intersection:
            return intersection

    def _setFeaturedSpeaker(self, speaker, sessions):
        # TODO: Implement add featured speaker to task queue
        pass<commit_msg>Fix issue with collecting sessions of a speaker and add first implementation for adding featured speakers to memcache<commit_after>#!/usr/bin/env python

"""
conference.py -- Udacity conference server-side Python App Engine API;
    uses Google Cloud Endpoints

$Id: conference.py,v 1.25 2014/05/24 23:42:19 wesc Exp wesc $

created by wesc on 2014 apr 21

"""

__author__ = '<EMAIL> (<NAME>)'

from datetime import datetime
from datetime import timedelta

import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote

from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.ext import ndb

from models import ConflictException
from models import Profile
from models import ProfileMiniForm
from models import ProfileForm
from models import StringMessage
from models import BooleanMessage
from models import Conference
from models import ConferenceForm
from models import ConferenceForms
from models import ConferenceQueryForms
from models import Session
from models import SessionForm
from models import SessionForms
from models import SessionType
from models import Wishlist
from models import WishlistForm
from models import Speaker
from models import SpeakerForm
from models import SpeakerForms
from models import TeeShirtSize

from settings import WEB_CLIENT_ID
from settings import ANDROID_CLIENT_ID
from settings import IOS_CLIENT_ID
from settings import ANDROID_AUDIENCE

from utils import getUserId

import logging


# Helper method during development, to log values of variables
def log_values(d={}):
    logging.debug('\n'.join(["{}: {}".format(i[0], i[1]) for i in d.items()]))


EMAIL_SCOPE = endpoints.EMAIL_SCOPE
API_EXPLORER_CLIENT_ID = endpoints.API_EXPLORER_CLIENT_ID
MEMCACHE_ANNOUNCEMENTS_KEY = "RECENT_ANNOUNCEMENTS"
MEMCACHE_FEATURED_KEY_PREFIX = "FEATURED_SPEAKER_"
ANNOUNCEMENT_TPL = ('Last chance to attend! The following conferences '
                    'are nearly sold out: %s')
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

DEFAULTS = {
    "city": "Default City",
    "maxAttendees": 0,
    "seatsAvailable": 0,
    "topics": ["Default", "Topic"],
}

SESSION_DEFAULTS = {
    "duration": 30,
    "speakers": [],
    "typeOfSession": "NOT_SPECIFIED"
}

OPERATORS = {
    'EQ':   '=',
    'GT':   '>',
    'GTEQ': '>=',
    'LT':   '<',
    'LTEQ': '<=',
    'NE':   '!='
}

FIELDS = {
    'CITY': 'city',
    'TOPIC': 'topics',
    'MONTH': 'month',
    'MAX_ATTENDEES': 'maxAttendees',
}

CONF_POST_REQUEST = endpoints.ResourceContainer(
    ConferenceForm,
    websafeConferenceKey=messages.StringField(1),
)

SESSION_GET_REQUEST_FILTERED = endpoints.ResourceContainer(
    message_types.VoidMessage,
    websafeConferenceKey=messages.StringField(1, required=True),
    typeOfSession=messages.StringField(2),  # XXX rename to filter? (generic)
)

SESSION_GET_REQUEST_SPEAKER = endpoints.ResourceContainer(
    message_types.VoidMessage,
    speaker=messages.StringField(1),
)

SESSION_POST_REQUEST = endpoints.ResourceContainer(
    SessionForm,
    websafeConferenceKey=messages.StringField(1),
)

GENERIC_WEBSAFEKEY_REQUEST = endpoints.ResourceContainer(
    message_types.VoidMessage,
    websafeKey=messages.StringField(1, required=True),
)

SESSION_POST_REQUEST_MODIFY_SPEAKERS = endpoints.ResourceContainer(
    websafeSessionKey=messages.StringField(1, required=True),
    websafeSpeakerKey=messages.StringField(2, required=True),
)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


@endpoints.api(name='conference', version='v1', audiences=[ANDROID_AUDIENCE],
               allowed_client_ids=[WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID,
                                   ANDROID_CLIENT_ID, IOS_CLIENT_ID],
               scopes=[EMAIL_SCOPE])
class ConferenceApi(remote.Service):
    """ Conference API v0.1
    """

    @staticmethod
    def get_authed_user():
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization Required')
        return user

# - - - Conference objects - - - - - - - - - - - - - - - - -

    def _copyConferenceToForm(self, conf, displayName):
        """ Copy relevant fields from Conference to ConferenceForm.
        """
        cf = ConferenceForm()
        for field in cf.all_fields():
            if hasattr(conf, field.name):
                # convert Date to date string; just copy others
                if field.name.endswith('Date'):
                    setattr(cf, field.name, str(getattr(conf, field.name)))
                else:
                    setattr(cf, field.name, getattr(conf, field.name))
            elif field.name == "websafeKey":
                setattr(cf, field.name, conf.key.urlsafe())
        if displayName:
            setattr(cf, 'organizerDisplayName', displayName)
        cf.check_initialized()
        return cf

    def _createConferenceObject(self, request):
        """ Create or update Conference object

        This method will return a ConferenceForm/request.
        """
        # preload necessary data items
        user = self.get_authed_user()
        user_id = getUserId(user)

        if not request.name:
            raise endpoints.BadRequestException(
                "Conference 'name' field required")

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name)
                for field in request.all_fields()}
        del data['websafeKey']
        del data['organizerDisplayName']

        # add default values for those missing
        # (both data model & outbound Message)
        for df in DEFAULTS:
            if data[df] in (None, []):
                data[df] = DEFAULTS[df]
                setattr(request, df, DEFAULTS[df])

        # convert dates from strings to Date objects; set month based on
        # start_date
        if data['startDate']:
            data['startDate'] = datetime.strptime(data['startDate'][:10],
                                                  "%Y-%m-%d").date()
            data['month'] = data['startDate'].month
        else:
            data['month'] = 0
        if data['endDate']:
            data['endDate'] = datetime.strptime(data['endDate'][:10],
                                                "%Y-%m-%d").date()

        # set seatsAvailable to be same as maxAttendees, if it's not
        # filled in on creation
        if (data["seatsAvailable"] is None) and (data["maxAttendees"] > 0):
            data["seatsAvailable"] = data["maxAttendees"]
        # generate Profile Key based on user ID and Conference
        # ID based on Profile key get Conference key from ID
        prof_key = ndb.Key(Profile, user_id)
        conf_id = Conference.allocate_ids(size=1, parent=prof_key)[0]
        conf_key = ndb.Key(Conference, conf_id, parent=prof_key)
        data['key'] = conf_key
        data['organizerUserId'] = request.organizerUserId = user_id

        # create Conference, send email to organizer confirming
        # creation of Conference & return (modified) ConferenceForm
        Conference(**data).put()
        taskqueue.add(params={'email': user.email(),
                              'conferenceInfo': repr(request)},
                      url='/tasks/send_confirmation_email')
        return request

    @ndb.transactional()
    def _updateConferenceObject(self, request):
        user = self.get_authed_user()
        user_id = getUserId(user)

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name)
                for field in request.all_fields()}

        # update existing conference
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()
        # check that conference exists
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: {}'.format(
                    request.websafeConferenceKey)
                )

        # check that user is owner
        if user_id != conf.organizerUserId:
            raise endpoints.ForbiddenException(
                'Only the owner can update the conference.')

        # Not getting all the fields, so don't create a new object; just
        # copy relevant fields from ConferenceForm to Conference object
        for field in request.all_fields():
            data = getattr(request, field.name)
            # only copy fields where we get data
            if data not in (None, []):
                # special handling for dates (convert string to Date)
                if field.name in ('startDate', 'endDate'):
                    data = datetime.strptime(data, "%Y-%m-%d").date()
                    if field.name == 'startDate':
                        conf.month = data.month
                # write to Conference object
                setattr(conf, field.name, data)
        conf.put()
        prof = ndb.Key(Profile, user_id).get()
        return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))

    @endpoints.method(ConferenceForm, ConferenceForm, path='conference',
                      http_method='POST', name='createConference')
    def createConference(self, request):
        """ Create new conference.
        """
        return self._createConferenceObject(request)

    @endpoints.method(CONF_POST_REQUEST, ConferenceForm,
                      path='conference/{websafeConferenceKey}',
                      http_method='PUT', name='updateConference')
    def updateConference(self, request):
        """ Update conference with provided fields

        This method will return a ConferenceForm with updated info.
        """
        return self._updateConferenceObject(request)

    @endpoints.method(GENERIC_WEBSAFEKEY_REQUEST, ConferenceForm,
                      path='conference/{websafeKey}',
                      http_method='GET', name='getConference')
    def getConference(self, request):
        """ Return requested conference (by websafeKey)
        """
        # get Conference object from request; bail if not found
        conf = ndb.Key(urlsafe=request.websafeKey).get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: {}'.format(
                    request.websafeKey)
                )
        prof = conf.key.parent().get()
        # return ConferenceForm
        return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))

    @endpoints.method(message_types.VoidMessage, ConferenceForms,
                      path='conferences/created',
                      http_method='POST', name='getConferencesCreated')
    def getConferencesCreated(self, request):
        """ Return conferences created by user
        """
        # make sure user is authed
        user = self.get_authed_user()
        user_id = getUserId(user)

        # create ancestor query for all key matches for this user
        confs = Conference.query(ancestor=ndb.Key(Profile, user_id))
        prof = ndb.Key(Profile, user_id).get()
        # return set of ConferenceForm objects per Conference
        return ConferenceForms(
            items=[
                self._copyConferenceToForm(conf, getattr(prof, 'displayName'))
                for conf in confs]
        )

    def _getQuery(self, request):
        """ Return formatted query from the submitted filters
        """
        q = Conference.query()
        inequality_filter, filters = self._formatFilters(request.filters)

        # If exists, sort on inequality filter first
        if not inequality_filter:
            q = q.order(Conference.name)
        else:
            q = q.order(ndb.GenericProperty(inequality_filter))
            q = q.order(Conference.name)

        for filtr in filters:
            if filtr["field"] in ["month", "maxAttendees"]:
                filtr["value"] = int(filtr["value"])
            formatted_query = ndb.query.FilterNode(
                filtr["field"],
                filtr["operator"],
                filtr["value"])
            q = q.filter(formatted_query)
        return q

    def _formatFilters(self, filters):
        """ Parse, check validity and format user supplied filters
        """
        formatted_filters = []
        inequality_field = None

        for f in filters:
            filtr = {field.name: getattr(f, field.name)
                     for field in f.all_fields()}

            try:
                filtr["field"] = FIELDS[filtr["field"]]
                filtr["operator"] = OPERATORS[filtr["operator"]]
            except KeyError:
                raise endpoints.BadRequestException(
                    "Filter contains invalid field or operator.")

            # Every operation except "=" is an inequality
            if filtr["operator"] != "=":
                # check if inequality operation has been used in previous
                # filters disallow the filter if inequality was performed on a
                # different field before track the field on which the
                # inequality operation is performed
                if inequality_field and inequality_field != filtr["field"]:
                    raise endpoints.BadRequestException(
                        "Inequality filter is allowed on only one field.")
                else:
                    inequality_field = filtr["field"]

            formatted_filters.append(filtr)
        return (inequality_field, formatted_filters)

    def _getConferenceOrganisers(self, conferences):
        """ Return a dict with organiser id's and names

        The dictionary has organiser id's as keys, and their names as values,
        based on queried conferences.
        """
        # need to fetch organiser displayName from profiles
        # get all keys and use get_multi for speed
        organisers = [(ndb.Key(Profile, conf.organizerUserId))
                      for conf in conferences]
        profiles = ndb.get_multi(organisers)

        # put display names in a dict for easier fetching
        names = {}
        for profile in profiles:
            names[profile.key.id()] = profile.displayName
        return names

    @endpoints.method(ConferenceQueryForms, ConferenceForms,
                      path='conferences/query',
                      http_method='POST',
                      name='queryConferences')
    def queryConferences(self, request):
        """ Query for conferences.
        """
        conferences = self._getQuery(request)

        names = self._getConferenceOrganisers(conferences)
        # return individual ConferenceForm object per Conference
        return ConferenceForms(
            items=[self._copyConferenceToForm(
                conf, names[conf.organizerUserId]) for conf in conferences]
        )

    # TASK 3
    def _intersectQueries(self, q1, q2):
        """ Return objects according to an intersection of two queries
        """
        return ndb.get_multi(
            set(q1.fetch(keys_only=True))
            & set(q2.fetch(keys_only=True)))

    # TASK 3
    @endpoints.method(message_types.VoidMessage, ConferenceForms,
                      path='conferences/upcoming',
                      http_method='POST',
                      name='getUpcomingConferences')
    def getUpcomingConferences(self, request):
        """ List all conferences that will be held in the upcoming three months
        """
        date_today = datetime.today().date()
        date_until = (date_today + timedelta(3*365/12))

        confs_from = Conference.query(
            Conference.endDate >= date_today)
        confs_till = Conference.query(
            Conference.startDate <= date_until
        )

        confs = self._intersectQueries(confs_from, confs_till)
        names = self._getConferenceOrganisers(confs)

        return ConferenceForms(
            items=[self._copyConferenceToForm(
                conf, names[conf.organizerUserId]) for conf in confs])

    # TASK 3
    @endpoints.method(message_types.VoidMessage, SessionForms,
                      path='sessions/nonworkshopbefore7',
                      http_method='GET', name='getNonWorkshopsBeforeSevenPM')
    def getNonWorkshopsBeforeSevenPM(self, request):
        """ Only show non-workshop sessions before 7PM
        """
        non_workshop = Session.query(
            Session.typeOfSession != 'WORKSHOP')
        before_seven = Session.query(
            # XXX replace startTime with endTime (which has to be
            # calculated first
            Session.startTime < datetime.strptime("19:00", "%H:%M").time())

        sessions = self._intersectQueries(non_workshop, before_seven)

        return SessionForms(
            items=[self._copySessionToForm(sess) for sess in sessions])

    # TASK 3
    @endpoints.method(message_types.VoidMessage, ConferenceForms,
                      path='conferences/not_sold_out_in_amsterdam',
                      http_method='GET',
                      name='getConferencesNotSoldOutInAmsterdam')
    def getConferencesNotSoldOutInAmsterdam(self, request):
        """ Only show conferences in Amsterdam that are not sold out
        """
        in_amsterdam = Conference.query(
            Conference.city == 'Amsterdam')
        not_sold_out = Conference.query(
            Conference.seatsAvailable > 0)

        confs = self._intersectQueries(in_amsterdam, not_sold_out)
        names = self._getConferenceOrganisers(confs)

        return ConferenceForms(
            items=[self._copyConferenceToForm(
                conf, names[conf.organizerUserId]) for conf in confs])

# - - - Session objects - - - - - - - - - - - - - - - - - - -

    def _copySessionToForm(self, session):
        """ Copy relevant fields from Session to SessionForm
        """
        sf = SessionForm()
        for field in sf.all_fields():
            if hasattr(session, field.name):
                # XXX Save an endTime when startTime and duration is
                # both given
                if field.name in ('date', 'startTime'):
                    # convert Date and Time to date and time string
                    setattr(sf, field.name, str(getattr(session, field.name)))
                elif field.name == 'typeOfSession':
                    # convert typeOfSession string to Enum
                    setattr(sf, field.name, getattr(SessionType,
                            getattr(session, field.name)))
                elif field.name == 'speakers':
                    setattr(sf, field.name, [str(speaker)
                            for speaker in getattr(session, field.name)])
                else:
                    # just copy the others
                    setattr(sf, field.name, getattr(session, field.name))
            elif field.name == "websafeKey":
                setattr(sf, field.name, session.key.urlsafe())
        sf.check_initialized()
        return sf

    def _createSessionObject(self, request):
        """ Create or update Session object, returning SessionForm/request
        """
        # preload necesarry data items
        user = self.get_authed_user()
        user_id = getUserId(user)

        conf_key = ndb.Key(urlsafe=request.websafeConferenceKey)
        conf = conf_key.get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key {}'.format(
                    request.websafeConferenceKey)
            )

        if user_id != conf.organizerUserId:
            raise endpoints.ForbiddenException(
                'Only the owner of the conference may create a session.'
            )

        if not request.name:
            raise endpoints.BadRequestException(
                "Session 'name' field required")
        if not request.websafeConferenceKey:
            raise endpoints.BadRequestException(
                "Session 'websafeConferenceKey' field required")

        # copy SessionForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name)
                for field in request.all_fields()}

        # add default values for those missing (both data model &
        # outbound Message)
        for sf in SESSION_DEFAULTS:
            if data[sf] in (None, []):
                data[sf] = SESSION_DEFAULTS[sf]
                setattr(request, sf, SESSION_DEFAULTS[sf])

        # convert date from string to Date object
        if data['date']:
            data['date'] = datetime.strptime(data['date'], "%Y-%m-%d").date()

        # convert startTime from string to Time
        if data['startTime']:
            data['startTime'] = datetime.strptime(data['startTime'][:5],
                                                  "%H:%M").time()

        data['typeOfSession'] = str(data['typeOfSession'])
        if data['speakers']:
            spkr_keys = [ndb.Key(
                urlsafe=speaker) for speaker in data['speakers']]

            # Update session if there already is a websafeKey
            if data['websafeKey']:
                for speaker in spkr_keys:
                    self._updateSpeakerForSession(
                        websafeSpeakerKey=speaker.urlsafe(),
                        websafeSessionKey=data['websafeKey'],
                        add=True)
            data['speakers'] = spkr_keys

        # generate Conference Key based on websafeConferenceKey and
        # Session ID based on Conference Key and get Session websafe key
        # from ID.
        sess_id = Session.allocate_ids(size=1, parent=conf_key)[0]
        sess_key = ndb.Key(Session, sess_id, parent=conf_key)
        data['key'] = sess_key

        del data['websafeKey']
        del data['websafeConferenceKey']

        # create Session, send email to organizer confirming creation of
        # Session and return (modified) SessionForm
        session = Session(**data).put()

        # Check if there is more than one session by speakers of this
        # session.
        if data['speakers']:
            for speaker in data['speakers']:
                speaker_schedule = self._getSpeakerSchedule(
                    speaker, session.key.parent())
                if len(speaker_schedule) > 1:
                    self._updateFeaturedSpeaker(speaker, speaker_schedule, True)

        taskqueue.add(params={'email': user.email(),
                      'sessionInfo': repr(request)},
                      url='/tasks/send_confirmation_email')
        return self._copySessionToForm(session.get())

    def _updateSpeakerForSession(self, websafeSpeakerKey, websafeSessionKey,
                                 add):
        """ Based on the calling endpoint, add or remove a Speaker
        """
        session = ndb.Key(urlsafe=websafeSessionKey).get()
        if not session:
            raise endpoints.NotFoundException(
                'No session found with key: {}'.format(
                    websafeSessionKey
                )
            )

        spkr_key = ndb.Key(urlsafe=websafeSpeakerKey)
        if not spkr_key:
            raise endpoints.NotFoundException(
                'No speaker found with key: {}'.format(
                    websafeSpeakerKey
                )
            )

        conf_key = session.key.parent()
        if add:
            if spkr_key not in session.speakers:
                session.speakers.append(spkr_key)
                session.put()
                speaker_schedule = self._getSpeakerSchedule(
                    spkr_key, session.key.parent())
                if len(speaker_schedule) > 1:
                    self._updateFeaturedSpeaker(spkr_key, speaker_schedule, True)
        else:
            if spkr_key in session.speakers:
                session.speakers.remove(spkr_key)
                session.put()
                speaker_schedule = self._getSpeakerSchedule(
                    spkr_key, conf_key)
                if len(speaker_schedule) < 2:
                    self._updateFeaturedSpeaker(spkr_key, speaker_schedule, False)
                # Check if speaker is still a featured speaker

        return self._copySessionToForm(session)

    def _getSpeakerSchedule(self, speaker, conf):
        """ Return a speaker's schedule

        This will return a schedule with sessions a speaker is scheduled for at
        the given conference, so it can be sued to determine if the speaker is
        (still) a featured speaker.
        """
        all_sess_by_spkr = Session.query(Session.speakers == speaker)
        all_sess_of_conf = Session.query(ancestor=conf)
        return self._intersectQueries(all_sess_by_spkr, all_sess_of_conf)

    def _updateFeaturedSpeaker(self, speaker, schedule, is_added):
        # TODO: Implement add featured speaker to task queue
        if is_added:
            sessions = ', '.join([session.name for session in schedule])
            announcement = "SPEAKER {} IS FEATURED SPEAKER AT THESE SESSIONS: {}".format(speaker.get().name, sessions)
            memcache.add(key=MEMCACHE_FEATURED_KEY_PREFIX+speaker.urlsafe(), value=announcement, time=3600)
        else:
            sessions = ', '.join([session.name for session in schedule])
            memcache.delete(MEMCACHE_FEATURED_KEY_PREFIX+speaker.urlsafe())

        pass
{'fraction_non_alphanumeric': 0.09266051348192333, 'fraction_numerical': 0.0030833098222562572, 'mean_word_length': 2.118393766103186, 'pattern_counts': {'":': 20, '<': 16, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6291050', 'n_tokens_mistral': 13542, 'n_tokens_neox': 12962, 'n_words': 3430}