本文整理汇总了Python中taggit.models.TaggedItem类的典型用法代码示例。如果您正苦于以下问题:Python TaggedItem类的具体用法?Python TaggedItem怎么用?Python TaggedItem使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TaggedItem类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_tag_cloud
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)
示例2: _delete_and_recreate_tags
def _delete_and_recreate_tags(slug):
source_demo = Submission.objects.using("default").get(slug=slug)
destination_demo = Submission.objects.using("new").get(slug=slug)
source_type, destination_type = _get_demo_content_types()
source_tags = source_demo.taggit_tags.all()
source_tags_names = [tag.name for tag in source_tags]
destination_tags = destination_demo.taggit_tags.using("new").all()
destination_tags_names = [tag.name for tag in destination_tags]
if source_tags_names == destination_tags_names:
logger.info(
"%s: Found %s matching tag(s): %s" % (source_demo.slug, len(destination_tags), destination_tags_names)
)
return destination_tags
else:
dest_demo_tagged_items = TaggedItem.objects.using("new").filter(
tag__in=[tag for tag in destination_tags], object_id=destination_demo.id, content_type=destination_type
)
dest_demo_tagged_items.using("new").delete()
logger.info("%s: Migrating %s tag(s): %s" % (source_demo.slug, len(source_tags), source_tags_names))
for source_tag in source_tags:
try:
destination_tag = Tag.objects.using("new").get(name=source_tag.name)
except Tag.DoesNotExist:
destination_tag = Tag(name=source_tag.name)
destination_tag.save(using="new")
destination_demo_tag = TaggedItem(
content_type=destination_type, object_id=destination_demo.id, tag=destination_tag
)
destination_demo_tag.save(using="new")
return destination_tags
示例3: entry_tags
def entry_tags(self):
"""Returns a :class:`QuerySet` of :class:`.Tag`\ s that are used on any entries in this blog."""
entry_pks = list(self.entries.values_list('pk', flat=True))
kwargs = {
'%s__object_id__in' % TaggedItem.tag_relname(): entry_pks
}
return TaggedItem.tags_for(BlogEntry).filter(**kwargs)
示例4: get_tags
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)
示例5: tag_cloud
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)
示例6: get_tags
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)
示例7: extract_document
def extract_document(cls, obj_id):
"""Extracts indexable attributes from a Question and its answers."""
# Note: Need to keep this in sync with
# tasks.update_question_vote_chunk.
obj = cls.uncached.values(
'id', 'title', 'content', 'num_answers', 'solution_id',
'is_locked', 'created', 'updated', 'num_votes_past_week',
'creator__username').get(pk=obj_id)
d = {}
d['id'] = obj['id']
d['model'] = cls.get_model_name()
d['title'] = obj['title']
d['question_content'] = obj['content']
d['num_answers'] = obj['num_answers']
d['is_solved'] = bool(obj['solution_id'])
d['is_locked'] = obj['is_locked']
d['has_answers'] = bool(obj['num_answers'])
# We do this because get_absolute_url is an instance method
# and we don't want to create an instance because it's a DB
# hit and expensive. So we do it by hand. get_absolute_url
# doesn't change much, so this is probably ok.
d['url'] = reverse('questions.answers',
kwargs={'question_id': obj['id']})
# TODO: Sphinx stores created and updated as seconds since the
# epoch, so we convert them to that format here so that the
# search view works correctly. When we ditch Sphinx, we should
# see if it's faster to filter on ints or whether we should
# switch them to dates.
d['created'] = int(time.mktime(obj['created'].timetuple()))
d['updated'] = int(time.mktime(obj['updated'].timetuple()))
d['question_creator'] = obj['creator__username']
d['num_votes'] = (QuestionVote.objects
.filter(question=obj['id'])
.count())
d['num_votes_past_week'] = obj['num_votes_past_week']
d['tag'] = list(TaggedItem.tags_for(
Question, Question(pk=obj_id)).values_list('name', flat=True))
answer_values = list(Answer.objects
.filter(question=obj_id)
.values_list('content',
'creator__username'))
d['answer_content'] = [a[0] for a in answer_values]
d['answer_creator'] = list(set([a[1] for a in answer_values]))
if not answer_values:
d['has_helpful'] = False
else:
d['has_helpful'] = Answer.objects.filter(
question=obj_id).filter(votes__helpful=True).exists()
d['indexed_on'] = int(time.time())
return d
示例8: handle
def handle(self, *arg, **kwargs):
print '##########################################################'
print '### This file is generated by ./manage.py dump_topics. ###'
print '##########################################################'
print 'from tower import ugettext as _\n'
for tag in TaggedItem.tags_for(Document):
print '_("""{tag}""", "KB Topic")'.format(tag=tag.name)
示例9: set_taggeditems
def set_taggeditems(self):
objects = OldTaggedItem.objects.all()
print objects.count()
for old_tagitem in objects:
old_id = old_tagitem.tag_id
new_id = self.dupemap.get(old_id, old_id)
new_tags = NewTag.objects.filter(id=new_id)
if new_tags:
new_tag = new_tags[0]
new_tagitem = NewTaggedItem(
id=old_tagitem.id,
tag=new_tag,
content_type=old_tagitem.content_type,
object_id=old_tagitem.object_id,
)
new_tagitem.save()
print NewTaggedItem.objects.count()
示例10: userfeed_tags
def userfeed_tags(user):
'''Return all the UserFeed tags for a user.'''
#ct = ContentType.objects.get_for_model(UserFeed)
kwargs = {
"userfeed__in": UserFeed.objects.filter(user=user)
}
tags = TaggedItem.tag_model().objects.filter(**kwargs).distinct()
return tags
示例11: read
def read(cls, req, slug=None, model=None):
if model:
models = [System, Module, Interface, ArchitecturalPattern]
lookup = dict(zip(map(lambda model: model._meta.object_name.lower(), models), models))
return TaggedItem.tags_for(lookup[model])
if slug:
return map(lambda i: i.content_object, TaggedItem.objects.select_related().filter(tag=Tag.objects.get(slug=slug)))
else:
return map(lambda i: dict(name=i['name'], count=i['count'], resource_uri=reverse('api_tag', args=[i['slug']])), Tag.objects.values('name', 'slug').annotate(count=Count('taggit_taggeditem_items')).order_by('-count'))
示例12: lookups
def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each tuple is the coded value
for the option that will appear in the URL query. The second element is the
human-readable name for the option that will appear in the right sidebar.
"""
list = []
tags = TaggedItem.tags_for(model_admin.model)
for tag in tags:
list.append( (tag.name, (tag.name)) )
return list
示例13: clean
def clean(self):
data = self.cleaned_data
if data['auto']:
tags = set(data['tags'])
tags.update([
tag
for tag in TaggedItem.tags_for(models.Post)
if re.search(r'\b%s\b' % tag.name, data['content'], re.I|re.M)
])
data['tags'] = list(tags)
return data
示例14: lookups
def lookups(self, request, model_admin):
"""
Returns tuple of tuples (key, value) for available Tag choices.
:param request: the Request instance.
:param model_admin: the ModelAdmin instance.
:rtype: tuple.
"""
return (
(tag.id, tag.name)
for tag in TaggedItem.tags_for(Ticket)
)
示例15: forwards
def forwards(self, orm):
for entry in orm['checklists.Entry'].objects.all():
tags = TaggedItem.tags_for(orm['checklists.Entry'], entry)
for label in [unicode(tag) for tag in tags]:
names = {}
# Add localized fields for the SpeciesGroup table
for language_code, language_name in settings.LANGUAGES:
if settings.LANGUAGE_CODE == language_code:
names['name_%s' % language_code] = label.capitalize()
else:
names['name_%s' % language_code] = ''
tag, created = orm['checklists.EntryTag'].objects.get_or_create(
slug=label.lower(),
**names
)
entry.tags.add(tag)