From 67dec73208b81ce51ca30c7bd7f192b59dbc8c03 Mon Sep 17 00:00:00 2001 From: nsantacruz Date: Thu, 14 Nov 2024 14:55:21 +0200 Subject: [PATCH] feat(topics): add utility funcs to topic model --- topics/models/topic.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/topics/models/topic.py b/topics/models/topic.py index 6bba6523d4..9613518ace 100644 --- a/topics/models/topic.py +++ b/topics/models/topic.py @@ -1,12 +1,32 @@ from django.db import models +import random from topics.models.pool import TopicPool +class TopicManager(models.Manager): + def sample_topic_slugs(self, order, pool: str = None, limit=10) -> list[str]: + if pool: + topics = self.get_topic_slugs_by_pool(pool) + else: + topics = self.all().values_list('slug', flat=True) + if order == 'random': + return random.sample(list(topics), min(limit, len(topics))) + else: + raise Exception("Invalid order: '{}'".format(order)) + + def get_pools_by_topic_slug(self, topic_slug: str) -> list[str]: + return self.filter(topic_slug=topic_slug).values_list("pools__name", flat=True) + + def get_topic_slugs_by_pool(self, pool: str) -> list[str]: + return self.filter(pools__name=pool).values_list("slug", flat=True) + + class Topic(models.Model): slug = models.CharField(max_length=255, unique=True) en_title = models.CharField(max_length=255, blank=True, default="") he_title = models.CharField(max_length=255, blank=True, default="") - pools = models.ManyToManyField(TopicPool, related_name="topics") + pools = models.ManyToManyField(TopicPool, related_name="topics", blank=True) + objects = TopicManager() def __str__(self): return f"Topic('{self.slug}')"