Commits

offline committed 01b6243

you now need to register model with tags

  • Participants
  • Parent commits 3cf8a71

Comments (0)

Files changed (5)

File tagging2/__init__.py

+__all__ = ('register',)
+
+class AlreadyRegistered(Exception):
+    """
+    An attempt was made to register a model for MPTT more than once.
+    """
+    pass
+
+registry = []
+
+def register(model, tag_manager_attr='tagging'):
+    """
+    Sets the given model class up for Modified Preorder Tree Traversal.
+    """
+    from django.db.models import signals as model_signals
+    from django.db.models import FieldDoesNotExist, PositiveIntegerField
+    from django.utils.translation import ugettext as _
+
+    from tagging2 import methods
+    from tagging2.managers import TagManager
+
+    if model in registry:
+        raise AlreadyRegistered(_('The model %s has already been registered.') % model.__name__)
+    registry.append(model)
+
+    # Add tree options to the model's Options
+    model._meta.tag_manager_attr = tag_manager_attr
+
+    # Add tree methods for model instances
+    setattr(model, 'tags_for_object', methods.tags_for_object)
+    setattr(model, 'tag_add', methods.tag_add)
+    setattr(model, 'tag_list', methods.tag_list)
+    setattr(model, 'tags_update', methods.tags_update)
+
+    # Add a custom tree manager
+    TagManager().contribute_to_class(model, tag_manager_attr)
+    setattr(model, '_tag_manager', getattr(model, tag_manager_attr))
+

File tagging2/fields.py

         super(TagField, self).__init__(*args, **kwargs)
 
     def contribute_to_class(self, cls, name):
-        manager = TagManager()
-        manager.model = cls
-        setattr(cls, 'tagging', manager)
-        
-        setattr(cls, 'tags_for_object', tags_for_object)
-        setattr(cls, 'tags_update', tags_update)
-        setattr(cls, 'tag_list', tag_list)
-        
         super(TagField, self).contribute_to_class(cls, name)
 
         # Make this object the descriptor for field access.
         setattr(cls, self.name, self)
 
-
-        # Save tags back to the database post-save
         signals.post_save.connect(self._save, cls, True)
 
     def __get__(self, instance, owner=None):
         defaults = {'form_class': forms.TagField}
         defaults.update(kwargs)
         return super(TagField, self).formfield(**defaults)
+

File tagging2/managers.py

             tags.append(tag)
         return tags
 
-
     def usage_for_model(self, filters=None, min_count=None, top=False):
         """
         Obtain a list of all tags associated with Model excluding filtered instances.
             
         return self._get_usage(queryset, min_count, top)
 
-
     def cloud_for_model(self, steps=None, distribution=LOGARITHMIC,
                         filters=None, min_count=None, top=False):
         """
         tags = self.usage_for_model(filters, min_count, top)
         return calculate_cloud(tags, steps, distribution)
 
-    
     def get_items_by_tags(self, tags):
         ctype = ContentType.objects.get_for_model(self.model)
         tags = get_tag_list(tags)
         items = self.model._default_manager.filter(pk__in=items_id_list)
         return items
     
-    
     def related_tags_cloud(self, tags, steps=None, min_count=None):
         """
         Obtain tags cloud used in instances along with tags provided
         tags = self._get_usage(items)
         return calculate_cloud(tags, steps=steps)
         
-        
     def related_tags(self, tags, min_count=None, top=False):
         """
         Obtain a list of tags related to a given list of tags - that
         """
         items = self.get_objects_with_any_tags(tags)
         return self._get_usage(items, min_count, top)
-
-    
     
     def _get_tagged_items_by_tags(self, tags):
         tags = get_tag_list(tags)
         tagged_items = TaggedItem.objects.filter(tag__in=tags, content_type=ctype).order_by("object_id")
         return tagged_items
     
-    
     def get_objects_with_all_tags(self, tags):
         """
         Create a ``QuerySet`` containing instances of the specified
 
         return items
 
-
     def get_objects_with_any_tags(self, tags):
         """
         Create a ``QuerySet`` containing instances of the specified
         items = self.model._default_manager.filter(pk__in=objects_id_list)
         
         return items
-        
 

File tagging2/methods.py

 from utils import parse_tag_input, generate_key_hash
 import settings
 
+
 def tags_for_object(self):
     """
     return Tag model queryset matching all tags associated with the given

File tagging2/models.py

 from django.utils.translation import ugettext_lazy as _
 
 
-
 class Tag(models.Model):
     """
     A tag.
     """
     name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
 
-
     class Meta:
         db_table = 'tagging_tag'
         ordering = ('name',)
         return self.name
 
 
-
 class TaggedItem(models.Model):
     """
     Holds the relationship between a tag and the item being tagged.