Ejemplo n.º 1
0
def change_color_by_category(request):
    course_id = request.POST['course_id']
    category = request.POST['category']
    color = request.POST['color']
    bg_color = request.POST['bg_color']
    term = request.POST['term']

    ret = get_user_from_session(request)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_user = ret.body

    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = SelectSection.get_selected_section_by_user(o_user)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    select_sections = ret.body

    select_section_list = []
    for select_section in select_sections:
        if select_section.section.course == o_course and select_section.section.category == category and select_section.section.term == term:
            select_section.bgcolor = bg_color
            select_section.color = color
            select_section.save()
            select_section_list.append(select_section.to_dict())

    return response(body=select_section_list)
Ejemplo n.º 2
0
def delete_section_by_category(request):
    """DELETE /api/selectsections"""
    course_id = request.POST['course_id']
    category = request.POST['category']
    term = request.POST['term']

    ret = get_user_from_session(request)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_user = ret.body

    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = SelectSection.get_selected_section_by_user(o_user)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    select_sections = ret.body

    for select_section in select_sections:
        if select_section.section.course == o_course and select_section.section.category == category and select_section.section.term == term:
            select_section.delete()
    return response()
Ejemplo n.º 3
0
def get_sibling_sections(request, course_id):
    """POST /api/courses/:course:id/sections"""
    category = request.POST['category']
    term = request.POST['term']
    type = request.POST['type']
    section_id = request.POST['section_id']

    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = Section.get_sections_by_course(o_course)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    sections = ret.body

    section_list = []
    for section in sections:
        if section.category == category and section.term == term and section.type == type and section.id != section_id:
            section_list.append(dict(
                sibling=section.to_dict(),
                self_id=section_id,
            ))
    return response(body=section_list)
