fix shout

This commit is contained in:
knst-kotov 2021-08-28 13:13:50 +03:00
parent 9721bd41d5
commit a13b979850
6 changed files with 35 additions and 47 deletions

View File

@ -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.user import User
from orm.message import Message from orm.message import Message
from orm.topic import Topic from orm.topic import Topic
from orm.notification import Notification from orm.notification import Notification
from orm.shout import Shout from orm.shout import Shout, ShoutAuthor
from orm.base import Base, engine from orm.base import Base, engine
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"] __all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"]

View File

@ -6,12 +6,10 @@ from orm.base import Base
class Community(Base): class Community(Base):
__tablename__ = 'community' __tablename__ = 'community'
# id is auto number # id is auto number
name: str = Column(String, nullable=False, comment="Name") name: str = Column(String, nullable=False, comment="Name")
slug: str = Column(String, unique = True, nullable = False) slug: str = Column(String, unique = True, nullable = False)
desc: str = Column(String, nullable=False, default='') desc: str = Column(String, nullable=False, default='')
pic: str = Column(String, nullable=False, default='') pic: str = Column(String, nullable=False, default='')
# org_id: str = Column(ForeignKey("organization.id"), nullable=True)
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at") createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
createdBy: str = Column(ForeignKey("user.id"), nullable=False, comment="Creator") createdBy: str = Column(ForeignKey("user.id"), nullable=False, comment="Creator")

View File

