Python taggit.models.Tag() Examples
The following are 30
code examples of taggit.models.Tag().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
taggit.models
, or try the search function
.
Example #1
Source File: widgets.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_context(self, name, value, attrs): context = super().get_context(name, value, attrs) if self.tag_model == Tag: autocomplete_url = reverse('wagtailadmin_tag_autocomplete') else: autocomplete_url = reverse( 'wagtailadmin_tag_model_autocomplete', args=(self.tag_model._meta.app_label, self.tag_model._meta.model_name) ) if self.free_tagging is None: free_tagging = getattr(self.tag_model, 'free_tagging', True) else: free_tagging = self.free_tagging context['widget']['autocomplete_url'] = autocomplete_url context['widget']['options_json'] = json.dumps({ 'allowSpaces': getattr(settings, 'TAG_SPACES_ALLOWED', True), 'tagLimit': getattr(settings, 'TAG_LIMIT', None), 'autocompleteOnly': not free_tagging, }) return context
Example #2
Source File: test_tag.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_create_with_tags_with_plain_taggable_manager(self): class PlaceForm(ClusterForm): class Meta: model = NonClusterPlace exclude_formsets = ['tagged_items', 'reviews'] fields = ['name', 'tags'] form = PlaceForm({ 'name': "Mission Burrito", 'tags': "burrito, fajita" }, instance=NonClusterPlace()) self.assertTrue(form.is_valid()) mission_burrito = form.save() reloaded_mission_burrito = NonClusterPlace.objects.get(pk=mission_burrito.pk) self.assertEqual( set(reloaded_mission_burrito.tags.all()), set([Tag.objects.get(name='burrito'), Tag.objects.get(name='fajita')]) )
Example #3
Source File: test_tag.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_create_with_tags(self): class PlaceForm(ClusterForm): class Meta: model = Place exclude_formsets = ['tagged_items', 'reviews'] fields = ['name', 'tags'] form = PlaceForm({ 'name': "Mission Burrito", 'tags': "burrito, fajita" }, instance=Place()) self.assertTrue(form.is_valid()) mission_burrito = form.save() reloaded_mission_burrito = Place.objects.get(pk=mission_burrito.pk) self.assertEqual( set(reloaded_mission_burrito.tags.all()), set([Tag.objects.get(name='burrito'), Tag.objects.get(name='fajita')]) )
Example #4
Source File: test_tag.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_tag_form_field(self): class PlaceForm(ClusterForm): class Meta: model = Place exclude_formsets = ['tagged_items', 'reviews'] fields = ['name', 'tags'] mission_burrito = Place(name='Mission Burrito') mission_burrito.tags.add('mexican', 'burrito') form = PlaceForm(instance=mission_burrito) self.assertEqual(2, len(form['tags'].value())) expected_instance = TaggedPlace if TAGGIT_VERSION < (1,) else Tag self.assertEqual(expected_instance, form['tags'].value()[0].__class__) form = PlaceForm({ 'name': "Mission Burrito", 'tags': "burrito, fajita" }, instance=mission_burrito) self.assertTrue(form.is_valid()) mission_burrito = form.save(commit=False) self.assertTrue(Tag.objects.get(name='burrito') in mission_burrito.tags.all()) self.assertTrue(Tag.objects.get(name='fajita') in mission_burrito.tags.all()) self.assertFalse(Tag.objects.get(name='mexican') in mission_burrito.tags.all())
Example #5
Source File: views.py From Django-2-by-Example with MIT License | 6 votes |
def post_list(request, tag_slug=None): object_list = Post.published.all() tag = None if tag_slug: tag = get_object_or_404(Tag, slug=tag_slug) object_list = object_list.filter(tags__in=[tag]) paginator = Paginator(object_list, 3) # 3 posts in each page page = request.GET.get('page') try: posts = paginator.page(page) except PageNotAnInteger: # If page is not an integer deliver the first page posts = paginator.page(1) except EmptyPage: # If page is out of range deliver last page of results posts = paginator.page(paginator.num_pages) return render(request, 'blog/post/list.html', {'page': page, 'posts': posts, 'tag': tag})
Example #6
Source File: views.py From Django-2-by-Example with MIT License | 6 votes |
def post_list(request, tag_slug=None): object_list = Post.published.all() tag = None if tag_slug: tag = get_object_or_404(Tag, slug=tag_slug) object_list = object_list.filter(tags__in=[tag]) paginator = Paginator(object_list, 3) # 3 posts in each page page = request.GET.get('page') try: posts = paginator.page(page) except PageNotAnInteger: # If page is not an integer deliver the first page posts = paginator.page(1) except EmptyPage: # If page is out of range deliver last page of results posts = paginator.page(paginator.num_pages) return render(request, 'blog/post/list.html', {'page': page, 'posts': posts, 'tag': tag})
Example #7
Source File: views.py From Django-3-by-Example with MIT License | 6 votes |
def post_list(request, tag_slug=None): object_list = Post.published.all() tag = None if tag_slug: tag = get_object_or_404(Tag, slug=tag_slug) object_list = object_list.filter(tags__in=[tag]) paginator = Paginator(object_list, 3) # 3 posts in each page page = request.GET.get('page') try: posts = paginator.page(page) except PageNotAnInteger: # If page is not an integer deliver the first page posts = paginator.page(1) except EmptyPage: # If page is out of range deliver last page of results posts = paginator.page(paginator.num_pages) return render(request, 'blog/post/list.html', {'page': page, 'posts': posts, 'tag': tag})
Example #8
Source File: views.py From Django-3-by-Example with MIT License | 6 votes |
def post_list(request, tag_slug=None): object_list = Post.published.all() tag = None if tag_slug: tag = get_object_or_404(Tag, slug=tag_slug) object_list = object_list.filter(tags__in=[tag]) paginator = Paginator(object_list, 3) # 3 posts in each page page = request.GET.get('page') try: posts = paginator.page(page) except PageNotAnInteger: # If page is not an integer deliver the first page posts = paginator.page(1) except EmptyPage: # If page is out of range deliver last page of results posts = paginator.page(paginator.num_pages) return render(request, 'blog/post/list.html', {'page': page, 'posts': posts, 'tag': tag})
Example #9
Source File: tests.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_tags_autocomplete_custom_model(self): response = self.client.get( reverse('wagtailadmin_tag_model_autocomplete', args=('tests', 'restauranttag')), {'term': 'ital'} ) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/json') data = json.loads(response.content.decode('utf-8')) self.assertEqual(data, ['Italian']) # should not return results from the standard Tag model response = self.client.get( reverse('wagtailadmin_tag_model_autocomplete', args=('tests', 'restauranttag')), {'term': 'test'} ) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/json') data = json.loads(response.content.decode('utf-8')) self.assertEqual(data, [])
Example #10
Source File: test_widgets.py From django-taggit-labels with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialization(self): customized_widget = LabelWidget(model=MyCustomTag) self.assertEqual(customized_widget.model, MyCustomTag) default_widget = LabelWidget() self.assertEqual(default_widget.model, Tag)
Example #11
Source File: tests.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): self.login() Tag.objects.create(name="Test", slug="test") RestaurantTag.objects.create(name="Italian", slug="italian") RestaurantTag.objects.create(name="Indian", slug="indian")
Example #12
Source File: test_widgets.py From django-taggit-labels with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): Tag.objects.create(name="Python", slug="python") Tag.objects.create(name="Django", slug="django") Tag.objects.create(name="Advanced Computering", slug="advanced-computering") MyCustomTag.objects.create(name="Coffee", slug="coffee") MyCustomTag.objects.create(name="tea", slug="tea") MyCustomTag.objects.create(name=u"À bientôt", slug="a-bientot") self.article = Content.objects.create(title="My test") self.article.tags.add("Python") self.post = MyContent.objects.create(title="My test") self.post.tags.add("Coffee")
Example #13
Source File: load_state_tags.py From connect with MIT License | 5 votes |
def handle(self, *args, **options): """Handle command.""" tags = [] for state in STATES: tags.append(Tag(name=state, slug=state)) Tag.objects.bulk_create(tags)
Example #14
Source File: test_widgets.py From django-taggit-labels with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_new(self): """Render method shouldn't error out with missing or string tags""" widget = LabelWidget(model=MyCustomTag) widget.render("tags", None, attrs={"id": u"id_tags"}) widget.render("tags", "'My Tag', 'Another tag'", attrs={"id": u"id_tags"})
Example #15
Source File: mock.py From intake with MIT License | 5 votes |
def make_tag(name="example"): tag = Tag(name=name) tag.save() return tag
Example #16
Source File: signals.py From kpi with GNU Affero General Public License v3.0 | 5 votes |
def tag_uid_post_save(sender, instance, created, raw, **kwargs): """ Make sure we have a TagUid object for each newly-created Tag """ if raw or not created: return TagUid.objects.get_or_create(tag=instance)
Example #17
Source File: test_tags.py From kpi with GNU Affero General Public License v3.0 | 5 votes |
def test_query_tags(self): TAG_NAME = 'Some-Tag' self.assertEqual(Tag.objects.count(), 0) self.coll.tags.add(TAG_NAME) self.assertEqual(Tag.objects.count(), 1)
Example #18
Source File: test_tags.py From kpi with GNU Affero General Public License v3.0 | 5 votes |
def test_can_query_all_assets_by_tag(self): TAG_NAME = 'Some-Asset-Tag' self.sa.tags.add(TAG_NAME) tag_obj = Tag.objects.get(name=TAG_NAME) tagged_survey_items = Asset.objects.filter(tags=tag_obj) self.assertEqual(tagged_survey_items.count(), 1) # alternative method to query by tag string self.assertEqual(Asset.objects.filter_by_tag_name(TAG_NAME).count(), 1)
Example #19
Source File: test_tags.py From kpi with GNU Affero General Public License v3.0 | 5 votes |
def test_can_query_all_collections_by_tag(self): TAG_NAME = 'Some-Collection-Tag' self.coll.tags.add(TAG_NAME) tag_obj = Tag.objects.get(name=TAG_NAME) tagged_collections = Collection.objects.filter(tags=tag_obj) self.assertEqual(tagged_collections.count(), 1) # alternative method to query by tag string self.assertEqual(Collection.objects.filter_by_tag_name(TAG_NAME).count(), 1)
Example #20
Source File: tags.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def add_arguments(self, parser): super().add_arguments(parser) count = self.subs.add_parser('count', help='Count tag usage') count.add_argument('name', help='Tag name we want to count.') count.set_defaults(func=self.count) delete = self.subs.add_parser('delete', help='Delete tag') delete.add_argument('name', help='Tag name we want to delete.') delete.add_argument('--force', action='store_true', help='Force delete even if tag is still used.') delete.set_defaults(func=self.delete) rename = self.subs.add_parser('rename', help='Rename a tag') rename.add_argument('old', help='Old name.') rename.add_argument('new', help='New name.') rename.set_defaults(func=self.rename) replace = self.subs.add_parser('replace', help='Replace tag by another and delete it') replace.add_argument('old', help='Old tag name.') replace.add_argument('new', help='New tag name.') replace.set_defaults(func=self.replace) sanitize = self.subs.add_parser('sanitize', help=('Sanitize existing tags.\n' 'Remove duplicates, clean characters...')) sanitize.set_defaults(func=self.sanitize) list_ = self.subs.add_parser('list', help='List tags') list_.set_defaults(func=self.list)
Example #21
Source File: widgets.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, *args, **kwargs): self.tag_model = kwargs.pop('tag_model', Tag) # free_tagging = None means defer to the tag model's setting self.free_tagging = kwargs.pop('free_tagging', None) super().__init__(*args, **kwargs)
Example #22
Source File: test_tag.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_can_access_tags_on_unsaved_instance(self): mission_burrito = Place(name='Mission Burrito') self.assertEqual(0, mission_burrito.tags.count()) mission_burrito.tags.add('mexican', 'burrito') self.assertEqual(2, mission_burrito.tags.count()) self.assertEqual(Tag, mission_burrito.tags.all()[0].__class__) self.assertTrue([tag for tag in mission_burrito.tags.all() if tag.name == 'mexican']) mission_burrito.save() self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count()) mission_burrito.tags.remove('burrito') self.assertEqual(1, mission_burrito.tags.count()) # should not affect database until we save self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count()) mission_burrito.save() self.assertEqual(1, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count()) mission_burrito.tags.clear() self.assertEqual(0, mission_burrito.tags.count()) # should not affect database until we save self.assertEqual(1, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count()) mission_burrito.save() self.assertEqual(0, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count()) mission_burrito.tags.set('mexican', 'burrito') self.assertEqual(2, mission_burrito.tags.count()) self.assertEqual(0, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count()) mission_burrito.save() self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
Example #23
Source File: test_tags_command.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def test_sanitize_tags(): foo = Tag.objects.create(name='foo') Foo = Tag.objects.create(name='Foo') Bar = Tag.objects.create(name='Bar') # Create a tag with upper case first. bar = Tag.objects.create(name='bar') bar_ = Tag.objects.create(name='bar;') Bar_ = Tag.objects.create(name='Bar;') tag_to_delete = Tag.objects.create(name=':') clean = Tag.objects.create(name="Other:") half_clean1 = Tag.objects.create(name="Other:Foo,") half_clean2 = Tag.objects.create(name="Other:foo") doc1 = DocumentFactory(tags=[foo, bar, clean]) doc2 = DocumentFactory(tags=[foo, Bar, clean, half_clean2]) doc3 = DocumentFactory(tags=[Foo, bar]) doc4 = DocumentFactory(tags=[Foo, Bar_, half_clean1, half_clean2]) doc5 = DocumentFactory(tags=[Foo, foo, bar_, Bar]) doc6 = DocumentFactory(tags=[Foo, foo, Bar_, tag_to_delete]) call_command('tags', 'sanitize') all_tag_names = list(Tag.objects.all().order_by('name') .values_list('name', flat=True)) assert all_tag_names == ['bar', 'foo', 'other', 'other:foo'] assert sorted(doc1.tags.names()) == ['bar', 'foo', 'other'] assert sorted(doc2.tags.names()) == ['bar', 'foo', 'other', 'other:foo'] assert sorted(doc3.tags.names()) == ['bar', 'foo'] assert sorted(doc4.tags.names()) == ['bar', 'foo', 'other:foo'] assert sorted(doc5.tags.names()) == ['bar', 'foo'] assert sorted(doc6.tags.names()) == ['bar', 'foo']
Example #24
Source File: test_tags_command.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def test_list_should_list_tags_and_slugs(capsys): tag = Tag.objects.create(name='Some Tag') call_command('tags', 'list',) out, err = capsys.readouterr() assert tag.name in out assert tag.slug in out
Example #25
Source File: test_tags_command.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def test_delete_should_delete_tag(): tag1 = Tag.objects.create(name='tag1') call_command('tags', 'delete', 'tag1') assert not Tag.objects.filter(name='tag1') assert not Tag.objects.filter(pk=tag1.pk)
Example #26
Source File: test_tags_command.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def test_replace_should_replace_and_delete_tag(): tag1 = Tag.objects.create(name='tag1') tag2 = Tag.objects.create(name='tag2') doc = DocumentFactory(tags=[tag1]) call_command('tags', 'replace', 'tag1', 'tag2') assert not Tag.objects.filter(name='tag1') assert tag1 not in doc.tags.all() assert tag2 in doc.tags.all() assert tag1.id != tag2.id
Example #27
Source File: test_tags_command.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def test_rename_should_exit_if_new_name_already_exists(): DocumentFactory(tags=['tag1']) Tag.objects.create(name='tag2') with pytest.raises(SystemExit): call_command('tags', 'rename', 'tag1', 'tag2') assert Tag.objects.filter(name='tag1') assert Tag.objects.filter(name='tag2')
Example #28
Source File: test_tags_command.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def test_rename_should_exit_on_non_existing_tag(): DocumentFactory(tags=['tag1']) with pytest.raises(SystemExit): call_command('tags', 'rename', 'tag3', 'tag2') assert Tag.objects.filter(name='tag1')
Example #29
Source File: test_tags_command.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def test_rename_should_rename_tag(): doc = DocumentFactory(tags=['tag1']) tag1 = Tag.objects.get(name='tag1') call_command('tags', 'rename', 'tag1', 'tag2') assert not Tag.objects.filter(name='tag1') assert 'tag2' in doc.tags.names() assert 'tag1' not in doc.tags.names() assert tag1.id == Tag.objects.get(name='tag2').id
Example #30
Source File: tags.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def replace(self, options): if options['old'] == options['new']: exit('Nothing to rename, tags are equal.') old = self.get_tag_or_exit(options['old']) new, created = Tag.objects.get_or_create(name=options['new']) if created: notice('Created tag "{new}"'.format(**options)) relations = TaggedItem.objects.filter(tag=old) for relation in relations: content = relation.content_object notice('Processing "{}"'.format(repr(content))) relation.delete() content.tags.add(new) old.delete() notice('Deleted "{}"'.format(old))