本文整理汇总了Python中tagging.utils.parse_tag_input函数的典型用法代码示例。如果您正苦于以下问题:Python parse_tag_input函数的具体用法?Python parse_tag_input怎么用?Python parse_tag_input使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_tag_input函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_with_simple_space_delimited_tags
def test_with_simple_space_delimited_tags(self):
""" Test with simple space-delimited tags. """
self.assertEquals(parse_tag_input('one'), [u'one'])
self.assertEquals(parse_tag_input('one two'), [u'one', u'two'])
self.assertEquals(parse_tag_input('one two three'), [u'one', u'three', u'two'])
self.assertEquals(parse_tag_input('one one two two'), [u'one', u'two'])
示例2: test_with_simple_space_delimited_tags
def test_with_simple_space_delimited_tags(self):
""" Test with simple space-delimited tags. """
self.assertEquals(parse_tag_input("one"), [u"one"])
self.assertEquals(parse_tag_input("one two"), [u"one", u"two"])
self.assertEquals(parse_tag_input("one two three"), [u"one", u"three", u"two"])
self.assertEquals(parse_tag_input("one one two two"), [u"one", u"two"])
示例3: add_search_results
def add_search_results(self, request, queryset, search_results):
for item in queryset:
meta_content = parse_tag_input(item.tags)
meta_content += parse_tag_input(item.alias)
meta_content += [item.term, item.short_definition]
meta_content = " ".join(meta_content)
#print "meta: %r" % meta_content
search_results.add(
model_instance=item,
# displayed short_definition of the result hit
headline="%s: %s" % (item.term, item.short_definition),
# displayed in the result list
language=item.language,
# Link to the hit
url=item.get_absolute_url(),
# the main content -> result lines would be cut out from hits in this content
content=item.get_search_content(request),
# hits in meta content has a higher score, but the content would not displayed
meta_content=meta_content,
)
示例4: testDoubleQuotedMultipleWords
def testDoubleQuotedMultipleWords(self):
"""A completed quote will trigger this. Unclosed quotes are ignored."""
self.assertEqual([u'one'], parse_tag_input('"one'))
self.assertEqual([u'one', u'two'], parse_tag_input('"one two'))
self.assertEqual([u'one', u'three', u'two'], parse_tag_input('"one two three'))
self.assertEqual([u'one two'], parse_tag_input('"one two"'))
self.assertEqual([u'a-one', u'a-two and a-three'], parse_tag_input('a-one "a-two and a-three"'))
示例5: test_with_comma_delimited_multiple_words
def test_with_comma_delimited_multiple_words(self):
""" Test with comma-delimited multiple words.
An unquoted comma in the input will trigger this. """
self.assertEquals(parse_tag_input(",one"), [u"one"])
self.assertEquals(parse_tag_input(",one two"), [u"one two"])
self.assertEquals(parse_tag_input(",one two three"), [u"one two three"])
self.assertEquals(parse_tag_input("a-one, a-two and a-three"), [u"a-one", u"a-two and a-three"])
示例6: test_with_double_quoted_multiple_words
def test_with_double_quoted_multiple_words(self):
""" Test with double-quoted multiple words.
A completed quote will trigger this. Unclosed quotes are ignored. """
self.assertEquals(parse_tag_input('"one'), [u"one"])
self.assertEquals(parse_tag_input('"one two'), [u"one", u"two"])
self.assertEquals(parse_tag_input('"one two three'), [u"one", u"three", u"two"])
self.assertEquals(parse_tag_input('"one two"'), [u"one two"])
self.assertEquals(parse_tag_input('a-one "a-two and a-three"'), [u"a-one", u"a-two and a-three"])
示例7: test_with_comma_delimited_multiple_words
def test_with_comma_delimited_multiple_words(self):
""" Test with comma-delimited multiple words.
An unquoted comma in the input will trigger this. """
self.assertEquals(parse_tag_input(',one'), [u'one'])
self.assertEquals(parse_tag_input(',one two'), [u'one two'])
self.assertEquals(parse_tag_input(',one two three'), [u'one two three'])
self.assertEquals(parse_tag_input('a-one, a-two and a-three'),
[u'a-one', u'a-two and a-three'])
示例8: pre_save_handler
def pre_save_handler(sender, instance, **kwargs):
"""
Intercept attempts to save and sort the tag field alphabetically, so
we won't have different permutations in the filter list.
"""
taglist = parse_tag_input(instance.tags)
instance.tags = taglist_to_string(taglist)
示例9: update_tags
def update_tags(self, obj, tag_names):
"""
Update tags associated with an object.
"""
ctype = ContentType.objects.get_for_model(obj)
current_tags = list(self.filter(items__content_type__pk=ctype.pk,
items__object_id=obj.pk))
updated_tag_names = parse_tag_input(tag_names)
if settings.FORCE_LOWERCASE_TAGS:
updated_tag_names = [t.lower() for t in updated_tag_names]
# Remove tags which no longer apply
tags_for_removal = [tag for tag in current_tags \
if tag.name not in updated_tag_names]
if len(tags_for_removal):
TaggedItem._default_manager.filter(content_type__pk=ctype.pk,
object_id=obj.pk,
tag__in=tags_for_removal).delete()
# Add new tags
current_tag_names = [tag.name for tag in current_tags]
for tag_name in updated_tag_names:
if tag_name not in current_tag_names:
tag, created = self.get_or_create(name=tag_name)
try:
TaggedItem._default_manager.create(tag=tag, object=obj)
except IntegrityError:
# there is a race condition between querying for current_tags above
# and calling create() here. another process / transaction
# may have already created this tag. This scenario usually occurs
# with two concurrent calls to a django model's save() method
# if the model has a TagField that is being changed. should be
# harmless to pass here since the TaggedItem already exists
pass
示例10: update_tags
def update_tags(self, obj, tag_names, labels=None):
"""
Update tags associated with an object.
"""
ctype = ContentType.objects.get_for_model(obj)
##TODO: This fails silently in non-rel when it should not
#current_tags = list(self.filter(items__content_type__pk=ctype.pk,
# items__object_id=obj.pk))
##This works, divided into two queries
items = TaggedItem.objects.filter(content_type__pk=ctype.pk, object_id=obj.pk)
current_tags = self.filter(pk__in=[item.tag_id for item in items])
updated_tag_names = parse_tag_input(tag_names)
if settings.FORCE_LOWERCASE_TAGS:
updated_tag_names = [t.lower() for t in updated_tag_names]
# Remove tags which no longer apply
tags_for_removal = [tag for tag in current_tags \
if tag.name not in updated_tag_names]
if len(tags_for_removal):
TaggedItem._default_manager.filter(content_type__pk=ctype.pk,
object_id=obj.pk,
tag__in=tags_for_removal).delete()
# Add new tags
current_tag_names = [tag.name for tag in current_tags]
for tag_name in updated_tag_names:
if tag_name not in current_tag_names:
tag, created = self.get_or_create(name=tag_name)
TaggedItem._default_manager.create(tag=tag, object=obj)
示例11: update_tags
def update_tags(self, obj, tag_names, user=None):
"""
Update tags associated with an object.
user - the user adding the new tags
"""
ctype = ContentType.objects.get_for_model(obj)
current_tags = list(self.filter(items__content_type__pk=ctype.pk,
items__object_id=obj.pk))
updated_tag_names = parse_tag_input(tag_names)
if settings.FORCE_LOWERCASE_TAGS:
updated_tag_names = [t.lower() for t in updated_tag_names]
updated_tag_name_slugs = [slugify(t) for t in updated_tag_names]
update_tag_name_dict = dict(zip(updated_tag_name_slugs, updated_tag_names))
# Remove tags which no longer apply
tags_for_removal = [tag for tag in current_tags \
if tag.slug not in updated_tag_name_slugs]
if len(tags_for_removal):
TaggedItem._default_manager.filter(content_type__pk=ctype.pk,
object_id=obj.pk,
tag__in=tags_for_removal).delete()
# Add new tags
current_tag_names = [tag.name for tag in current_tags]
current_tag_name_slugs = [tag.slug for tag in current_tags]
for tag_slug in updated_tag_name_slugs:
if tag_slug not in current_tag_name_slugs:
tag_name = update_tag_name_dict[tag_slug]
tag, created = self.get_or_create(slug=tag_slug, defaults=dict(name=tag_name))
TaggedItem._default_manager.create(tag=tag, object=obj, user=user)
示例12: update_tags
def update_tags(self, obj, tag_names, default_namespace=None, q=None):
"""
Update tags associated with an object.
Accepts a ``default_namespace`` parameter that is assigned to tags
with no namespace specified.
"""
ctype = ContentType.objects.get_for_model(obj)
current_tags = self.filter(items__content_type__pk=ctype.pk,
items__object_id=obj.pk)
if q is not None:
current_tags = current_tags.filter(q)
current_tags = list(current_tags)
updated_tag_names = parse_tag_input(tag_names,
default_namespace=default_namespace)
if conf.FORCE_LOWERCASE_TAGS:
updated_tag_names = [t.lower() for t in updated_tag_names]
# Remove tags which no longer apply
tags_for_removal = [tag for tag in current_tags \
if unicode(tag) not in updated_tag_names]
if len(tags_for_removal):
TaggedItem._default_manager.filter(content_type__pk=ctype.pk,
object_id=obj.pk, tag__in=tags_for_removal).delete()
# Add new tags
current_tag_names = [unicode(tag) for tag in current_tags]
for tag_name in updated_tag_names:
if tag_name not in current_tag_names:
tag, created = self.get_or_create(**get_tag_parts(tag_name))
TaggedItem._default_manager.create(tag=tag, object=obj)
示例13: add_tag
def add_tag(self, obj, tag_name):
"""
Associates the given object with a tag.
"""
tag_names = parse_tag_input(tag_name)
if not len(tag_names):
raise AttributeError(_('No tags were given: "%s".') % tag_name)
if len(tag_names) > 1:
raise AttributeError(_('Multiple tags were given: "%s".') % tag_name)
tag_name = tag_names[0]
if settings.FORCE_LOWERCASE_TAGS:
tag_name = tag_name.lower()
tag, created = self.get_or_create(slug=slugify(tag_name),
defaults={'name': tag_name})
if not created:
# check if there is a preferred synonym for this tag
try:
related_tag = RelatedTag.objects.get(tag=tag, relation_type='=>')
except RelatedTag.DoesNotExist:
pass
else:
# there is a preferred synonym; use it instead of the original
tag = related_tag.related_tag
ctype = ContentType.objects.get_for_model(obj)
TaggedItem._default_manager.get_or_create(
tag=tag, content_type=ctype, object_id=obj.pk)
示例14: add_tag
def add_tag(self, obj, tag_name):
"""
Associates the given object with a tag.
"""
tag_names = parse_tag_input(tag_name)
if not len(tag_names):
raise AttributeError(_('No tags were given: "%s".') % tag_name)
if len(tag_names) > 1:
raise AttributeError(_('Multiple tags were given: "%s".') % tag_name)
tag_name = tag_names[0]
if settings.FORCE_LOWERCASE_TAGS:
tag_name = tag_name.lower()
ctype = ContentType.objects.get_for_model(obj)
###ADDED BY RHIZOME TO DISTINGUISH TYPE
post_content_type, artwork_content_type, member_exhibition_content_type = get_content_types()
if ctype == post_content_type:
tag, created = self.get_or_create(slug=tag_name, type="editorial")
if ctype == artwork_content_type:
tag, created = self.get_or_create(slug=tag_name, type="artbase")
if ctype == member_exhibition_content_type:
tag, created = self.get_or_create(slug=tag_name, type="member_exhibition")
TaggedItem._default_manager.get_or_create(
tag=tag, content_type=ctype, object_id=obj.pk)
示例15: update_tags
def update_tags(self, obj, tag_names):
"""
Update tags associated with an object.
"""
ctype = ContentType.objects.get_for_model(obj)
current_tags = list(self.filter(items__content_type__pk=ctype.pk, items__object_id=obj.pk))
updated_tag_names = parse_tag_input(tag_names)
if settings.FORCE_LOWERCASE_TAGS:
updated_tag_names = [t.lower() for t in updated_tag_names]
# Remove tags which no longer apply
tags_for_removal = [tag for tag in current_tags if tag.name not in updated_tag_names]
if len(tags_for_removal):
TaggedItem._default_manager.filter(
content_type__pk=ctype.pk, object_id=obj.pk, tag__in=tags_for_removal
).delete()
# Add new tags
current_tag_names = [tag.name for tag in current_tags]
for tag_name in updated_tag_names:
if tag_name not in current_tag_names:
tag, created = self.get_or_create(name=tag_name)
try:
TaggedItem._default_manager.create(tag=tag, object=obj)
except:
pass