Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Jul 4, 2015
2 parents 6884ed8 + 67aa662 commit 5c83497
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
Changelog
=========

0.16.0 (07.04.2015)
~~~~~~~~~~~~~~~~~~~
* Add option to allow case-insensitive tags
* https://github.com/alex/django-taggit/pull/325

0.15.0 (06.23.2015)
~~~~~~~~~~~~~~~~~~~
* Fix wrong slugs for non-latin chars
* Only works if optional GPL dependency (unidecode) is installed
* https://github.com/alex/django-taggit/pull/315
* https://github.com/alex/django-taggit/pull/273
* https://github.com/alex/django-taggit/pull/315
* https://github.com/alex/django-taggit/pull/273

0.14.0 (04.26.2015)
~~~~~~~~~~~~~~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions docs/getting_started.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ And then to any model you want tagging on do the following::
# ... fields here

tags = TaggableManager()

.. note::

If you want ``django-taggit`` to be **CASE INSENSITIVE** when looking up existing tags, you'll have to set to ``True`` the TAGGIT_CASE_INSENSITIVE setting (by default ``False``)::

TAGGIT_CASE_INSENSITIVE = True

2 changes: 1 addition & 1 deletion taggit/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = (0, 15, 0)
VERSION = (0, 16, 0)
29 changes: 23 additions & 6 deletions taggit/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django import VERSION
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.db import models, router
from django.db.models.fields import Field
from django.db.models.fields.related import (add_lazy_relation, ManyToManyRel,
Expand Down Expand Up @@ -158,14 +159,30 @@ def add(self, *tags):
raise ValueError("Cannot add {0} ({1}). Expected {2} or str.".format(
t, type(t), type(self.through.tag_model())))

# If str_tags has 0 elements Django actually optimizes that to not do a
# query. Malcolm is very smart.
existing = self.through.tag_model().objects.filter(
name__in=str_tags
)
if getattr(settings, 'TAGGIT_CASE_INSENSITIVE', False):
# Some databases can do case-insensitive comparison with IN, which
# would be faster, but we can't rely on it or easily detect it.
existing = []
tags_to_create = []

for name in str_tags:
try:
tag = self.through.tag_model().objects.get(name__iexact=name)
existing.append(tag)
except self.through.tag_model().DoesNotExist:
tags_to_create.append(name)
else:
# If str_tags has 0 elements Django actually optimizes that to not do a
# query. Malcolm is very smart.
existing = self.through.tag_model().objects.filter(
name__in=str_tags
)

tags_to_create = str_tags - set(t.name for t in existing)

tag_objs.update(existing)

for new_tag in str_tags - set(t.name for t in existing):
for new_tag in tags_to_create:
tag_objs.add(self.through.tag_model().objects.create(name=new_tag))

for tag in tag_objs:
Expand Down
8 changes: 8 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.db import connection
from django.test import TestCase, TransactionTestCase
from django.test.utils import override_settings
from django.utils.encoding import force_text

from .forms import CustomPKFoodForm, DirectFoodForm, FoodForm, OfficialFoodForm
Expand Down Expand Up @@ -389,6 +390,13 @@ def test_prefetch_no_extra_join(self):
join_clause = 'INNER JOIN "%s"' % self.taggeditem_model._meta.db_table
self.assertEqual(connection.queries[-1]['sql'].count(join_clause), 1, connection.queries[-2:])

@override_settings(TAGGIT_CASE_INSENSITIVE=True)
def test_with_case_insensitive_option(self):
spain = self.tag_model.objects.create(name="Spain", slug="spain")
orange = self.food_model.objects.create(name="orange")
orange.tags.add('spain')
self.assertEqual(list(orange.tags.all()), [spain])


class TaggableManagerDirectTestCase(TaggableManagerTestCase):
food_model = DirectFood
Expand Down

0 comments on commit 5c83497

Please sign in to comment.