Exemple #1
0
    def test_userWords(self):
        u = User(username='******', email='*****@*****.**')
        w = Word(context="wordtest")
        w2 = Word(context="word2test")
        db.session.add(u)
        db.session.add(w)
        db.session.add(w2)
        db.session.commit()
        uw = UserWord(word_id=w.id, user_id=u.id)
        uw2 = UserWord(word_id=w2.id, user_id=u.id)
        db.session.add(uw)
        db.session.add(uw2)
        db.session.commit()

        self.assertEqual(u.words.all(), [uw, uw2])
Exemple #2
0
def reply(chat_id):
    chat = Chat.query.filter_by(id=chat_id).first_or_404()
    request_text = request.form['msg']
    request_msg = Message(chat_id=chat_id,
                          text=request_text,
                          author=Message.AUTHOR_USER,
                          order=chat.messages_count + 1)
    if app.config['NEURAL_LOGIC']:
        from app import sess, model, enc_vocab, rev_dec_vocab
        response_text = execute.decode_line(sess, model, enc_vocab,
                                            rev_dec_vocab, request_text)
    else:
        response_text = request.form['msg']

    response_msg = Message(chat_id=chat_id,
                           text=response_text,
                           author=Message.AUTHOR_BOT,
                           order=chat.messages_count + 2)
    for word in basic_tokenizer(request_text.encode()):
        db.session.add(UserWord(word=word))
    for word in basic_tokenizer(response_text.encode()):
        db.session.add(BotWord(word=word))

    chat.messages_count = chat.messages_count + 2

    db.session.add(request_msg)
    db.session.add(response_msg)
    db.session.commit()
    return jsonify(response_msg.as_dict())
Exemple #3
0
def create():
    identity = get_jwt_identity()
    user = User.query.filter_by(username=identity['username']).first()
    g.current_user = user
    data = request.get_json() or {}
    dictionary = Dictionary.query.get(data['dictionary_id'])
    if dictionary in g.current_user.dictionaries:
        dictionary_id = dictionary.id
    else:
        abort(403)
    if 'name' not in data:
        return bad_request('word cannot be blank')
    if Word.query.filter_by(name=data['name']).first():
        word = Word.query.filter_by(name=data['name']).first()
    else:
        word = Word(data["name"], created_by=g.current_user.username)
        db.session.add(word)
        db.session.commit()
    user_word = UserWord(word.id, dictionary_id, g.current_user.id)
    db.session.add(user_word)
    try:
        db.session.commit()
    except IntegrityError:
        return bad_request('word already is in your dictionary')
    if 'description' in data:
        user_word.description = data['description']
        db.session.add(user_word)
        db.session.commit()
    if 'translations' in data:
        for t in data['translations']:
            trns = Translation(word.id, dictionary.id, t)
            db.session.add(trns)
            db.session.commit()
    response = jsonify(word.as_json())
    response.status_code = 201
    response.headers['Location'] = url_for('words.show', id=word.id)
    return response
Exemple #4
0
def save_word_to_dict():
    # TODO
    word = request.form["word"]
    w = Word.query.filter_by(context=word).first()
    if w:
        uw = UserWord(word_id=w.id, user_id=current_user.id)
        db.session.add(uw)
        db.session.commit()
        return jsonify({
            'success': True,
            'message': f'`{w.context}` is successfully saved'
        })
    return jsonify({
        'success': False,
        'message': f'`{word}` couldnt saved'
    })
Exemple #5
0
    def test_follow_words(self):
        # create four users
        u1 = User(username='******', email='*****@*****.**')
        u2 = User(username='******', email='*****@*****.**')
        u3 = User(username='******', email='*****@*****.**')
        u4 = User(username='******', email='*****@*****.**')
        db.session.add_all([u1, u2, u3, u4])

        # create four posts
        now = datetime.utcnow()
        w1 = Word(name="cool")
        w2 = Word(name="sadie")
        w3 = Word(name="new")

        db.session.add_all([w1, w2, w3])
        db.session.commit()

        # users each have dictionary
        d1 = Dictionary(name='john dict', user_id=u1.id)
        d2 = Dictionary(name='susan dict', user_id=u2.id)
        d3 = Dictionary(name='mary dict', user_id=u3.id)
        d4 = Dictionary(name='david dict', user_id=u4.id)

        db.session.add_all([d1, d2, d3, d4])
        db.session.commit()

        # users add words to their dictionaries
        uw1 = UserWord(word_id=w1.id, user_id=u1.id, dictionary_id=d1.id)
        uw2 = UserWord(word_id=w1.id, user_id=u2.id, dictionary_id=d2.id)
        uw3 = UserWord(word_id=w1.id, user_id=u3.id, dictionary_id=d3.id)

        uw4 = UserWord(word_id=w2.id, user_id=u4.id, dictionary_id=d4.id)
        uw5 = UserWord(word_id=w3.id, user_id=u4.id, dictionary_id=d4.id)
        uw6 = UserWord(word_id=w2.id, user_id=u3.id, dictionary_id=d3.id)

        db.session.add_all([uw1, uw2, uw3, uw4, uw5, uw6])
        db.session.commit()

        # setup the followers
        u1.follow(u2)  # john follows susan
        u1.follow(u4)  # john follows david
        u2.follow(u3)  # susan follows mary
        u3.follow(u4)  # mary follows david
        db.session.commit()

        # check the followed posts of each user
        f1 = u1.friend_highlights().all() # john should see susan's and david dictionary words
        f2 = u2.friend_highlights().all() # susan should see mary words
        f3 = u3.friend_highlights().all()
        f4 = u4.friend_highlights().all()
        self.assertEqual(f1, [uw5, uw4, uw2])
        self.assertEqual(f2, [uw6, uw3])
        self.assertEqual(f3, [uw5, uw4])
        self.assertEqual(f4, [])
Exemple #6
0
    def test_unique_words_in_dict(self):
      u1 = User(username='******', email='*****@*****.**')
      d1 = Dictionary(name='john dict', user_id=u1.id)
      w1 = Word(name="cool")
      db.session.add_all([u1, d1, w1])
      db.session.commit()

      uw1 = UserWord(word_id=w1.id, user_id=u1.id, dictionary_id=d1.id)

      db.session.add(uw1)
      db.session.commit()

      # trying to add the same word twice
      db.session.add(uw1)

      try:
        db.session.commit()

      except IntegrityError:
        db.session.rollback()
        words = db.session.query(UserWord).filter(UserWord.word_id == w1.id, UserWord.user_id == u1.id, UserWord.dictionary_id == d1.id)
        self.assertTrue(words.count() == 1)
Exemple #7
0
def index():
    identity = get_jwt_identity()
    user = User.query.filter_by(username=identity['username']).first()
    g.current_user = user
    words = g.current_user.words
    return jsonify(UserWord.as_json_collection(words))