def add_user_with_cohort():
    '''
        Creates user and adds them to a cohort
        Requires input form (email, password, username, inv_code)
    '''
    email = request.form.get("email")
    password = request.form.get("password")
    username = request.form.get("username")
    inv_code = request.form.get("inv_code")
    try:
        cohort_id = Cohort.get_id(inv_code)
        cohort = Cohort.find(cohort_id)
    except:
        flask.abort(400)

    if not len(password) == 0:
        if cohort.cohort_still_has_capacity():
            try:
                user = User(email,
                            username,
                            password,
                            invitation_code=inv_code)
                user.cohort = cohort
                db.session.add(user)
                db.session.commit()
                return 'OK'
            except ValueError:
                flask.abort(400)
            except sqlalchemy.exc.IntegrityError:
                flask.abort(400)
        return 'no more space in class!'
    flask.abort(400)
Beispiel #2
0
    def test_authorize_anonymous(self):
        random_uuid = str(uuid.uuid4())
        new_password = self.faker.password()
        anonymous_user = User.create_anonymous(random_uuid, new_password)
        self.db.session.add(anonymous_user)
        self.db.session.commit()

        result = User.authorize_anonymous(random_uuid, new_password)

        assert result is not None and result == anonymous_user
Beispiel #3
0
def unstarred_word(word_id, user_id):
    word = UserWord.query.get(word_id)
    user = User.find_by_id(user_id)
    user.starred_words.remove(word)
    model.db.session.commit()
    print word + " is now *unstarred* for user " + user.name
    return "OK"
Beispiel #4
0
def unstarred_word(word_id,user_id):
    word = UserWord.query.get(word_id)
    user = User.find_by_id(user_id)
    user.starred_words.remove(word)
    model.db.session.commit()
    print word + " is now *unstarred* for user " + user.name
    return "OK"
Beispiel #5
0
def reset_password(email):
    """
    This endpoint can be used to rest a users password.
    To do this a uniquecode is required.
    """
    last_code = UniqueCode.last_code(email)
    code = request.form.get("code", None)
    if not (last_code == code):
        return make_error(400, "Invalid code")

    password = request.form.get("password", None)
    if len(password) < 4:
        return make_error(400, "Password should be at least 4 characters long")

    user = User.find(email)
    if user is None:
        return make_error(400, "Email unknown")
    user.update_password(password)
    db_session.commit()

    # Delete all the codes for this user
    for x in UniqueCode.all_codes_for(email):
        db_session.delete(x)
    db_session.commit()

    return "OK"
Beispiel #6
0
def add_anon_user():
    """
    
        Creates anonymous user, then redirects to the get_session
        endpoint. Returns a session
        
    """

    # These two are post parameters required by the method
    uuid = request.form.get("uuid", None)
    password = request.form.get("password", None)

    # These two are optional
    language_code = request.form.get("learned_language_code", None)
    native_code = request.form.get("native_language_code", None)

    try:
        new_user = User.create_anonymous(uuid, password, language_code, native_code)
        db_session.add(new_user)
        db_session.commit()
    except ValueError as e:
        flask.abort(flask.make_response("Could not create anon user.", 400))
    except sqlalchemy.exc.IntegrityError as e:
        flask.abort(flask.make_response("Could not create anon user. Maybe uuid already exists?", 400))
    return get_anon_session(uuid)
Beispiel #7
0
def set_default_encounter_based_prob():
    zeeguu.app.test_request_context().push()
    zeeguu.db.session.commit()
    default_probability = 0.5
    languages = Language.all()
    users = User.find_all()
    for user in users:
        for lang in languages:
            marked_words_of_user_in_text = []
            words_of_all_bookmarks_content = []
            for bookmark in Bookmark.find_by_specific_user(user):
                if bookmark.origin.language == lang:
                    # bookmark_content_words = re.sub("[^\w]", " ",  bookmark.text.content).split()
                    bookmark_content_words = re.findall(r'(?u)\w+', bookmark.text.content)
                    words_of_all_bookmarks_content.extend(bookmark_content_words)
                    marked_words_of_user_in_text.append(bookmark.origin.word)
            words_known_from_user= [word for word in words_of_all_bookmarks_content if word not in marked_words_of_user_in_text]
            for word_known in words_known_from_user:
                if RankedWord.exists(word_known, lang):
                   rank = RankedWord.find(word_known, lang)
                   if EncounterBasedProbability.exists(user, rank):
                       prob = EncounterBasedProbability.find(user,rank, default_probability)
                       prob.not_looked_up_counter +=1
                   else:
                       prob = EncounterBasedProbability.find(user,rank,default_probability)
                       zeeguu.db.session.add(prob)
     		       zeeguu.db.session.commit()
    print 'job2'
