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 _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 process_school(request, schema, entity):
    from voluptuous import Schema

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

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

    school = entity.from_dict(obj)
    to_put = [school]

    if not obj.get('key'):
        # this is a new school. add the all groups group
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        group_admin = Group(key=ndb.Key(
            Group, ADMIN_GROUPS_ID + "%s" %
            (school.key.id()), namespace="_x_"))
        group_admin.name = "Admin"
        group_admin.school = school.key
        group_admin.default = True

        group_staff = Group(key=ndb.Key(
            Group, STAFF_GROUPS_ID + "%s" %
            (school.key.id()), namespace="_x_"))
        group_staff.name = "Staff"
        group_staff.school = school.key
        group_staff.default = True

        to_put.append(group_admin)
        to_put.append(group_staff)

    ndb.put_multi(to_put)

    return school
def process_school(request, schema, entity):
    from voluptuous import Schema

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

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

    school = entity.from_dict(obj)
    to_put = [school]

    if not obj.get('key'):
        # this is a new school. add the all groups group
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        group_admin = Group(key=ndb.Key(Group, ADMIN_GROUPS_ID + "%s" % (school.key.id()),
            namespace="_x_"))
        group_admin.name = "Admin"
        group_admin.school = school.key
        group_admin.default = True

        group_staff = Group(key=ndb.Key(Group, STAFF_GROUPS_ID + "%s" % (school.key.id()),
            namespace="_x_"))
        group_staff.name = "Staff"
        group_staff.school = school.key
        group_staff.default = True

        to_put.append(group_admin)
        to_put.append(group_staff)

    ndb.put_multi(to_put)

    return school