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 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)
all_users = User.query.all() for user in all_users: learned_languages = [] # Grab the learned language and add it to learned_languages learned_languages.append(user.learned_language) # Go through the user sources user_feed_registrations = RSSFeedRegistration.feeds_for_user(user) for feed_reg in user_feed_registrations: # Try catch is needed because apparently there are NoneType feeds in registrations.. try: feed = feed_reg.rss_feed feed_language = feed.language if feed_language not in learned_languages: learned_languages.append(feed_language) except Exception as e: print(e) for language in learned_languages: user_language = UserLanguage.find_or_create(session, user, language) user_language.reading_news = True session.add(user_language) print(f'Added user_languages for user user {user}') session.commit()
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")