formatted, linted, fixed

This commit is contained in:
2022-09-04 20:20:38 +03:00
parent f7b9a066b9
commit 71f3ac5ed6
22 changed files with 357 additions and 303 deletions

View File

@@ -1,109 +1,112 @@
from resolvers.auth import (
login,
sign_out,
is_email_used,
register,
confirm,
auth_forget,
auth_reset,
)
from resolvers.zine import (
get_shout_by_slug,
follow,
unfollow,
view_shout,
top_month,
top_overall,
recent_published,
recent_all,
top_viewed,
shouts_by_authors,
shouts_by_topics,
shouts_by_communities,
)
from resolvers.profile import (
get_users_by_slugs,
get_current_user,
get_user_reacted_shouts,
get_user_roles,
)
from resolvers.topics import (
topic_follow,
topic_unfollow,
topics_by_author,
topics_by_community,
topics_all,
)
# from resolvers.feed import shouts_for_feed, my_candidates
from resolvers.reactions import (
create_reaction,
delete_reaction,
update_reaction,
get_all_reactions,
)
from resolvers.collab import invite_author, remove_author
from resolvers.editor import create_shout, delete_shout, update_shout
from resolvers.community import (
create_community,
delete_community,
get_community,
get_communities,
)
__all__ = [
"follow",
"unfollow",
# auth
"login",
"register",
"is_email_used",
"confirm",
"auth_forget",
"auth_reset" "sign_out",
# profile
"get_current_user",
"get_users_by_slugs",
# zine
"shouts_for_feed",
"my_candidates",
"recent_published",
"recent_reacted",
"recent_all",
"shouts_by_topics",
"shouts_by_authors",
"shouts_by_communities",
"get_user_reacted_shouts",
"top_month",
"top_overall",
"top_viewed",
"view_shout",
"view_reaction",
"get_shout_by_slug",
# editor
"create_shout",
"update_shout",
"delete_shout",
# collab
"invite_author",
"remove_author"
# topics
"topics_all",
"topics_by_community",
"topics_by_author",
"topic_follow",
"topic_unfollow",
# communities
"get_community",
"get_communities",
"create_community",
"delete_community",
# reactions
"get_shout_reactions",
"reactions_follow",
"reactions_unfollow",
"create_reaction",
"update_reaction",
"delete_reaction",
"get_all_reactions",
]
from resolvers.auth import (
login,
sign_out,
is_email_used,
register,
confirm,
auth_forget,
auth_reset,
)
from resolvers.zine import (
get_shout_by_slug,
follow,
unfollow,
view_shout,
top_month,
top_overall,
recent_published,
recent_all,
top_viewed,
shouts_by_authors,
shouts_by_topics,
shouts_by_communities,
)
from resolvers.profile import (
get_users_by_slugs,
get_current_user,
get_user_reacted_shouts,
get_user_roles,
)
from resolvers.topics import (
topic_follow,
topic_unfollow,
topics_by_author,
topics_by_community,
topics_all,
)
# from resolvers.feed import shouts_for_feed, my_candidates
from resolvers.reactions import (
create_reaction,
delete_reaction,
update_reaction,
get_all_reactions,
)
# from resolvers.collab import invite_author, remove_author
from resolvers.editor import create_shout, delete_shout, update_shout
from resolvers.community import (
create_community,
delete_community,
get_community,
get_communities,
)
__all__ = [
"follow",
"unfollow",
# auth
"login",
"register",
"is_email_used",
"confirm",
"auth_forget",
"auth_reset",
"sign_out",
# profile
"get_current_user",
"get_users_by_slugs",
"get_user_roles",
# zine
"shouts_for_feed",
"my_candidates",
"recent_published",
"recent_reacted",
"recent_all",
"shouts_by_topics",
"shouts_by_authors",
"shouts_by_communities",
"get_user_reacted_shouts",
"top_month",
"top_overall",
"top_viewed",
"view_shout",
"view_reaction",
"get_shout_by_slug",
# editor
"create_shout",
"update_shout",
"delete_shout",
# collab
"invite_author",
"remove_author",
# topics
"topics_all",
"topics_by_community",
"topics_by_author",
"topic_follow",
"topic_unfollow",
# communities
"get_community",
"get_communities",
"create_community",
"delete_community",
# reactions
"get_shout_reactions",
"reactions_follow",
"reactions_unfollow",
"create_reaction",
"update_reaction",
"delete_reaction",
"get_all_reactions",
]

View File

@@ -128,11 +128,11 @@ async def login(_, info: GraphQLResolveInfo, email: str, password: str = ""):
async def sign_out(_, info: GraphQLResolveInfo):
token = info.context["request"].headers[JWT_AUTH_HEADER]
status = await Authorize.revoke(token)
return True
return status
@query.field("isEmailUsed")
async def is_email_used(_, info, email):
with local_session() as session:
user = session.query(User).filter(User.email == email).first()
return not user is None
return user is not None

View File

@@ -4,15 +4,14 @@ from orm.user import User
from base.resolvers import mutation, query
from auth.authenticate import login_required
from datetime import datetime
from typing import Collection
from sqlalchemy import and_
@mutation.field("createCollection")
@login_required
async def create_collection(_, info, input):
auth = info.context["request"].auth
user_id = auth.user_id
# auth = info.context["request"].auth
# user_id = auth.user_id
collection = Collection.create(
slug=input.get("slug", ""),
title=input.get("title", ""),
@@ -73,35 +72,7 @@ async def get_user_collections(_, info, userslug):
collections = (
session.query(Collection)
.where(
and_(
Collection.createdBy == userslug, Collection.publishedAt != None
)
)
.all()
)
for c in collections:
shouts = (
session.query(ShoutCollection)
.filter(ShoutCollection.collection == c.id)
.all()
)
c.amount = len(shouts)
return collections
@query.field("getMyCollections")
async def get_user_collections(_, info, userslug):
collections = []
with local_session() as session:
user = session.query(User).filter(User.slug == userslug).first()
if user:
# TODO: check rights here
collections = (
session.query(Collection)
.where(
and_(
Collection.createdBy == userslug, Collection.publishedAt != None
)
and_(Collection.createdBy == userslug, bool(Collection.publishedAt))
)
.all()
)

View File

@@ -13,13 +13,16 @@ from sqlalchemy import and_
async def create_community(_, info, input):
auth = info.context["request"].auth
user_id = auth.user_id
community = Community.create(
slug=input.get("slug", ""),
title=input.get("title", ""),
desc=input.get("desc", ""),
pic=input.get("pic", ""),
)
with local_session() as session:
user = session.query(User).where(User.id == user_id).first()
community = Community.create(
slug=input.get("slug", ""),
title=input.get("title", ""),
desc=input.get("desc", ""),
pic=input.get("pic", ""),
createdBy=user.slug,
createdAt=datetime.now(),
)
return {"community": community}

View File

@@ -28,10 +28,10 @@ async def create_shout(_, info, input):
topic_slugs.append(input["mainTopic"])
for slug in topic_slugs:
topic = ShoutTopic.create(shout=new_shout.slug, topic=slug)
ShoutTopic.create(shout=new_shout.slug, topic=slug)
new_shout.topic_slugs = topic_slugs
task = GitTask(input, user.username, user.email, "new shout %s" % (new_shout.slug))
GitTask(input, user.username, user.email, "new shout %s" % (new_shout.slug))
# await ShoutCommentsStorage.send_shout(new_shout)
@@ -54,10 +54,10 @@ async def update_shout(_, info, input):
return {"error": "shout not found"}
authors = [author.id for author in shout.authors]
if not user_id in authors:
if user_id not in authors:
scopes = auth.scopes
print(scopes)
if not Resource.shout_id in scopes:
if Resource.shout_id not in scopes:
return {"error": "access denied"}
shout.update(input)
@@ -68,7 +68,7 @@ async def update_shout(_, info, input):
for topic in input.get("topic_slugs", []):
ShoutTopic.create(shout=slug, topic=topic)
task = GitTask(input, user.username, user.email, "update shout %s" % (slug))
GitTask(input, user.username, user.email, "update shout %s" % (slug))
return {"shout": shout}

View File

@@ -41,7 +41,7 @@ async def user_unpublished_shouts(_, info, page=1, size=10) -> List[Shout]:
shouts = (
session.query(Shout)
.join(ShoutAuthor)
.where(and_(Shout.publishedAt == None, ShoutAuthor.user == user.slug))
.where(and_(not bool(Shout.publishedAt), ShoutAuthor.user == user.slug))
.order_by(desc(Shout.createdAt))
.limit(size)
.offset(page * size)

View File

@@ -1,6 +1,8 @@
from base.resolvers import mutation, query, subscription
from auth.authenticate import login_required
import asyncio, uuid, json
import asyncio
import uuid
import json
from datetime import datetime
from base.redis import redis
@@ -259,7 +261,7 @@ async def mark_as_read(_, info, chatId, ids):
chat = json.loads(chat)
users = set(chat["users"])
if not user.slug in users:
if user.slug not in users:
return {"error": "access denied"}
for id in ids:

View File

@@ -1,5 +1,4 @@
from sqlalchemy import desc
from sqlalchemy.orm import joinedload, selectinload
from orm.reaction import Reaction
from base.orm import local_session
from orm.shout import ShoutReactionsFollower
@@ -24,7 +23,7 @@ def reactions_follow(user, slug, auto=False):
if auto and fw:
return
elif not auto and fw:
if not fw.deletedAt is None:
if bool(fw.deletedAt):
fw.deletedAt = None
fw.auto = False
session.commit()
@@ -130,17 +129,19 @@ async def get_shout_reactions(_, info, slug, page, size):
return reactions
@query.field("reactionsForSlugs")
async def get_shout_reactions(_, info, slugs, page, size):
@query.field("reactionsForShouts")
async def get_reactions_for_shouts(_, info, shoutslugs, page, size):
offset = page * size
reactions = []
with local_session() as session:
for slug in slugs:
for slug in shoutslugs:
reactions += (
session.query(Reaction)
.filter(Reaction.shout == slug)
.limit(size)
.where(not bool(Reaction.deletedAt))
.order_by(desc("createdAt"))
.offset(offset)
.limit(size)
.all()
)
for r in reactions:
@@ -148,25 +149,6 @@ async def get_shout_reactions(_, info, slugs, page, size):
return reactions
@query.field("reactionsAll")
async def get_all_reactions(_, info, page=1, size=10):
offset = page * size
reactions = []
with local_session() as session:
reactions = (
session.query(Reaction)
.filter(Reaction.deletedAt == None)
.order_by(desc("createdAt"))
.offset(offset)
.limit(size)
)
for r in reactions:
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
reactions = list(reactions)
reactions.sort(key=lambda x: x.createdAt, reverse=True)
return reactions
@query.field("reactionsByAuthor")
async def get_reactions_by_author(_, info, slug, page=1, size=50):
offset = page * size

View File

@@ -11,44 +11,44 @@ from resolvers.topics import topic_follow, topic_unfollow
from resolvers.community import community_follow, community_unfollow
from resolvers.reactions import reactions_follow, reactions_unfollow
from auth.authenticate import login_required
from sqlalchemy import select, desc, and_, text
from sqlalchemy import select, desc, and_
from sqlalchemy.orm import selectinload
@query.field("topViewed")
async def top_viewed(_, info, page, size):
async with ShoutsCache.lock:
return ShoutsCache.top_viewed[(page - 1) * size : page * size]
return ShoutsCache.top_viewed[((page - 1) * size) : (page * size)]
@query.field("topMonth")
async def top_month(_, info, page, size):
async with ShoutsCache.lock:
return ShoutsCache.top_month[(page - 1) * size : page * size]
return ShoutsCache.top_month[((page - 1) * size) : (page * size)]
@query.field("topOverall")
async def top_overall(_, info, page, size):
async with ShoutsCache.lock:
return ShoutsCache.top_overall[(page - 1) * size : page * size]
return ShoutsCache.top_overall[((page - 1) * size) : (page * size)]
@query.field("recentPublished")
async def recent_published(_, info, page, size):
async with ShoutsCache.lock:
return ShoutsCache.recent_published[(page - 1) * size : page * size]
return ShoutsCache.recent_published[((page - 1) * size) : (page * size)]
@query.field("recentAll")
async def recent_all(_, info, page, size):
async with ShoutsCache.lock:
return ShoutsCache.recent_all[(page - 1) * size : page * size]
return ShoutsCache.recent_all[((page - 1) * size) : (page * size)]
@query.field("recentReacted")
async def recent_reacted(_, info, page, size):
async with ShoutsCache.lock:
return ShoutsCache.recent_reacted[(page - 1) * size : page * size]
return ShoutsCache.recent_reacted[((page - 1) * size) : (page * size)]
@mutation.field("viewShout")
@@ -66,10 +66,7 @@ async def get_shout_by_slug(_, info, slug):
select_options = [selectinload(getattr(Shout, field)) for field in selected_fields]
shout = {}
with local_session() as session:
try:
s = text(open("src/queries/shout-by-slug.sql", "r").read() % slug)
except:
pass
# s = text(open("src/queries/shout-by-slug.sql", "r").read() % slug)
shout = (
session.query(Shout)
.options(select_options)
@@ -93,7 +90,7 @@ async def shouts_by_topics(_, info, slugs, page, size):
shouts = (
session.query(Shout)
.join(ShoutTopic)
.where(and_(ShoutTopic.topic.in_(slugs), Shout.publishedAt != None))
.where(and_(ShoutTopic.topic.in_(slugs), bool(Shout.publishedAt)))
.order_by(desc(Shout.publishedAt))
.limit(size)
.offset(page * size)
@@ -106,14 +103,14 @@ async def shouts_by_topics(_, info, slugs, page, size):
@query.field("shoutsByCollection")
async def shouts_by_topics(_, info, collection, page, size):
async def shouts_by_collection(_, info, collection, page, size):
page = page - 1
shouts = []
with local_session() as session:
shouts = (
session.query(Shout)
.join(ShoutCollection, ShoutCollection.collection == collection)
.where(and_(ShoutCollection.shout == Shout.slug, Shout.publishedAt != None))
.where(and_(ShoutCollection.shout == Shout.slug, bool(Shout.publishedAt)))
.order_by(desc(Shout.publishedAt))
.limit(size)
.offset(page * size)
@@ -132,7 +129,7 @@ async def shouts_by_authors(_, info, slugs, page, size):
shouts = (
session.query(Shout)
.join(ShoutAuthor)
.where(and_(ShoutAuthor.user.in_(slugs), Shout.publishedAt != None))
.where(and_(ShoutAuthor.user.in_(slugs), bool(Shout.publishedAt)))
.order_by(desc(Shout.publishedAt))
.limit(size)
.offset(page * size)
@@ -161,7 +158,7 @@ async def shouts_by_communities(_, info, slugs, page, size):
.join(ShoutTopic)
.where(
and_(
Shout.publishedAt != None,
bool(Shout.publishedAt),
ShoutTopic.topic.in_(
select(Topic.slug).where(Topic.community.in_(slugs))
),