def cohort_files(cohort_id): """ Gets the files associated with a cohort """ check_permission_for_cohort(cohort_id) cohort = Cohort.find(cohort_id) articles = CohortArticleMap.get_articles_info_for_cohort(cohort) return json.dumps(articles)
def valid_invite_code(invite_code): if zeeguu.core.app.config.get( "INVITATION_CODES" ) and invite_code in zeeguu.core.app.config.get("INVITATION_CODES"): return True if Cohort.exists_with_invite_code(invite_code): return True return False
def _link_teacher_cohort(user_id, cohort_id): """ Takes user_id and cohort_id and links them together in teacher_cohort_map table. """ from zeeguu.core.model import TeacherCohortMap user = User.find_by_id(user_id) cohort = Cohort.find(cohort_id) db.session.add(TeacherCohortMap(user, cohort)) db.session.commit() return "added teacher_cohort relationship"
def create_own_cohort(): """ Creates a cohort in the database. Requires form input (inv_code, name, language_id, max_students, teacher_id) """ def _link_teacher_cohort(user_id, cohort_id): """ Takes user_id and cohort_id and links them together in teacher_cohort_map table. """ from zeeguu.core.model import TeacherCohortMap user = User.find_by_id(user_id) cohort = Cohort.find(cohort_id) db.session.add(TeacherCohortMap(user, cohort)) db.session.commit() return "added teacher_cohort relationship" params = request.form inv_code = params.get("inv_code") name = params.get("name") # language_id is deprecated and kept here for backwards compatibility # use language_code instead language_code = params.get("language_code") or params.get("language_id") if name is None or inv_code is None or language_code is None: flask.abort(400) available_languages = Language.available_languages() code_allowed = False for code in available_languages: if language_code in str(code): code_allowed = True if not code_allowed: flask.abort(400) language = Language.find_or_create(language_code) teacher_id = flask.g.user.id max_students = params.get("max_students") if int(max_students) < 1: flask.abort(400) try: c = Cohort(inv_code, name, language, max_students) db.session.add(c) db.session.commit() _link_teacher_cohort(teacher_id, c.id) return "OK" except ValueError: flask.abort(400) return "ValueError" except sqlalchemy.exc.IntegrityError: flask.abort(400) return "IntegrityError"
def add_colleague_to_cohort(): cohort_id = request.form.get("cohort_id") colleague_email = request.form.get("colleague_email") check_permission_for_cohort(id) colleague = User.find(colleague_email) cohort = Cohort.find(cohort_id) db.session.add(TeacherCohortMap(colleague, cohort)) db.session.commit() return "OK"
def _get_student_cohort_and_period_from_POST_params(to_string=True, ): student_id = flask.request.form.get("student_id") number_of_days = flask.request.form.get("number_of_days") cohort_id = flask.request.form.get("cohort_id") try: user = User.query.filter_by(id=student_id).one() cohort = Cohort.find(cohort_id) except NoResultFound: flask.abort(400) check_permission_for_user(user.id) from_date, to_date = _convert_number_of_days_to_date_interval( number_of_days, to_string) return user, cohort, from_date, to_date
def add_article_to_cohort(): """ Gets all the articles of this teacher """ cohort = Cohort.find(request.form.get("cohort_id")) check_permission_for_cohort(cohort.id) article = Article.find_by_id(request.form.get("article_id")) if not CohortArticleMap.find(cohort.id, article.id): now = datetime.now() new_mapping = CohortArticleMap(cohort, article, now) db.session.add(new_mapping) db.session.commit() return "OK"
def delete_article_from_cohort(): """ Gets all the articles of this teacher """ cohort = Cohort.find(request.form.get("cohort_id")) check_permission_for_cohort(cohort.id) article = Article.find_by_id(request.form.get("article_id")) mapping = CohortArticleMap.find(cohort.id, article.id) if mapping: db.session.delete(mapping) db.session.commit() return "OK" else: return make_error(401, "That article does not belong to the cohort!")
def join_cohort_api(): invite_code = request.form.get("invite_code", "") if not invite_code: flask.abort(400) try: cohort = Cohort.find_by_code(invite_code) flask.g.user.cohort_id = cohort.id db_session.add(flask.g.user) db_session.commit() return "OK" except Exception as e: from sentry_sdk import capture_exception capture_exception(e) flask.abort(500)
def upload_articles(cohort_id): """ uploads articles for a cohort with input from a POST request """ check_permission_for_cohort(cohort_id) try: for article_data in json.loads(request.data): url = Url("userarticle/{}".format(uuid.uuid4().hex)) title = article_data["title"] authors = article_data["authors"] content = article_data["content"] summary = article_data["summary"] published_time = datetime.now() language_code = article_data["language_code"] language = Language.find(language_code) new_article = Article( url, title, authors, content, summary, published_time, None, # rss feed language, ) db.session.add(new_article) db.session.flush() db.session.refresh(new_article) cohort = Cohort.find(cohort_id) now = datetime.now() new_cohort_article_map = CohortArticleMap(cohort, new_article, now) db.session.add(new_cohort_article_map) db.session.commit() return "OK" except ValueError: flask.abort(400) return "ValueError"
def get_cohort_info(id): """ Takes id of cohort and returns dictionary with id, name, inv_code, max_students, cur_students and language_name """ try: c = Cohort.find(id) name = c.name inv_code = c.inv_code max_students = c.max_students cur_students = c.get_current_student_count() try: language_id = c.language_id language = Language.query.filter_by(id=language_id).one() language_name = language.name except ValueError: language_name = "None" except sqlalchemy.orm.exc.NoResultFound: language_name = "None" dictionary = { "id": str(id), "name": name, "inv_code": inv_code, "max_students": max_students, "cur_students": cur_students, "language_name": language_name, "declared_level_min": c.declared_level_min, "declared_level_max": c.declared_level_max, "teachers_for_cohort": teachers_for_cohort(id), } return dictionary except ValueError: flask.abort(400) return "ValueError" except sqlalchemy.orm.exc.NoResultFound: flask.abort(400) return "NoResultFound"
def cohort_name(id): cohort = Cohort.find(id) return { "name": cohort.name }