Skip to content

Commit

Permalink
Implemented configurable forcing of tags to lowercase before they are…
Browse files Browse the repository at this point in the history
… saved to the database
  • Loading branch information
jonathan.buchanan committed Aug 20, 2007
1 parent b9d9a96 commit f5ca980
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
17 changes: 17 additions & 0 deletions docs/overview.txt
Expand Up @@ -47,6 +47,23 @@ creates permission objects for all installed apps that need them.
That's it!


Settings
========

Some of django-tagging's behaviour may be configured by adding the
appropriate settings to your project's settings file.

The following settings are available:

FORCE_LOWERCASE_TAGS
--------------------

Default: ``False``

A boolean that turns on/off forcing of tags to lowercase before they
are saved to the database.


Tags
====

Expand Down
8 changes: 6 additions & 2 deletions tagging/fields.py
Expand Up @@ -2,6 +2,7 @@
from django.db.models.fields import CharField
from django.dispatch import dispatcher

from tagging import settings
from tagging.models import Tag
from tagging.validators import isTagList

Expand Down Expand Up @@ -63,7 +64,10 @@ def __set__(self, instance, value):
"""
if instance is None:
raise AttributeError('%s can only be set on instances.' % self.name)
self._set_instance_tag_cache(instance, value)
if settings.FORCE_LOWERCASE_TAGS:
self._set_instance_tag_cache(instance, value.lower())
else:
self._set_instance_tag_cache(instance, value)

def _save(self, signal, sender, instance):
"""
Expand All @@ -87,7 +91,7 @@ def _get_instance_tag_cache(self, instance):

def _set_instance_tag_cache(self, instance, tags):
"""
Helper: set and instance's tag cache.
Helper: set an instance's tag cache.
"""
setattr(instance, '_%s_cache' % self.attname, tags)

Expand Down
3 changes: 3 additions & 0 deletions tagging/managers.py
Expand Up @@ -8,6 +8,7 @@
from django.db.models.query import QuerySet, parse_lookup
from django.contrib.contenttypes.models import ContentType

from tagging import settings
from tagging.utils import calculate_cloud, get_tag_name_list, get_tag_list, LOGARITHMIC

# Python 2.3 compatibility
Expand All @@ -25,6 +26,8 @@ def update_tags(self, obj, tag_names):
current_tags = list(self.filter(items__content_type__pk=ctype.id,
items__object_id=obj._get_pk_val()))
updated_tag_names = set(get_tag_name_list(tag_names))
if settings.FORCE_LOWERCASE_TAGS:
updated_tag_names = [t.lower() for t in updated_tag_names]

TaggedItemModel = self._get_related_model_by_accessor('items')

Expand Down
13 changes: 13 additions & 0 deletions tagging/settings.py
@@ -0,0 +1,13 @@
"""
Convenience module for access of custom tagging application settings,
which enforces default settings when the main settings module which has
been configured does not contain the appropriate settings.
"""
from django.conf import settings

# Whether to force all tags to lowercase before they are saved to the
# database. Default is False.
try:
FORCE_LOWERCASE_TAGS = settings.FORCE_LOWERCASE_TAGS
except AttributeError:
FORCE_LOWERCASE_TAGS = False
17 changes: 17 additions & 0 deletions tagging/tests/tests.py
Expand Up @@ -2,6 +2,7 @@
r"""
>>> import os
>>> from django import newforms as forms
>>> from tagging import settings
>>> from tagging.models import Tag, TaggedItem
>>> from tagging.tests.models import Article, Link, Perch, Parrot, FormTest
>>> from tagging.utils import calculate_cloud, get_tag_name_list, get_tag_list, get_tag, LINEAR
Expand Down Expand Up @@ -217,6 +218,22 @@
>>> Tag.objects.get_for_object(f1)
[]
# Forcing tags to lowercase
>>> settings.FORCE_LOWERCASE_TAGS = True
>>> Tag.objects.update_tags(dead, 'foO bAr Ter')
>>> Tag.objects.get_for_object(dead)
[<Tag: bar>, <Tag: foo>, <Tag: ter>]
>>> Tag.objects.update_tags(dead, 'foO bAr baZ')
>>> Tag.objects.get_for_object(dead)
[<Tag: bar>, <Tag: baz>, <Tag: foo>]
>>> Tag.objects.update_tags(dead, None)
>>> f1.tags = u'TEST5'
>>> f1.save()
>>> Tag.objects.get_for_object(f1)
[<Tag: test5>]
>>> f1.tags
u'test5'
# Retrieving tags by Model ####################################################
>>> Tag.objects.usage_for_model(Parrot)
Expand Down

0 comments on commit f5ca980

Please sign in to comment.