consistent naming of database fields

This commit is contained in:
Igor Lobanov 2022-11-29 20:13:03 +01:00
parent c064170d24
commit 8209cc744c
20 changed files with 113 additions and 114 deletions

View File

@ -73,8 +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_id"] = 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
@ -92,13 +91,13 @@ async def migrate(entry, storage):
).where( ).where(
User.id == reaction_dict["createdBy"] User.id == reaction_dict["createdBy"]
).filter( ).filter(
ShoutReactionsFollower.shout_id == reaction.shout_id ShoutReactionsFollower.shoutId == reaction.shoutId
).first() ).first()
if not following1: if not following1:
following1 = ShoutReactionsFollower.create( following1 = ShoutReactionsFollower.create(
follower_id=reaction_dict["createdBy"], followerId=reaction_dict["createdBy"],
shout_id=reaction.shout_id, shoutId=reaction.shoutId,
auto=True auto=True
) )
session.add(following1) session.add(following1)
@ -110,7 +109,7 @@ async def migrate(entry, storage):
).join( ).join(
Topic Topic
).where( ).where(
TopicFollower.follower_id == reaction_dict["createdBy"] TopicFollower.followerId == reaction_dict["createdBy"]
).filter( ).filter(
Topic.slug == t Topic.slug == t
).first() ).first()
@ -121,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(
follower_id=reaction_dict["createdBy"], followerId=reaction_dict["createdBy"],
topic_id=topic.id, topicId=topic.id,
auto=True auto=True
) )
session.add(topic_following) session.add(topic_following)
@ -135,7 +134,7 @@ async def migrate(entry, storage):
.first() .first()
) )
re_reaction_dict = { re_reaction_dict = {
"shout_id": reaction_dict["shout_id"], "shoutId": reaction_dict["shoutId"],
"replyTo": reaction.id, "replyTo": reaction.id,
"kind": ReactionKind.LIKE "kind": ReactionKind.LIKE
if comment_rating_old["value"] > 0 if comment_rating_old["value"] > 0
@ -151,14 +150,14 @@ async def migrate(entry, storage):
following2 = session.query( following2 = session.query(
ShoutReactionsFollower ShoutReactionsFollower
).where( ).where(
ShoutReactionsFollower.follower_id == re_reaction_dict['createdBy'] ShoutReactionsFollower.followerId == re_reaction_dict['createdBy']
).filter( ).filter(
ShoutReactionsFollower.shout_id == rr.shout_id ShoutReactionsFollower.shoutId == rr.shoutId
).first() ).first()
if not following2: if not following2:
following2 = ShoutReactionsFollower.create( following2 = ShoutReactionsFollower.create(
follower_id=re_reaction_dict['createdBy'], followerId=re_reaction_dict['createdBy'],
shout_id=rr.shout_id, shoutId=rr.shoutId,
auto=True auto=True
) )
session.add(following2) session.add(following2)
@ -191,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.shout_id == comment.shout_id ShoutReactionsFollower.shoutId == comment.shoutId
).filter( ).filter(
ShoutReactionsFollower.follower_id == comment.createdBy ShoutReactionsFollower.followerId == comment.createdBy
).first() ).first()
if not srf: if not srf:
srf = ShoutReactionsFollower.create(shout_id=comment.shout_id, follower_id=comment.createdBy, auto=True) srf = ShoutReactionsFollower.create(shoutId=comment.shoutId, followerId=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.shout_id == s.id ShoutReactionsFollower.shoutId == s.id
).filter( ).filter(
User.slug == userslug User.slug == userslug
).first() ).first()
if not srf: if not srf:
srf = ShoutReactionsFollower.create(shout_id=s.id, follower_id=follower.id, auto=True) srf = ShoutReactionsFollower.create(shoutId=s.id, followerId=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.follower_id == follower.id TopicFollower.followerId == follower.id
).filter( ).filter(
TopicFollower.topic_id == topic.id TopicFollower.topicId == topic.id
).first() ).first()
if not tf: if not tf:
tf = TopicFollower.create( tf = TopicFollower.create(
topic_id=topic.id, topicId=topic.id,
follower_id=follower.id, followerId=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({"topic_id": new_topic.id}) shout_topic_old.update({"topicId": 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(
**{"shout_id": shout.id, "topic_id": new_topic.id} **{"shoutId": shout.id, "topicId": 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,
"shout_id": shout.id, "shoutId": 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.shout_id == reaction_dict["shout_id"] Reaction.shoutId == reaction_dict["shoutId"]
).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"],
"rater_id": rater.id, "raterId": rater.id,
"user_id": user.id, "userId": 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(
author_id=user.id, authorId=user.id,
follower_id=rater.id, followerId=rater.id,
auto=True auto=True
) )
session.add(af) session.add(af)

View File

@ -11,7 +11,7 @@ class CollabAuthor(Base):
id = None # type: ignore id = None # type: ignore
collab = Column(ForeignKey("collab.id"), primary_key=True) collab = Column(ForeignKey("collab.id"), primary_key=True)
author_id = Column(ForeignKey("user.id"), primary_key=True) authorId = Column(ForeignKey("user.id"), primary_key=True)
accepted = Column(Boolean, default=False) accepted = Column(Boolean, default=False)

View File

@ -9,8 +9,8 @@ class ShoutCollection(Base):
__tablename__ = "shout_collection" __tablename__ = "shout_collection"
id = None # type: ignore id = None # type: ignore
shout_id = Column(ForeignKey("shout.id"), primary_key=True) shoutId = Column(ForeignKey("shout.id"), primary_key=True)
collection_id = Column(ForeignKey("collection.id"), primary_key=True) collectionId = 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
follower_id = Column(ForeignKey("user.id"), primary_key=True) followerId = Column(ForeignKey("user.id"), primary_key=True)
community_id = Column(ForeignKey("community.id"), primary_key=True) communityId = 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

@ -122,7 +122,7 @@ class Operation(Base):
class Resource(Base): class Resource(Base):
__tablename__ = "resource" __tablename__ = "resource"
resource_class = Column( resourceClass = Column(
String, nullable=False, unique=True, comment="Resource class" String, nullable=False, unique=True, comment="Resource class"
) )
name = Column(String, nullable=False, unique=True, comment="Resource name") name = Column(String, nullable=False, unique=True, comment="Resource name")
@ -134,7 +134,7 @@ class Resource(Base):
for res in ["shout", "topic", "reaction", "chat", "message", "invite", "community", "user"]: for res in ["shout", "topic", "reaction", "chat", "message", "invite", "community", "user"]:
r = session.query(Resource).filter(Resource.name == res).first() r = session.query(Resource).filter(Resource.name == res).first()
if not r: if not r:
r = Resource.create(name=res, resource_class=res) r = Resource.create(name=res, resourceClass=res)
session.add(r) session.add(r)
session.commit() session.commit()
@ -142,19 +142,19 @@ class Resource(Base):
class Permission(Base): class Permission(Base):
__tablename__ = "permission" __tablename__ = "permission"
__table_args__ = ( __table_args__ = (
UniqueConstraint("role_id", "operation_id", "resource_id"), UniqueConstraint("roleId", "operationId", "resourceId"),
{"extend_existing": True}, {"extend_existing": True},
) )
role_id = Column( roleId = Column(
ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role" ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role"
) )
operation_id = Column( operationId = Column(
ForeignKey("operation.id", ondelete="CASCADE"), ForeignKey("operation.id", ondelete="CASCADE"),
nullable=False, nullable=False,
comment="Operation", comment="Operation",
) )
resource_id = Column( resourceId = 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(role_id=1, operation_id=1, resource_id=1), Permission(roleId=1, operationId=1, resourceId=1),
Permission(role_id=1, operation_id=2, resource_id=1), Permission(roleId=1, operationId=2, resourceId=1),
Permission(role_id=1, operation_id=3, resource_id=1), Permission(roleId=1, operationId=3, resourceId=1),
Permission(role_id=1, operation_id=4, resource_id=1), Permission(roleId=1, operationId=4, resourceId=1),
Permission(role_id=2, operation_id=4, resource_id=1), Permission(roleId=2, operationId=4, resourceId=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")
shout_id = Column(ForeignKey("shout.id"), nullable=False, index=True) shoutId = 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
shout_id = Column(ForeignKey("shout.id"), primary_key=True, index=True) shoutId = Column(ForeignKey("shout.id"), primary_key=True, index=True)
topic_id = Column(ForeignKey("topic.id"), primary_key=True, index=True) topicId = 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
follower_id = Column(ForeignKey("user.id"), primary_key=True, index=True) followerId = Column(ForeignKey("user.id"), primary_key=True, index=True)
shout_id = Column(ForeignKey("shout.id"), primary_key=True, index=True) shoutId = 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
shout_id = Column(ForeignKey("shout.id"), primary_key=True, index=True) shoutId = Column(ForeignKey("shout.id"), primary_key=True, index=True)
user_id = Column(ForeignKey("user.id"), primary_key=True, index=True) userId = 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
follower_id = Column(ForeignKey("user.id"), primary_key=True, index=True) followerId = Column(ForeignKey("user.id"), primary_key=True, index=True)
topic_id = Column(ForeignKey("topic.id"), primary_key=True, index=True) topicId = 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
user_id = Column(Integer, ForeignKey("user.id")) userId = 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
rater_id = Column(ForeignKey("user.id"), primary_key=True, index=True) raterId = Column(ForeignKey("user.id"), primary_key=True, index=True)
user_id = Column(ForeignKey("user.id"), primary_key=True, index=True) userId = 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
user_id = Column(ForeignKey("user.id"), primary_key=True, index=True) userId = Column(ForeignKey("user.id"), primary_key=True, index=True)
role_id = Column(ForeignKey("role.id"), primary_key=True, index=True) roleId = 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
follower_id = Column(ForeignKey("user.id"), primary_key=True, index=True) followerId = Column(ForeignKey("user.id"), primary_key=True, index=True)
author_id = Column(ForeignKey("user.id"), primary_key=True, index=True) authorId = 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.user_id) ratings = relationship(UserRating, foreign_keys=UserRating.userId)
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.resource_id not in scope: if p.resourceId not in scope:
scope[p.resource_id] = set() scope[p.resourceId] = set()
scope[p.resource_id].add(p.operation_id) scope[p.resourceId].add(p.operationId)
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"
viewer_id = Column(ForeignKey("user.id"), index=True, default=1) viewerId = Column(ForeignKey("user.id"), index=True, default=1)
shout_id = Column(ForeignKey("shout.id"), index=True, default=1) shoutId = 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

@ -26,7 +26,7 @@ async def create_shout(_, info, inp):
new_shout = Shout.create(**inp) new_shout = Shout.create(**inp)
# NOTE: shout made by one first author # NOTE: shout made by one first author
sa = ShoutAuthor.create(shout_id=new_shout.id, user_id=user.id) sa = ShoutAuthor.create(shoutId=new_shout.id, userId=user.id)
session.add(sa) session.add(sa)
reactions_follow(user, new_shout.slug, True) reactions_follow(user, new_shout.slug, True)
@ -37,14 +37,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(shout_id=new_shout.id, topic_id=topic.id) st = ShoutTopic.create(shoutId=new_shout.id, topicId=topic.id)
session.add(st) session.add(st)
tf = session.query(TopicFollower).where( tf = session.query(TopicFollower).where(
and_(TopicFollower.follower_id == user.id, TopicFollower.topic_id == topic.id) and_(TopicFollower.followerId == user.id, TopicFollower.topicId == topic.id)
) )
if not tf: if not tf:
tf = TopicFollower.create(follower_id=user.id, topic_id=topic.id, auto=True) tf = TopicFollower.create(followerId=user.id, topicId=topic.id, auto=True)
session.add(tf) session.add(tf)
new_shout.topic_slugs = topic_slugs new_shout.topic_slugs = topic_slugs
@ -74,7 +74,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.shout_id not in scopes: if Resource.shoutId not in scopes:
return {"error": "access denied"} return {"error": "access denied"}
else: else:
shout.update(inp) shout.update(inp)
@ -82,7 +82,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.shout_id == shout.id).all() links = session.query(ShoutTopic).where(ShoutTopic.shoutId == 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.follower_id User, User.id == AuthorFollower.followerId
).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.author_id User, User.id == AuthorFollower.authorId
).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.user_id: if author.id == author_caption.userId:
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.shout_id)).label('shouts_stat') func.count(distinct(shout_author_aliased.shoutId)).label('shouts_stat')
) )
q = q.outerjoin(author_followers, author_followers.author_id == User.id).add_columns( q = q.outerjoin(author_followers, author_followers.authorId == User.id).add_columns(
func.count(distinct(author_followers.follower_id)).label('followers_stat') func.count(distinct(author_followers.followerId)).label('followers_stat')
) )
q = q.outerjoin(author_following, author_following.follower_id == User.id).add_columns( q = q.outerjoin(author_following, author_following.followerId == User.id).add_columns(
func.count(distinct(author_following.author_id)).label('followings_stat') func.count(distinct(author_following.authorId)).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.user_id == User.id).add_columns( # q = q.outerjoin(user_rating_aliased, user_rating_aliased.userId == 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.follower_id).where(User.slug == slug) q = q.join(AuthorFollower).join(User, User.id == AuthorFollower.followerId).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.author_id aliased_user, aliased_user.id == AuthorFollower.authorId
).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.user_id == user.id) .where(UserRole.userId == 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(follower_id=user.id, author_id=author.id) af = AuthorFollower.create(followerId=user.id, authorId=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.author_id).filter( ).join(User, User.id == AuthorFollower.authorId).filter(
and_( and_(
AuthorFollower.follower_id == user.id, User.slug == slug AuthorFollower.followerId == 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.user_id) q = q.join(ShoutAuthor, User.id == ShoutAuthor.userId)
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.follower_id == user.id, ShoutReactionsFollower.followerId == user.id,
ShoutReactionsFollower.shout_id == shout.id, ShoutReactionsFollower.shoutId == shout.id,
)).first() )).first()
) )
if not following: if not following:
following = ShoutReactionsFollower.create( following = ShoutReactionsFollower.create(
follower_id=user.id, followerId=user.id,
shout_id=shout.id, shoutId=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.follower_id == user.id, ShoutReactionsFollower.followerId == user.id,
ShoutReactionsFollower.shout_id == shout.id ShoutReactionsFollower.shoutId == 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.shout_id == reaction.shout_id).all() approvers_reactions = session.query(Reaction).where(Reaction.shoutId == reaction.shoutId).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.shout_id == reaction.shout_id).all() approvers_reactions = session.query(Reaction).where(Reaction.shoutId == reaction.shoutId).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.shout_id == ReactedShout.id ReactedShout, Reaction.shoutId == 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.topic_id).add_columns( q = q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topicId).add_columns(
func.count(distinct(ShoutTopic.shout_id)).label('shouts_stat') func.count(distinct(ShoutTopic.shoutId)).label('shouts_stat')
).outerjoin(ShoutAuthor, ShoutTopic.shout_id == ShoutAuthor.shout_id).add_columns( ).outerjoin(ShoutAuthor, ShoutTopic.shoutId == ShoutAuthor.shoutId).add_columns(
func.count(distinct(ShoutAuthor.user_id)).label('authors_stat') func.count(distinct(ShoutAuthor.userId)).label('authors_stat')
).outerjoin(TopicFollower, ).outerjoin(TopicFollower,
and_( and_(
TopicFollower.topic_id == Topic.id, TopicFollower.topicId == Topic.id,
TopicFollower.follower_id == ShoutAuthor.id TopicFollower.followerId == ShoutAuthor.id
)).add_columns( )).add_columns(
func.count(distinct(TopicFollower.follower_id)).label('followers_stat') func.count(distinct(TopicFollower.followerId)).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(topic_id=topic.id, follower=user.id) following = TopicFollower.create(topicId=topic.id, followerId=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.follower_id == user.id, TopicFollower.followerId == 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.shout_id == Shout.id).group_by(Topic.id).having(func.count(Shout.id) > 2) q = q.join(Shout, ShoutTopic.shoutId == 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

@ -319,8 +319,8 @@ type Operation {
} }
type Permission { type Permission {
operation_id: Int! operationId: Int!
resource_id: Int! resourceId: 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.shout_id == shout.id ViewedEntry.shoutId == 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(**{
"viewer_id": viewer.id, "viewerId": viewer.id,
"shout_id": shout.id, "shoutId": shout.id,
"amount": amount "amount": amount
}) })
session.add(viewed) session.add(viewed)