some fixes for models
This commit is contained in:
parent
0cb0b85bce
commit
94518adcc5
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -130,8 +130,8 @@ dmypy.json
|
|||
.idea
|
||||
temp.*
|
||||
|
||||
*.sqlite3
|
||||
|
||||
discours.key
|
||||
discours.crt
|
||||
Pipfile.lock
|
||||
|
||||
migration/data
|
|
@ -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())
|
||||
|
|
|
@ -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"]
|
||||
}
|
||||
|
|
|
@ -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.
|
|
@ -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) # [ <var1>, .. ]
|
||||
|
||||
|
@ -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) # [ <var1>, .. ]
|
|
@ -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 <start index>:<end>")
|
||||
|
||||
# TODO: debug, logix
|
|
@ -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)
|
|
@ -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__':
|
||||
|
|
|
@ -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")
|
||||
|
|
21
orm/user.py
21
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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user