def set_privacy(bot, update): chat_id = update.message.chat_id chat_type = update.message.chat.type user_id = update.message.from_user.id msg_id = update.message.message_id user = User.get(user_id) if user: privacy = user.public if privacy: public = False msg = 'Your statistics is *private*' else: public = True msg = 'Your statistics is *public*\n\n' \ '{0}/user/{1}'.format(CONFIG['site_url'], user_id) User.update(user_id, {'public': public}) cache.delete('user_{}'.format(user_id)) else: msg = 'User not found' bot.sendMessage(chat_id, msg, parse_mode=ParseMode.MARKDOWN, reply_to_message_id=msg_id)
def get(uid): cached = cache.get('user_{}'.format(uid)) if cached: return cached else: q = db.query(User) \ .filter(User.uid == uid) \ .limit(1) \ .all() if q: cache.set('user_{}'.format(uid), q[0]) cache.delete('web_user_{}'.format(uid)) return q[0] else: return False
def get(cid): cached = cache.get('chat_{}'.format(cid)) if cached: return cached else: q = db.query(Chat) \ .filter(Chat.cid == cid) \ .order_by(Chat.id.desc()) \ .limit(1) \ .all() if q: cache.set('chat_{}'.format(cid), q[0]) cache.delete('web_chat_{}'.format(cid)) return q[0] else: return False
def add(self, cid, title, public_link=''): chat = self.get(cid) if chat: if chat.title != title: self.update(cid, {'title': title}) if chat.public_link != public_link: self.update(cid, {'public_link': public_link}) else: db.add(Chat(cid=cid, title=title, public_link=public_link)) cache.set('chat_{}'.format(cid), Chat(cid=cid, title=title)) cache.delete('web_chat_{}'.format(cid)) db.commit()
def update_to_supergroup(bot, update): old_id = update.message.migrate_from_chat_id new_id = update.message.chat_id user_id = update.message.from_user.id if old_id: UserStat.update(user_id, old_id, {'cid': new_id}) Entity.update_all(old_id, {'cid': new_id}) Chat.update(old_id, {'cid': new_id}) # Update all rows in chat_stats for c in db.query(ChatStat)\ .filter(ChatStat.cid == old_id)\ .all(): c.cid = new_id db.commit() bot.sendMessage(new_id, 'Group was updated to supergroup') cache.delete('last_{}'.format(old_id)) logger.info('Group {} was updated to supergroup {}'.format( old_id, new_id))
def add(self, uid, username, fullname): user = self.get(uid) update = {} if user: if user.username != username: update['username'] = username if user.fullname != fullname: update['fullname'] = fullname if update: self.update(uid, update) else: db.add(User(uid=uid, username=username, fullname=fullname)) db.commit() cache.set('user_'.format(uid), User(uid=uid, username=username, fullname=fullname)) cache.delete('web_user_{}'.format(uid))
def refresh_flags(): key = get_db_flags.make_cache_key() cache.delete(key)
def refresh_states(): key = get_states.make_cache_key() cache.delete(key)
def clear_from_order(user: User, session_id): orders_key = __current_orders_key(user, session_id) cache.delete(orders_key)