Пример #1
0
def get_classes():
    """
    List upcoming classes for today
    :return:
    """
    date_received = request.vars['date']
    date = datestr_to_python("%Y-%m-%d", date_received)

    set_headers()

    from openstudio.os_class_schedule import ClassSchedule

    cs = ClassSchedule(date,
                       # filter_starttime_from=time_from
                       )

    return dict(classes=cs.get_day_list())
Пример #2
0
def classes_get_day(date, filter_id_school_classtype,
                    filter_id_school_location, filter_id_school_level,
                    filter_id_teacher):
    """
        :param weekday: ISO weekday (1-7)
        :return: List of classes for day
    """
    from openstudio.os_class_schedule import ClassSchedule

    cs = ClassSchedule(date,
                       filter_id_school_classtype=filter_id_school_classtype,
                       filter_id_school_location=filter_id_school_location,
                       filter_id_school_level=filter_id_school_level,
                       filter_id_teacher=filter_id_teacher,
                       filter_public=True,
                       sorting='starttime')

    return cs.get_day_list()
Пример #3
0
def _schedule_get(year, week, sorting, TeacherID, ClassTypeID, LocationID,
                  LevelID):
    # classes
    data = dict()
    data['classes'] = dict()
    for day in range(1, 8):
        date = iso_to_gregorian(int(year), int(week), int(day))

        class_schedule = ClassSchedule(date,
                                       filter_id_school_classtype=ClassTypeID,
                                       filter_id_school_location=LocationID,
                                       filter_id_school_level=LevelID,
                                       filter_id_teacher=TeacherID,
                                       filter_public=True,
                                       sorting=sorting)

        key = str(NRtoDay(day))

        class_data = dict(classes=class_schedule.get_day_list(), date=date)

        data['classes'][key] = class_data

    # Teachers and classtypes this week
    teacher_ids_this_week = []
    classtype_ids_this_week = []
    location_ids_this_week = []
    weekdays = [
        T('Monday'),
        T('Tuesday'),
        T('Wednesday'),
        T('Thursday'),
        T('Friday'),
        T('Saturday'),
        T('Sunday')
    ]
    for weekday in weekdays:
        for cls in data['classes'][weekday]['classes']:
            # check teachers
            if 'TeacherID' in cls and cls[
                    'TeacherID'] not in teacher_ids_this_week:
                teacher_ids_this_week.append(cls['TeacherID'])
            if 'TeacherID2' in cls and cls[
                    'TeacherID2'] not in teacher_ids_this_week:
                teacher_ids_this_week.append(cls['TeacherID2'])
            # check classtypes
            if 'ClassTypeID' in cls and cls[
                    'ClassTypeID'] not in classtype_ids_this_week:
                classtype_ids_this_week.append(cls['ClassTypeID'])
            # check locations
            if 'LocationID' in cls and cls[
                    'LocationID'] not in location_ids_this_week:
                location_ids_this_week.append(cls['LocationID'])

    # Define caching
    caching = (cache.ram, 120)
    if web2pytest.is_running_under_test(request, request.application):
        caching = None

    # ClassTypes
    classtypes = []
    query = (db.school_classtypes.Archived == False) & \
            (db.school_classtypes.AllowAPI == True) & \
            (db.school_classtypes.id.belongs(classtype_ids_this_week))
    rows = db(query).select(db.school_classtypes.id,
                            db.school_classtypes.Name,
                            db.school_classtypes.Color,
                            db.school_classtypes.Link,
                            db.school_classtypes.Description,
                            db.school_classtypes.thumbsmall,
                            db.school_classtypes.thumblarge,
                            orderby=db.school_classtypes.Name,
                            cache=caching)

    for row in rows:

        thumblarge_url = ''
        thumbsmall_url = ''

        if row.thumblarge:
            thumblarge_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumblarge, extension=''))
        if row.thumbsmall:
            thumbsmall_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumbsmall, extension=''))

        classtypes.append(
            dict(
                id=row.id,
                Name=row.Name,
                Color=row.Color,
                Link=row.Link,
                LinkThumbSmall=thumbsmall_url,
                LinkThumbLarge=thumblarge_url,
                Description=row.Description,
            ))

    data['classtypes'] = classtypes

    # Teachers
    query = (db.auth_user.trashed == False) & \
            (db.auth_user.teacher == True) & \
            (db.auth_user.id.belongs(teacher_ids_this_week))
    teachers = []
    rows = db(query).select(db.auth_user.id,
                            db.auth_user.full_name,
                            db.auth_user.teacher_role,
                            db.auth_user.teacher_bio,
                            db.auth_user.teacher_bio_link,
                            db.auth_user.teacher_website,
                            db.auth_user.thumbsmall,
                            db.auth_user.thumblarge,
                            orderby=db.auth_user.full_name,
                            cache=caching)
    for row in rows:
        name = row.full_name

        thumblarge_url = ''
        thumbsmall_url = ''

        if row.thumblarge:
            thumblarge_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumblarge, extension=''))

        if row.thumbsmall:
            thumbsmall_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumbsmall, extension=''))

        teachers.append(
            dict(
                id=row.id,
                name=name,  # for legacy purposes. Was the only one with name.
                Role=row.teacher_role,
                LinkToBio=row.teacher_bio_link,
                Bio=row.teacher_bio,
                Website=row.teacher_website,
                LinkThumbLarge=thumblarge_url,
                LinkThumbSmall=thumbsmall_url,
                Name=name))

    data['teachers'] = teachers

    # Locations
    query = (db.school_locations.AllowAPI == True) & \
            (db.school_locations.Archived == False) & \
            (db.school_locations.id.belongs(location_ids_this_week))
    rows = db(query).select(db.school_locations.id,
                            db.school_locations.Name,
                            db.school_locations.Address,
                            db.school_locations.Color,
                            cache=caching).as_list()
    data['locations'] = rows

    # Practice levels
    query = (db.school_levels.Archived == False)
    rows = db(query).select(db.school_levels.id,
                            db.school_levels.Name,
                            db.school_levels.Color,
                            cache=caching).as_list()
    data['levels'] = rows

    return data