def recompute_for_users():
    """

        recomputes only those caches that are already in the table
        and belong to a user. if multiple users have the same preferences
        the computation is donne only for the first because this is how
        recompute_recommender_cache_if_needed does.

        To think about:
        - what happens when this script is triggered simultaneously
        with triggering recompute_recommender_cache_if_needed from
        the UI? will there end up be duplicated recommendations?
        should we add a uninque constraint on (hash x article)?

        Note:

        in theory, the recomputing should be doable independent of users
        in practice, the recompute_recommender_cache takes the user as input.
        for that function to become independent of the user we need to be
        able to recover the ids of the languages, topics, searchers, etc. from the
        content_hash
        to do this their ids would need to be comma separated

        OTOH, in the future we might still want to have a per-user cache
        because the recommendations might be different for each user
        since every user has different language levels!!!

    :param existing_hashes:
    :return:
    """
    already_done = []
    for user_id in User.all_recent_user_ids():
        try:
            user = User.find_by_id(user_id)
            reading_pref_hash = reading_preferences_hash(user)
            if reading_pref_hash not in already_done:
                recompute_recommender_cache_if_needed(user, session)
                print(f"Success for {reading_pref_hash} and {user}")
                already_done.append(reading_pref_hash)
            else:
                print(
                    f"nno need to do for {user}. hash {reading_pref_hash} already done"
                )
        except Exception as e:
            print(f"Failed for user {user}")
Beispiel #9
0
    def setUp(self):
        app.testing = True
        self.app = app.test_client()

        with app.test_request_context():
            create_minimal_test_db(zeeguu.db)

        self.session = self.get_session()
        self.user = User.find(TEST_EMAIL)
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.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'
Beispiel #11
0
    def test_password_hash(self):
        p1 = "test"
        p2 = "pass"
        user = User.find("*****@*****.**")

        hash1 = util.password_hash(p1,user.password_salt)
        hash2 = util.password_hash(p2, user.password_salt)
        assert hash1 != hash2

        assert user.authorize("*****@*****.**", "pass") != None
Beispiel #12
0
    def test_password_hash(self):
        p1 = "test"
        p2 = "pass"
        user = User.find("*****@*****.**")

        hash1 = util.password_hash(p1,user.password_salt)
        hash2 = util.password_hash(p2, user.password_salt)
        assert hash1 != hash2

        assert user.authorize("*****@*****.**", "pass") != None
Beispiel #13
0
    def test_create_anonymous(self):
        self.user = UserRule().user
        new_password = self.faker.password()
        self.user.update_password(new_password)

        user_to_check = User.create_anonymous(str(self.user.id), new_password,
                                              self.user.learned_language.code,
                                              self.user.native_language.code)

        assert user_to_check.email == str(
            self.user.id) + User.ANONYMOUS_EMAIL_DOMAIN
        assert user_to_check.name == str(self.user.id)
        assert user_to_check.learned_language == self.user.learned_language
        assert user_to_check.native_language == self.user.native_language
Beispiel #14
0
def login():
    form = flask.request.form
    if flask.request.method == "POST" and form.get("login", False):
        password = form.get("password", None)
        email = form.get("email", None)
        if password is None or email is None:
            flask.flash("Please enter your email address and password")
        else:
            user = User.authorize(email, password)
            if user is None:
                flask.flash("Invalid email and password combination")
            else:
                flask.session["user"] = user.id
                flask.session.permanent = True
                return flask.redirect(flask.request.args.get("next") or flask.url_for("account.my_account"))
    return flask.render_template("login.html")
Beispiel #15
0
def add_user(email):
    """
    Creates user, then redirects to the get_session
    endpoint. Returns a session
    """
    password = flask.request.form.get("password", None)
    username = flask.request.form.get("username", None)
    if password is None:
        flask.abort(400)
    try:
        zeeguu.db.session.add(User(email, username, password))
        zeeguu.db.session.commit()
    except ValueError:
        flask.abort(400)
    except sqlalchemy.exc.IntegrityError:
        flask.abort(400)
    return get_session(email)
