def dialog_post_api_fetch(post_id, route): try: post_id = int(post_id) except: return _error("Ungültige Anfrage, bitte im globalen Forum melden.") cuser = muser.getCurrentUser() if not cuser.isLoggedIn(): return _error("Nur angemeldete Benutzer können Beiträge melden.") if not mforum.Article.exists(post_id): return _error("Beitrag nicht gefunden.") post = mforum.Article(post_id) live = bool(request.values.get("live", 0)) if live: if post.isClosed() or post.isDeleted(): return "<div style='color: #c00; font-family: \"Open Sans\", Arial, sans-serif'>Beitrag ist bereits geschlossen oder gelöscht.</div>" if route not in ["flagging/closure/duplicate", "closure/duplicate"]: return "<div style='color: #c00; font-family: \"Open Sans\", Arial, sans-serif'>Ein Fehler ist aufgetreten. Bitte im <a href='/f/0' target='_blank' style='color: #600; font-weight: bold;'>globalen Forum</a> melden.</div>" search = request.values.get("search", "").strip() if search == "": return "" id = None if search.startswith("#"): try: id = int(search[1:]) except: id = None if not mforum.Article.exists(id): id = None if id: data = mforum.Article(id) return render_template("forum/raw_qna.html", data=data) else: return "<div style='font-family: \"Open Sans\", Arial, sans-serif'>Bitte wähle ein Suchergebnis aus der Liste rechts.</div>" else: if post.isClosed() or post.isDeleted(): return "[]" if route not in ["flagging/closure/duplicate", "closure/duplicate"]: return "[]" search = request.values.get("search", "").strip() if search == "": return "[]" forum = mforum.Forum(post.getDetail("forumID")) data = forum.getArticles(search, "score")[:80] data = [{ "id": k.id, "title": k.getHTMLTitle(), "score": k.getScore(), "tags": k.getTags(), "closed": k.isClosed() } for k in data if not k.isDeleted()] return jsonify(data[:10])
def survey_edit(id, label=None): cuser = muser.getCurrentUser() if msurvey.Survey.exists(id): survey = msurvey.Survey(id) forum = mforum.Forum(survey.getDetail("associated_forum")) if not (cuser.isDev() or survey.getDetail("survey_owner") == cuser.id) or cuser.isDisabled(): if survey.getDetail("associated_forum") == 0: abort(404) else: cou = mcourses.Courses(forum.id) if not cou.getCourseRole(cuser) > 3 or cuser.isDIsabled(): abort(404) if request.method == "GET": if label != survey.getLabel(): return redirect( url_for("survey_edit", id=id, label=survey.getLabel())) else: return render_template("survey/edit.html", title="Umfrage bearbeiten - " + survey.getTitle(), survey=survey, forum=forum) elif request.method == "POST": survey.setDetail("content", request.json["content"]) survey.setDetail("title", request.json["title"]) survey.setDetail("state", request.json["state"]) return "ok" else: abort(404)
def survey_results(id, label=None): cuser = muser.getCurrentUser() if msurvey.Survey.exists(id): survey = msurvey.Survey(id) forum = mforum.Forum(survey.getDetail("associated_forum")) if not (cuser.isDev() or survey.getDetail("survey_owner") == cuser.id): if survey.getDetail("associated_forum") == 0: abort(404) else: cou = mcourses.Courses(forum.id) if not cou.getCourseRole(cuser) > 3: abort(404) if request.method == "GET": if label != survey.getLabel(): return redirect( url_for("survey_results", id=id, label=survey.getLabel())) else: return render_template("survey/results_direct.html", title="Umfrageergebnisse - " + survey.getTitle(), survey=survey, forum=forum, int=int, len=len) elif request.method == "POST": RESULT = {} con = survey.getContent() i = 1 for c in con: if c["type"] == "text-answer": RESULT[i] = { "question": c["data"]["question"], "data": [], "type": "text-answer" } if c["type"] == "multiple-choice": selc = len(c["data"]["choices"]) RESULT[i] = { "question": c["data"]["question"], "data": ["0" for _ in range(selc)], "type": "multiple-choice", "choices": c["data"]["choices"] } i += 1 subm = survey.getSubmissions() for s in subm: s = json.loads(s[0]) for field, value in list(s.items()): r = RESULT[int(field)] if r["type"] == "text-answer": if value: r["data"].append(value) elif r["type"] == "multiple-choice": r["data"][int(value)] += str( int(r["data"][int(value) - 1]) + 1) survey.setDetail("results", json.dumps(RESULT)) return "done" else: abort(404)
def survey_submit(id, label=None): cuser = muser.getCurrentUser() if not cuser.isLoggedIn() or cuser.isDisabled(): abort(403) if msurvey.Survey.exists(id): survey = msurvey.Survey(id) forum = mforum.Forum(survey.getDetail("associated_forum")) if not survey.hasSubmission(cuser): survey.addSubmission(cuser, json.dumps(request.json)) return "ok" else: abort(404)
def survey_show(id, label=None): if msurvey.Survey.exists(id): survey = msurvey.Survey(id) forum = mforum.Forum(survey.getDetail("associated_forum")) if label != survey.getLabel(): return redirect( url_for("survey_show", id=id, label=survey.getLabel())) else: return render_template("survey/direct.html", title="Umfrage - " + survey.getTitle(), survey=survey, forum=forum) else: abort(404)
def course_admin(id, label=None, page="identity"): if not mcourses.Courses.exists(id): abort(404) course = mcourses.Courses(id) cuser = muser.getCurrentUser() if course.getLabel() != label and request.method != "POST": return redirect(url_for("course_admin", id=id, label=course.getLabel())) if not (cuser.isMod() or course.getCourseRole(cuser) == 4) or cuser.isDisabled(): abort(404) if page == "identity": if request.method == "POST": data = request.json title = data["title"].strip() shortdesc = data["shortdesc"].strip() longdesc = data["longdesc"].strip() requirements = data["requirements"].strip() errors = _validateCourse(title, shortdesc, longdesc, requirements) if len(errors): return jsonify({"result": "error", "errors": errors}) else: course.setDetail("title", title) course.setDetail("shortdesc", shortdesc) course.setDetail("longdesc", longdesc) course.setDetail("requirements", requirements) return jsonify({"result": "ok"}) else: return render_template('courses/admin/identity.html', title=course.getTitle(), thispage="courses", course=course) elif page == "picture": if request.method == "POST": data = request.json url = data["picture_url"].strip() if url != "": if not re.match("^[a-zA-Z]+\/[a-zA-Z_0-9+]+\.jpg$", url): url = "" course.setDetail("picture_url", url) return "true" else: directories = os.listdir( os.path.join(os.getcwd(), "main/static/CourseStockImages_small")) files = [(i, os.listdir( os.path.join(os.getcwd(), "main/static/CourseStockImages_small", i))) for i in directories] return render_template('courses/admin/picture.html', title=course.getTitle(), thispage="courses", course=course, files=files) elif page == "announcements": return render_template("announcements/list.html", title=course.getTitle(), forum=mforum.Forum(course.id), announcements=mforum.ForumAnnouncement.byForum( course.id, True), thispage="courses") elif page == "publish": if request.method == "POST": course.setDetail("state", 1) return "{ok}" else: return render_template('courses/admin/publish.html', title=course.getTitle(), thispage="courses", course=course) elif page == "membership": if request.method == "POST": if request.json["action"] == "give-role": user_to_receive = muser.User(request.json["user"]) role_to_receive = request.json["role"] if not role_to_receive in range(1, 5): return jsonify({ "result": "error", "error": "Ungültige Ziel-Rolle" }) if course.isEnrolled(user_to_receive): course.setCourseRole(user_to_receive, role_to_receive) return jsonify({"result": "success"}) else: return jsonify({ "result": "error", "error": "Nur möglich für Benutzer, die im Kurs eingeschrieben sind." }) elif request.json["action"] == "revoke-role": user_to_receive = muser.User(request.json["user"]) if course.isEnrolled(user_to_receive) and course.getCourseRole( user_to_receive) != 1: course.setCourseRole(user_to_receive, 1) return jsonify({"result": "success"}) elif request.json["action"] == "kick-remove": user_to_receive = muser.User(request.json["user"]) if course.isEnrolled(user_to_receive) and course.getCourseRole( user_to_receive) == 1: course.unenroll(user_to_receive) user_to_receive.customflag( "Benutzer von " + cuser.getHTMLName(False) + " (#" + str(cuser.id) + ") aus dem Kurs " + course.getTitle() + " (#" + str(course.id) + ") geworfen.", muser.User.from_id(-1)) return jsonify({"result": "success"}) else: return jsonify({ "result": "error", "error": "Nicht möglich: Benutzer nicht eingeschrieben oder Benutzer hat Rolle." }) return jsonify({"result": "error", "error": "Ungültige Anfrage"}) else: return render_template('courses/admin/membership.html', title=course.getTitle(), thispage="courses", course=course) else: abort(404)