update topics logix
This commit is contained in:
parent
0d87f52d3a
commit
741ede054d
2
create_crt.sh
Normal file → Executable file
2
create_crt.sh
Normal file → Executable file
|
@ -7,4 +7,4 @@ openssl req -newkey rsa:4096 \
|
||||||
-nodes \
|
-nodes \
|
||||||
-out discours.crt \
|
-out discours.crt \
|
||||||
-keyout discours.key \
|
-keyout discours.key \
|
||||||
-subj "/C=RU/ST=Moscow/L=Moscow/O=Discours/OU=Site/CN=test-api.discours.io"
|
-subj "/C=RU/ST=Moscow/L=Moscow/O=Discours/OU=Site/CN=build.discours.io"
|
||||||
|
|
|
@ -15,8 +15,8 @@ def migrate(entry):
|
||||||
'''
|
'''
|
||||||
topic_dict = {
|
topic_dict = {
|
||||||
'slug': entry['slug'],
|
'slug': entry['slug'],
|
||||||
'createdBy': entry['createdBy'],
|
# 'createdBy': entry['createdBy'],
|
||||||
'createdAt': date_parse(entry['createdAt']),
|
# 'createdAt': date_parse(entry['createdAt']),
|
||||||
'title': entry['title'].lower(),
|
'title': entry['title'].lower(),
|
||||||
'parents': [],
|
'parents': [],
|
||||||
'children': [],
|
'children': [],
|
||||||
|
|
|
@ -21,8 +21,8 @@ def migrate(entry):
|
||||||
ts = datetime.fromtimestamp(entry['createdAt']/1000)
|
ts = datetime.fromtimestamp(entry['createdAt']/1000)
|
||||||
topic_dict = {
|
topic_dict = {
|
||||||
'slug': entry['slug'],
|
'slug': entry['slug'],
|
||||||
'createdBy': entry['createdBy'],
|
# 'createdBy': entry['createdBy'],
|
||||||
'createdAt': ts,
|
# 'createdAt': ts,
|
||||||
'title': entry['title'].lower(),
|
'title': entry['title'].lower(),
|
||||||
'parents': [],
|
'parents': [],
|
||||||
'children': []
|
'children': []
|
||||||
|
|
|
@ -2,14 +2,14 @@ from orm.rbac import Operation, Resource, Permission, Role
|
||||||
from orm.community import Community
|
from orm.community import Community
|
||||||
from orm.user import User, UserRating
|
from orm.user import User, UserRating
|
||||||
from orm.message import Message
|
from orm.message import Message
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic, TopicSubscription
|
||||||
from orm.notification import Notification
|
from orm.notification import Notification
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay,\
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay,\
|
||||||
ShoutRatingStorage, ShoutViewStorage
|
ShoutRatingStorage, ShoutViewStorage
|
||||||
from orm.base import Base, engine, local_session
|
from orm.base import Base, engine, local_session
|
||||||
from orm.comment import Comment, CommentRating
|
from orm.comment import Comment, CommentRating
|
||||||
|
|
||||||
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification", "ShoutRating", "Comment", "CommentRating", "UserRating"]
|
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "TopicSubscription", "Notification", "ShoutRating", "Comment", "CommentRating", "UserRating"]
|
||||||
|
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
Operation.init_table()
|
Operation.init_table()
|
||||||
|
|
10
orm/topic.py
10
orm/topic.py
|
@ -11,13 +11,18 @@ Connection = Table('topic_connections',
|
||||||
UniqueConstraint('parent', 'child', name='unique_usage')
|
UniqueConstraint('parent', 'child', name='unique_usage')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class TopicSubscription(Base):
|
||||||
|
__tablename__ = "topic_subscrptions"
|
||||||
|
|
||||||
|
id = None
|
||||||
|
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
||||||
|
user = Column(ForeignKey('user.id'), primary_key = True)
|
||||||
|
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||||
|
|
||||||
class Topic(Base):
|
class Topic(Base):
|
||||||
__tablename__ = 'topic'
|
__tablename__ = 'topic'
|
||||||
|
|
||||||
slug: str = Column(String, unique = True, nullable = False)
|
slug: str = Column(String, unique = True, nullable = False)
|
||||||
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
|
||||||
createdBy: str = Column(ForeignKey("user.id"), nullable=False, comment="Author")
|
|
||||||
title: str = Column(String, nullable=False, comment="Title")
|
title: str = Column(String, nullable=False, comment="Title")
|
||||||
body: str = Column(String, nullable=True, comment="Body")
|
body: str = Column(String, nullable=True, comment="Body")
|
||||||
pic: str = Column(String, nullable=True, comment="Picture")
|
pic: str = Column(String, nullable=True, comment="Picture")
|
||||||
|
@ -26,4 +31,5 @@ class Topic(Base):
|
||||||
parents = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.parent, secondaryjoin=slug==Connection.c.child, viewonly=True)
|
parents = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.parent, secondaryjoin=slug==Connection.c.child, viewonly=True)
|
||||||
# list of Topics where the current node is the "parent"
|
# list of Topics where the current node is the "parent"
|
||||||
children = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.child, secondaryjoin=slug==Connection.c.parent)
|
children = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.child, secondaryjoin=slug==Connection.c.parent)
|
||||||
|
community = Column(ForeignKey("community.slug"), nullable=True, comment="Community")
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ from resolvers.auth import login, sign_out, 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, get_shout_by_slug
|
from resolvers.zine import create_shout, get_shout_by_slug
|
||||||
from resolvers.profile import get_user_by_slug, get_current_user
|
from resolvers.profile import get_user_by_slug, get_current_user
|
||||||
|
from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, topics_by_community, topics_by_slugs
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"login",
|
"login",
|
||||||
|
@ -17,5 +18,10 @@ __all__ = [
|
||||||
"create_shout",
|
"create_shout",
|
||||||
"get_current_user",
|
"get_current_user",
|
||||||
"get_user_by_slug",
|
"get_user_by_slug",
|
||||||
"get_shout_by_slug"
|
"get_shout_by_slug",
|
||||||
|
"topics_by_slugs",
|
||||||
|
"topics_by_community",
|
||||||
|
"topics_by_author",
|
||||||
|
"topic_subscribe",
|
||||||
|
"topic_unsubscribe"
|
||||||
]
|
]
|
||||||
|
|
45
resolvers/topics.py
Normal file
45
resolvers/topics.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
from orm import Topic, TopicSubscription
|
||||||
|
from orm.base import local_session
|
||||||
|
from resolvers.base import mutation, query, subscription
|
||||||
|
from auth.authenticate import login_required
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
@query.field("topicsBySlugs")
|
||||||
|
async def topics_by_slugs(_, info, slugs):
|
||||||
|
topics = []
|
||||||
|
with local_session() as session:
|
||||||
|
topics = session.query(Topic).filter(Topic.slug in slugs)
|
||||||
|
return topics
|
||||||
|
|
||||||
|
@query.field("topicsByCommunity")
|
||||||
|
async def topics_by_community(_, info, community):
|
||||||
|
topics = []
|
||||||
|
with local_session() as session:
|
||||||
|
topics = session.query(Topic).filter(Topic.community == community)
|
||||||
|
return topics
|
||||||
|
|
||||||
|
@query.field("topicsByAuthor")
|
||||||
|
async def topics_by_author(_, info, author):
|
||||||
|
topics = []
|
||||||
|
with local_session() as session:
|
||||||
|
topics = session.query(Topic).filter(Topic.community == community)
|
||||||
|
return topics
|
||||||
|
|
||||||
|
@mutation.field("topicSubscribe")
|
||||||
|
@login_required
|
||||||
|
async def topic_subscribe(_, info, slug):
|
||||||
|
auth = info.context["request"].auth
|
||||||
|
user_id = auth.user_id
|
||||||
|
sub = TopicSubscription.create({ user: user_id, topic: slug })
|
||||||
|
return {} # type Result
|
||||||
|
|
||||||
|
@mutation.field("topicUnsubscribe")
|
||||||
|
@login_required
|
||||||
|
async def topic_unsubscribe(_, info, slug):
|
||||||
|
auth = info.context["request"].auth
|
||||||
|
user_id = auth.user_id
|
||||||
|
sub = session.query(TopicSubscription).filter(TopicSubscription.user == user_id and TopicSubscription.topic == slug).first()
|
||||||
|
with local_session() as session:
|
||||||
|
session.delete(sub)
|
||||||
|
return {} # type Result
|
||||||
|
return { "error": "session error" }
|
|
@ -75,6 +75,10 @@ type Mutation {
|
||||||
# rateUser(value: Int!): Result!
|
# rateUser(value: Int!): Result!
|
||||||
# updateOnlineStatus: Result!
|
# updateOnlineStatus: Result!
|
||||||
updateProfile(profile: ProfileInput!): Result!
|
updateProfile(profile: ProfileInput!): Result!
|
||||||
|
|
||||||
|
# topics
|
||||||
|
topicSubscribe(slug: String!): Result!
|
||||||
|
topicUnsubscribe(slug: String!): Result!
|
||||||
}
|
}
|
||||||
|
|
||||||
################################### Query
|
################################### Query
|
||||||
|
@ -107,6 +111,11 @@ type Query {
|
||||||
favoritesShouts(limit: Int): [Shout]!
|
favoritesShouts(limit: Int): [Shout]!
|
||||||
topAuthors(limit: Int): [User]!
|
topAuthors(limit: Int): [User]!
|
||||||
|
|
||||||
|
# topics
|
||||||
|
topicsBySlugs(slugs: [String]!): [Topic]!
|
||||||
|
topicsByCommunity(community: String!): [Topic]!
|
||||||
|
topicsByAuthor(author: String!): [Topic]!
|
||||||
|
|
||||||
# getOnlineUsers: [User!]!
|
# getOnlineUsers: [User!]!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,6 +129,7 @@ type Subscription {
|
||||||
onlineUpdated: [User!]!
|
onlineUpdated: [User!]!
|
||||||
shoutUpdated: Shout!
|
shoutUpdated: Shout!
|
||||||
userUpdated: User!
|
userUpdated: User!
|
||||||
|
topicUpdated: Shout!
|
||||||
}
|
}
|
||||||
|
|
||||||
############################################ Entities
|
############################################ Entities
|
||||||
|
@ -253,8 +263,6 @@ type Topic {
|
||||||
title: String
|
title: String
|
||||||
body: String
|
body: String
|
||||||
pic: String
|
pic: String
|
||||||
createdBy: Int! # User
|
|
||||||
createdAt: DateTime!
|
|
||||||
parents: [String] # NOTE: topic can have parent topics
|
parents: [String] # NOTE: topic can have parent topics
|
||||||
children: [String] # and children
|
children: [String] # and children
|
||||||
cat_id: String
|
cat_id: String
|
||||||
|
|
|
@ -5,7 +5,8 @@ import sys
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
dev_mode = len(sys.argv) > 1 and sys.argv[1] == "dev"
|
dev_mode = len(sys.argv) > 1 and sys.argv[1] == "dev"
|
||||||
if dev_mode :
|
if dev_mode:
|
||||||
|
print("DEV MODE")
|
||||||
uvicorn.run("main:app", host="0.0.0.0", port=8080, ssl_keyfile="discours.key", ssl_certfile="discours.crt", reload=True)
|
uvicorn.run("main:app", host="0.0.0.0", port=8080, ssl_keyfile="discours.key", ssl_certfile="discours.crt", reload=True)
|
||||||
else :
|
else :
|
||||||
uvicorn.run("main:app", host="0.0.0.0", port=PORT)
|
uvicorn.run("main:app", host="0.0.0.0", port=PORT)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user