Beispiel #16
0
def get_session(email):
    """
    If the email and password match,
    a new sessionId is created, and returned
    as a string. This sessionId has to be passed
    along all the other requests that are annotated
    with @with_user in this file
    """
    password = flask.request.form.get("password", None)
    if password is None:
        flask.abort(400)
    user = User.authorize(email, password)
    if user is None:
        flask.abort(401)
    session = Session.for_user(user)
    zeeguu.db.session.add(session)
    zeeguu.db.session.commit()
    return str(session.id)
Beispiel #17
0
def get_session(email):
    """
        If the email and password match,
        a sessionId is returned as a string.
        This sessionId can to be passed
        along all the other requests that are annotated
        with @with_user in this file
    """
    password = request.form.get("password", None)
    if password is None:
        return make_error(400, "Password not given")
    user = User.authorize(email, password)
    if user is None:
        return make_error(401, "Invalid credentials")
    session = Session.for_user(user)
    db_session.add(session)
    db_session.commit()
    return str(session.id)
Beispiel #18
0
def get_session(email):
    """
    If the email and password match,
    a new sessionId is created, and returned
    as a string. This sessionId has to be passed
    along all the other requests that are annotated
    with @with_user in this file
    """
    password = flask.request.form.get("password", None)
    if password is None:
        flask.abort(400)
    user = User.authorize(email, password)
    if user is None:
        flask.abort(401)
    session = Session.for_user(user)
    zeeguu.db.session.add(session)
    zeeguu.db.session.commit()
    return str(session.id)
Beispiel #19
0
def login():
    form = flask.request.form
    if flask.request.method == "POST" and form.get("login", False):
        password = form.get("password", None)
        email = form.get("email", None)
        if password is None or email is None:
            flask.flash("Please enter your email address and password")
        else:
            user = User.authorize(email, password)
            if user is None:
                flask.flash("Invalid email and password combination")
            else:
                flask.session["user"] = user.id
                flask.session.permanent = True
                return flask.redirect(
                    flask.request.args.get("next")
                    or flask.url_for("account.my_account"))
    return flask.render_template("login.html")
Beispiel #20
0
def add_user(email):
    """
   
        Creates user, then redirects to the get_session
        endpoint. Returns a session
        
    """
    password = request.form.get("password")
    username = request.form.get("username")
    if password is None or len(password) < 4:
        return make_error(400, "Password should be at least 4 characters long")
    try:
        db_session.add(User(email, username, password))
        db_session.commit()
    except ValueError:
        return make_error(400, "Invalid value")
    except sqlalchemy.exc.IntegrityError:
        return make_error(401, "There is already an account for this email.")
    return get_session(email)
Beispiel #21
0
def get_anon_session(uuid):
    """
    
        If the uuid and password match, a  sessionId is
        returned as a string. This sessionId can to be passed
        along all the other requests that are annotated
        with @with_user in this file
        
    """
    password = request.form.get("password", None)

    if password is None:
        flask.abort(400)
    user = User.authorize_anonymous(uuid, password)
    if user is None:
        flask.abort(401)
    session = Session.for_user(user)
    db_session.add(session)
    db_session.commit()
    return str(session.id)
def set_default_exercise_based_prob():
    zeeguu.app.test_request_context().push()
    zeeguu.db.session.commit()
    users = User.find_all()
    languages = Language.all()

    for user in users:
        for language in languages:
            user_words_by_language = UserWord.find_by_language(language)
            for word in user_words_by_language:
                if ExerciseBasedProbability.exists(user, word):
                    prob = ExerciseBasedProbability.find(user, word)
                    bookmarks_by_user_and_word = Bookmark.find_all_by_user_and_word(user, word)
                    total_prob = 0
                    for bookmark in bookmarks_by_user_and_word:
                        prob.calculate_known_bookmark_probability(bookmark)
                        total_prob += float(prob.probability)
                    if bookmarks_by_user_and_word:
                        prob.probability = total_prob / len(bookmarks_by_user_and_word)
                    zeeguu.db.session.commit()
    print "job1"
