default user role; updateProfile method
This commit is contained in:
@@ -14,6 +14,8 @@ __all__ = ["User", "Role", "Community", "Operation", "Permission", "Message", "S
|
||||
Base.metadata.create_all(engine)
|
||||
Operation.init_table()
|
||||
Resource.init_table()
|
||||
Community.init_table()
|
||||
Role.init_table()
|
||||
|
||||
with local_session() as session:
|
||||
ShoutRatingStorage.init(session)
|
||||
|
@@ -33,7 +33,7 @@ class Base(declarative_base()):
|
||||
@classmethod
|
||||
def create(cls: Generic[T], **kwargs) -> Generic[T]:
|
||||
instance = cls(**kwargs)
|
||||
return instance.save()
|
||||
return instance.save(session)
|
||||
|
||||
def save(self) -> Generic[T]:
|
||||
with local_session() as session:
|
||||
|
@@ -1,15 +1,31 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
|
||||
from sqlalchemy.orm import relationship, backref
|
||||
from orm.base import Base
|
||||
from orm.base import Base, local_session
|
||||
|
||||
|
||||
class Community(Base):
|
||||
__tablename__ = 'community'
|
||||
# id is auto number
|
||||
|
||||
name: str = Column(String, nullable=False, comment="Name")
|
||||
slug: str = Column(String, unique = True, nullable = False)
|
||||
desc: str = Column(String, nullable=False, default='')
|
||||
pic: str = Column(String, nullable=False, default='')
|
||||
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||
createdBy: str = Column(ForeignKey("user.id"), nullable=False, comment="Creator")
|
||||
|
||||
@staticmethod
|
||||
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
|
||||
)
|
||||
|
||||
Community.default_community = default
|
||||
|
19
orm/rbac.py
19
orm/rbac.py
@@ -7,6 +7,7 @@ from sqlalchemy import String, Integer, Column, ForeignKey, UniqueConstraint, Ty
|
||||
from sqlalchemy.orm import relationship, selectinload
|
||||
|
||||
from orm.base import Base, REGISTRY, engine, local_session
|
||||
from orm.community import Community
|
||||
|
||||
|
||||
class ClassType(TypeDecorator):
|
||||
@@ -31,13 +32,27 @@ class ClassType(TypeDecorator):
|
||||
class Role(Base):
|
||||
__tablename__ = 'role'
|
||||
|
||||
# id is auto field
|
||||
|
||||
name: str = Column(String, nullable=False, comment="Role Name")
|
||||
desc: str = Column(String, nullable=True, comment="Role Description")
|
||||
community: int = Column(ForeignKey("community.id", ondelete="CASCADE"), nullable=False, comment="Community")
|
||||
permissions = relationship(lambda: Permission)
|
||||
|
||||
@staticmethod
|
||||
def init_table():
|
||||
with local_session() as session:
|
||||
default = session.query(Role).filter(Role.name == "author").first()
|
||||
if default:
|
||||
Role.default_role = default
|
||||
return
|
||||
|
||||
default = Role.create(
|
||||
name = "author",
|
||||
desc = "Role for author",
|
||||
community = Community.default_community.id
|
||||
)
|
||||
|
||||
Role.default_role = default
|
||||
|
||||
class Operation(Base):
|
||||
__tablename__ = 'operation'
|
||||
name: str = Column(String, nullable=False, unique=True, comment="Operation Name")
|
||||
|
@@ -94,7 +94,7 @@ class UserStorage:
|
||||
async def add_user(user):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
self.users[id] = user
|
||||
self.users[user.id] = user
|
||||
|
||||
@staticmethod
|
||||
async def del_user(id):
|
||||
|
Reference in New Issue
Block a user