コード例 #1
0
def cohort_files(cohort_id):
    '''
        Gets the files associated with a cohort
    '''
    cohort = Cohort.find(cohort_id) 
    articles = CohortArticleMap.get_articles_info_for_cohort(cohort)
    return json.dumps(articles)
コード例 #2
0
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}
        return dictionary
    except ValueError:
        flask.abort(400)
        return 'ValueError'
    except sqlalchemy.orm.exc.NoResultFound:
        flask.abort(400)
        return "NoResultFound"
コード例 #3
0
def cohort_files(cohort_id):
    """
    Gets the files associated with a cohort
    """
    cohort = Cohort.find(cohort_id)
    articles = CohortArticleMap.get_articles_info_for_cohort(cohort)
    return json.dumps(articles)
コード例 #4
0
def cohort_articles_for_user(user):
    try:
        cohort = Cohort.find(user.cohort_id)
        cohort_articles = CohortArticleMap.get_articles_info_for_cohort(cohort)
        return cohort_articles
    except NoResultFound as e:
        return []
コード例 #5
0
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
コード例 #6
0
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'
コード例 #7
0
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'
コード例 #8
0
def create_own_cohort():
    """
    Creates a cohort in the database.
    Requires form input (inv_code, name, language_id, max_students, teacher_id)

    """

    if not _is_teacher(flask.g.user.id):
        flask.abort(401)

    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"
コード例 #9
0
ファイル: student.py プロジェクト: mircealungu/Zeeguu-API
def join_cohort():
    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 cohort.name
    except Exception as e:
        from sentry_sdk import capture_exception

        capture_exception(e)
        flask.abort(500)
コード例 #10
0
def add_article_to_cohort():
    """
    Gets all the articles of this teacher
    """

    cohort = Cohort.find(request.form.get("cohort_id"))

    if not has_permission_for_cohort(cohort.id):
        flask.abort(401)

    article = Article.find_by_id(request.form.get("article_id"))

    if not CohortArticleMap.find(cohort.id, article.id):
        new_mapping = CohortArticleMap(cohort, article)
        db.session.add(new_mapping)
        db.session.commit()

    return "OK"
コード例 #11
0
def delete_article_from_cohort():
    """
    Gets all the articles of this teacher
    """

    cohort = Cohort.find(request.form.get("cohort_id"))

    if not has_permission_for_cohort(cohort.id):
        flask.abort(401)

    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!")
コード例 #12
0
def upload_articles(cohort_id):
    '''
        uploads articles for a cohort with input from a POST request
    '''
    if (not has_permission_for_cohort(cohort_id)):
        flask.abort(401)
    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)
            new_cohort_article_map = CohortArticleMap(cohort, new_article)

            db.session.add(new_cohort_article_map)
        db.session.commit()
        return 'OK'
    except ValueError:
        flask.abort(400)
        return 'ValueError'
コード例 #13
0
def upload_articles(cohort_id):
    """
    uploads articles for a cohort with input from a POST request
    """
    if not has_permission_for_cohort(cohort_id):
        flask.abort(401)
    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)
            new_cohort_article_map = CohortArticleMap(cohort, new_article)

            db.session.add(new_cohort_article_map)
        db.session.commit()
        return "OK"
    except ValueError:
        flask.abort(400)
        return "ValueError"
コード例 #14
0
def upload_articles(cohort_id):
    '''
        uploads articles for a cohort with input from a POST request
    '''
    if (not has_permission_for_cohort(cohort_id)):
        flask.abort(401)
    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)
            new_cohort_article_map = CohortArticleMap(cohort, new_article)

            db.session.add(new_cohort_article_map)
        db.session.commit()
        return 'OK'
    except ValueError:
        flask.abort(400)
        return 'ValueError'
コード例 #15
0
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,
        }
        return dictionary
    except ValueError:
        flask.abort(400)
        return "ValueError"
    except sqlalchemy.orm.exc.NoResultFound:
        flask.abort(400)
        return "NoResultFound"
コード例 #16
0
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
        }
        return dictionary
    except ValueError:
        flask.abort(400)
        return 'ValueError'
    except sqlalchemy.orm.exc.NoResultFound:
        flask.abort(400)
        return "NoResultFound"
コード例 #17
0
ファイル: accounts.py プロジェクト: mircealungu/Zeeguu-API
def _valid_invite_code(invite_code: str):
    return (
            invite_code in zeeguu_core.app.config.get("INVITATION_CODES")
            or Cohort.exists_with_invite_code(invite_code)
    )
コード例 #18
0
def _valid_invite_code(invite_code: str):
    return (invite_code in zeeguu_core.app.config.get("INVITATION_CODES")
            or Cohort.exists_with_invite_code(invite_code))