Ejemplo n.º 1
0
    def updates(bot, update):
        """Returns list of updated posts"""
        user_id = utils.get_user_id(update)
        cm = db.CityModel()
        wm = db.WatchModel()
        pm = db.PostsModel()

        user_city = cm.get_city(user_id)
        watchlist = wm.watchlist(user_id)

        posts = []
        for item in watchlist:
            for post in feed.get_posts(user_city, item['keyword']):
                if not pm.is_post_seen(user_id, post.post_id):
                    posts.append(post)
                    pm.mark_as_seen(user_id, post.post_id)

        if not posts:
            utils.send_message_with_keyboard(bot, update, "No updates so far.")
            return

        for ten_posts in utils.chunks(posts, 10):
            updates = ""
            for post in ten_posts:
                updates += post.oneline
            utils.send_message_with_keyboard(bot, update, updates)
Ejemplo n.º 2
0
    def test_update_city_empty_user_id(self, mocker):
        FAKE_USER_ID = ''
        FAKE_CITY_NAME = 'austin'

        cm = db.CityModel()
        mocker.patch.object(cm.table, 'update')
        with pytest.raises(exceptions.NoUserException):
            cm.update_city(FAKE_USER_ID, FAKE_CITY_NAME)
Ejemplo n.º 3
0
    def test_set_city_with_empty_user(self, mocker):
        FAKE_CITY = 'city'
        FAKE_USER_ID = ''

        cm = db.CityModel()
        mocker.patch.object(cm.table, 'insert_one')
        with pytest.raises(exceptions.NoUserException):
            cm.set_city(FAKE_USER_ID, FAKE_CITY)
Ejemplo n.º 4
0
    def test_update_city_empty_city(self, mocker):
        FAKE_USER_ID = 'userid'
        FAKE_CITY_NAME = ''

        cm = db.CityModel()
        mocker.patch.object(cm.table, 'update')
        with pytest.raises(exceptions.EmptyCityException):
            cm.update_city(FAKE_USER_ID, FAKE_CITY_NAME)
Ejemplo n.º 5
0
    def test_update_city(self, mocker):
        FAKE_USER_ID = 'userid'
        FAKE_CITY_NAME = 'austin'

        cm = db.CityModel()
        update_one = mocker.patch.object(cm.table, 'update_one')
        cm.update_city(FAKE_USER_ID, FAKE_CITY_NAME)
        update_one.assert_called_once_with(
            {'user_id': FAKE_USER_ID}, {'$set': {'city': FAKE_CITY_NAME}})
Ejemplo n.º 6
0
    def test_set_city(self, mocker):
        FAKE_CITY = 'city'
        FAKE_USER_ID = 'userid'

        cm = db.CityModel()
        mocker.patch.object(cm, 'is_city_set', return_value=False)
        insert_one = mocker.patch.object(cm.table, 'insert_one')
        cm.set_city(FAKE_USER_ID, FAKE_CITY)
        insert_one.assert_called_once_with({
            'city': FAKE_CITY,
            'user_id': FAKE_USER_ID})
Ejemplo n.º 7
0
    def search(bot, update):
        user_id = utils.get_user_id(update)
        cm = db.CityModel()
        user_city = cm.get_city(user_id)

        posts = feed.get_posts(user_city, update.message.text)

        text = ""
        for p in posts:
            text += p.oneline

        if not text:
            text = "Empty search result."
        utils.send_message_with_keyboard(bot, update, text)
Ejemplo n.º 8
0
    def city(bot, update):
        city_name = utils.extract_command_value(update)
        if city_name not in utils.get_craigslist_sites():
            bot.sendMessage(chat_id=update.message.chat_id,
                            text="City not found. Please send /citylist "
                            "to check list of avaliable cities.")
            return

        user_id = utils.get_user_id(update)

        cm = db.CityModel()
        cm.set_city(user_id=user_id, city=city_name)

        utils.send_message_with_keyboard(bot, update,
                                         "City set to %s" % city_name)
Ejemplo n.º 9
0
 def test_get_city_without_user(self):
     cm = db.CityModel()
     with pytest.raises(exceptions.NoUserException):
         cm.get_city('')
Ejemplo n.º 10
0
 def test_get_city(self, mocker):
     cm = db.CityModel()
     find_one = mocker.patch.object(cm.table, 'find_one')
     cm.get_city('user_id')
     find_one.assert_called_once_with({'user_id': 'user_id'})
Ejemplo n.º 11
0
 def restart(bot, update):
     db.PostsModel().delete_all()
     db.WatchModel().delete_all()
     db.CityModel().delete_all()
     utils.send_message_with_keyboard(bot, update, "Total reset")