def change_lesson(request): if not request.is_ajax(): return HttpResponseBadRequest() room_name = request.GET.get("room_name", None) try: r = Room.objects.get(room_name=room_name) except Room.DoesNotExist: return ajax_bad_request("Room " + room_name + " not found") lesson = request.GET.get("lesson", None) if lesson is None: return ajax_bad_request("Error: no lesson set") try: get_lesson(lesson) except KeyError: return ajax_bad_request("Error: unknown lesson") prev_lessons = get_previous_lessons(r) prev_lessons.append(r.lesson) r.previous_lessons = json.dumps(prev_lessons) r.lesson = lesson r.save() students = Student.objects.filter(room=r) for student in students: student.current_state = 0 student.is_finished = False student.is_syncing = False student.save() return HttpResponseNoContent()
def control_cmd(request): if not request.is_ajax(): return HttpResponseBadRequest() room_name = request.GET.get("room_name", None) b = request.GET.get("cmd", None) if b is None: return ajax_bad_request("Command not found") try: r = Room.objects.get(room_name=room_name) except Room.DoesNotExist: return ajax_bad_request("Room doesn't exist: " + room_name) cmd = Cmd(int(b)) if cmd == Cmd.START: r.state = RoomStates.RUNNING.value for student in Student.objects.filter(room=r): student.is_syncing = False student.is_finished = False student.save() elif cmd == Cmd.STOP: r.state = RoomStates.CLOSED.value for student in Student.objects.filter(room=r): student.delete() elif cmd == Cmd.PAUSE: r.state = RoomStates.PAUSED.value else: return ajax_bad_request("Room command does not exist: " + str(cmd)) r.save() return HttpResponseNoContent()
def get_results(request, room_name): if not request.is_ajax(): return HttpResponseBadRequest() try: room = Room.objects.get(room_name=room_name) except Room.DoesNotExist: return ajax_bad_request("Room doesn't exist") lesson = request.GET.get("lesson", None) try: get_lesson(lesson) except KeyError: return ajax_bad_request("Error: unknown lesson") num_students = Student.objects.filter(room=room).count() states = get_lesson(lesson).all_states() state_results = [] for state in states: r = state.result_svg(room_name) if r is not None: completed = LessonStateModel.objects.filter( room=room, state=state.state_number(), choice__isnull=False).count() state_results.append({ 'state_name': state.name(), 'completed': completed, 'svg': r }) return JsonResponse({ 'results': state_results, 'no_students': num_students, 'current_lesson': room.lesson, 'prev_lessons': get_previous_lessons(room) })
def get_sync_state(request): if not request.is_ajax(): return HttpResponseBadRequest() room_name = request.GET.get("room_name", None) try: r = Room.objects.get(room_name=room_name) synced = all_synced(r) finished = all_finished(r) show_modal = False if finished and r.state != RoomStates.WAITING.value: show_modal = True r.state = RoomStates.WAITING.value r.save() elif synced and r.state != RoomStates.PAUSED.value: show_modal = True r.state = RoomStates.PAUSED.value r.save() return JsonResponse({ "state": r.state, "finished": finished, "synced": synced, "show_modal": show_modal }) except Room.DoesNotExist: return ajax_bad_request("Room " + room_name + " not found")
def remove_room(request): if not request.is_ajax(): return HttpResponseBadRequest() room_name = request.GET.get("room_name", None) try: r = Room.objects.get(room_name=room_name) r.delete() return HttpResponseNoContent() except Room.DoesNotExist: return ajax_bad_request("Room " + room_name + " not found")
def get_students(request): if not request.is_ajax(): return HttpResponseBadRequest() room_name = request.GET.get("room_name", None) try: room = Room.objects.get(room_name=room_name) students = get_students_for_room(room) finished = all_finished(room) return JsonResponse({"students": students, 'finished': finished}) except Room.DoesNotExist: return ajax_bad_request("Room " + room_name + " not found")
def create_test_students(request): if not request.is_ajax(): return HttpResponseBadRequest room_name = request.GET.get("room_name", None) if room_name is None: return HttpResponseBadRequest try: room = Room.objects.get(room_name=room_name) except Room.DoesNotExist: return ajax_bad_request("Room" + room_name + " doesn't exist") for _ in range(0, 10): Student(user_name=random_word()[:6], session=None, room=room).save() return HttpResponseNoContent()
def remove_student(request): if not request.is_ajax(): return HttpResponseBadRequest() # TODO: Check auth / permission room_name = request.GET.get("room_name", None) try: r = Room.objects.get(room_name=room_name) except Room.DoesNotExist: return ajax_bad_request("Room " + room_name + " not found") student = request.GET.get("student", None) if not student: return ajax_bad_request("Error: no such student") try: s = Student.objects.get(pk=student, room=r) s.delete() except Student.DoesNotExist: return ajax_bad_request("Error: student not in room") for l in LessonStateModel.objects.filter(room=r, student=s): l.delete() if Student.objects.filter(room=r).count() == 0: r.state = RoomStates.WAITING.value r.save() return JsonResponse({'state': r.state})
def create_room(request): if not request.is_ajax(): return HttpResponseBadRequest() if request.method == "POST": room_name = request.POST.get("room_name", None) room_name = room_name.strip() if room_name is None: return ajax_bad_request("Error: empty room name") if re.search("[^A-Za-z0-9.-]", room_name): return ajax_bad_request( "Error: room name contains unallowed characters") lesson = request.POST.get("lesson", None) if lesson is None: return ajax_bad_request("Error: no lesson set") try: get_lesson(lesson) except KeyError: return ajax_bad_request("Error: unknown lesson") try: Room.objects.get(room_name=room_name) return ajax_bad_request("Error: room already exist: " + room_name) except Room.DoesNotExist: Room.objects.create(room_name=room_name, lesson=lesson) return HttpResponseNoContent()