def get_seens(cls, chat_id): with Session() as session: ids_q = session.query( cls.seen).filter(cls.chat_id == chat_id).first() ids = json.loads(ids_q.seen) session.close() return ids
def get_random_content(cls, theme, chat_id): print('theme', theme) with Session() as session: ids_seen = User.get_seens(chat_id) if theme in ids_seen.keys(): ids_seen = ids_seen[theme] else: ids_seen = [] ids = session.query(cls.id).join(Themes, Themes.id == cls.theme_id).filter( not_(cls.id.in_(ids_seen)), Themes.theme == theme).all() if not ids: User.clear_seen(chat_id, theme) ids = session.query(cls.id).join( Themes, Themes.id == cls.theme_id).filter( Themes.theme == theme).all() id = choice(ids).id User.add_seen_id(chat_id, theme, id) content = session.query( cls.title, cls.body, ).filter(cls.id == id, ).first() session.close() return content
def get(cls, data): with Session() as session: user = session.query(cls).filter( cls.chat_id == data.chat.id, cls.username == data.chat.username, ).first() session.close() return user
def list(cls, category): with Session() as session: lst = session.query(Themes.theme).join( ContentCategory, ContentCategory.id == Themes.category_id, ).filter(ContentCategory.category == category, ).all() session.close() return lst
def update_memory(cls, chat_id, datas): with Session() as session: mem = cls.read_memory(chat_id) mem.update(datas) session.query(cls).filter(cls.chat_id == chat_id).update( {'memory': json.dumps(mem)}) session.commit() session.close()
def add_seen_id(cls, chat_id, theme, id): with Session() as session: ids = cls.get_seens(chat_id) if theme not in ids.keys(): ids.update({theme: []}) ids[theme].append(id) session.query(cls).filter(cls.chat_id == chat_id).update( {'seen': json.dumps(ids)}) session.commit() session.close()
def create(cls, data): with Session() as session: session.add(cls(username=data.chat.username, chat_id=data.chat.id)) session.commit() return cls.get(data)
def list(cls): with Session() as session: lst = session.query(ContentCategory).all() session.close() return lst
def clear_seen(cls, chat_id, theme): with Session() as session: seens = cls.get_seens(chat_id) seens.update({theme: []}) session.query(cls).filter(cls.chat_id == chat_id).update( {'seen': json.dumps(seens)})
def read_memory(cls, user_chat_id): with Session() as session: mem = session.query( User.memory).filter(cls.chat_id == user_chat_id).first() session.close() return json.loads(mem.memory)
def get_seens(cls, chat_id): with Session() as session: ids_q = session.query( cls.seen).filter(cls.chat_id == chat_id).first() ids = json.loads(ids_q.seen) session.close() return ids if __name__ == "__main__": # Base.metadata.create_all(engine) from database.dbconnector import Session from content_description import categories, themes, content with Session() as session: for cat in categories: check = session.query(ContentCategory).filter( ContentCategory.category == cat).first() if not check: session.add(ContentCategory(category=cat)) session.commit() for cat in themes.keys(): cat_id = session.query(ContentCategory.id).filter( ContentCategory.category == cat).first() for th in themes.get(cat): check = session.query(Themes).filter( Themes.theme == th).first() if not check: session.add(Themes(category_id=cat_id.id, theme=th))