fix updateShout

This commit is contained in:
knst-kotov 2021-08-28 18:12:13 +03:00
parent a13b979850
commit 862c19ed15
5 changed files with 30 additions and 20 deletions

View File

@ -4,7 +4,7 @@ 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, ShoutAuthor from orm.shout import Shout, ShoutAuthor, ShoutTopic
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

@ -51,6 +51,12 @@ class Base(declarative_base()):
session.commit() session.commit()
return self return self
def update(self, input):
column_names = self.__table__.columns.keys()
for (name, value) in input.items():
if name in column_names:
setattr(self, name, value)
def dict(self) -> Dict[str, Any]: def dict(self) -> Dict[str, Any]:
column_names = self.__table__.columns.keys() column_names = self.__table__.columns.keys()
return {c: getattr(self, c) for c in column_names} return {c: getattr(self, c) for c in column_names}

View File

@ -12,11 +12,12 @@ class ShoutAuthor(Base):
shout = Column(ForeignKey('shout.id'), primary_key = True) shout = Column(ForeignKey('shout.id'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True) user = Column(ForeignKey('user.id'), primary_key = True)
ShoutTopics = Table('shout_topics', class ShoutTopic(Base):
Base.metadata, __tablename__ = 'shout_topic'
Column('shout', Integer, ForeignKey('shout.id')),
Column('topic', Integer, ForeignKey('topic.id')) id = None
) shout = Column(ForeignKey('shout.id'), primary_key = True)
topic = Column(ForeignKey('topic.id'), primary_key = True)
class ShoutRatings(Base): class ShoutRatings(Base):
__tablename__ = "user_ratings" __tablename__ = "user_ratings"
@ -47,7 +48,7 @@ class Shout(Base):
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=ShoutAuthor.__tablename__) # NOTE: multiple authors authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors
topics = relationship(lambda: Topic, secondary=ShoutTopics) topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
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)
old_id: str = Column(String, nullable = True) old_id: str = Column(String, nullable = True)

View File

@ -1,4 +1,4 @@
from orm import Shout, ShoutAuthor, User, Community, Resource from orm import Shout, ShoutAuthor, ShoutTopic, 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
@ -8,6 +8,7 @@ from settings import SHOUTS_REPO
import subprocess import subprocess
import asyncio import asyncio
from datetime import datetime
from pathlib import Path from pathlib import Path
@ -108,16 +109,17 @@ 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
with local_session() as session: session = local_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.id == id).first() shout = session.query(Shout).filter(Shout.id == id).first()
if not shout: if not shout:
return { return {
"error" : "shout not found" "error" : "shout not found"
} }
if shout.authors[0] != user_id: authors = [author.id for author in shout.authors]
if not user_id in authors:
scopes = auth.scopes scopes = auth.scopes
print(scopes) print(scopes)
if not Resource.shout_id in scopes: if not Resource.shout_id in scopes:
@ -125,14 +127,15 @@ async def update_shout(_, info, id, input):
"error" : "access denied" "error" : "access denied"
} }
shout.body = input["body"], shout.update(input)
shout.replyTo = input.get("replyTo"), shout.updatedAt = datetime.now()
shout.versionOf = input.get("versionOf"), session.commit()
shout.tags = input.get("tags"), session.close()
shout.topics = input.get("topics")
with local_session() as session: for topic in input.get("topics"):
session.commit() ShoutTopic.create(
shout = shout.id,
topic = topic)
task = GitTask( task = GitTask(
input, input,

View File

@ -27,7 +27,7 @@ input ShoutInput {
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: [Int]
title: String title: String
subtitle: String subtitle: String
versionOf: String versionOf: String