@ -5,11 +5,12 @@ from sqlalchemy.orm import relationship
from orm import Permission, User, Topic from orm import Permission, User, Topic
from orm.base import Base from orm.base import Base
ShoutAuthors = Table('shout_authors', class ShoutAuthor(Base):
Base.metadata, __tablename__ = "shout_author"
Column('shout', Integer, ForeignKey('shout.id')),
Column('user_id', Integer, ForeignKey('user.id')) id = None
) shout = Column(ForeignKey('shout.id'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True)
ShoutTopics = Table('shout_topics', ShoutTopics = Table('shout_topics',
Base.metadata, Base.metadata,
@ -45,7 +46,7 @@ class Shout(Base):
title: str = Column(String, nullable = True) title: str = Column(String, nullable = True)
subtitle: str = Column(String, nullable = True) subtitle: str = Column(String, nullable = True)
layout: 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) topics = relationship(lambda: Topic, secondary=ShoutTopics)
rating: int = Column(Integer, nullable=True, comment="Rating") rating: int = Column(Integer, nullable=True, comment="Rating")
ratings = relationship(ShoutRatings, foreign_keys=ShoutRatings.shout_id) ratings = relationship(ShoutRatings, foreign_keys=ShoutRatings.shout_id)

View File

@ -7,6 +7,7 @@ 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.rbac import Role from orm.rbac import Role
from orm.topic import Topic
class UserNotifications(Base): class UserNotifications(Base):
__tablename__ = 'user_notifications' __tablename__ = 'user_notifications'
@ -26,14 +27,14 @@ class UserRatings(Base):
UserRoles = Table("user_roles", UserRoles = Table("user_roles",
Base.metadata, Base.metadata,
Column('user_id', Integer, ForeignKey('user.id')), Column('user_id', Integer, ForeignKey('user.id'), primary_key = True),
Column('role_id', Integer, ForeignKey('role.id')) Column('role_id', Integer, ForeignKey('role.id'), primary_key = True)
) )
UserTopics = Table("user_topics", UserTopics = Table("user_topics",
Base.metadata, Base.metadata,
Column('user_id', Integer, ForeignKey('user.id')), Column('user_id', Integer, ForeignKey('user.id'), primary_key = True),
Column('topic_id', Integer, ForeignKey('topic.id')) Column('topic_id', Integer, ForeignKey('topic.id'), primary_key = True)
) )
class User(Base): class User(Base):

View File

@ -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 orm.base import local_session
from resolvers.base import mutation, query from resolvers.base import mutation, query
@ -15,10 +15,9 @@ class GitTask:
queue = asyncio.Queue() 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.slug = input["slug"];
self.shout_body = input["body"]; self.shout_body = input["body"];
self.org = org; #FIXME
self.username = username; self.username = username;
self.user_email = user_email; self.user_email = user_email;
self.comment = comment; self.comment = comment;
@ -26,7 +25,7 @@ class GitTask:
GitTask.queue.put_nowait(self) GitTask.queue.put_nowait(self)
def init_repo(self): def init_repo(self):
repo_path = "%s/%s" % (SHOUTS_REPO, self.org) repo_path = "%s" % (SHOUTS_REPO)
Path(repo_path).mkdir() Path(repo_path).mkdir()
@ -35,7 +34,7 @@ class GitTask:
print(output) print(output)
def execute(self): def execute(self):
repo_path = "%s/%s" % (SHOUTS_REPO, self.org) repo_path = "%s" % (SHOUTS_REPO)
if not Path(repo_path).exists(): if not Path(repo_path).exists():
self.init_repo() self.init_repo()
@ -84,25 +83,16 @@ async def create_shout(_, info, input):
auth = info.context["request"].auth auth = info.context["request"].auth
user_id = auth.user_id user_id = auth.user_id
# org_id = org = input["org_id"]
with local_session() as session: with local_session() as session:
user = session.query(User).filter(User.id == user_id).first() user = session.query(User).filter(User.id == user_id).first()
# org = session.query(Organization).filter(Organization.id == org_id).first()
new_shout = Shout.create( new_shout = Shout.create(**input)
slug = input["slug"], ShoutAuthor.create(
# org_id = org_id, shout = new_shout.id,
authors = [user_id, ], user = user_id)
body = input["body"],
replyTo = input.get("replyTo"),
versionOf = input.get("versionOf"),
tags = input.get("tags"),
topics = input.get("topics")
)
task = GitTask( task = GitTask(
input, input,
org.name,
user.username, user.username,
user.email, user.email,
"new shout %s" % (new_shout.slug) "new shout %s" % (new_shout.slug)
@ -114,16 +104,13 @@ async def create_shout(_, info, input):
@mutation.field("updateShout") @mutation.field("updateShout")
@login_required @login_required
async def update_shout(_, info, input): async def update_shout(_, info, id, input):
auth = info.context["request"].auth auth = info.context["request"].auth
user_id = auth.user_id user_id = auth.user_id
slug = input["slug"]
# org_id = org = input["org_id"]
with local_session() as session: with local_session() as session:
user = session.query(User).filter(User.id == user_id).first() user = session.query(User).filter(User.id == user_id).first()
shout = session.query(Shout).filter(Shout.slug == slug).first() shout = session.query(Shout).filter(Shout.id == id).first()
# org = session.query(Organization).filter(Organization.id == org_id).first()
if not shout: if not shout:
return { return {
@ -149,7 +136,6 @@ async def update_shout(_, info, input):
task = GitTask( task = GitTask(
input, input,
org.name,
user.username, user.username,
user.email, user.email,
"update shout %s" % (shout.slug) "update shout %s" % (shout.slug)

View File

@ -23,13 +23,13 @@ type MessageResult {
} }
input ShoutInput { input ShoutInput {
org_id: Int!
slug: String! slug: String!
body: String! body: String!
replyTo: String # another shout replyTo: String # another shout
tags: [String] # actual values tags: [String] # actual values
topics: [String] # topic-slugs topics: [String] # topic-slugs
title: String title: String
subtitle: String
versionOf: String versionOf: String
visibleForRoles: [String] # role ids are strings visibleForRoles: [String] # role ids are strings
visibleForUsers: [Int] visibleForUsers: [Int]
@ -66,9 +66,9 @@ type Mutation {
# shout # shout
createShout(input: ShoutInput!): ShoutResult! createShout(input: ShoutInput!): ShoutResult!
updateShout(input: ShoutInput!): ShoutResult! updateShout(id: Int!, input: ShoutInput!): ShoutResult!
deleteShout(slug: String!): Result! deleteShout(id: Int!): Result!
rateShout(slug: String!, value: Int!): Result! rateShout(id: Int!, value: Int!): Result!
# user profile # user profile
# rateUser(value: Int!): Result! # rateUser(value: Int!): Result!
@ -179,6 +179,7 @@ type Message {
# is publication # is publication
type Shout { type Shout {
id: Int!
authors: [Int!]! authors: [Int!]!
slug: String! slug: String!
body: String! body: String!