Ejemplo n.º 4
0
def get_meetings_by_section(request, course_id, section_id):
    """GET /api/courses/:course_id/sections/:section_id/meetings"""
    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = Section.get_section_by_id(section_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_section = ret.body

    ret = o_section.belong_to(o_course)
    if ret.error is not Error.OK:
        return error_response(ret.error)

    ret = TimeSlot.get_meetings_by_section(o_section)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    meetings = ret.body

    meeting_list = []
    for meeting in meetings:
        meeting_list.append(meeting.to_dict())
    return response(body=meeting_list)
Ejemplo n.º 5
0
def reply_to_trail(request):
    trail_id = request.POST['trail_id']
    metric = request.POST['metric']
    # study_id = request.POST['study_id']

    # # get study by id
    # ret = Study.get_study_by_id(study_id)
    # if ret.error is not Error.OK:
    #     return error_response(ret.error)
    # o_study = ret.body
    # if not isinstance(o_study, Study):
    #     return error_response(Error.STRANGE)

    # get trail by id
    ret = Trail.get_trail_by_id(trail_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_trail = ret.body
    if not isinstance(o_trail, Trail):
        return error_response(Error.STRANGE)

    o_study = o_trail.r_study

    metric_str = json.dumps(metric, ensure_ascii=False)
    o_trail.set_metric(metric_str)

    ret = select_and_run(o_study)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    return response(body=ret.body)
Ejemplo n.º 6
0
def auto_search(request):
    query = request.GET.get('q', None)
    term = request.GET.get('term', None)
    if not query or not term:
        return error_response(Error.NO_SEARCH_PARAMETER)

    qs = SearchQuerySet().models(Course)
    results = qs.auto_query(query)

    result_list = []
    for result in results:
        ret = Section.get_sections_by_course(result.object)
        if ret.error is not Error.OK:
            return error_response(ret.error)
        if not ret.body:
            continue
        sections = ret.body
        for section in sections:
            if section.term == term:
                result_list.append(result)
                break

        # result_list.append(result)
    sorted_results = sorted(result_list, key=lambda x: x.score, reverse=True)

    final_result = []
    for result in sorted_results:
        final_result.append(result.object.to_dict(term))

    return response(body=final_result)
Ejemplo n.º 7
0
def courses_page(request, course_id):
    ret = get_user_from_session(request)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_user = ret.body

    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    return render(request, 'courses.html', dict(course_id=o_course.id))
Ejemplo n.º 8
0
def get_events_by_user(request):
    ret = get_user_from_session(request)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_user = ret.body

    ret = UserTodo.get_user_todo_by_user(o_user)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    user_todos = ret.body

    event_list = []
    for user_todo in user_todos:
        event_list.append(user_todo.event.to_dict())
    return response(body=event_list)
Ejemplo n.º 9
0
def get_assessments_by_course(request, course_id):
    """GET /api/courses/:course_id/assessments"""
    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = Assessment.get_assessment_by_course(o_course)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    assessments = ret.body

    assessment_list = []
    for assessment in assessments:
        assessment_list.append(assessment.to_dict())
    return response(body=assessment_list)
Ejemplo n.º 10
0
def get_terms(request):
    """GET /api/courses/terms"""
    ret = Section.get_terms()
    if ret.error is not Error.OK:
        return error_response(ret.error)
    terms = ret.body
    return response(body=terms)
Ejemplo n.º 11
0
def get_sections_by_course(request, course_id):
    """GET /api/courses/:course_id/sections"""
    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = Section.get_sections_by_course(o_course)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    sections = ret.body

    section_list = []
    for section in sections:
        section_list.append(section.to_dict())
    return response(body=section_list)
Ejemplo n.º 12
0
def get_selected_sections_by_user(request):
    """GET /api/usersections"""
    ret = get_user_from_session(request)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_user = ret.body

    ret = UserSection.get_selected_section_by_user(o_user)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    select_sections = ret.body

    section_list = []
    for select_section in select_sections:
        section_list.append(select_section.to_dict())
    return response(body=section_list)
Ejemplo n.º 13
0
def get_section(request, course_id, section_id):
    """GET /api/courses/:course_id/sections/:section_id"""
    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = Section.get_section_by_id(section_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_section = ret.body

    ret = o_section.belong_to(o_course)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    return response(body=o_section)
Ejemplo n.º 14
0
def get_course_info(request, course_id):
    """GET /api/courses/:course_id"""
    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body
    return response(body=o_course.to_dict())
Ejemplo n.º 15
0
def delete_event(request):
    event_id = request.POST['event_id']

    ret = Event.delete_event_by_id(event_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    return response()
Ejemplo n.º 16
0
 def wrapper(request, *args, **kwargs):
     """decorator declaration, get func params to validate"""
     for require_param in r_params:
         if require_param not in request.GET:
             return error_response(Error.REQUIRE_PARAM,
                                   append_msg=require_param)
     return func(request, *args, **kwargs)
Ejemplo n.º 17
0
def get_components_by_course(request, course_id):
    """GET /api/courses/:course_id/components"""
    ret = Course.get_course_by_id(course_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_course = ret.body

    ret = Component.get_component_by_course(o_course)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    components = ret.body

    component_list = []
    for component in components:
        component_list.append(component.to_dict())
    return response(body=component_list)
Ejemplo n.º 18
0
def get_algo_list(request):
    ret = Algorithm.get_algo_list()
    if ret.error is not Error.OK:
        return error_response(ret.error)
    algo_list = ret.body

    return response(body=algo_list)
Ejemplo n.º 19
0
def router_course(request, course_id):
    """
    /api/courses/:course_id
    GET: get_course_info
    """
    if request.method == "GET":
        return get_course_info(request, course_id)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 20
0
def router_course_id_section_id_meeting(request, course_id, section_id):
    """
    /api/courses/:course_id/sections/:section_id/meetings
    GET: get_meetings_by_section
    """
    if request.method == "GET":
        return get_meetings_by_section(request, course_id, section_id)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 21
0
def router_course_id_section_id(request, course_id, section_id):
    """
    /api/course/:course_id/sections/section_id
    GET: get_section
    """
    if request.method == 'GET':
        return get_section(request, course_id, section_id)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 22
0
def router_selectsection_color(request):
    """
    /api/selectsections/color
    POST: change_color_by_category
    """
    if request.method == "POST":
        return change_color_by_category(request)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 23
0
def router_event_id(request, event_id):
    """
    /api/events/:event_id
    DELETE: delete event by event_id
    """
    if request.method == "DELETE":
        return delete_event(request, event_id)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 24
0
 def wrapper(request, *args, **kwargs):
     """decorator declaration, get func params to validate"""
     if request.body:
         try:
             request.POST = json.loads(request.body.decode())
         except json.JSONDecodeError as err:
             print(str(err))
         return func(request, *args, **kwargs)
     return error_response(Error.REQUIRE_JSON)
Ejemplo n.º 25
0
def router_event_week(request):
    """
    /api/events/week
    POST: get event by range of date
    """
    if request.method == "POST":
        return get_event_by_date_range(request)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 26
0
def router_course_term(request):
    """
    /api/courses/terms
    GET: get_terms
    """
    if request.method == "GET":
        return get_terms(request)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 27
0
def get_event_by_date_range(request):
    start_date = request.POST['start_date']
    end_date = request.POST['end_date']

    ret = get_user_from_session(request)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_user = ret.body

    ret = UserTodo.get_user_event_by_date_range(
        start_date=start_date,
        end_date=end_date,
        o_user=o_user,
    )
    if ret.error is not Error.OK:
        return error_response(ret.error)
    event_list = ret.body
    return response(body=event_list)
Ejemplo n.º 28
0
def router_todo(request):
    """
    /api/events/todo
    POST: create an UserTodo
    """
    if request.method == "POST":
        return create_user_todo(request)
    else:
        return error_response(Error.ERROR_METHOD)
Ejemplo n.º 29
0
def delete_select_section(request, section_id):
    """DELETE /api/selectsections/:section_id"""
    ret = get_user_from_session(request)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_user = ret.body

    ret = Section.get_section_by_id(section_id)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_section = ret.body

    ret = SelectSection.get_select_section(o_user, o_section)
    if ret.error is not Error.OK:
        return error_response(ret.error)
    o_select_section = ret.body
    o_select_section.delete()
    return response()
Ejemplo n.º 30
0
def router_course_id_assessment(request, course_id):
    """
    /api/courses/:courses_id/assessments
    GET: get_assessments_by_course
    """
    if request.method == "GET":
        return get_assessments_by_course(request, course_id)
    else:
        return error_response(Error.ERROR_METHOD)