コード例 #1
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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
コード例 #2
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
    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
コード例 #3
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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
コード例 #4
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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
コード例 #5
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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()
コード例 #6
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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()
コード例 #7
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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)
コード例 #8
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 def list(cls):
     with Session() as session:
         lst = session.query(ContentCategory).all()
         session.close()
     return lst
コード例 #9
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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)})
コード例 #10
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
 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)
コード例 #11
0
ファイル: db_models.py プロジェクト: LoganStalker/BotAnekdot
    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))