diff --git a/orm/__init__.py b/orm/__init__.py index 2d0bbc53..29570ed6 100644 --- a/orm/__init__.py +++ b/orm/__init__.py @@ -1,9 +1,10 @@ -from orm.rbac import Community, Operation, Resource, Permission, Role +from orm.rbac import Operation, Resource, Permission, Role +from orm.community import Community from orm.user import User from orm.message import Message from orm.topic import Topic from orm.notification import Notification -from orm.shout import Shout +from orm.shout import Shout, ShoutAuthor from orm.base import Base, engine __all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"] diff --git a/orm/community.py b/orm/community.py index e95df8eb..a7efce3a 100644 --- a/orm/community.py +++ b/orm/community.py @@ -6,12 +6,10 @@ from orm.base import Base class Community(Base): __tablename__ = 'community' - # id is auto number + # 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='') - # org_id: str = Column(ForeignKey("organization.id"), nullable=True) + 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") - \ No newline at end of file diff --git a/orm/shout.py b/orm/shout.py index a0a9113f..a4304bb1 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -5,11 +5,12 @@ from sqlalchemy.orm import relationship from orm import Permission, User, Topic from orm.base import Base -ShoutAuthors = Table('shout_authors', - Base.metadata, - Column('shout', Integer, ForeignKey('shout.id')), - Column('user_id', Integer, ForeignKey('user.id')) -) +class ShoutAuthor(Base): + __tablename__ = "shout_author" + + id = None + shout = Column(ForeignKey('shout.id'), primary_key = True) + user = Column(ForeignKey('user.id'), primary_key = True) ShoutTopics = Table('shout_topics', Base.metadata, @@ -45,7 +46,7 @@ class Shout(Base): title: str = Column(String, nullable = True) subtitle: str = Column(String, nullable = True) layout: str = Column(String, nullable = True) - authors = relationship(lambda: User, secondary=ShoutAuthors) # NOTE: multiple authors + authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors topics = relationship(lambda: Topic, secondary=ShoutTopics) rating: int = Column(Integer, nullable=True, comment="Rating") ratings = relationship(ShoutRatings, foreign_keys=ShoutRatings.shout_id) diff --git a/orm/user.py b/orm/user.py index 48b31f74..831ee84c 100644 --- a/orm/user.py +++ b/orm/user.py @@ -7,6 +7,7 @@ from sqlalchemy.orm import relationship from orm import Permission from orm.base import Base, local_session from orm.rbac import Role +from orm.topic import Topic class UserNotifications(Base): __tablename__ = 'user_notifications' @@ -26,14 +27,14 @@ class UserRatings(Base): UserRoles = Table("user_roles", Base.metadata, - Column('user_id', Integer, ForeignKey('user.id')), - Column('role_id', Integer, ForeignKey('role.id')) + Column('user_id', Integer, ForeignKey('user.id'), primary_key = True), + Column('role_id', Integer, ForeignKey('role.id'), primary_key = True) ) UserTopics = Table("user_topics", Base.metadata, - Column('user_id', Integer, ForeignKey('user.id')), - Column('topic_id', Integer, ForeignKey('topic.id')) + Column('user_id', Integer, ForeignKey('user.id'), primary_key = True), + Column('topic_id', Integer, ForeignKey('topic.id'), primary_key = True) ) class User(Base): diff --git a/resolvers/zine.py b/resolvers/zine.py index f8f268d1..25016c70 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -1,4 +1,4 @@ -from orm import Shout, User, Community, Resource +from orm import Shout, ShoutAuthor, User, Community, Resource from orm.base import local_session from resolvers.base import mutation, query @@ -15,10 +15,9 @@ class GitTask: queue = asyncio.Queue() - def __init__(self, input, org, username, user_email, comment): + def __init__(self, input, username, user_email, comment): self.slug = input["slug"]; self.shout_body = input["body"]; - self.org = org; #FIXME self.username = username; self.user_email = user_email; self.comment = comment; @@ -26,7 +25,7 @@ class GitTask: GitTask.queue.put_nowait(self) def init_repo(self): - repo_path = "%s/%s" % (SHOUTS_REPO, self.org) + repo_path = "%s" % (SHOUTS_REPO) Path(repo_path).mkdir() @@ -35,7 +34,7 @@ class GitTask: print(output) def execute(self): - repo_path = "%s/%s" % (SHOUTS_REPO, self.org) + repo_path = "%s" % (SHOUTS_REPO) if not Path(repo_path).exists(): self.init_repo() @@ -84,25 +83,16 @@ async def create_shout(_, info, input): auth = info.context["request"].auth user_id = auth.user_id - # org_id = org = input["org_id"] with local_session() as session: user = session.query(User).filter(User.id == user_id).first() - # org = session.query(Organization).filter(Organization.id == org_id).first() - - new_shout = Shout.create( - slug = input["slug"], - # org_id = org_id, - authors = [user_id, ], - body = input["body"], - replyTo = input.get("replyTo"), - versionOf = input.get("versionOf"), - tags = input.get("tags"), - topics = input.get("topics") - ) + + new_shout = Shout.create(**input) + ShoutAuthor.create( + shout = new_shout.id, + user = user_id) task = GitTask( input, - org.name, user.username, user.email, "new shout %s" % (new_shout.slug) @@ -114,16 +104,13 @@ async def create_shout(_, info, input): @mutation.field("updateShout") @login_required -async def update_shout(_, info, input): +async def update_shout(_, info, id, input): auth = info.context["request"].auth user_id = auth.user_id - slug = input["slug"] - # org_id = org = input["org_id"] with local_session() as session: user = session.query(User).filter(User.id == user_id).first() - shout = session.query(Shout).filter(Shout.slug == slug).first() - # org = session.query(Organization).filter(Organization.id == org_id).first() + shout = session.query(Shout).filter(Shout.id == id).first() if not shout: return { @@ -149,7 +136,6 @@ async def update_shout(_, info, input): task = GitTask( input, - org.name, user.username, user.email, "update shout %s" % (shout.slug) diff --git a/schema.graphql b/schema.graphql index 95f68453..3a0ed1f7 100644 --- a/schema.graphql +++ b/schema.graphql @@ -23,13 +23,13 @@ type MessageResult { } input ShoutInput { - org_id: Int! slug: String! body: String! replyTo: String # another shout tags: [String] # actual values topics: [String] # topic-slugs title: String + subtitle: String versionOf: String visibleForRoles: [String] # role ids are strings visibleForUsers: [Int] @@ -66,9 +66,9 @@ type Mutation { # shout createShout(input: ShoutInput!): ShoutResult! - updateShout(input: ShoutInput!): ShoutResult! - deleteShout(slug: String!): Result! - rateShout(slug: String!, value: Int!): Result! + updateShout(id: Int!, input: ShoutInput!): ShoutResult! + deleteShout(id: Int!): Result! + rateShout(id: Int!, value: Int!): Result! # user profile # rateUser(value: Int!): Result! @@ -179,6 +179,7 @@ type Message { # is publication type Shout { + id: Int! authors: [Int!]! slug: String! body: String! @@ -239,4 +240,4 @@ type Token { ownerId: Int! usedAt: DateTime value: String! -} \ No newline at end of file +}