core/services/zine/topics.py

68 lines
1.9 KiB
Python
Raw Normal View History

import asyncio
from orm.topic import Topic
class TopicStorage:
2022-09-03 10:50:14 +00:00
topics = {}
lock = asyncio.Lock()
@staticmethod
def init(session):
self = TopicStorage
topics = session.query(Topic)
self.topics = dict([(topic.slug, topic) for topic in topics])
2022-09-07 16:19:06 +00:00
for tpc in self.topics.values():
self.load_parents(tpc)
2022-09-03 10:50:14 +00:00
print("[zine.topics] %d precached" % len(self.topics.keys()))
@staticmethod
def load_parents(topic):
self = TopicStorage
parents = []
for parent in self.topics.values():
if topic.slug in parent.children:
parents.append(parent.slug)
topic.parents = parents
return topic
@staticmethod
2022-09-05 13:28:21 +00:00
async def get_topics_all():
2022-09-03 10:50:14 +00:00
self = TopicStorage
async with self.lock:
2022-09-05 13:28:21 +00:00
return list(self.topics.values())
2022-09-03 10:50:14 +00:00
@staticmethod
async def get_topics_by_slugs(slugs):
self = TopicStorage
async with self.lock:
if not slugs:
return self.topics.values()
topics = filter(lambda topic: topic.slug in slugs, self.topics.values())
return list(topics)
@staticmethod
async def get_topics_by_community(community):
self = TopicStorage
async with self.lock:
topics = filter(
lambda topic: topic.community == community, self.topics.values()
)
return list(topics)
@staticmethod
2022-09-07 16:19:06 +00:00
async def get_topics_by_author(author):
self = TopicStorage
async with self.lock:
topics = filter(
lambda topic: topic.community == author, self.topics.values()
)
return list(topics)
@staticmethod
async def update_topic(topic):
2022-09-03 10:50:14 +00:00
self = TopicStorage
async with self.lock:
self.topics[topic.slug] = topic
self.load_parents(topic)