Exemplo n.º 1
0
def update_cohort(cohort_id):
    """
    changes details of a specified cohort.
    requires input form (inv_code, name, max_students)

    """
    if not has_permission_for_cohort(cohort_id):
        flask.abort(401)
    try:
        params = request.form

        cohort_to_change = Cohort.query.filter_by(id=cohort_id).one()
        cohort_to_change.inv_code = params.get("inv_code")
        cohort_to_change.name = params.get("name")

        # language_id is deprecated; use language_code instead
        language_code = params.get("language_code") or params.get(
            "language_id")
        cohort_to_change.language_id = Language.find(language_code).id

        cohort_to_change.declared_level_min = params.get("declared_level_min")
        cohort_to_change.declared_level_max = params.get("declared_level_max")

        db.session.commit()
        return "OK"
    except ValueError:
        flask.abort(400)
        return "ValueError"
    except sqlalchemy.exc.IntegrityError:
        flask.abort(400)
        return "IntegrityError"
Exemplo n.º 2
0
def cleanup_all_articles_in_language(language_code):
    language_id = Language.find(language_code).id
    all_articles = Article.query.filter_by(language_id=language_id).all()
    for each in all_articles:
        cleaned_content = cleanup_non_content_bits(each.content)
        if cleaned_content != each.content:
            each.content = cleaned_content
            zeeguu_core.db.session.add(each)
            print(each.title + "\n\n")
    zeeguu_core.db.session.commit()
Exemplo n.º 3
0
def _get_past_translation(word: str,
                          from_lang_code: str,
                          to_lang_code: str,
                          context: str,
                          user: User = None):
    try:

        from_language = Language.find(from_lang_code)

        to_language = Language.find(to_lang_code)

        try:
            origin_word = UserWord.find(word, from_language)
            text = Text.query.filter_by(content=context).one()
        except NoResultFound:
            return None

        query = Bookmark.query.join(UserWord, UserWord.id==Bookmark.translation_id).\
            filter(UserWord.language_id==to_language.id,
                   Bookmark.origin_id==origin_word.id,
                   Bookmark.origin_id==origin_word.id,
                   Bookmark.text_id==text.id)

        if user:
            query = query.filter(Bookmark.user_id == user.id)

        # prioritize older users
        query.order_by(Bookmark.user_id.asc())

        if query.first():
            return query.first().translation.word
        else:
            return None

    except Exception as e:
        capture_exception(e)

        return None
Exemplo n.º 4
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'
Exemplo n.º 5
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"
Exemplo n.º 6
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'
Exemplo n.º 7
0
    def set_learned_language(self, language_code, session=None):
        self.learned_language = Language.find(language_code)

        from zeeguu_core.model import UserLanguage

        # disable the exercises and reading for all the other languages
        all_other_languages = (UserLanguage.query.filter(
            User.id == self.id).filter(
                UserLanguage.doing_exercises == True).all())
        for each in all_other_languages:
            each.doing_exercises = False
            each.reading_news = False
            if session:
                session.add(each)

        language = UserLanguage.find_or_create(session, self,
                                               self.learned_language)
        language.reading_news = True
        language.doing_exercises = True

        if session:
            session.add(language)
Exemplo n.º 8
0
 def set_native_language(self, code):
     self.native_language = Language.find(code)
import zeeguu_core
from zeeguu_core.model import Article, Language, LocalizedTopic

session = zeeguu_core.db.session

counter = 0

languages = Language.available_languages()
languages = [Language.find('da')]

for language in languages:
    articles = Article.query.filter(Article.language == language).order_by(Article.id.desc()).all()

    loc_topics = LocalizedTopic.all_for_language(language)

    total_articles = len(articles)
    for article in articles:
        counter += 1
        print(f"{article.title}")
        print(f"{article.url.as_string()}")
        for loc_topic in loc_topics:
            if loc_topic.matches_article(article):
                article.add_topic(loc_topic.topic)
                print(f" #{loc_topic.topic_translated}")
        print("")
        session.add(article)

        if counter % 1000 == 0:
            percentage = (100 * counter / total_articles) / 100
            print(f"{counter} dorticles done ({percentage}%). last article id: {article.id}. Comitting... ")
            session.commit()