def get_tags(self, entries=None, language=None): """Returns tags used to tag post and its count. Results are ordered by count.""" if not entries: entries = self if language: entries = entries.filter_by_language(language) entries = entries.distinct() if not entries: return [] kwargs = TaggedItem.bulk_lookup_kwargs(entries) # aggregate and sort counted_tags = dict(TaggedItem.objects .filter(**kwargs) .values('tag') .annotate(count=models.Count('tag')) .values_list('tag', 'count')) # and finally get the results tags = Tag.objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.count = counted_tags[tag.pk] return sorted(tags, key=lambda x: -x.count)
def get_tag_cloud(model, queryset, tags_filter=None): if tags_filter is None: tags_filter = set() for item in queryset.all(): tags_filter.update(item.tags.all()) tags_filter = set([tag.id for tag in tags_filter]) tags = set( TaggedItem.objects.filter( content_type__model=model.__name__.lower()).values_list('tag_id', flat=True)) if tags_filter is not None: tags = tags.intersection(tags_filter) tag_ids = list(tags) kwargs = TaggedItem.bulk_lookup_kwargs(queryset) kwargs['tag_id__in'] = tag_ids counted_tags = dict( TaggedItem.objects.filter(**kwargs).values('tag').annotate( count=models.Count('tag')).values_list('tag', 'count')) tags = TaggedItem.tag_model().objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.count = counted_tags[tag.pk] return sorted(tags, key=lambda x: -x.count)
def get_tags(self, request, namespace): """ Get tags with articles count for given namespace string. Return list of Tag objects ordered by custom 'num_articles' attribute. """ if (request and hasattr(request, 'toolbar') and request.toolbar and request.toolbar.edit_mode): articles = self.namespace(namespace) else: articles = self.published().namespace(namespace) if not articles: # return empty iterable early not to perform useless requests return [] kwargs = TaggedItem.bulk_lookup_kwargs(articles) # aggregate and sort counted_tags = dict( TaggedItem.objects.filter(**kwargs).values('tag').annotate( tag_count=models.Count('tag')).values_list('tag', 'tag_count')) # and finally get the results tags = Tag.objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.num_articles = counted_tags[tag.pk] return sorted(tags, key=attrgetter('num_articles'), reverse=True)
def get_tag_cloud(model, queryset, tags_filter=None): if tags_filter is None: tags_filter = set() for item in queryset.all(): tags_filter.update(item.tags.all()) tags_filter = set([tag.id for tag in tags_filter]) tags = set(TaggedItem.objects.filter( content_type__model=model.__name__.lower() ).values_list('tag_id', flat=True)) if tags_filter is not None: tags = tags.intersection(tags_filter) tag_ids = list(tags) kwargs = TaggedItem.bulk_lookup_kwargs(queryset) kwargs['tag_id__in'] = tag_ids counted_tags = dict(TaggedItem.objects .filter(**kwargs) .values('tag') .annotate(count=models.Count('tag')) .values_list('tag', 'count')) tags = TaggedItem.tag_model().objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.count = counted_tags[tag.pk] return sorted(tags, key=lambda x: -x.count)
def get_tags(self, request, namespace): """ Get tags with articles count for given namespace string. Return list of Tag objects ordered by custom 'num_articles' attribute. """ if (request and hasattr(request, 'toolbar') and request.toolbar and request.toolbar.edit_mode): articles = self.namespace(namespace) else: articles = self.published().namespace(namespace) if not articles: # return empty iterable early not to perform useless requests return [] kwargs = TaggedItem.bulk_lookup_kwargs(articles) # aggregate and sort counted_tags = dict(TaggedItem.objects .filter(**kwargs) .values('tag') .annotate(tag_count=models.Count('tag')) .values_list('tag', 'tag_count')) # and finally get the results tags = Tag.objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.num_articles = counted_tags[tag.pk] return sorted(tags, key=attrgetter('num_articles'), reverse=True)
def tag_cloud(self, other_model=None, queryset=None, published=True): from taggit.models import TaggedItem tag_ids = self._taglist(other_model, queryset) kwargs = {} if published: kwargs = TaggedItem.bulk_lookup_kwargs(self.model.objects.published()) kwargs['tag_id__in'] = tag_ids counted_tags = dict(TaggedItem.objects .filter(**kwargs) .values('tag') .annotate(count=models.Count('tag')) .values_list('tag', 'count')) tags = TaggedItem.tag_model().objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.count = counted_tags[tag.pk] return sorted(tags, key=lambda x: -x.count)
def get_tags(self, language): """Returns tags used to tag post and its count. Results are ordered by count.""" # get tagged post entries = self.filter_by_language(language).distinct() if not entries: return [] kwargs = TaggedItem.bulk_lookup_kwargs(entries) # aggregate and sort counted_tags = dict( TaggedItem.objects.filter(**kwargs).values('tag').annotate( count=models.Count('tag')).values_list('tag', 'count')) # and finally get the results tags = Tag.objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.count = counted_tags[tag.pk] return sorted(tags, key=lambda x: -x.count)
def get_tags(self, namespace): """ Get tags with articles count for given namespace string. Returns list of Tag objects with ordered by custom 'num_entries' attribute. """ entries = self.filter(app_config__namespace=namespace) if not entries: return [] kwargs = TaggedItem.bulk_lookup_kwargs(entries) # aggregate and sort counted_tags = dict( TaggedItem.objects.filter(**kwargs).values('tag').annotate( tag_count=models.Count('tag')).values_list('tag', 'tag_count')) # and finally get the results tags = Tag.objects.filter(pk__in=counted_tags.keys()) for tag in tags: tag.num_entries = counted_tags[tag.pk] return sorted(tags, key=attrgetter('num_entries'), reverse=True)