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()
def admin_page(request): """ 管理界面 """ admin = get_user_from_session(request) login = '******' if admin is None else 'none' logout = 'none' if admin is None else 'inline' login = '******' + login logout = 'display: ' + logout # 获取关键字列表 keywords = Keyword.objects.all() key_list = [] for kw in keywords: key_list.append( dict( id=kw.pk, kw=kw.kw, count=kw.count, web_count=kw.web_count, )) lasting = Config.objects.get(key='lasting').value interval = Config.objects.get(key='interval').value return render( request, 'admin.html', dict( lasting=lasting, interval=interval, login=login, logout=logout, keywords=key_list, is_login=admin is not None, ))
def create_study(request): """ client ask server to create a study for running """ config = request.POST['config'] algo_id = request.POST['algo_id'] c_ip = request.POST['client_ip'] c_port = request.POST['client_port'] ret = get_user_from_session(request) if ret.error is not Error.OK: return error_response(ret.error) o_user = ret.body if not isinstance(o_user, User): return error_response(Error.STRANGE) ret = Algorithm.get_algo_by_id(algo_id) if ret.error is not Error.OK: return error_response(ret.error) o_algo = ret.body if not isinstance(o_algo, Algorithm): return error_response(Error.STRANGE) ret = Study.create(o_user, o_algo, config, c_ip, c_port) 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) return response(body=o_study.to_dict())
def pause_study(request): study_id = request.POST['study_id'] ret = get_user_from_session(request) if ret.error is not Error.OK: return error_response(ret.error) o_user = ret.body if not isinstance(o_user, User): return error_response(Error.STRANGE) # get study from study_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) # check study belongs to user if not o_study.belong(o_user): return error_response(Error.NO_RIGHT_MODIFY_STUDY) if o_study.status not in [Study.STATUS_RUNNING]: return error_response(Error.STUDY_IS_NOT_RUNNING) o_study.status = Study.STATUS_PAUSED o_study.save() return response(body=o_study.to_dict())
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)
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))
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)
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)
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)
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()
def create_user_todo(request): event_id = request.POST['event_id'] ret = get_user_from_session(request) if ret.error is not Error.OK: return error_response(ret.error) o_user = ret.body ret = Event.get_event_by_id(event_id) if ret.error is not Error.OK: return error_response(ret.error) o_event = ret.body ret = UserTodo.create_user_todo( o_user=o_user, o_event=o_event, ) if ret.error is not Error.OK: return error_response(ret.error) o_user_todo = ret.body return response(body=o_user_todo.to_dict())
def create_select_section_by_section_id(request): """POST /api/usersections/:section_id""" bg_color = request.POST['bg_color'] color = request.POST['color'] section_id = request.POST['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 = UserSection.select_section(o_user, o_section, bg_color, color) if ret.error is not Error.OK: return error_response(ret.error) o_select_section = ret.body return response(body=o_select_section.to_dict())
def run_study(request): """ client ask server to run a study """ study_id = request.POST['study_id'] # get user from session ret = get_user_from_session(request) if ret.error is not Error.OK: return error_response(ret.error) o_user = ret.body if not isinstance(o_user, User): return error_response(Error.STRANGE) # get study from study_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) # check study belongs to user if not o_study.belong(o_user): return error_response(Error.NO_RIGHT_MODIFY_STUDY) # check study status if o_study.status not in [Study.STATUS_READY, Study.STATUS_PAUSED]: return error_response(Error.STUDY_IS_RUNNING_OR_FINISHED) o_study.status = Study.STATUS_RUNNING o_study.save() ret = select_and_run(o_study) if ret.error is not Error.OK: o_study.status = Study.STATUS_PAUSED o_study.save() return error_response(ret.error) return response(body=ret.body)
def create_algo(request): algo_name = request.POST['algo_name'] algo_path = request.POST['algo_path'] ret = get_user_from_session(request) if ret.error is not Error.OK: return error_response(ret.error) o_user = ret.body if not isinstance(o_user, User): return error_response(Error.STRANGE) ret = Algorithm.create(algo_name, algo_path) if ret.error is not Error.OK: return error_response(ret.error) o_algo = ret.body if not isinstance(o_algo, Algorithm): return error_response(Error.STRANGE) ret = select_and_create(o_algo) if ret.error is not Error.OK: return error_response(ret.error) return response(body=ret.body)
def require_login_func(request): o_user = get_user_from_session(request) return o_user is not None
def create_select_section(request): """POST /api/usersections""" color_scheme = [{ 'bgcolor': "#A3E7FC", 'color': 'black' }, { 'bgcolor': "#3F7CAC", 'color': 'white' }, { 'bgcolor': "#439A86", 'color': 'white' }, { 'bgcolor': "#E25186", 'color': 'white' }, { 'bgcolor': "#F9DC5C", 'color': 'black' }, { 'bgcolor': "#ECCBD9", 'color': 'black' }, { 'bgcolor': "#392B58", 'color': 'white' }, { 'bgcolor': "#FAC9B8", 'color': 'black' }, { 'bgcolor': "#D3F8E2", 'color': 'balck' }, { 'bgcolor': "#004E64", 'color': 'white' }, { 'bgcolor': "#8CB369", 'color': 'black' }] course_id = request.POST['course_id'] 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 = Section.get_sections_by_course(o_course) if ret.error is not Error.OK: return error_response(ret.error) sections = ret.body select_section_list = [] for o_section in sections: if o_section.term == term: # type_list.append(o_section.type) ret = UserSection.get_selected_section_by_details( o_user, o_course, term, ) if ret.error is not Error.OK: return error_response(ret.error) same_color = ret.body if same_color: bgcolor = same_color[0].bgcolor color = same_color[0].color else: rand = random.randint(0, len(color_scheme) - 1) bgcolor = color_scheme[rand]['bgcolor'] color = color_scheme[rand]['color'] ret = UserSection.select_section( o_user=o_user, o_section=o_section, bgcolor=bgcolor, color=color, ) if ret.error is not Error.OK: return error_response(ret.error) select_section_list.append(ret.body.to_dict()) return response(body=select_section_list)
def wrapper(request, *args, **kwargs): ret = get_user_from_session(request) if ret.error is not Error.OK: return error_response(ret.error) return func(request, *args, **kwargs)