id suffix removed from database relation fields

This commit is contained in:
Igor Lobanov 2022-11-30 18:21:15 +01:00
parent 084d50ac57
commit 479b2230d6
19 changed files with 110 additions and 110 deletions

View File

@ -73,7 +73,7 @@ async def migrate(entry, storage):
Shout Shout
).where(Shout.slug == shout_dict["slug"]).one() ).where(Shout.slug == shout_dict["slug"]).one()
reaction_dict["shoutId"] = shout.id reaction_dict["shout"] = shout.id
reaction_dict["createdBy"] = author.id if author else 1 reaction_dict["createdBy"] = author.id if author else 1
reaction_dict["kind"] = ReactionKind.COMMENT reaction_dict["kind"] = ReactionKind.COMMENT
@ -91,13 +91,13 @@ async def migrate(entry, storage):
).where( ).where(
User.id == reaction_dict["createdBy"] User.id == reaction_dict["createdBy"]
).filter( ).filter(
ShoutReactionsFollower.shoutId == reaction.shoutId ShoutReactionsFollower.shout == reaction.shout
).first() ).first()
if not following1: if not following1:
following1 = ShoutReactionsFollower.create( following1 = ShoutReactionsFollower.create(
followerId=reaction_dict["createdBy"], follower=reaction_dict["createdBy"],
shoutId=reaction.shoutId, shout=reaction.shout,
auto=True auto=True
) )
session.add(following1) session.add(following1)
@ -109,7 +109,7 @@ async def migrate(entry, storage):
).join( ).join(
Topic Topic
).where( ).where(
TopicFollower.followerId == reaction_dict["createdBy"] TopicFollower.follower == reaction_dict["createdBy"]
).filter( ).filter(
Topic.slug == t Topic.slug == t
).first() ).first()
@ -120,8 +120,8 @@ async def migrate(entry, storage):
).where(Topic.slug == t).one() ).where(Topic.slug == t).one()
topic_following = TopicFollower.create( topic_following = TopicFollower.create(
followerId=reaction_dict["createdBy"], follower=reaction_dict["createdBy"],
topicId=topic.id, topic=topic.id,
auto=True auto=True
) )
session.add(topic_following) session.add(topic_following)
@ -134,7 +134,7 @@ async def migrate(entry, storage):
.first() .first()
) )
re_reaction_dict = { re_reaction_dict = {
"shoutId": reaction_dict["shoutId"], "shout": reaction_dict["shout"],
"replyTo": reaction.id, "replyTo": reaction.id,
"kind": ReactionKind.LIKE "kind": ReactionKind.LIKE
if comment_rating_old["value"] > 0 if comment_rating_old["value"] > 0
@ -150,14 +150,14 @@ async def migrate(entry, storage):
following2 = session.query( following2 = session.query(
ShoutReactionsFollower ShoutReactionsFollower
).where( ).where(
ShoutReactionsFollower.followerId == re_reaction_dict['createdBy'] ShoutReactionsFollower.follower == re_reaction_dict['createdBy']
).filter( ).filter(
ShoutReactionsFollower.shoutId == rr.shoutId ShoutReactionsFollower.shout == rr.shout
).first() ).first()
if not following2: if not following2:
following2 = ShoutReactionsFollower.create( following2 = ShoutReactionsFollower.create(
followerId=re_reaction_dict['createdBy'], follower=re_reaction_dict['createdBy'],
shoutId=rr.shoutId, shout=rr.shout,
auto=True auto=True
) )
session.add(following2) session.add(following2)
@ -190,13 +190,13 @@ def migrate_2stage(rr, old_new_id):
session.add(comment) session.add(comment)
srf = session.query(ShoutReactionsFollower).where( srf = session.query(ShoutReactionsFollower).where(
ShoutReactionsFollower.shoutId == comment.shoutId ShoutReactionsFollower.shout == comment.shout
).filter( ).filter(
ShoutReactionsFollower.followerId == comment.createdBy ShoutReactionsFollower.follower == comment.createdBy
).first() ).first()
if not srf: if not srf:
srf = ShoutReactionsFollower.create(shoutId=comment.shoutId, followerId=comment.createdBy, auto=True) srf = ShoutReactionsFollower.create(shout=comment.shout, follower=comment.createdBy, auto=True)
session.add(srf) session.add(srf)
session.commit() session.commit()

View File

@ -91,12 +91,12 @@ async def create_shout(shout_dict, userslug):
).join( ).join(
User User
).where( ).where(
ShoutReactionsFollower.shoutId == s.id ShoutReactionsFollower.shout == s.id
).filter( ).filter(
User.slug == userslug User.slug == userslug
).first() ).first()
if not srf: if not srf:
srf = ShoutReactionsFollower.create(shoutId=s.id, followerId=follower.id, auto=True) srf = ShoutReactionsFollower.create(shout=s.id, follower=follower.id, auto=True)
session.add(srf) session.add(srf)
session.commit() session.commit()
@ -226,15 +226,15 @@ async def add_topics_follower(entry, storage, userslug):
tf = session.query( tf = session.query(
TopicFollower TopicFollower
).where( ).where(
TopicFollower.followerId == follower.id TopicFollower.follower == follower.id
).filter( ).filter(
TopicFollower.topicId == topic.id TopicFollower.topic == topic.id
).first() ).first()
if not tf: if not tf:
tf = TopicFollower.create( tf = TopicFollower.create(
topicId=topic.id, topic=topic.id,
followerId=follower.id, follower=follower.id,
auto=True auto=True
) )
session.add(tf) session.add(tf)
@ -325,7 +325,7 @@ async def topics_aftermath(entry, storage):
.first() .first()
) )
if shout_topic_old: if shout_topic_old:
shout_topic_old.update({"topicId": new_topic.id}) shout_topic_old.update({"topic": new_topic.id})
else: else:
shout_topic_new = ( shout_topic_new = (
session.query(ShoutTopic) session.query(ShoutTopic)
@ -338,7 +338,7 @@ async def topics_aftermath(entry, storage):
if not shout_topic_new: if not shout_topic_new:
try: try:
ShoutTopic.create( ShoutTopic.create(
**{"shoutId": shout.id, "topicId": new_topic.id} **{"shout": shout.id, "topic": new_topic.id}
) )
except Exception: except Exception:
print("[migration] shout topic error: " + newslug) print("[migration] shout topic error: " + newslug)
@ -373,14 +373,14 @@ async def content_ratings_to_reactions(entry, slug):
if content_rating["value"] > 0 if content_rating["value"] > 0
else ReactionKind.DISLIKE, else ReactionKind.DISLIKE,
"createdBy": reactedBy.id, "createdBy": reactedBy.id,
"shoutId": shout.id, "shout": shout.id,
} }
cts = content_rating.get("createdAt") cts = content_rating.get("createdAt")
if cts: if cts:
reaction_dict["createdAt"] = date_parse(cts) reaction_dict["createdAt"] = date_parse(cts)
reaction = ( reaction = (
session.query(Reaction).filter( session.query(Reaction).filter(
Reaction.shoutId == reaction_dict["shoutId"] Reaction.shout == reaction_dict["shout"]
).filter( ).filter(
Reaction.createdBy == reaction_dict["createdBy"] Reaction.createdBy == reaction_dict["createdBy"]
).filter( ).filter(

View File

@ -123,15 +123,15 @@ def migrate_2stage(entry, id_map):
user_rating_dict = { user_rating_dict = {
"value": rating_entry["value"], "value": rating_entry["value"],
"raterId": rater.id, "rater": rater.id,
"userId": user.id, "user": user.id,
} }
user_rating = UserRating.create(**user_rating_dict) user_rating = UserRating.create(**user_rating_dict)
if user_rating_dict['value'] > 0: if user_rating_dict['value'] > 0:
af = AuthorFollower.create( af = AuthorFollower.create(
authorId=user.id, author=user.id,
followerId=rater.id, follower=rater.id,
auto=True auto=True
) )
session.add(af) session.add(af)

View File

@ -9,8 +9,8 @@ class ShoutCollection(Base):
__tablename__ = "shout_collection" __tablename__ = "shout_collection"
id = None # type: ignore id = None # type: ignore
shoutId = Column(ForeignKey("shout.id"), primary_key=True) shout = Column(ForeignKey("shout.id"), primary_key=True)
collectionId = Column(ForeignKey("collection.id"), primary_key=True) collection = Column(ForeignKey("collection.id"), primary_key=True)
class Collection(Base): class Collection(Base):

View File

@ -8,8 +8,8 @@ class CommunityFollower(Base):
__tablename__ = "community_followers" __tablename__ = "community_followers"
id = None # type: ignore id = None # type: ignore
followerId = Column(ForeignKey("user.id"), primary_key=True) follower = Column(ForeignKey("user.id"), primary_key=True)
communityId = Column(ForeignKey("community.id"), primary_key=True) community = Column(ForeignKey("community.id"), primary_key=True)
joinedAt = Column( joinedAt = Column(
DateTime, nullable=False, default=datetime.now, comment="Created at" DateTime, nullable=False, default=datetime.now, comment="Created at"
) )

View File

@ -142,19 +142,19 @@ class Resource(Base):
class Permission(Base): class Permission(Base):
__tablename__ = "permission" __tablename__ = "permission"
__table_args__ = ( __table_args__ = (
UniqueConstraint("roleId", "operationId", "resourceId"), UniqueConstraint("role", "operation", "resource"),
{"extend_existing": True}, {"extend_existing": True},
) )
roleId = Column( role = Column(
ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role" ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role"
) )
operationId = Column( operation = Column(
ForeignKey("operation.id", ondelete="CASCADE"), ForeignKey("operation.id", ondelete="CASCADE"),
nullable=False, nullable=False,
comment="Operation", comment="Operation",
) )
resourceId = Column( resource = Column(
ForeignKey("resource.id", ondelete="CASCADE"), ForeignKey("resource.id", ondelete="CASCADE"),
nullable=False, nullable=False,
comment="Resource", comment="Resource",
@ -164,11 +164,11 @@ class Permission(Base):
if __name__ == "__main__": if __name__ == "__main__":
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
ops = [ ops = [
Permission(roleId=1, operationId=1, resourceId=1), Permission(role=1, operation=1, resource=1),
Permission(roleId=1, operationId=2, resourceId=1), Permission(role=1, operation=2, resource=1),
Permission(roleId=1, operationId=3, resourceId=1), Permission(role=1, operation=3, resource=1),
Permission(roleId=1, operationId=4, resourceId=1), Permission(role=1, operation=4, resource=1),
Permission(roleId=2, operationId=4, resourceId=1), Permission(role=2, operation=4, resource=1),
] ]
global_session.add_all(ops) global_session.add_all(ops)
global_session.commit() global_session.commit()

View File

@ -33,7 +33,7 @@ class Reaction(Base):
updatedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Last Editor") updatedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Last Editor")
deletedAt = Column(DateTime, nullable=True, comment="Deleted at") deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
deletedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Deleted by") deletedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Deleted by")
shoutId = Column(ForeignKey("shout.id"), nullable=False, index=True) shout = Column(ForeignKey("shout.id"), nullable=False, index=True)
replyTo = Column( replyTo = Column(
ForeignKey("reaction.id"), nullable=True, comment="Reply to reaction ID" ForeignKey("reaction.id"), nullable=True, comment="Reply to reaction ID"
) )

View File

@ -13,16 +13,16 @@ class ShoutTopic(Base):
__tablename__ = "shout_topic" __tablename__ = "shout_topic"
id = None # type: ignore id = None # type: ignore
shoutId = Column(ForeignKey("shout.id"), primary_key=True, index=True) shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
topicId = Column(ForeignKey("topic.id"), primary_key=True, index=True) topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
class ShoutReactionsFollower(Base): class ShoutReactionsFollower(Base):
__tablename__ = "shout_reactions_followers" __tablename__ = "shout_reactions_followers"
id = None # type: ignore id = None # type: ignore
followerId = Column(ForeignKey("user.id"), primary_key=True, index=True) follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
shoutId = Column(ForeignKey("shout.id"), primary_key=True, index=True) shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
auto = Column(Boolean, nullable=False, default=False) auto = Column(Boolean, nullable=False, default=False)
createdAt = Column( createdAt = Column(
DateTime, nullable=False, default=datetime.now, comment="Created at" DateTime, nullable=False, default=datetime.now, comment="Created at"
@ -34,8 +34,8 @@ class ShoutAuthor(Base):
__tablename__ = "shout_author" __tablename__ = "shout_author"
id = None # type: ignore id = None # type: ignore
shoutId = Column(ForeignKey("shout.id"), primary_key=True, index=True) shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
userId = Column(ForeignKey("user.id"), primary_key=True, index=True) user = Column(ForeignKey("user.id"), primary_key=True, index=True)
caption = Column(String, nullable=True, default="") caption = Column(String, nullable=True, default="")

View File

@ -9,8 +9,8 @@ class TopicFollower(Base):
__tablename__ = "topic_followers" __tablename__ = "topic_followers"
id = None # type: ignore id = None # type: ignore
followerId = Column(ForeignKey("user.id"), primary_key=True, index=True) follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
topicId = Column(ForeignKey("topic.id"), primary_key=True, index=True) topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
createdAt = Column( createdAt = Column(
DateTime, nullable=False, default=datetime.now, comment="Created at" DateTime, nullable=False, default=datetime.now, comment="Created at"
) )

View File

@ -11,7 +11,7 @@ from orm.rbac import Role
class UserNotifications(Base): class UserNotifications(Base):
__tablename__ = "user_notifications" __tablename__ = "user_notifications"
# id auto # id auto
userId = Column(Integer, ForeignKey("user.id")) user = Column(Integer, ForeignKey("user.id"))
kind = Column(String, ForeignKey("notification.kind")) kind = Column(String, ForeignKey("notification.kind"))
values = Column(JSONType, nullable=True) # [ <var1>, .. ] values = Column(JSONType, nullable=True) # [ <var1>, .. ]
@ -20,8 +20,8 @@ class UserRating(Base):
__tablename__ = "user_rating" __tablename__ = "user_rating"
id = None # type: ignore id = None # type: ignore
raterId = Column(ForeignKey("user.id"), primary_key=True, index=True) rater = Column(ForeignKey("user.id"), primary_key=True, index=True)
userId = Column(ForeignKey("user.id"), primary_key=True, index=True) user = Column(ForeignKey("user.id"), primary_key=True, index=True)
value = Column(Integer) value = Column(Integer)
@staticmethod @staticmethod
@ -33,16 +33,16 @@ class UserRole(Base):
__tablename__ = "user_role" __tablename__ = "user_role"
id = None # type: ignore id = None # type: ignore
userId = Column(ForeignKey("user.id"), primary_key=True, index=True) user = Column(ForeignKey("user.id"), primary_key=True, index=True)
roleId = Column(ForeignKey("role.id"), primary_key=True, index=True) role = Column(ForeignKey("role.id"), primary_key=True, index=True)
class AuthorFollower(Base): class AuthorFollower(Base):
__tablename__ = "author_follower" __tablename__ = "author_follower"
id = None # type: ignore id = None # type: ignore
followerId = Column(ForeignKey("user.id"), primary_key=True, index=True) follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
authorId = Column(ForeignKey("user.id"), primary_key=True, index=True) author = Column(ForeignKey("user.id"), primary_key=True, index=True)
createdAt = Column( createdAt = Column(
DateTime, nullable=False, default=datetime.now, comment="Created at" DateTime, nullable=False, default=datetime.now, comment="Created at"
) )
@ -72,7 +72,7 @@ class User(Base):
links = Column(JSONType, nullable=True, comment="Links") links = Column(JSONType, nullable=True, comment="Links")
oauth = Column(String, nullable=True) oauth = Column(String, nullable=True)
notifications = relationship(lambda: UserNotifications) notifications = relationship(lambda: UserNotifications)
ratings = relationship(UserRating, foreign_keys=UserRating.userId) ratings = relationship(UserRating, foreign_keys=UserRating.user)
roles = relationship(lambda: Role, secondary=UserRole.__tablename__) roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
oid = Column(String, nullable=True) oid = Column(String, nullable=True)
@ -104,9 +104,9 @@ class User(Base):
scope = {} scope = {}
for role in self.roles: for role in self.roles:
for p in role.permissions: for p in role.permissions:
if p.resourceId not in scope: if p.resource not in scope:
scope[p.resourceId] = set() scope[p.resource] = set()
scope[p.resourceId].add(p.operationId) scope[p.resource].add(p.operation)
return scope return scope

View File

@ -6,8 +6,8 @@ from base.orm import Base, local_session
class ViewedEntry(Base): class ViewedEntry(Base):
__tablename__ = "viewed" __tablename__ = "viewed"
viewerId = Column(ForeignKey("user.id"), index=True, default=1) viewer = Column(ForeignKey("user.id"), index=True, default=1)
shoutId = Column(ForeignKey("shout.id"), index=True, default=1) shout = Column(ForeignKey("shout.id"), index=True, default=1)
amount = Column(Integer, default=1) amount = Column(Integer, default=1)
createdAt = Column( createdAt = Column(
DateTime, nullable=False, default=datetime.now, comment="Created at" DateTime, nullable=False, default=datetime.now, comment="Created at"

View File

@ -52,7 +52,7 @@ async def create_shout(_, info, inp):
session.add(new_collab) session.add(new_collab)
# NOTE: shout made by one first author # NOTE: shout made by one first author
sa = ShoutAuthor.create(shoutId=new_shout.id, userId=user.id) sa = ShoutAuthor.create(shout=new_shout.id, user=user.id)
session.add(sa) session.add(sa)
reactions_follow(user, new_shout.slug, True) reactions_follow(user, new_shout.slug, True)
@ -63,14 +63,14 @@ async def create_shout(_, info, inp):
for slug in topic_slugs: for slug in topic_slugs:
topic = session.query(Topic).where(Topic.slug == slug).one() topic = session.query(Topic).where(Topic.slug == slug).one()
st = ShoutTopic.create(shoutId=new_shout.id, topicId=topic.id) st = ShoutTopic.create(shout=new_shout.id, topic=topic.id)
session.add(st) session.add(st)
tf = session.query(TopicFollower).where( tf = session.query(TopicFollower).where(
and_(TopicFollower.followerId == user.id, TopicFollower.topicId == topic.id) and_(TopicFollower.follower == user.id, TopicFollower.topic == topic.id)
) )
if not tf: if not tf:
tf = TopicFollower.create(followerId=user.id, topicId=topic.id, auto=True) tf = TopicFollower.create(follower=user.id, topic=topic.id, auto=True)
session.add(tf) session.add(tf)
new_shout.topic_slugs = topic_slugs new_shout.topic_slugs = topic_slugs
@ -100,7 +100,7 @@ async def update_shout(_, info, inp):
if user_id not in authors: if user_id not in authors:
scopes = auth.scopes scopes = auth.scopes
print(scopes) print(scopes)
if Resource.shoutId not in scopes: if Resource.shout not in scopes:
return {"error": "access denied"} return {"error": "access denied"}
else: else:
shout.update(inp) shout.update(inp)
@ -108,7 +108,7 @@ async def update_shout(_, info, inp):
session.add(shout) session.add(shout)
if inp.get("topics"): if inp.get("topics"):
# remove old links # remove old links
links = session.query(ShoutTopic).where(ShoutTopic.shoutId == shout.id).all() links = session.query(ShoutTopic).where(ShoutTopic.shout == shout.id).all()
for topiclink in links: for topiclink in links:
session.delete(topiclink) session.delete(topiclink)
# add new topic links # add new topic links

View File

@ -31,7 +31,7 @@ async def search_recipients(_, info, query: str, limit: int = 50, offset: int =
with local_session() as session: with local_session() as session:
# followings # followings
result += session.query(AuthorFollower.author).join( result += session.query(AuthorFollower.author).join(
User, User.id == AuthorFollower.followerId User, User.id == AuthorFollower.follower
).where( ).where(
User.slug.startswith(query) User.slug.startswith(query)
).offset(offset + len(result)).limit(more_amount) ).offset(offset + len(result)).limit(more_amount)
@ -39,7 +39,7 @@ async def search_recipients(_, info, query: str, limit: int = 50, offset: int =
more_amount = limit more_amount = limit
# followers # followers
result += session.query(AuthorFollower.follower).join( result += session.query(AuthorFollower.follower).join(
User, User.id == AuthorFollower.authorId User, User.id == AuthorFollower.author
).where( ).where(
User.slug.startswith(query) User.slug.startswith(query)
).offset(offset + len(result)).limit(offset + len(result) + limit) ).offset(offset + len(result)).limit(offset + len(result) + limit)

View File

@ -68,7 +68,7 @@ async def load_shout(_, info, slug):
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug): for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
for author in shout.authors: for author in shout.authors:
if author.id == author_caption.userId: if author.id == author_caption.user:
author.caption = author_caption.caption author.caption = author_caption.caption
return shout return shout

View File

@ -23,19 +23,19 @@ def add_author_stat_columns(q):
user_rating_aliased = aliased(UserRating) user_rating_aliased = aliased(UserRating)
q = q.outerjoin(shout_author_aliased).add_columns( q = q.outerjoin(shout_author_aliased).add_columns(
func.count(distinct(shout_author_aliased.shoutId)).label('shouts_stat') func.count(distinct(shout_author_aliased.shout)).label('shouts_stat')
) )
q = q.outerjoin(author_followers, author_followers.authorId == User.id).add_columns( q = q.outerjoin(author_followers, author_followers.author == User.id).add_columns(
func.count(distinct(author_followers.followerId)).label('followers_stat') func.count(distinct(author_followers.follower)).label('followers_stat')
) )
q = q.outerjoin(author_following, author_following.followerId == User.id).add_columns( q = q.outerjoin(author_following, author_following.follower == User.id).add_columns(
func.count(distinct(author_following.authorId)).label('followings_stat') func.count(distinct(author_following.author)).label('followings_stat')
) )
q = q.add_columns(literal(0).label('rating_stat')) q = q.add_columns(literal(0).label('rating_stat'))
# FIXME # FIXME
# q = q.outerjoin(user_rating_aliased, user_rating_aliased.userId == User.id).add_columns( # q = q.outerjoin(user_rating_aliased, user_rating_aliased.user == User.id).add_columns(
# # TODO: check # # TODO: check
# func.sum(user_rating_aliased.value).label('rating_stat') # func.sum(user_rating_aliased.value).label('rating_stat')
# ) # )
@ -120,7 +120,7 @@ async def get_followed_authors(_, _info, slug) -> List[User]:
async def followed_authors(slug) -> List[User]: async def followed_authors(slug) -> List[User]:
q = select(User) q = select(User)
q = add_author_stat_columns(q) q = add_author_stat_columns(q)
q = q.join(AuthorFollower).join(User, User.id == AuthorFollower.followerId).where(User.slug == slug) q = q.join(AuthorFollower).join(User, User.id == AuthorFollower.follower).where(User.slug == slug)
return get_authors_from_query(q) return get_authors_from_query(q)
@ -132,7 +132,7 @@ async def user_followers(_, _info, slug) -> List[User]:
aliased_user = aliased(User) aliased_user = aliased(User)
q = q.join(AuthorFollower).join( q = q.join(AuthorFollower).join(
aliased_user, aliased_user.id == AuthorFollower.authorId aliased_user, aliased_user.id == AuthorFollower.author
).where( ).where(
aliased_user.slug == slug aliased_user.slug == slug
) )
@ -147,7 +147,7 @@ async def get_user_roles(slug):
session.query(Role) session.query(Role)
.options(joinedload(Role.permissions)) .options(joinedload(Role.permissions))
.join(UserRole) .join(UserRole)
.where(UserRole.userId == user.id) .where(UserRole.user == user.id)
.all() .all()
) )
@ -193,7 +193,7 @@ async def rate_user(_, info, rated_userslug, value):
def author_follow(user, slug): def author_follow(user, slug):
with local_session() as session: with local_session() as session:
author = session.query(User).where(User.slug == slug).one() author = session.query(User).where(User.slug == slug).one()
af = AuthorFollower.create(followerId=user.id, authorId=author.id) af = AuthorFollower.create(follower=user.id, author=author.id)
session.add(af) session.add(af)
session.commit() session.commit()
@ -204,9 +204,9 @@ def author_unfollow(user, slug):
flw = ( flw = (
session.query( session.query(
AuthorFollower AuthorFollower
).join(User, User.id == AuthorFollower.authorId).filter( ).join(User, User.id == AuthorFollower.author).filter(
and_( and_(
AuthorFollower.followerId == user.id, User.slug == slug AuthorFollower.follower == user.id, User.slug == slug
) )
).first() ).first()
) )
@ -221,7 +221,7 @@ def author_unfollow(user, slug):
async def get_authors_all(_, _info): async def get_authors_all(_, _info):
q = select(User) q = select(User)
q = add_author_stat_columns(q) q = add_author_stat_columns(q)
q = q.join(ShoutAuthor, User.id == ShoutAuthor.userId) q = q.join(ShoutAuthor, User.id == ShoutAuthor.user)
return get_authors_from_query(q) return get_authors_from_query(q)

View File

@ -20,15 +20,15 @@ def reactions_follow(user: User, slug: str, auto=False):
following = ( following = (
session.query(ShoutReactionsFollower).where(and_( session.query(ShoutReactionsFollower).where(and_(
ShoutReactionsFollower.followerId == user.id, ShoutReactionsFollower.follower == user.id,
ShoutReactionsFollower.shoutId == shout.id, ShoutReactionsFollower.shout == shout.id,
)).first() )).first()
) )
if not following: if not following:
following = ShoutReactionsFollower.create( following = ShoutReactionsFollower.create(
followerId=user.id, follower=user.id,
shoutId=shout.id, shout=shout.id,
auto=auto auto=auto
) )
session.add(following) session.add(following)
@ -41,8 +41,8 @@ def reactions_unfollow(user, slug):
following = ( following = (
session.query(ShoutReactionsFollower).where(and_( session.query(ShoutReactionsFollower).where(and_(
ShoutReactionsFollower.followerId == user.id, ShoutReactionsFollower.follower == user.id,
ShoutReactionsFollower.shoutId == shout.id ShoutReactionsFollower.shout == shout.id
)).first() )).first()
) )
@ -74,7 +74,7 @@ def check_to_publish(session, user, reaction):
]: ]:
if is_published_author(user): if is_published_author(user):
# now count how many approvers are voted already # now count how many approvers are voted already
approvers_reactions = session.query(Reaction).where(Reaction.shoutId == reaction.shoutId).all() approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
approvers = [user.slug, ] approvers = [user.slug, ]
for ar in approvers_reactions: for ar in approvers_reactions:
a = ar.createdBy a = ar.createdBy
@ -93,7 +93,7 @@ def check_to_hide(session, user, reaction):
ReactionKind.UNPROOF ReactionKind.UNPROOF
]: ]:
# if is_published_author(user): # if is_published_author(user):
approvers_reactions = session.query(Reaction).where(Reaction.shoutId == reaction.shoutId).all() approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
declines = 0 declines = 0
for r in approvers_reactions: for r in approvers_reactions:
if r.kind in [ if r.kind in [
@ -232,7 +232,7 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0):
).join( ).join(
CreatedByUser, Reaction.createdBy == CreatedByUser.id CreatedByUser, Reaction.createdBy == CreatedByUser.id
).join( ).join(
ReactedShout, Reaction.shoutId == ReactedShout.id ReactedShout, Reaction.shout == ReactedShout.id
) )
if by.get("shout"): if by.get("shout"):

View File

@ -8,16 +8,16 @@ from orm import Shout, User
def add_topic_stat_columns(q): def add_topic_stat_columns(q):
q = q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topicId).add_columns( q = q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic).add_columns(
func.count(distinct(ShoutTopic.shoutId)).label('shouts_stat') func.count(distinct(ShoutTopic.shout)).label('shouts_stat')
).outerjoin(ShoutAuthor, ShoutTopic.shoutId == ShoutAuthor.shoutId).add_columns( ).outerjoin(ShoutAuthor, ShoutTopic.shout == ShoutAuthor.shout).add_columns(
func.count(distinct(ShoutAuthor.userId)).label('authors_stat') func.count(distinct(ShoutAuthor.user)).label('authors_stat')
).outerjoin(TopicFollower, ).outerjoin(TopicFollower,
and_( and_(
TopicFollower.topicId == Topic.id, TopicFollower.topic == Topic.id,
TopicFollower.followerId == ShoutAuthor.id TopicFollower.follower == ShoutAuthor.id
)).add_columns( )).add_columns(
func.count(distinct(TopicFollower.followerId)).label('followers_stat') func.count(distinct(TopicFollower.follower)).label('followers_stat')
) )
q = q.group_by(Topic.id) q = q.group_by(Topic.id)
@ -119,7 +119,7 @@ async def topic_follow(user, slug):
with local_session() as session: with local_session() as session:
topic = session.query(Topic).where(Topic.slug == slug).one() topic = session.query(Topic).where(Topic.slug == slug).one()
following = TopicFollower.create(topicId=topic.id, followerId=user.id) following = TopicFollower.create(topic=topic.id, follower=user.id)
session.add(following) session.add(following)
session.commit() session.commit()
@ -129,7 +129,7 @@ async def topic_unfollow(user, slug):
sub = ( sub = (
session.query(TopicFollower).join(Topic).filter( session.query(TopicFollower).join(Topic).filter(
and_( and_(
TopicFollower.followerId == user.id, TopicFollower.follower == user.id,
Topic.slug == slug Topic.slug == slug
) )
).first() ).first()
@ -145,7 +145,7 @@ async def topic_unfollow(user, slug):
async def topics_random(_, info, amount=12): async def topics_random(_, info, amount=12):
q = select(Topic) q = select(Topic)
q = add_topic_stat_columns(q) q = add_topic_stat_columns(q)
q = q.join(Shout, ShoutTopic.shoutId == Shout.id).group_by(Topic.id).having(func.count(Shout.id) > 2) q = q.join(Shout, ShoutTopic.shout == Shout.id).group_by(Topic.id).having(func.count(Shout.id) > 2)
q = q.order_by(func.random()).limit(amount) q = q.order_by(func.random()).limit(amount)
return get_topics_from_query(q) return get_topics_from_query(q)

View File

@ -321,8 +321,8 @@ type Operation {
} }
type Permission { type Permission {
operationId: Int! operation: Int!
resourceId: Int! resource: Int!
} }
type Role { type Role {

View File

@ -128,7 +128,7 @@ class ViewedStorage:
try: try:
shout = session.query(Shout).where(Shout.slug == shout_slug).one() shout = session.query(Shout).where(Shout.slug == shout_slug).one()
shout_views = session.query(func.sum(ViewedEntry.amount)).where( shout_views = session.query(func.sum(ViewedEntry.amount)).where(
ViewedEntry.shoutId == shout.id ViewedEntry.shout == shout.id
).all()[0][0] ).all()[0][0]
self.by_shouts[shout_slug] = shout_views self.by_shouts[shout_slug] = shout_views
self.update_topics(session, shout_slug) self.update_topics(session, shout_slug)
@ -168,8 +168,8 @@ class ViewedStorage:
viewer = session.query(User).where(User.slug == viewer).one() viewer = session.query(User).where(User.slug == viewer).one()
viewed = ViewedEntry.create(**{ viewed = ViewedEntry.create(**{
"viewerId": viewer.id, "viewer": viewer.id,
"shoutId": shout.id, "shout": shout.id,
"amount": amount "amount": amount
}) })
session.add(viewed) session.add(viewed)