Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #95 -- don't stomp over attributes/fields of a model in tagging…
….register
- Loading branch information
brosner
committed
Jan 22, 2010
1 parent
2f94ef7
commit 8b4e635
Showing
1 changed file
with
27 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
from django.utils.translation import ugettext as _ | ||
|
||
from tagging.managers import ModelTaggedItemManager, TagDescriptor | ||
|
||
|
||
|
||
VERSION = (0, 4, 'pre') | ||
|
||
|
||
|
||
class AlreadyRegistered(Exception): | ||
""" | ||
An attempt was made to register a model more than once. | ||
""" | ||
pass | ||
|
||
|
||
registry = [] | ||
|
||
|
||
def register(model, tag_descriptor_attr='tags', | ||
tagged_item_manager_attr='tagged'): | ||
""" | ||
Sets the given model class up for working with tags. | ||
""" | ||
|
||
if model in registry: | ||
raise AlreadyRegistered( | ||
_('The model %s has already been registered.') % model.__name__) | ||
registry.append(model) | ||
raise AlreadyRegistered("The model '%s' has already been " | ||
"registered." % model._meta.object_name) | ||
if hasattr(model, tag_descriptor_attr): | ||
raise AttributeError("'%s' already has an attribute '%s'. You must " | ||
"provide a custom tag_descriptor_attr to register." % ( | ||
model._meta.object_name, | ||
tag_descriptor_attr, | ||
) | ||
) | ||
if hasattr(model, tagged_item_manager_attr): | ||
raise AttributeError("'%s' already has an attribute '%s'. You must " | ||
"provide a custom tagged_item_manager_attr to register." % ( | ||
model._meta.object_name, | ||
tagged_item_manager_attr, | ||
) | ||
) | ||
|
||
# Add tag descriptor | ||
setattr(model, tag_descriptor_attr, TagDescriptor()) | ||
|
||
# Add custom manager | ||
ModelTaggedItemManager().contribute_to_class(model, | ||
tagged_item_manager_attr) | ||
ModelTaggedItemManager().contribute_to_class(model, tagged_item_manager_attr) | ||
|
||
# Finally register in registry | ||
registry.append(model) |