Example #1
0
def get_schedule(school_id):
    try:
        _school_id = int(school_id)
        _days = int(request.args.get('days', ctrl_schedule.DEFAULT_DAYS))
        _class = request.args.get('class', ctrl_schedule.DEFAULT_CLASS)
    except ValueError:
        return general.create_error_message("Unable to convert given parameter!");

    try:
        res = ctrl_schedule.get_schedule_for_date(get_db(), _school_id, _days, _class)
    except ValueError as e:
        return general.create_error_message(str(e))

    return general.convert_json(res)
Example #2
0
def __process_schedule(db, data):
    new_schedule = sort_schedule(data.get_schedule())
    old_schedule = sort_schedule(ctrl_schedule.get_schedule_for_date(db, data.get_school_id(), data.get_date()))
    update_list = []
    delete_list = []
    insert_list = []

    n = 0
    o = 0

    while o < len(old_schedule) and n < len(new_schedule):
        o_key = ctrl_schedule.get_unique_real_key(old_schedule[o])
        n_key = ctrl_schedule.get_unique_real_key(new_schedule[n])

        new_schedule[n]["_id"] = old_schedule[o]["_id"]

        if sorted(old_schedule[o].items()) == sorted(new_schedule[n].items()):
            o += 1
            n += 1
            continue
        elif o_key == n_key:
            update_list += [new_schedule[n]]
            o += 1
            n += 1
        elif o_key < n_key:
            delete_list += [old_schedule[o]]
            o += 1
        elif o_key > n_key:
            insert_list += [new_schedule[n]]
            n += 1

    if o < len(old_schedule):
        delete_list += old_schedule[o:]
    elif n < len(new_schedule):
        insert_list += new_schedule[n:]

    ctrl_schedule.insert_schedules(db, insert_list)
    ctrl_schedule.delete_schedules(db, delete_list)
    ctrl_schedule.update_schedules(db, update_list)

    get_log_supp().info("schedule: %s added, %s updated, %s deleted." % (len(insert_list), len(update_list), len(delete_list)))