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)
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)
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)
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)
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}})
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})
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)
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)
def test_get_city_without_user(self): cm = db.CityModel() with pytest.raises(exceptions.NoUserException): cm.get_city('')
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'})
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")