diff --git a/migrate.py b/migrate.py index 38c66e9b..485fbf95 100644 --- a/migrate.py +++ b/migrate.py @@ -122,19 +122,7 @@ def shouts(content_data, shouts_by_slug, shouts_by_oid): # limiting try: limit = int(sys.argv[2]) if len(sys.argv) > 2 else len(content_data) except ValueError: limit = len(content_data) - - with local_session() as session: - community = session.query(Community).filter(Community.id == 0).first() - if not community: - Community.create(**{ - 'id' : 0, - 'slug': 'discours.io', - 'name': 'Дискурс', - 'pic': 'https://discours.io/images/logo-min.svg', - 'createdBy': '0', - 'createdAt': date_parse(OLD_DATE) - }) - + if not topics_by_cat: with local_session() as session: topics = session.query(Topic).all() @@ -270,11 +258,6 @@ if __name__ == '__main__': users_by_oid = {} users_by_slug = {} - with local_session() as session: - default_user = session.query(User).filter(User.id == 0).first() - if not default_user: - default_user = User.create(id = 0, email = "discours@discours.io", username = "discours", slug = "default", old_id = 0) - user_id_map = {} with local_session() as session: users_list = session.query(User).all() diff --git a/migration/tables/content_items.py b/migration/tables/content_items.py index 5a8ab125..55fdc0df 100644 --- a/migration/tables/content_items.py +++ b/migration/tables/content_items.py @@ -11,6 +11,7 @@ from transliterate import translit from datetime import datetime from sqlalchemy.exc import IntegrityError from orm.base import local_session +from orm.community import Community DISCOURS_USER = { 'id': 9999999, @@ -71,7 +72,7 @@ def migrate(entry, users_by_oid, topics_by_oid): r = { 'layout': type2layout[entry['type']], 'title': entry['title'], - 'community': 0, + 'community': Community.default_community.id, 'authors': [], 'topics': [], 'views': entry.get('views', 0), diff --git a/orm/__init__.py b/orm/__init__.py index 1a14abbd..f7c61801 100644 --- a/orm/__init__.py +++ b/orm/__init__.py @@ -14,6 +14,7 @@ __all__ = ["User", "Role", "Community", "Operation", "Permission", "Message", "S Base.metadata.create_all(engine) Operation.init_table() Resource.init_table() +User.init_table() Community.init_table() Role.init_table() diff --git a/orm/base.py b/orm/base.py index d653b6ce..0904d1f6 100644 --- a/orm/base.py +++ b/orm/base.py @@ -33,7 +33,7 @@ class Base(declarative_base()): @classmethod def create(cls: Generic[T], **kwargs) -> Generic[T]: instance = cls(**kwargs) - return instance.save(session) + return instance.save() def save(self) -> Generic[T]: with local_session() as session: diff --git a/orm/community.py b/orm/community.py index 8f45ee51..05be74e8 100644 --- a/orm/community.py +++ b/orm/community.py @@ -18,14 +18,11 @@ class Community(Base): def init_table(): with local_session() as session: default = session.query(Community).filter(Community.slug == "default").first() - if default: - Community.default_community = default - return - - default = Community.create( - name = "default", - slug = "default", - createdBy = 0 - ) + if not default: + default = Community.create( + name = "default", + slug = "default", + createdBy = 0 #TODO: use default user + ) Community.default_community = default diff --git a/orm/user.py b/orm/user.py index 33e4401a..f0db95a6 100644 --- a/orm/user.py +++ b/orm/user.py @@ -4,9 +4,8 @@ from datetime import datetime from sqlalchemy import Table, Column, Integer, String, ForeignKey, Boolean, DateTime, JSON as JSONType from sqlalchemy.orm import relationship, selectinload -from orm import RoleStorage from orm.base import Base, local_session -from orm.rbac import Role +from orm.rbac import Role, RoleStorage from orm.topic import Topic import asyncio @@ -62,6 +61,20 @@ class User(Base): roles = relationship(lambda: Role, secondary=UserRole.__tablename__) topics = relationship(lambda: Topic, secondary=UserTopics) old_id: str = Column(String, nullable = True) + + @staticmethod + def init_table(): + with local_session() as session: + default = session.query(User).filter(User.slug == "default").first() + if not default: + default = User.create( + id = 0, + email = "discours@discours.io", + username = "discours", + slug = "default" + ) + + User.default_user = default async def get_permission(self): scope = {}