create default user; minor fixes
This commit is contained in:
parent
6e939f43db
commit
d0fdfd2b31
19
migrate.py
19
migrate.py
|
@ -122,19 +122,7 @@ def shouts(content_data, shouts_by_slug, shouts_by_oid):
|
||||||
# limiting
|
# limiting
|
||||||
try: limit = int(sys.argv[2]) if len(sys.argv) > 2 else len(content_data)
|
try: limit = int(sys.argv[2]) if len(sys.argv) > 2 else len(content_data)
|
||||||
except ValueError: limit = 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:
|
if not topics_by_cat:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
topics = session.query(Topic).all()
|
topics = session.query(Topic).all()
|
||||||
|
@ -270,11 +258,6 @@ if __name__ == '__main__':
|
||||||
users_by_oid = {}
|
users_by_oid = {}
|
||||||
users_by_slug = {}
|
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 = {}
|
user_id_map = {}
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
users_list = session.query(User).all()
|
users_list = session.query(User).all()
|
||||||
|
|
|
@ -11,6 +11,7 @@ from transliterate import translit
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from orm.base import local_session
|
from orm.base import local_session
|
||||||
|
from orm.community import Community
|
||||||
|
|
||||||
DISCOURS_USER = {
|
DISCOURS_USER = {
|
||||||
'id': 9999999,
|
'id': 9999999,
|
||||||
|
@ -71,7 +72,7 @@ def migrate(entry, users_by_oid, topics_by_oid):
|
||||||
r = {
|
r = {
|
||||||
'layout': type2layout[entry['type']],
|
'layout': type2layout[entry['type']],
|
||||||
'title': entry['title'],
|
'title': entry['title'],
|
||||||
'community': 0,
|
'community': Community.default_community.id,
|
||||||
'authors': [],
|
'authors': [],
|
||||||
'topics': [],
|
'topics': [],
|
||||||
'views': entry.get('views', 0),
|
'views': entry.get('views', 0),
|
||||||
|
|
|
@ -14,6 +14,7 @@ __all__ = ["User", "Role", "Community", "Operation", "Permission", "Message", "S
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
Operation.init_table()
|
Operation.init_table()
|
||||||
Resource.init_table()
|
Resource.init_table()
|
||||||
|
User.init_table()
|
||||||
Community.init_table()
|
Community.init_table()
|
||||||
Role.init_table()
|
Role.init_table()
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class Base(declarative_base()):
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls: Generic[T], **kwargs) -> Generic[T]:
|
def create(cls: Generic[T], **kwargs) -> Generic[T]:
|
||||||
instance = cls(**kwargs)
|
instance = cls(**kwargs)
|
||||||
return instance.save(session)
|
return instance.save()
|
||||||
|
|
||||||
def save(self) -> Generic[T]:
|
def save(self) -> Generic[T]:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
|
|
|
@ -18,14 +18,11 @@ class Community(Base):
|
||||||
def init_table():
|
def init_table():
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
default = session.query(Community).filter(Community.slug == "default").first()
|
default = session.query(Community).filter(Community.slug == "default").first()
|
||||||
if default:
|
if not default:
|
||||||
Community.default_community = default
|
default = Community.create(
|
||||||
return
|
name = "default",
|
||||||
|
slug = "default",
|
||||||
default = Community.create(
|
createdBy = 0 #TODO: use default user
|
||||||
name = "default",
|
)
|
||||||
slug = "default",
|
|
||||||
createdBy = 0
|
|
||||||
)
|
|
||||||
|
|
||||||
Community.default_community = default
|
Community.default_community = default
|
||||||
|
|
17
orm/user.py
17
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 import Table, Column, Integer, String, ForeignKey, Boolean, DateTime, JSON as JSONType
|
||||||
from sqlalchemy.orm import relationship, selectinload
|
from sqlalchemy.orm import relationship, selectinload
|
||||||
|
|
||||||
from orm import RoleStorage
|
|
||||||
from orm.base import Base, local_session
|
from orm.base import Base, local_session
|
||||||
from orm.rbac import Role
|
from orm.rbac import Role, RoleStorage
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -62,6 +61,20 @@ class User(Base):
|
||||||
roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
|
roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
|
||||||
topics = relationship(lambda: Topic, secondary=UserTopics)
|
topics = relationship(lambda: Topic, secondary=UserTopics)
|
||||||
old_id: str = Column(String, nullable = True)
|
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):
|
async def get_permission(self):
|
||||||
scope = {}
|
scope = {}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user