Last active
July 10, 2018 06:23
-
-
Save monokrome/e12c884a019a3d8b3ed5156c250db103 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typing | |
from django.db.models import Manager | |
from django.db.models.query import QuerySet | |
class TopicManager(Manager): | |
def having_names(self, names: str, *, create_missing: bool = False, **create_kwargs: typing.Dict[str, typing.Any]) -> QuerySet: | |
if not len(names): | |
return self.none() | |
topic_names = set(names.lower().split(',')) | |
# TODO: Support use directly as QuerySet | |
related_topics = self.all() | |
for name in topic_names: | |
related_topics |= related_topics.filter(name__iexact=name) | |
if create_missing and len(related_topics) != len(topic_names): | |
missing_topics = topic_names.difference([topic.name for topic in related_topics]) | |
topics_to_create = [] | |
for name in missing_topics: | |
topic = self.model(name=name, **create_kwargs) | |
topic.prepare_for_save() | |
topics_to_create.append(topic) | |
related_topics |= self.filter(pk__in=[ | |
topic.pk | |
for topic in self.bulk_create(topics_to_create) | |
]) | |
return related_topics |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment