schema fix

This commit is contained in:
Untone 2021-09-05 10:16:28 +03:00
parent 8b8942225b
commit aaca27ba88
6 changed files with 68 additions and 43 deletions

View File

@ -2,7 +2,7 @@ from typing import List
from datetime import datetime from datetime import datetime
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from orm import Permission, User, Topic from orm import Permission, User, Topic, Comment
from orm.base import Base from orm.base import Base
class ShoutAuthor(Base): class ShoutAuthor(Base):
@ -12,6 +12,13 @@ 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)
class ShoutViewer(Base):
__tablename__ = "shout_viewer"
id = None
shout = Column(ForeignKey('shout.id'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True)
class ShoutTopic(Base): class ShoutTopic(Base):
__tablename__ = 'shout_topic' __tablename__ = 'shout_topic'
@ -49,14 +56,16 @@ class Shout(Base):
replyTo: int = Column(ForeignKey("shout.id"), nullable=True) replyTo: int = Column(ForeignKey("shout.id"), nullable=True)
versionOf: int = Column(ForeignKey("shout.id"), nullable=True) versionOf: int = Column(ForeignKey("shout.id"), nullable=True)
tags: str = Column(String, nullable=True) tags: str = Column(String, nullable=True)
published: bool = Column(Boolean, default=False) publishedBy: bool = Column(ForeignKey("user.id"), nullable=True)
publishedAt: str = Column(DateTime, nullable=True) publishedAt: str = Column(DateTime, nullable=True)
cover: str = Column(String, nullable = True) cover: str = Column(String, nullable = True)
title: str = Column(String, nullable = True) title: str = Column(String, nullable = True)
subtitle: str = Column(String, nullable = True) subtitle: str = Column(String, nullable = True)
comments = relationship(Comment)
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=ShoutTopic.__tablename__) topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
ratings = relationship(ShoutRating, foreign_keys=ShoutRating.shout_id) ratings = relationship(ShoutRating, foreign_keys=ShoutRating.shout_id)
views = relationship(ShoutViewByDay) views = relationship(ShoutViewByDay)
visibleFor = relationship(lambda: User, secondary=ShoutViewer.__tablename__)
old_id: str = Column(String, nullable = True) old_id: str = Column(String, nullable = True)

View File

@ -1,11 +1,11 @@
from resolvers.auth import login, sign_out, get_user, is_email_free, register, confirm from resolvers.auth import login, sign_out, get_user, is_email_free, register, confirm
from resolvers.inbox import create_message, delete_message, update_message, get_messages from resolvers.inbox import create_message, delete_message, update_message, get_messages
from resolvers.zine import create_shout from resolvers.zine import create_shout, get_shout_by_slug
from resolvers.profile import get_user_by_slug, get_current_user
__all__ = [ __all__ = [
"login", "login",
"register", "register",
"get_user",
"is_email_free", "is_email_free",
"confirm", "confirm",
# TODO: "reset_password_code", # TODO: "reset_password_code",
@ -14,5 +14,8 @@ __all__ = [
"delete_message", "delete_message",
"get_messages", "get_messages",
"update_messages", "update_messages",
"create_shout" "create_shout",
"get_current_user",
"get_user_by_slug",
"get_shout_by_slug"
] ]

View File

@ -73,15 +73,6 @@ async def sign_out(_, info: GraphQLResolveInfo):
status = await Authorize.revoke(token) status = await Authorize.revoke(token)
return True return True
@query.field("getCurrentUser")
@login_required
async def get_user(_, info):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
user = session.query(User).filter(User.id == user_id).first()
return { "user": user }
@query.field("isEmailFree") @query.field("isEmailFree")
async def is_email_free(_, info, email): async def is_email_free(_, info, email):
with local_session() as session: with local_session() as session:

21
resolvers/profile.py Normal file
View File

@ -0,0 +1,21 @@
from orm import User
from orm.base import local_session
from resolvers.base import mutation, query, subscription
from auth.authenticate import login_required
import asyncio
@query.field("getUserBySlug") # get a public profile
async def get_user_by_slug(_, info, slug):
with local_session() as session:
user = session.query(User).filter(User.slug == slug).first()
return { "user": user } # TODO: remove some fields for public
@query.field("getCurrentUser")
@login_required
async def get_user(_, info):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
user = session.query(User).filter(User.id == user_id).first()
return { "user": user }

View File

@ -258,24 +258,20 @@ async def update_shout(_, info, id, input):
"shout" : shout "shout" : shout
} }
# TODO: get shout with comments query @query.field("getShoutBySlug") #FIXME: add shout joined with comments
async def get_shout_by_slug(_, info, slug):
@query.field("getShout") #FIXME: add shout joined with comments # month_ago = datetime.now() - timedelta(days = 30)
async def get_shout(_, info, shout_id):
month_ago = datetime.now() - timedelta(days = 30)
with local_session() as session: with local_session() as session:
stmt = select(Comment, func.sum(CommentRating.value).label("rating")).\ stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\
join(CommentRating).\ join(ShoutRating).\
where(CommentRating.ts > month_ago).\ # where(ShoutRating.ts > month_ago).\
where(Comment.shout == shout_id).\ where(Shout.slug == slug).\
# join(ShoutComment) # TODO: join(Comment) to .comments
group_by(Shout.id).\
order_by(desc("rating")).\
limit(limit) limit(limit)
shouts = [] shouts = []
for row in session.execute(stmt): for row in session.execute(stmt):
shout = row.Shout shout = row.Shout
shout.rating = row.rating shout.rating = row.rating
shout.comments # TODO: shout.comments =
shouts.append(shout) shouts.append(shout)
return shout return shout

View File

@ -83,21 +83,24 @@ type Query {
isEmailFree(email: String!): Result! isEmailFree(email: String!): Result!
signIn(email: String!, password: String): AuthResult! signIn(email: String!, password: String): AuthResult!
signOut: Result! signOut: Result!
# user profile
# profile
getCurrentUser: UserResult! getCurrentUser: UserResult!
getUserById(id: Int!): UserResult! getUserBySlug(slug: String!): UserResult!
# getUserRating(shout: Int): Int! # rateUser(shout: Int): Int!
# messages # messages
getMessages(count: Int = 100, page: Int = 1): [Message!]! getMessages(count: Int = 100, page: Int = 1): [Message!]!
# shouts # shouts
getShoutBySlug(slug: String!): Shout! # NOTE: with .comments: Comments[]
# getShoutRating(shout: Int): Int! # getShoutRating(shout: Int): Int!
# shoutsByAuthor(author: Int): [Shout]! # shoutsByAuthor(author: Int): [Shout]!
# shoutsByReplyTo(shout: Int): [Shout]! # shoutsByReplyTo(shout: Int): [Shout]!
# shoutsByTags(tags: [String]): [Shout]! # shoutsByTags(tags: [String]): [Shout]!
# shoutsByTime(time: DateTime): [Shout]! # shoutsByTime(time: DateTime): [Shout]!
# mainpage
topShoutsByView(limit: Int): [Shout]! topShoutsByView(limit: Int): [Shout]!
topShoutsByRating(limit: Int): [Shout]! topShoutsByRating(limit: Int): [Shout]!
favoritesShouts(limit: Int): [Shout]! favoritesShouts(limit: Int): [Shout]!
@ -184,6 +187,7 @@ type Comment {
id: Int! id: Int!
author: Int! author: Int!
body: String! body: String!
replyTo: Int!
createdAt: DateTime! createdAt: DateTime!
updatedAt: DateTime! updatedAt: DateTime!
shout: Int! shout: Int!
@ -198,29 +202,30 @@ type Comment {
# is publication # is publication
type Shout { type Shout {
id: Int! id: Int!
authors: [Int!]!
slug: String! slug: String!
body: String! body: String!
createdAt: DateTime! createdAt: DateTime!
updatedAt: DateTime! authors: [User!]!
community: Int comments: [Comment]
ratigns: [Rating]
visibleFor: [User]
community: Community
cover: String cover: String
layout: String layout: String
deletedAt: DateTime
deletedBy: Int
rating: Int rating: Int
ratigns: [Rating] views: Int
published: Boolean! replyTo: Shout
publishedAt: DateTime # if there is no published field - it is not published versionOf: Shout
replyTo: Int # another shout
tags: [String] # actual values tags: [String] # actual values
topics: [String] # topic-slugs, order has matter topics: [String] # topic-slugs, order has matter
title: String title: String
subtitle: String subtitle: String
versionOf: String updatedAt: DateTime
visibleForRoles: [String] # role ids are strings updatedBy: Int # can be user id?
visibleForUsers: [Int] deletedAt: DateTime
views: Int deletedBy: Int
publishedBy: Int # if there is no published field - it is not published
publishedAt: DateTime
old_id: String old_id: String
} }