some fixes for models

This commit is contained in:
Untone 2021-08-19 18:33:39 +03:00
parent 0cb0b85bce
commit 94518adcc5
10 changed files with 31 additions and 38 deletions

6
.gitignore vendored
View File

@ -130,8 +130,8 @@ dmypy.json
.idea .idea
temp.* temp.*
*.sqlite3
discours.key discours.key
discours.crt discours.crt
Pipfile.lock Pipfile.lock
migration/data

View File

@ -25,12 +25,12 @@ class Identity:
def identity_oauth(input) -> User: def identity_oauth(input) -> User:
with local_session() as session: with local_session() as session:
user = session.query(OrmUser).filter( 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() ).first()
if not user: if not user:
user = OrmUser.create(**input) user = OrmUser.create(**input)
if not user.oauth_id: if not user.oauth:
user.oauth_id = input["oauth_id"] user.oauth = input["oauth"]
session.commit() session.commit()
user = User(**user.dict()) user = User(**user.dict())

View File

@ -57,9 +57,9 @@ async def oauth_authorize(request):
token = await client.authorize_access_token(request) token = await client.authorize_access_token(request)
resp = await client.get('user', token=token) resp = await client.get('user', token=token)
profile = resp.json() profile = resp.json()
oauth_id = profile["id"] oauth = profile["id"]
user_input = { user_input = {
"oauth_id" : oauth_id, "oauth" : oauth,
"email" : profile["email"], "email" : profile["email"],
"username" : profile["name"] "username" : profile["name"]
} }

View File

@ -11,7 +11,7 @@ class Like(Base):
author_id: str = Column(ForeignKey("user.id"), nullable=False, comment="Author") author_id: str = Column(ForeignKey("user.id"), nullable=False, comment="Author")
value: str = Column(String, nullable=False, comment="Value") 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") user: str = Column(ForeignKey("user.id"), nullable=True, comment="Liked user")
# TODO: add resolvers, debug, etc. # TODO: add resolvers, debug, etc.

View File

@ -4,7 +4,7 @@ from orm.base import Base
class Notification(Base): class Notification(Base):
__tablename__ = 'notification' __tablename__ = 'notification'
kind: str = Column(String, primary_key = True) kind: str = Column(String, unique = True, primary_key = True)
template: str = Column(String, nullable = False) template: str = Column(String, nullable = False)
variables: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ] variables: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ]
@ -12,6 +12,6 @@ class UserNotification(Base):
__tablename__ = 'user_notification' __tablename__ = 'user_notification'
id: int = Column(Integer, primary_key = True) id: int = Column(Integer, primary_key = True)
user: int = Column(ForeignKey("user.id")) user_id: int = Column(Integer, ForeignKey("user.id"))
kind: int = Column(ForeignKey("notification.kind"), nullable = False) kind: str = Column(String, ForeignKey("notification.kind"))
values: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ] values: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ]

View File

@ -9,10 +9,10 @@ from orm.base import Base
class Proposal(Base): class Proposal(Base):
__tablename__ = 'proposal' __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") body: str = Column(String, nullable=False, comment="Body")
createdAt: str = Column(datetime, nullable=False, comment="Created at") 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 <start index>:<end>") range: str = Column(String, nullable=True, comment="Range in format <start index>:<end>")
# TODO: debug, logix # TODO: debug, logix

View File

@ -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)

View File

@ -36,7 +36,7 @@ class Role(Base):
name: str = Column(String, nullable=False, unique=True, comment="Role Name") name: str = Column(String, nullable=False, unique=True, comment="Role Name")
org_id: int = Column(ForeignKey("organization.id", ondelete="CASCADE"), nullable=False, comment="Organization") org_id: int = Column(ForeignKey("organization.id", ondelete="CASCADE"), nullable=False, comment="Organization")
permissions = relationship("Permission") permissions = relationship(lambda: Permission)
class Operation(Base): class Operation(Base):
__tablename__ = 'operation' __tablename__ = 'operation'
@ -71,7 +71,7 @@ class Permission(Base):
role_id: int = Column(ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role") 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") 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__': if __name__ == '__main__':

View File

@ -9,11 +9,9 @@ from orm.base import Base
class Shout(Base): class Shout(Base):
__tablename__ = 'shout' __tablename__ = 'shout'
id = None
slug: str = Column(String, primary_key=True) slug: str = Column(String, primary_key=True)
org_id: str = Column(ForeignKey("organization.id"), nullable=False) org_id: int = Column(Integer, ForeignKey("organization.id"), nullable=False, comment="Organization")
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") body: str = Column(String, nullable=False, comment="Body")
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at") createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
updatedAt: str = Column(DateTime, nullable=True, comment="Updated at") updatedAt: str = Column(DateTime, nullable=True, comment="Updated at")

View File

@ -1,19 +1,24 @@
from typing import List 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 sqlalchemy.orm import relationship
from orm import Permission from orm import Permission
from orm.base import Base, local_session from orm.base import Base, local_session
from orm.rating import Rating
from orm.notification import UserNotification 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): class UserRole(Base):
__tablename__ = 'user_role' __tablename__ = 'user_role'
id: int = Column(Integer, primary_key = True) id: int = Column(Integer, primary_key = True)
user_id: int = Column(ForeignKey("user.id"), primary_key = True) user_id: int = Column(Integer, ForeignKey("user.id"))
role: str = Column(ForeignKey("role.name"), primary_key = True) role_id str = Column(String, ForeignKey("role.name"))
class User(Base): class User(Base):
__tablename__ = 'user' __tablename__ = 'user'
@ -30,11 +35,11 @@ class User(Base):
emailConfirmed: bool = Column(Boolean, default=False) emailConfirmed: bool = Column(Boolean, default=False)
createdAt: DateTime = Column(DateTime, nullable=False, comment="Created at") createdAt: DateTime = Column(DateTime, nullable=False, comment="Created at")
wasOnlineAt: DateTime = Column(DateTime, nullable=False, comment="Was online 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) oauth: str = Column(String, nullable=True)
notifications = relationship("Notification", secondary=UserNotification.__table__) notifications = relationship(lambda: UserNotification)
ratings = relationship("Rating", secondary=Rating.__table__) ratings = relationship(lambda: UserRating)
roles = relationship("Role", secondary=UserRole.__table__) roles = relationship(lambda: UserRole)
@classmethod @classmethod
def get_permission(cls, user_id): def get_permission(cls, user_id):