model upgrade
This commit is contained in:
parent
94518adcc5
commit
ee3b186ba1
|
@ -1,10 +1,13 @@
|
||||||
from orm.rbac import Organization, Operation, Resource, Permission, Role
|
from orm.rbac import Organization, Operation, Resource, Permission, Role
|
||||||
from orm.user import User
|
from orm.user import User
|
||||||
from orm.message import Message
|
from orm.message import Message
|
||||||
|
from orm.topic import Topic
|
||||||
|
from orm.rating import Rating
|
||||||
|
from orm.notification import Notification
|
||||||
from orm.shout import Shout
|
from orm.shout import Shout
|
||||||
from orm.base import Base, engine
|
from orm.base import Base, engine
|
||||||
|
|
||||||
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout"]
|
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Rating", "Notification"]
|
||||||
|
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
Operation.init_table()
|
Operation.init_table()
|
||||||
|
|
|
@ -7,11 +7,3 @@ class Notification(Base):
|
||||||
kind: str = Column(String, unique = True, 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>, .. ]
|
||||||
|
|
||||||
class UserNotification(Base):
|
|
||||||
__tablename__ = 'user_notification'
|
|
||||||
|
|
||||||
id: int = Column(Integer, primary_key = True)
|
|
||||||
user_id: int = Column(Integer, ForeignKey("user.id"))
|
|
||||||
kind: str = Column(String, ForeignKey("notification.kind"))
|
|
||||||
values: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ]
|
|
9
orm/rating.py
Normal file
9
orm/rating.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
from sqlalchemy import Column, Integer, String, ForeignKey
|
||||||
|
# from orm import Permission
|
||||||
|
from orm.base import Base
|
||||||
|
|
||||||
|
class Rating(Base):
|
||||||
|
__tablename__ = "rating"
|
||||||
|
|
||||||
|
createdBy: int = Column(Integer, ForeignKey("user.id"), primary_key = True)
|
||||||
|
value: int = Column(Integer, nullable=False)
|
27
orm/shout.py
27
orm/shout.py
|
@ -1,24 +1,37 @@
|
||||||
from typing import List
|
from typing import List
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
|
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
from orm import Permission
|
from orm import Permission, User, Topic
|
||||||
from orm.base import Base
|
from orm.base import Base
|
||||||
|
|
||||||
|
ShoutAuthors = Table('shout_authors',
|
||||||
|
Base.metadata,
|
||||||
|
Column('shout', String, ForeignKey('shout.slug')),
|
||||||
|
Column('user_id', Integer, ForeignKey('user.id'))
|
||||||
|
)
|
||||||
|
|
||||||
|
ShoutTopics = Table('shout_topics',
|
||||||
|
Base.metadata,
|
||||||
|
Column('shout', String, ForeignKey('shout.slug')),
|
||||||
|
Column('topic', String, ForeignKey('topic.slug'))
|
||||||
|
)
|
||||||
|
|
||||||
class Shout(Base):
|
class Shout(Base):
|
||||||
__tablename__ = 'shout'
|
__tablename__ = 'shout'
|
||||||
|
|
||||||
slug: str = Column(String, primary_key=True)
|
slug: str = Column(String, primary_key=True)
|
||||||
org_id: int = Column(Integer, ForeignKey("organization.id"), nullable=False, comment="Organization")
|
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")
|
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")
|
||||||
replyTo: str = Column(ForeignKey("shout.slug"), nullable=True)
|
replyTo: str = Column(ForeignKey("shout.slug"), nullable=True)
|
||||||
versionOf: str = Column(ForeignKey("shout.slug"), nullable=True)
|
versionOf: str = Column(ForeignKey("shout.slug"), nullable=True)
|
||||||
tags: str = Column(String, nullable=True)
|
tags: str = Column(String, nullable=True)
|
||||||
topics: str = Column(String, nullable=True)
|
|
||||||
views: int = Column(Integer, default=0)
|
views: int = Column(Integer, default=0)
|
||||||
|
published: bool = Column(Boolean, default=False)
|
||||||
# TODO: add all the fields
|
publishedAt: str = Column(DateTime, nullable=True)
|
||||||
|
cover: str = Column(String, nullable = True)
|
||||||
|
layout: str = Column(String, nullable = True)
|
||||||
|
authors = relationship(lambda: User, secondary=ShoutAuthors) # NOTE: multiple authors
|
||||||
|
topics = relationship(lambda: Topic, secondary=ShoutTopics)
|
||||||
|
|
18
orm/topic.py
Normal file
18
orm/topic.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from typing import List
|
||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
|
||||||
|
from sqlalchemy.orm import relationship, backref
|
||||||
|
from orm import Permission
|
||||||
|
from orm.base import Base
|
||||||
|
|
||||||
|
class Topic(Base):
|
||||||
|
__tablename__ = 'topic'
|
||||||
|
|
||||||
|
slug: str = Column(String, unique = True, nullable = False, primary_key=True)
|
||||||
|
org_id: str = Column(ForeignKey("organization.id"), nullable=False)
|
||||||
|
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||||
|
createdBy: str = Column(ForeignKey("user.id"), nullable=False, comment="Author")
|
||||||
|
value: str = Column(String, nullable=False, comment="Value")
|
||||||
|
alters = relationship(lambda: Topic, backref=backref("topic", remote_side=[slug]))
|
||||||
|
alter_id: str = Column(ForeignKey("topic.slug"))
|
||||||
|
# TODO: add all the fields
|
42
orm/user.py
42
orm/user.py
|
@ -1,27 +1,35 @@
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from sqlalchemy import 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
|
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.notification import UserNotification
|
from orm.rating import Rating
|
||||||
|
from orm.rbac import Role
|
||||||
|
|
||||||
class UserRating(Base):
|
class UserNotifications(Base):
|
||||||
__tablename__ = 'user_rating'
|
__tablename__ = 'user_notifications'
|
||||||
|
|
||||||
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)
|
id: int = Column(Integer, primary_key = True)
|
||||||
user_id: int = Column(Integer, ForeignKey("user.id"))
|
user_id: int = Column(Integer, ForeignKey("user.id"))
|
||||||
role_id str = Column(String, ForeignKey("role.name"))
|
kind: str = Column(String, ForeignKey("notification.kind"))
|
||||||
|
values: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ]
|
||||||
|
|
||||||
|
UserRatings = Table("user_ratings",
|
||||||
|
Base.metadata,
|
||||||
|
Column('user_id', Integer, ForeignKey('user.id')),
|
||||||
|
Column('rater_id', Integer, ForeignKey('rating.createdBy'))
|
||||||
|
)
|
||||||
|
|
||||||
|
UserRoles = Table("user_roles",
|
||||||
|
Base.metadata,
|
||||||
|
Column('user_id', Integer, ForeignKey('user.id')),
|
||||||
|
Column('role', String, ForeignKey('role.name'))
|
||||||
|
)
|
||||||
|
|
||||||
class User(Base):
|
class User(Base):
|
||||||
__tablename__ = 'user'
|
__tablename__ = "user"
|
||||||
|
|
||||||
email: str = Column(String, unique=True, nullable=False, comment="Email")
|
email: str = Column(String, unique=True, nullable=False, comment="Email")
|
||||||
username: str = Column(String, nullable=False, comment="Login")
|
username: str = Column(String, nullable=False, comment="Login")
|
||||||
|
@ -30,16 +38,16 @@ class User(Base):
|
||||||
userpic: str = Column(String, nullable=True, comment="Userpic")
|
userpic: str = Column(String, nullable=True, comment="Userpic")
|
||||||
viewname: str = Column(String, nullable=True, comment="Display name")
|
viewname: str = Column(String, nullable=True, comment="Display name")
|
||||||
rating: int = Column(Integer, nullable=True, comment="Rating")
|
rating: int = Column(Integer, nullable=True, comment="Rating")
|
||||||
slug: str = Column(String, unique=True, nullable=True, comment="Slug")
|
slug: str = Column(String, unique=True, comment="Author's slug")
|
||||||
muted: bool = Column(Boolean, default=False)
|
muted: bool = Column(Boolean, default=False)
|
||||||
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: JSONType = Column(JSONType, 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(lambda: UserNotification)
|
notifications = relationship(lambda: UserNotifications)
|
||||||
ratings = relationship(lambda: UserRating)
|
ratings = relationship(lambda: Rating, secondary=UserRatings)
|
||||||
roles = relationship(lambda: UserRole)
|
roles = relationship(lambda: Role, secondary=UserRoles)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_permission(cls, user_id):
|
def get_permission(cls, user_id):
|
||||||
|
@ -54,5 +62,5 @@ class User(Base):
|
||||||
return scope
|
return scope
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
print(User.get_permission(user_id=1))
|
print(User.get_permission(user_id=1))
|
||||||
|
|
|
@ -97,7 +97,7 @@ async def create_shout(_, info, input):
|
||||||
new_shout = Shout.create(
|
new_shout = Shout.create(
|
||||||
slug = input["slug"],
|
slug = input["slug"],
|
||||||
org_id = org_id,
|
org_id = org_id,
|
||||||
author_id = user_id,
|
authors = [user_id, ],
|
||||||
body = input["body"],
|
body = input["body"],
|
||||||
replyTo = input.get("replyTo"),
|
replyTo = input.get("replyTo"),
|
||||||
versionOf = input.get("versionOf"),
|
versionOf = input.get("versionOf"),
|
||||||
|
@ -135,7 +135,7 @@ async def update_shout(_, info, input):
|
||||||
"error" : "shout not found"
|
"error" : "shout not found"
|
||||||
}
|
}
|
||||||
|
|
||||||
if shout.author_id != user_id:
|
if shout.authors[0] != user_id:
|
||||||
scopes = auth.scopes
|
scopes = auth.scopes
|
||||||
print(scopes)
|
print(scopes)
|
||||||
if not Resource.shout_id in scopes:
|
if not Resource.shout_id in scopes:
|
||||||
|
|
|
@ -178,7 +178,9 @@ type Message {
|
||||||
type Shout {
|
type Shout {
|
||||||
org_id: Int!
|
org_id: Int!
|
||||||
slug: String!
|
slug: String!
|
||||||
author: Int!
|
authors: [Int!]!
|
||||||
|
cover: String
|
||||||
|
layout: String
|
||||||
body: String!
|
body: String!
|
||||||
createdAt: DateTime!
|
createdAt: DateTime!
|
||||||
updatedAt: DateTime!
|
updatedAt: DateTime!
|
||||||
|
@ -186,7 +188,8 @@ type Shout {
|
||||||
deletedBy: Int
|
deletedBy: Int
|
||||||
rating: Int
|
rating: Int
|
||||||
ratigns: [Rating]
|
ratigns: [Rating]
|
||||||
published: DateTime # if there is no published field - it is not published
|
published: Boolean!
|
||||||
|
publishedAt: DateTime # if there is no published field - it is not published
|
||||||
replyTo: String # another shout
|
replyTo: String # another shout
|
||||||
tags: [String] # actual values
|
tags: [String] # actual values
|
||||||
topics: [String] # topic-slugs, order has matter
|
topics: [String] # topic-slugs, order has matter
|
||||||
|
@ -202,6 +205,7 @@ type Topic {
|
||||||
createdBy: Int! # User
|
createdBy: Int! # User
|
||||||
createdAt: DateTime!
|
createdAt: DateTime!
|
||||||
value: String
|
value: String
|
||||||
|
desc: String
|
||||||
parents: [String] # NOTE: topic can have parent topics
|
parents: [String] # NOTE: topic can have parent topics
|
||||||
children: [String] # and children
|
children: [String] # and children
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user