def set_default_exercise_based_prob():
    zeeguu.app.test_request_context().push()
    zeeguu.db.session.commit()
    users = User.find_all()
    languages = Language.all()

    for user in users:
        for language in languages:
            user_words_by_language = UserWord.find_by_language(language)
            for word in user_words_by_language:
                if ExerciseBasedProbability.exists(user, word):
                    prob = ExerciseBasedProbability.find(user, word)
                    bookmarks_by_user_and_word = Bookmark.find_all_by_user_and_word(
                        user, word)
                    total_prob = 0
                    for bookmark in bookmarks_by_user_and_word:
                        prob.calculate_known_bookmark_probability(bookmark)
                        total_prob += float(prob.probability)
                    if bookmarks_by_user_and_word:
                        prob.probability = total_prob / len(
                            bookmarks_by_user_and_word)
                    zeeguu.db.session.commit()
    print 'job1'
Beispiel #24
0
def create_test_db():
    zeeguu.app.test_request_context().push()

    zeeguu.db.session.commit()
    zeeguu.db.drop_all()
    zeeguu.db.create_all()

    fr = Language("fr", "French")
    de = Language("de", "German")
    dk = Language("dk", "Danish")
    en = Language("en", "English")
    it = Language("it", "Italian")
    no = Language("no", "Norwegian")
    ro = Language("ro", "Romanian")
    es = Language("es", "Spanish")

    zeeguu.db.session.add(en)
    zeeguu.db.session.add(fr)
    zeeguu.db.session.add(de)
    zeeguu.db.session.add(dk)
    zeeguu.db.session.add(no)
    zeeguu.db.session.add(it)
    zeeguu.db.session.add(ro)
    zeeguu.db.session.add(es)
    zeeguu.db.session.commit()

    show_solution = ExerciseOutcome("Show solution")
    retry = ExerciseOutcome("Retry")
    correct = ExerciseOutcome("Correct")
    wrong = ExerciseOutcome("Wrong")
    typo = ExerciseOutcome("Typo")
    too_easy = ExerciseOutcome("Too easy")

    recognize = ExerciseSource("Recognize")
    translate = ExerciseSource("Translate")

    zeeguu.db.session.add(show_solution)
    zeeguu.db.session.add(retry)
    zeeguu.db.session.add(correct)
    zeeguu.db.session.add(wrong)
    zeeguu.db.session.add(typo)
    zeeguu.db.session.add(too_easy)

    zeeguu.db.session.add(recognize)
    zeeguu.db.session.add(translate)

    user = User("*****@*****.**", "Mircea", "pass", de, ro)
    user2 = User("*****@*****.**", "Ada", "pass", fr)

    zeeguu.db.session.add(user)
    zeeguu.db.session.add(user2)

    jan111 = datetime.date(2011, 01, 01)
    ian101 = datetime.date(2001, 01, 01)
    jan14 = datetime.date(2014, 1, 14)

    today_dict = {
        'sogar': 'actually',
        'sperren': 'to lock, to close',
        'Gitter': 'grates',
        'erfahren': 'to experience',
        'treffen': 'hit',
        'jeweils': 'always',
        'Darstellung': 'presentation',
        'Vertreter': 'representative',
        'Knecht': 'servant',
        'besteht': 'smtg. exists'
    }

    dict = {
        u'Spaß': 'fun',
        'solche': 'suchlike',
        'ehemaliger': 'ex',
        'betroffen': 'affected',
        'Ufer': 'shore',
        u'höchstens': 'at most'
    }

    french_dict = {'jambes': 'legs', 'de': 'of', 'et': 'and'}

    story_url = 'http://www.gutenberg.org/files/23393/23393-h/23393-h.htm'
    japanese_story = [
        # ['recht', 'right', 'Du hast recht', story_url],
        [
            'hauen', 'to chop', 'Da waren einmal zwei Holzhauer können',
            story_url
        ],
        [
            u'Wald', 'to arrive',
            u'Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren. Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren. Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren. Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren',
            story_url
        ],
        [
            'eingerichtet', 'established',
            u'Um in den Wald zu gelangen, mußten sie einen großen Fluß passieren, über den eine Fähre eingerichtet war',
            story_url
        ],
        [
            u'vorläufig', 'temporary',
            u'von der er des rasenden Sturmes wegen vorläufig nicht zurück konnte',
            story_url
        ],
        [
            u'werfen', 'to throw',
            u'Im Hause angekommen, warfen sie sich zur Erde,', story_url
        ],
        [
            'Tosen', 'roar',
            u'sie Tür und Fenster wohl verwahrt hatten und lauschten dem Tosen des Sturmes.sie Tür und Fenster wohl verwahrt hatten und lauschten dem Tosen des Sturmes.sie Tür und Fenster wohl verwahrt hatten und lauschten dem Tosen des Sturmes',
            story_url
        ],
        [
            'Entsetzen', 'horror', 'Entsetzt starrte Teramichi auf die Wolke',
            story_url
        ]
    ]

    add_ranked_word_to_db('de')

    for key in today_dict:
        add_bookmark(
            user, de, key, en, today_dict[key], jan111,
            "Keine bank durfe auf immunitat pochen, nur weil sie eine besonders herausgehobene bedeutung fur das finanzsystem habe, sagte holder, ohne namen von banken zu nennen"
            + key, "http://url2", "title of url2")

    for key in dict:
        add_bookmark(
            user, de, key, en, dict[key], ian101,
            "Deutlich uber dem medianlohn liegen beispielsweise forschung und entwicklung, tabakverarbeitung, pharma oder bankenwesen, am unteren ende der skala liegen die tieflohnbranchen detailhandel, gastronomie oder personliche dienstleistungen. "
            + key, "http://url1", "title of url1")

    for key in french_dict:
        add_bookmark(
            user, de, key, en, french_dict[key], ian101,
            "Deutlich uber dem medianlohn liegen beispielsweise forschung und entwicklung, tabakverarbeitung, pharma oder bankenwesen, am unteren ende der skala liegen die tieflohnbranchen detailhandel, gastronomie oder personliche dienstleistungen. "
            + key, "http://url1", "title of url1")
    for w in japanese_story:
        add_bookmark(user, de, w[0], en, w[1], jan14, w[2], w[3],
                     "japanese story")

    zeeguu.db.session.commit()
        return diffs



