def task_done(): user = get_current_user() if user is None: return "No user context" task_id = request.form["task_id"] task = Task.find_one({"id": ObjectId(task_id)}) tasklink = TaskLink.find_one({ "user": user.id, "task": task.id, "done": True }) if tasklink is not None: return "Task already done" tasktype = task.task_type.fetch() tasktypelink = TaskTypeLink.find_one({ "user": user.id, "task_type": tasktype.id }) if tasktypelink is not None: tasktypelink.done_tasks += 1 else: tasktypelink = TaskTypeLink(task_type=tasktype, user=user, done_tasks=1, status=0) tasktypelink.commit() tasklink = TaskLink(user=user, task=task, done=True) tasklink.commit() return "Query ok"
def app_logged_in(subj_name): if get_current_user() is None: return redirect(url_for("index")) subject_list = list(Subject.find({"hidden": False})) if subj_name not in [sub["name"] for sub in subject_list]: abort(404) subject = Subject.find_one({"name": subj_name}) tasks_list = [] raw_tasks = TaskType.find({"subject": subject.id}) for t in raw_tasks: tasks_list.append({ "number": int(t.number), "compete_percentage": 0, "points": t.points, "status": get_status_on_task(t), }) return render( "tasks.html", title="Задания", header_label="Все задания первой части", current_subj=subj_name, current_mode="tasks", subject_list=subject_list, tasks_list=tasks_list, task_list_length=len(tasks_list), )
def login(): if get_current_user() is not None: return redirect(url_for("index")) if request.method == "GET": success_on_creation = request.args.get("success") if success_on_creation: return render( "login.html", title="Войти", msg="Аккаунт успешно создан!", msg_color="#01cd6c", ) else: return render("login.html", title="Войти") email = request.form["email"] raw_password = request.form["password"] password = hash_password(raw_password) user = User.find_one({"email": email, "password": password}) if user: if not user.confirm_code: session["email"] = email session["password"] = password return redirect(url_for("index")) return render( "login.html", title="Войти", msg="Подтвердите почту!", msg_color="#f74a5a" ) return render( "login.html", title="Войти", msg="Произошла ошибка, проверьте введённые данные!", msg_color="#f74a5a", )
def app_logged_in(subj_name): user = get_current_user() if user is None: return redirect(url_for("index")) subject_list = list(Subject.find({"hidden": False})) if subj_name not in [sub["name"] for sub in subject_list]: abort(404) exclude = request.args.get("exclude", "").split() subject = Subject.find_one({"name": subj_name}) task_types = TaskType.find({"subject": subject.id}) tasks = [] for tt in task_types: if tt.number in exclude: continue rel_tasks = Task.find({"task_type": tt.id}) task = choice(list(rel_tasks)) session["task" + str(tt.number)] = str(task.id) text = None if task.text.pk is not None: text = task.text.fetch().body tasks.append({ "number": tt.number, "description": eval('"' + task.description + '"'), "text": text, "options": task.options, }) return render( "practice.html", title="Тест", header_label="Тест в формате ЕГЭ", current_subj=subj_name, current_mode="practice", subject_list=subject_list, tasks=tasks, )
def admin_main(): user = get_current_user() if user is None: return render("access-denied.html") if not user.is_admin: return render("access-denied.html") subjects = Subject.find({"hidden": False}) texts_list = [{"id": "5e4066a91c9d440000785ec1", "low": "Нет текста"}] texts = Text.find() for text in texts: if str(text.id) == "5e4066a91c9d440000785ec1": continue low = text.body if len(low) > 30: low = low[:30] texts_list.append({"id": str(text.id), "low": low}) if request.method == "GET": return render("add-task.html", subjects=subjects, texts_list=texts_list) subject = Subject.find_one({"label": request.form["subject"]}) task_type = TaskType.find_one( {"number": request.form["task_type"], "subject": subject.id} ) task_type.count_of_tasks += 1 task_type.commit() description = request.form["description"] var1 = request.form["var1"] var2 = request.form["var2"] var3 = request.form["var3"] var4 = request.form["var4"] var5 = request.form["var5"] options = [] if var1: options.append(var1) if var2: options.append(var2) if var3: options.append(var3) if var4: options.append(var4) if var5: options.append(var5) answers = request.form["answers"].replace(" ", "").split(",") explanation = request.form["explanation"] text = request.form["text"] task = Task( task_type=task_type, description=description, options=options, answers=answers, explanation=explanation, text=ObjectId(str(text)), ) task.commit() return render("add-task.html", subjects=subjects, ok=True, texts_list=texts_list)
def get_percentage(): user = get_current_user() if user is None: return "No user context" subj_name = request.form["subj_name"] number = request.form["number"] subject = Subject.find_one({"name": subj_name}) tasktype = TaskType.find_one({ "subject": subject.id, "number": str(number) }) return str(count_percentage_on_task(tasktype))
def settings_page(): user = get_current_user() if user is None: return redirect(url_for("index")) user_math_type = int(not user.is_math_basic) if request.method == "GET": return render( "settings.html", title="Настройки", header_label="Настройки", current_subj="russian", subject_list=[], user_math_type=user_math_type, ) oldpass = request.form["oldpass"] newpass = request.form["newpass"] math_type = int(request.form["math_type"]) print(oldpass, newpass, math_type) if not oldpass: user.is_math_basic = not bool(math_type) user.commit() return render( "settings.html", title="Настройки", header_label="Настройки", current_subj="russian", subject_list=[], user_math_type=math_type, ok=True, ) if hash_password(oldpass) != user.password: return render( "settings.html", title="Настройки", header_label="Настройки", current_subj="russian", subject_list=[], user_math_type=user_math_type, bad_password=True, ) user.password = hash_password(newpass) user.is_math_basic = not bool(math_type) user.commit() return render( "settings.html", title="Настройки", header_label="Настройки", current_subj="russian", subject_list=[], user_math_type=math_type, ok=True, )
def results(subj_name): if flask.request.method == "GET": abort(404) user = get_current_user() if user is None: return redirect(url_for("index")) tasks = [] right = 0 all = 0 test_time = int(request.form["time_counter"]) test_time = time.strftime("%H:%M:%S", time.gmtime(test_time)) subject_list = list(Subject.find({"hidden": False})) first_points = 0 for q in request.form: if q != "time_counter": number = q[4:] answer = request.form[q] all += 1 task = Task.find_one({"id": ObjectId(session[q])}) tl = TaskLink.find_one({"task": task.id, "user": user.id}) text = None if task.text.pk is not None: text = task.text.fetch().body tasks.append({ "user_answer": answer, "number": number, "description": eval('"' + task.description + '"'), "text": text, "options": task.options, "done": tl.done if tl else False, "answers": task.answers, "explanation": task.explanation, }) if answer in task.answers: first_points += points[subj_name]["first"][int(number)] right += 1 return render( "practice-results.html", title="Ответы на тест", header_label="Тест в формате ЕГЭ", current_subj=subj_name, current_mode="practice", subject_list=subject_list, tasks=tasks, right=right, all=all, time=test_time, first_points=first_points, second_points=points[subj_name]["second"][first_points], )
def task_practice(subj_name, task_id): if get_current_user() is None: return redirect(url_for("index")) if subj_name != "russian": abort(404) user = get_current_user() subject = Subject.find_one({"name": subj_name}) subject_list = list(Subject.find({"hidden": False})) task_type = TaskType.find_one({ "subject": subject.id, "number": str(task_id) }) tasks = [] raw_tasks = Task.find({"task_type": task_type.id}) for task in raw_tasks: tl = TaskLink.find_one({"task": task.id, "user": user.id}) text = None if task.text.pk is not None: text = task.text.fetch().body tasks.append({ "id": str(task.id), "number": task_type.number, "description": eval('"' + task.description + '"'), "text": str(text), "options": task.options, "done": tl.done if tl else False, "answers": task.answers, "explanation": task.explanation, }) return render( "task-practice.html", title="Задания", header_label="Практика задания", current_subj=subj_name, subject_list=subject_list, tasks=tasks, )
def app_logged_in(subj_name): if get_current_user() is None: return redirect(url_for("index")) subject_list = list(Subject.find({"hidden": False})) if subj_name not in [sub["name"] for sub in subject_list]: abort(404) return render( "learn.html", title="Теория", header_label="Вся теория по кодификаторам", current_subj=subj_name, current_mode="learn", subject_list=subject_list, cod_amount=11, cods=subject["cods"], )
def task_theory(subj_name, task_id): user = get_current_user() if user is None: return redirect(url_for("index")) if subj_name != "russian": abort(404) subject_list = list(Subject.find({"hidden": False})) subject = Subject.find_one({"name": subj_name}) task_type = TaskType.find_one({ "subject": subject.id, "number": str(task_id) }) return render( "task-theory.html", title="Задание", header_label="Теория по заданию", current_subj=subj_name, subject_list=subject_list, task_id=task_id, cods=list(task_type.cods), task_description=task_type.description, )
def change_status(): user = get_current_user() if user is None: return "No user context" subject_name = request.form["subject_name"] task_number = str(request.form["task_number"]) new_status = int(request.form["new_status"]) subject = Subject.find_one({"name": subject_name}) tasktype = TaskType.find_one({ "subject": subject.id, "number": task_number }) tasktypelink = TaskTypeLink.find_one({ "user": user.id, "task_type": tasktype.id }) if not tasktypelink: tasktypelink = TaskTypeLink(user=user.id, task_type=tasktype.id, status=new_status) else: tasktypelink.status = new_status tasktypelink.commit() return "Query ok"
def index(): user = get_current_user() if user is None: return redirect(url_for("auth.login")) return redirect(url_for("tasks.app_logged_in", subj_name="russian"))