def _get_group(group_name, group_lookup, school_urlsafe):
    """Return a group for the group name passed in. Checks the group cache
    first if not there then queries by the lower case name. If not there then
    creates a new group.
    """
    #check for existing group by name
    if group_name.lower() in group_lookup:
        logging.debug("group found in cache")
        error = "group found in cache"
        create_error_log(error, 'ERR')
        return group_lookup[group_name.lower()], None

    from .group import Group
    school_key = ndb.Key(urlsafe=school_urlsafe)
    group = Group.query(Group.name_ == group_name,
                        Group.school == school_key,
                        namespace='_x_').get()

    if group:
        logging.debug("group found in datastore")
        error = "group found in datastore"
        create_error_log(error, 'ERR')
        group_lookup[group_name.lower()] = group.key
        return group.key, None

    logging.debug("No group found for %s, creating a new one", group_name)
    group = Group(name=group_name)
    school_key = ndb.Key(urlsafe=school_urlsafe)
    group.school = school_key
    future = group.put_async()
    return group.key, future
def broadcast_to_groups(group_keys, event_key, message_key, batch_id):
    """Scan over the given set of groups, sending the broadcast to everyone
    in those groups.
    """
    from sosbeacon.group import ADMIN_GROUPS_ID
    from sosbeacon.group import Group
    from sosbeacon.utils import insert_tasks

    # This is done to dedupe the group list, for better resilience.
    group_keys = list(set(group_keys))

    if len(group_keys) == 1 and group_keys[0].id() == ADMIN_GROUPS_ID:
        group_keys = Group.query().order(Group.key).iter(keys_only=True)

    tasks = []
    for group_key in group_keys:
        # TODO: Batch tasks or start
        tasks.append(get_group_broadcast_task(group_key, event_key, message_key, batch_id))

        if len(tasks) > 10:
            insert_tasks(tasks, GROUP_TX_QUEUE)
            tasks = []

    if tasks:
        insert_tasks(tasks, GROUP_TX_QUEUE)
def _get_group(group_name, group_lookup, school_urlsafe):
    """Return a group for the group name passed in. Checks the group cache
    first if not there then queries by the lower case name. If not there then
    creates a new group.
    """
    #check for existing group by name
    if group_name.lower() in group_lookup:
        logging.debug("group found in cache")
        error = "group found in cache"
        create_error_log(error, 'ERR')
        return group_lookup[group_name.lower()], None

    from .group import Group
    school_key = ndb.Key(urlsafe = school_urlsafe)
    group = Group.query(Group.name_ == group_name, Group.school == school_key, namespace = '_x_').get()

    if group:
        logging.debug("group found in datastore")
        error = "group found in datastore"
        create_error_log(error, 'ERR')
        group_lookup[group_name.lower()] = group.key
        return group.key, None

    logging.debug("No group found for %s, creating a new one", group_name)
    group = Group(name=group_name)
    school_key = ndb.Key(urlsafe = school_urlsafe)
    group.school = school_key
    future = group.put_async()
    return group.key, future
def broadcast_to_groups(group_keys, event_key, message_key, batch_id):
    """Scan over the given set of groups, sending the broadcast to everyone
    in those groups.
    """
    from sosbeacon.group import ADMIN_GROUPS_ID
    from sosbeacon.group import Group
    from sosbeacon.utils import insert_tasks

    # This is done to dedupe the group list, for better resilience.
    group_keys = list(set(group_keys))

    if len(group_keys) == 1 and group_keys[0].id() == ADMIN_GROUPS_ID:
        group_keys = Group.query().order(Group.key).iter(keys_only=True)

    tasks = []
    for group_key in group_keys:
        # TODO: Batch tasks or start
        tasks.append(
            get_group_broadcast_task(group_key, event_key, message_key,
                                     batch_id))

        if len(tasks) > 10:
            insert_tasks(tasks, GROUP_TX_QUEUE)
            tasks = []

    if tasks:
        insert_tasks(tasks, GROUP_TX_QUEUE)
def process_group(request, schema, entity):
    from voluptuous import Schema
    from sosbeacon.group import Group

    session_store = sessions.get_store()
    session = session_store.get_session()

    if not 'u' in session:
        return False

    if not 's' in session:
        return False

    obj = json.loads(request.body)
    schema = Schema(schema, extra=True)

    if obj['name'] == '':
        return False

    #check group duplicate
    group_name = obj['name'].lower()
    school_key = ndb.Key(urlsafe=session.get('s'))

    check_name = Group.query(Group.name_ == group_name,
                             Group.school == school_key,
                             namespace='_x_')

    if check_name.get():
        return False

    try:
        obj = schema(obj)
    except:
        logging.exception('validation failed')
        logging.info(obj)

    obj['school'] = school_key

    group = entity.from_dict(obj)
    to_put = [group]
    ndb.put_multi(to_put)

    return group
def process_group(request, schema, entity):
    from voluptuous import Schema
    from sosbeacon.group import Group

    session_store = sessions.get_store()
    session = session_store.get_session()

    if not 'u' in session:
        return False

    if not 's' in session:
        return False

    obj = json.loads(request.body)
    schema = Schema(schema, extra=True)

    if obj['name'] == '':
        return False

    #check group duplicate
    group_name = obj['name'].lower()
    school_key = ndb.Key(urlsafe = session.get('s'))

    check_name = Group.query(Group.name_ == group_name, Group.school == school_key, namespace = '_x_')

    if check_name.get():
        return False

    try:
        obj = schema(obj)
    except:
        logging.exception('validation failed')
        logging.info(obj)

    obj['school'] = school_key

    group = entity.from_dict(obj)
    to_put = [group]
    ndb.put_multi(to_put)

    return group