def set_learned_language_level(self, language_code: str, level: str, session=None): learned_language = Language.find_or_create(language_code) from zeeguu_core.model import UserLanguage language = UserLanguage.find_or_create(session, self, learned_language) language.cefr_level = int(level) if session: session.add(language)
def create_anonymous(cls, uuid, password, learned_language_code=None, native_language_code=None): """ :param uuid: :param password: :param learned_language_code: :param native_language_code: :return: """ # since the DB must have an email we generate a fake one fake_email = uuid + cls.ANONYMOUS_EMAIL_DOMAIN if learned_language_code is not None: try: learned_language = Language.find_or_create( learned_language_code) except NoResultFound as e: learned_language = None else: learned_language = None if native_language_code is not None: try: native_language = Language.find_or_create(native_language_code) except NoResultFound as e: native_language = None else: native_language = None new_user = cls( fake_email, uuid, password, learned_language=learned_language, native_language=native_language, ) return new_user
def upload_own_text(): db_session.rollback() language = Language.find_or_create(request.form.get("language", "")) content = request.form.get("content", "") title = request.form.get("title", "") new_article_id = Article.create_from_upload( db_session, title, content, flask.g.user, language ) return str(new_article_id)
def update_own_text(id): language = Language.find_or_create(request.form.get("language", "")) content = request.form.get("content", "") title = request.form.get("title", "") a = Article.query.filter(Article.id == id).one() a.language = language a.content = content a.title = title db_session.add(a) db_session.commit() return "OK"
def __init__(self): super().__init__() self.rss_feed = self._create_model_object() self.feed = self.rss_feed self.save(self.rss_feed) lang1 = Language.find_or_create('de') url = Url.find_or_create(self.db.session, url_spiegel_rss) self.feed1 = RSSFeed.find_or_create(self.db.session, url, "", "", icon_name_spiegel, language=lang1) self.save(self.feed1)
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"
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) inv_code = request.form.get("inv_code") name = request.form.get("name") language_id = request.form.get("language_id") if name is None or inv_code is None or language_id is None: flask.abort(400) available_languages = Language.available_languages() code_allowed = False for code in available_languages: if language_id in str(code): code_allowed = True if not code_allowed: flask.abort(400) language = Language.find_or_create(language_id) teacher_id = flask.g.user.id max_students = request.form.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 tts(): import zeeguu_core from zeeguu_core.model import UserWord, Language db_session = zeeguu_core.db.session text_to_pronounce = request.form.get("text", "") language_id = request.form.get("language_id", "") if not text_to_pronounce: return "" user_word = UserWord.find_or_create(db_session, text_to_pronounce, Language.find_or_create(language_id)) audio_file_path = _file_name_for_user_word(user_word, language_id) if not os.path.isfile(DATA_FOLDER + audio_file_path): _save_speech_to_file(user_word, language_id, audio_file_path) return audio_file_path
test_feed = test_feed(_feed_url) feed_name = input(f"Feed name (Enter for: {test_feed.title}): ") or test_feed.title print(f'= {feed_name}') icon_name = input( "Icon name to be found in resources folder (e.g. 20min.png): ") print(f'= {icon_name}') description = input(f'Description (Enter for: {test_feed.description}): ') or test_feed.description print(f'= {description}') _language = input("Language code (e.g. en): ") print(f'= {_language}') feed_url = Url.find_or_create(zeeguu_core.db.session, _feed_url) language = Language.find_or_create(_language) rss_feed = RSSFeed.find_or_create(zeeguu_core.db.session, feed_url, feed_name, description, icon_name=icon_name, language=language) print("Done: ") print(rss_feed.title) print(rss_feed.description) print(rss_feed.language_id) print(rss_feed.url.as_string())
def create_account( db_session, username, password, invite_code, email, learned_language_code, native_language_code, learned_cefr_level, ): cohort_name = "" if password is None or len(password) < 4: raise Exception("Password should be at least 4 characters long") if not valid_invite_code(invite_code): raise Exception( "Invitation code is not recognized. Please contact us.") cohort = Cohort.query.filter_by(inv_code=invite_code).first() if cohort: if cohort.cohort_still_has_capacity(): cohort_name = cohort.name else: raise Exception( "No more places in this class. Please contact us ([email protected])." ) try: learned_language = Language.find_or_create(learned_language_code) native_language = Language.find_or_create(native_language_code) new_user = User( email, username, password, invitation_code=invite_code, cohort=cohort, learned_language=learned_language, native_language=native_language, ) db_session.add(new_user) learned_language = UserLanguage.find_or_create(db_session, new_user, learned_language) learned_language.cefr_level = int(learned_cefr_level) # TODO: although these are required... they should simply # be functions of CEFR level so at some further point should # removed learned_language.declared_level_min = 0 learned_language.declared_level_max = 11 db_session.add(learned_language) if cohort: if cohort.is_cohort_of_teachers: teacher = Teacher(new_user) db_session.add(teacher) db_session.commit() send_new_user_account_email(username, invite_code, cohort_name) return new_user except sqlalchemy.exc.IntegrityError: raise Exception("There is already an account for this email.") except Exception as e: print(e) raise Exception("Could not create the account")
feed_name = input( f"Feed name (Enter for: {test_feed.title}): ") or test_feed.title print(f'= {feed_name}') icon_name = input( "Icon name to be found in resources folder (e.g. 20min.png): ") print(f'= {icon_name}') description = input(f'Description (Enter for: {test_feed.description}): ' ) or test_feed.description print(f'= {description}') _language = input("Language code (e.g. en): ") print(f'= {_language}') feed_url = Url.find_or_create(zeeguu_core.db.session, _feed_url) language = Language.find_or_create(_language) rss_feed = RSSFeed.find_or_create(zeeguu_core.db.session, feed_url, feed_name, description, icon_name=icon_name, language=language) print("Done: ") print(rss_feed.title) print(rss_feed.description) print(rss_feed.language_id) print(rss_feed.url.as_string())