From 94518adcc5674c9361afe4818b745275845a2a0f Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 19 Aug 2021 18:33:39 +0300 Subject: [PATCH] some fixes for models --- .gitignore | 6 +++--- auth/identity.py | 6 +++--- auth/oauth.py | 4 ++-- orm/like.py | 2 +- orm/notification.py | 6 +++--- orm/proposal.py | 4 ++-- orm/rating.py | 10 ---------- orm/rbac.py | 4 ++-- orm/shout.py | 6 ++---- orm/user.py | 21 +++++++++++++-------- 10 files changed, 31 insertions(+), 38 deletions(-) delete mode 100644 orm/rating.py diff --git a/.gitignore b/.gitignore index 76bfe9af..cf8d70c7 100644 --- a/.gitignore +++ b/.gitignore @@ -130,8 +130,8 @@ dmypy.json .idea temp.* -*.sqlite3 - discours.key discours.crt -Pipfile.lock \ No newline at end of file +Pipfile.lock + +migration/data \ No newline at end of file diff --git a/auth/identity.py b/auth/identity.py index 6c446a79..b1a79e88 100644 --- a/auth/identity.py +++ b/auth/identity.py @@ -25,12 +25,12 @@ class Identity: def identity_oauth(input) -> User: with local_session() as session: user = session.query(OrmUser).filter( - or_(OrmUser.oauth_id == input["oauth_id"], OrmUser.email == input["email"]) + or_(OrmUser.oauth == input["oauth"], OrmUser.email == input["email"]) ).first() if not user: user = OrmUser.create(**input) - if not user.oauth_id: - user.oauth_id = input["oauth_id"] + if not user.oauth: + user.oauth = input["oauth"] session.commit() user = User(**user.dict()) diff --git a/auth/oauth.py b/auth/oauth.py index 20df1303..ea70792b 100644 --- a/auth/oauth.py +++ b/auth/oauth.py @@ -57,9 +57,9 @@ async def oauth_authorize(request): token = await client.authorize_access_token(request) resp = await client.get('user', token=token) profile = resp.json() - oauth_id = profile["id"] + oauth = profile["id"] user_input = { - "oauth_id" : oauth_id, + "oauth" : oauth, "email" : profile["email"], "username" : profile["name"] } diff --git a/orm/like.py b/orm/like.py index 9036c4c8..9034ebe5 100644 --- a/orm/like.py +++ b/orm/like.py @@ -11,7 +11,7 @@ class Like(Base): author_id: str = Column(ForeignKey("user.id"), nullable=False, comment="Author") value: str = Column(String, nullable=False, comment="Value") - shout: str = Column(ForeignKey("shout.id"), nullable=True, comment="Liked shout") + shout: str = Column(String, ForeignKey("shout.slug"), nullable=True, comment="Liked shout slug") user: str = Column(ForeignKey("user.id"), nullable=True, comment="Liked user") # TODO: add resolvers, debug, etc. \ No newline at end of file diff --git a/orm/notification.py b/orm/notification.py index aa9cfab1..32652403 100644 --- a/orm/notification.py +++ b/orm/notification.py @@ -4,7 +4,7 @@ from orm.base import Base class Notification(Base): __tablename__ = 'notification' - kind: str = Column(String, primary_key = True) + kind: str = Column(String, unique = True, primary_key = True) template: str = Column(String, nullable = False) variables: JSONType = Column(JSONType, nullable = True) # [ , .. ] @@ -12,6 +12,6 @@ class UserNotification(Base): __tablename__ = 'user_notification' id: int = Column(Integer, primary_key = True) - user: int = Column(ForeignKey("user.id")) - kind: int = Column(ForeignKey("notification.kind"), nullable = False) + user_id: int = Column(Integer, ForeignKey("user.id")) + kind: str = Column(String, ForeignKey("notification.kind")) values: JSONType = Column(JSONType, nullable = True) # [ , .. ] \ No newline at end of file diff --git a/orm/proposal.py b/orm/proposal.py index ce405fb5..73232791 100644 --- a/orm/proposal.py +++ b/orm/proposal.py @@ -9,10 +9,10 @@ from orm.base import Base class Proposal(Base): __tablename__ = 'proposal' - author_id: str = Column(ForeignKey("user.id"), nullable=False, comment="Author") + author_id: int = Column(Integer, ForeignKey("user.id"), nullable=False, comment="Author") body: str = Column(String, nullable=False, comment="Body") createdAt: str = Column(datetime, nullable=False, comment="Created at") - shout: str = Column(ForeignKey("shout.id"), nullable=False, comment="Updated at") + shout: str = Column(String, ForeignKey("shout.slug"), nullable=False, comment="Updated at") range: str = Column(String, nullable=True, comment="Range in format :") # TODO: debug, logix \ No newline at end of file diff --git a/orm/rating.py b/orm/rating.py deleted file mode 100644 index 86b62031..00000000 --- a/orm/rating.py +++ /dev/null @@ -1,10 +0,0 @@ - -from sqlalchemy import Column, Integer, String, ForeignKey -from orm.base import Base - -class Rating(Base): - __tablename__ = 'rating' - - id: int = Column(Integer, primary_key = True) - createdBy: int = Column(ForeignKey("user.id"), primary_key = True) - value: int = Column(Integer, nullable=False) \ No newline at end of file diff --git a/orm/rbac.py b/orm/rbac.py index 5a19438d..ced53e0f 100644 --- a/orm/rbac.py +++ b/orm/rbac.py @@ -36,7 +36,7 @@ class Role(Base): name: str = Column(String, nullable=False, unique=True, comment="Role Name") org_id: int = Column(ForeignKey("organization.id", ondelete="CASCADE"), nullable=False, comment="Organization") - permissions = relationship("Permission") + permissions = relationship(lambda: Permission) class Operation(Base): __tablename__ = 'operation' @@ -71,7 +71,7 @@ class Permission(Base): role_id: int = Column(ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role") operation_id: int = Column(ForeignKey("operation.id", ondelete="CASCADE"), nullable=False, comment="Operation") - resource_id: int = Column(ForeignKey("operation.id", ondelete="CASCADE"), nullable=False, comment="Resource") + resource_id: int = Column(ForeignKey("resource.id", ondelete="CASCADE"), nullable=False, comment="Resource") if __name__ == '__main__': diff --git a/orm/shout.py b/orm/shout.py index 45459473..80e46be0 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -9,11 +9,9 @@ from orm.base import Base class Shout(Base): __tablename__ = 'shout' - id = None - slug: str = Column(String, primary_key=True) - org_id: str = Column(ForeignKey("organization.id"), nullable=False) - author_id: str = Column(ForeignKey("user.id"), nullable=False, comment="Author") + org_id: int = Column(Integer, ForeignKey("organization.id"), nullable=False, comment="Organization") + author_id: int = Column(Integer, ForeignKey("user.id"), nullable = False, comment="Author") body: str = Column(String, nullable=False, comment="Body") createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at") updatedAt: str = Column(DateTime, nullable=True, comment="Updated at") diff --git a/orm/user.py b/orm/user.py index a73da42b..552158ba 100644 --- a/orm/user.py +++ b/orm/user.py @@ -1,19 +1,24 @@ from typing import List -from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, DateTime +from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, DateTime, JSON as JSONType from sqlalchemy.orm import relationship from orm import Permission from orm.base import Base, local_session -from orm.rating import Rating from orm.notification import UserNotification +class UserRating(Base): + __tablename__ = 'user_rating' + + createdBy: int = Column(Integer, ForeignKey("user.id"), primary_key = True) + value: int = Column(Integer, nullable=False) + class UserRole(Base): __tablename__ = 'user_role' id: int = Column(Integer, primary_key = True) - user_id: int = Column(ForeignKey("user.id"), primary_key = True) - role: str = Column(ForeignKey("role.name"), primary_key = True) + user_id: int = Column(Integer, ForeignKey("user.id")) + role_id str = Column(String, ForeignKey("role.name")) class User(Base): __tablename__ = 'user' @@ -30,11 +35,11 @@ class User(Base): emailConfirmed: bool = Column(Boolean, default=False) createdAt: DateTime = Column(DateTime, nullable=False, comment="Created at") wasOnlineAt: DateTime = Column(DateTime, nullable=False, comment="Was online at") - links: JSON = Column(JSON, nullable=True, comment="Links") + links: JSONType = Column(JSONType, nullable=True, comment="Links") oauth: str = Column(String, nullable=True) - notifications = relationship("Notification", secondary=UserNotification.__table__) - ratings = relationship("Rating", secondary=Rating.__table__) - roles = relationship("Role", secondary=UserRole.__table__) + notifications = relationship(lambda: UserNotification) + ratings = relationship(lambda: UserRating) + roles = relationship(lambda: UserRole) @classmethod def get_permission(cls, user_id):