if __name__ == "__main__":
    optimization_goals = OptimizationGoals(
        words_in_parallel=20, words_in_parallel_factor=3,
        repetition_correct_factor=0,
        repetition_incorrect_factor=0
    )

    # update exercise source stats
    BookmarkPriorityUpdater._update_exercise_source_stats()

    # optimize for algorithm for these users
    users = User.find_all()

    start = timer()

    user_ids = [user.id for user in users]
    results = []
    for user_id in user_ids:
        algorithm = ArtsRT()
        evaluator = AlgorithmEvaluator(user_id, algorithm, change_limit=1.0)
        variables_to_set = [
            ['d', getattr(algorithm, 'd'), +5],
            ['b', getattr(algorithm, 'b'), +10],
            ['w', getattr(algorithm, 'w'), +10]
        ]
        result = evaluator.fit_parameters(variables_to_set, optimization_goals)
        if result is not None:
Beispiel #26
0
 def test_validate_email(self):
     random_email = self.faker.email()
     assert User.validate_email('', random_email)
Beispiel #27
0
 def test_validate_name(self):
     random_name = self.faker.name()
     assert User.validate_name('', random_name)
Beispiel #28
0
def starred_word(word_id,user_id):
    word = UserWord.query.get(word_id)
    user = User.find_by_id(user_id)
    user.star(word)
    model.db.session.commit()
    return "OK"
Beispiel #29
0
 def test_validate_password(self):
     random_password = self.faker.password()
     assert User.validate_password('', random_password)
Beispiel #30
0
def starred_word(word_id, user_id):
    word = UserWord.query.get(word_id)
    user = User.find_by_id(user_id)
    user.star(word)
    model.db.session.commit()
    return "OK"
Beispiel #31
0
 def test_exists(self):
     assert User.exists(self.user)
 def __get_bookmarks_for_user(self, user_id):
     user = User.find_by_id(user_id)
     print('Using user ' + user.name + ' with id ' + str(user.id))
     return user.all_bookmarks()
Beispiel #33
0
    def test_authorize(self):
        new_password = self.faker.password()
        self.user.update_password(new_password)
        result = User.authorize(self.user.email, new_password)

        assert result is not None and result == self.user
Beispiel #34
0
#!/usr/bin/env python
"""

   Script that lists recent users

   To be called from a cron job.

"""

from zeeguu.model import User

for user_id in User.all_recent_user_ids():
    user = User.find_by_id(user_id)
    print(user.name)
    print(user.email)