Пример #4
0
def schedule_get_days():
    """

    """
    # forget session
    session.forget(response)
    # check extension
    result = call_check_extension()
    if result['error']:
        return result['error_msg']
    else:
        response.view = result['view']

    # check vars
    try:
        date_format = '%Y-%m-%d'
        user = request.vars['user']
        key = request.vars['key']
        date_start = request.vars['date_start']
        date_start = datetime.datetime.strptime(date_start, date_format)
        date_start = datetime.date(date_start.year, date_start.month,
                                   date_start.day)
        date_end = request.vars['date_end']
        date_end = datetime.datetime.strptime(date_end, date_format)
        date_end = datetime.date(date_end.year, date_end.month, date_end.day)

        if date_start > date_end:
            return T("date_end has to be bigger then date_start")

    except:
        return T("Missing value: user, key, date_start and date_end are \
                  required values, one or more was missing in your request. \
                  (Date format: yyyy-mm-dd)")

    # check auth
    auth_result = do_auth(user, key)
    if not auth_result['authenticated']:
        return auth_result['message']

    # check for TeacherID
    TeacherID = None
    if 'TeacherID' in request.vars:
        TeacherID = int(request.vars['TeacherID'])

    # check for ClassTypeID
    ClassTypeID = None
    if 'ClassTypeID' in request.vars:
        ClassTypeID = int(request.vars['ClassTypeID'])

    # check for LocationID
    LocationID = None
    if 'LocationID' in request.vars:
        LocationID = int(request.vars['LocationID'])

    # check for LevelID
    LevelID = None
    if 'LevelID' in request.vars:
        LevelID = int(request.vars['LevelID'])

    # check for SortBy
    sorting = 'location'
    if 'SortBy' in request.vars:
        if request.vars['SortBy'] == 'time':
            sorting = 'starttime'

    current_date = date_start
    delta = datetime.timedelta(days=1)
    data = {}
    data['schedule'] = []
    while current_date <= date_end:
        classes = []

        class_schedule = ClassSchedule(
            current_date,
            filter_id_school_classtype=ClassTypeID,
            filter_id_school_location=LocationID,
            filter_id_school_level=LevelID,
            filter_id_teacher=TeacherID,
            filter_public=True,
            sorting=sorting,
        )

        # Don't cache when running tests or when day == today
        if web2pytest.is_running_under_test(
                request, request.application) or current_date == TODAY_LOCAL:
            classes = class_schedule.get_day_list()
        else:
            cache_key = 'openstudio_api_schedule_get_days_' + str(current_date) + '_' + \
                        'sorting_' + sorting + '_' + \
                        'TeacherID_' + str(TeacherID) + '_' + \
                        'ClassTypeID_' + str(ClassTypeID) + '_' + \
                        'LocationID_' + str(LocationID) + '_' + \
                        'LevelID_' + str(LevelID)
            classes = cache.ram(cache_key,
                                lambda: class_schedule.get_day_list(),
                                time_expire=CACHE_LONG)

        data['schedule'].append({'classes': classes, 'date': current_date})

        current_date += delta

    # Define caching
    caching = (cache.ram, 120)
    if web2pytest.is_running_under_test(request, request.application):
        caching = None

    teacher_ids = []
    classtype_ids = []
    location_ids = []
    level_ids = []
    for day in data['schedule']:
        for cls in day['classes']:
            # check teachers
            if 'TeacherID' in cls and cls['TeacherID'] not in teacher_ids:
                teacher_ids.append(cls['TeacherID'])
            if 'TeacherID2' in cls and cls['TeacherID2'] not in teacher_ids:
                teacher_ids.append(cls['TeacherID2'])
            # check classtypes
            if 'ClassTypeID' in cls and cls['ClassTypeID'] not in classtype_ids:
                classtype_ids.append(cls['ClassTypeID'])
            # check locations
            if 'LocationID' in cls and cls['LocationID'] not in location_ids:
                location_ids.append(cls['LocationID'])
            # check levels
            if 'LevelID' in cls and cls['LevelID'] not in level_ids:
                level_ids.append(cls['LevelID'])

    # ClassTypes
    classtypes = []
    query = (db.school_classtypes.Archived == False) & \
            (db.school_classtypes.AllowAPI == True) & \
            (db.school_classtypes.id.belongs(classtype_ids))
    rows = db(query).select(db.school_classtypes.id,
                            db.school_classtypes.Name,
                            db.school_classtypes.Color,
                            db.school_classtypes.Link,
                            db.school_classtypes.Description,
                            db.school_classtypes.thumbsmall,
                            db.school_classtypes.thumblarge,
                            orderby=db.school_classtypes.Name,
                            cache=caching)

    for row in rows:

        thumblarge_url = ''
        thumbsmall_url = ''

        if row.thumblarge:
            thumblarge_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumblarge, extension=''))
        if row.thumbsmall:
            thumbsmall_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumbsmall, extension=''))

        classtypes.append(
            dict(
                id=row.id,
                Name=row.Name,
                Link=row.Link,
                Color=row.Color,
                LinkThumbSmall=thumbsmall_url,
                LinkThumbLarge=thumblarge_url,
                Description=row.Description,
            ))

    data['classtypes'] = classtypes

    # Teachers
    query = (db.auth_user.trashed == False) & \
            (db.auth_user.teacher == True) & \
            (db.auth_user.id.belongs(teacher_ids))
    teachers = []
    rows = db(query).select(db.auth_user.id,
                            db.auth_user.full_name,
                            db.auth_user.teacher_role,
                            db.auth_user.teacher_bio,
                            db.auth_user.teacher_bio_link,
                            db.auth_user.teacher_website,
                            db.auth_user.thumbsmall,
                            db.auth_user.thumblarge,
                            orderby=db.auth_user.full_name,
                            cache=caching)
    for row in rows:
        name = row.full_name

        thumblarge_url = ''
        thumbsmall_url = ''

        if row.thumblarge:
            thumblarge_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumblarge, extension=''))

        if row.thumbsmall:
            thumbsmall_url = '%s://%s%s' % (
                request.env.wsgi_url_scheme, request.env.http_host,
                URL('default', 'download', args=row.thumbsmall, extension=''))

        teachers.append(
            dict(
                id=row.id,
                name=name,  # for legacy purposes. Was the only one with name.
                Role=row.teacher_role,
                LinkToBio=row.teacher_bio_link,
                Bio=row.teacher_bio,
                Website=row.teacher_website,
                LinkThumbLarge=thumblarge_url,
                LinkThumbSmall=thumbsmall_url,
                Name=name))

    data['teachers'] = teachers

    # Locations
    query = (db.school_locations.AllowAPI == True) & \
            (db.school_locations.Archived == False) & \
            (db.school_locations.id.belongs(location_ids))
    rows = db(query).select(db.school_locations.id,
                            db.school_locations.Name,
                            db.school_locations.Address,
                            db.school_locations.Color,
                            cache=caching).as_list()
    data['locations'] = rows

    # Practice levels
    query = (db.school_levels.Archived == False) & \
            (db.school_levels.id.belongs(level_ids))
    rows = db(query).select(db.school_levels.id,
                            db.school_levels.Name,
                            db.school_levels.Color,
                            cache=caching).as_list()
    data['levels'] = rows

    ## allow all domains to request this resource
    ## Only enable when you really need it, server side implementation is recommended.
    # response.headers["Access-Control-Allow-Origin"] = "*"

    return dict(data=data)