Commits

offline committed 5be9286

couple of methods added

Comments (0)

Files changed (8)

 from django.contrib import admin
-from tagging.models import Tag, TaggedItem
+from newtags.models import Tag, TaggedItem
 
 admin.site.register(TaggedItem)
 admin.site.register(Tag)

newtags/fields.py

 """
-A custom Model Field for tagging.
+A custom Model Field for newtags.
 """
 import pdb
 from django.db.models import signals
 from django.db.models.fields import CharField
 from django.utils.translation import ugettext_lazy as _
 
-from tagging import settings
-from tagging.models import Tag
-from tagging.utils import edit_string_for_tags
+from newtags import settings
+from newtags.models import Tag
+from newtags.utils import edit_string_for_tags
 from managers import TagManager
 import methods
 
         return 'CharField'
 
     def formfield(self, **kwargs):
-        from tagging import forms
+        from newtags import forms
         defaults = {'form_class': forms.TagField}
         defaults.update(kwargs)
         return super(TagField, self).formfield(**defaults)
 from django import forms
 from django.utils.translation import ugettext as _
 
-from tagging import settings
-from tagging.models import Tag
-from tagging.utils import parse_tag_input
+from newtags import settings
+from newtags.models import Tag
+from newtags.utils import parse_tag_input
 
 class AdminTagForm(forms.ModelForm):
     class Meta:

newtags/managers.py

 from django.db import connection, models
 from django.db.models import Avg, Max, Min, Count
 
-from tagging.models import TaggedItem, Tag
-from tagging.utils import get_tag_list, LOGARITHMIC, calculate_cloud
+from newtags.models import TaggedItem, Tag
+from newtags.utils import get_tag_list, LOGARITHMIC, calculate_cloud
 
 
 class TagManager(models.Manager):
 
         ``distribution`` defines the type of font size distribution
         algorithm which will be used - logarithmic or linear. It must
-        be either ``tagging.utils.LOGARITHMIC`` or
-        ``tagging.utils.LINEAR``.
+        be either ``newtags.utils.LOGARITHMIC`` or
+        ``newtags.utils.LINEAR``.
 
         To limit the tags displayed in the cloud to those associated
         with a subset of the Model's instances, pass a dictionary of
         tags = list(self.usage_for_model(counts=True, filters=filters,
                                          min_count=min_count))
         return calculate_cloud(tags, steps, distribution)
+    
+    
+    
+    def get_by_model(self, tags):
+        tags = get_tag_list(tags)
+        if not tags:
+            return self.model._default_manager.none()
+        
+        return self.get_intersection_by_model(tags)
+
+
+
+    def get_intersection_by_model(self, tags):
+        """
+        Create a ``QuerySet`` containing instances of the specified
+        model associated with *all* of the given list of tags.
+        """
+        tags = get_tag_list(tags)
+        if not tags:
+            return self.model._default_manager.none()
+    
+        objects_id = set([i.id for i in self.model._default_manager.all()])
+        for tag in tags:
+            objects_id = objects_id & set([i.object_id for i in TaggedItem.objects.filter(tag=tag)])
+        return self.model._default_manager.filter(pk__in=objects_id)

newtags/methods.py

 from django.contrib.contenttypes.models import ContentType
-from tagging.models import Tag, TaggedItem
-from tagging.utils import parse_tag_input
-from tagging import settings
+from django.db.models import Avg, Max, Min, Count
+from newtags.models import Tag, TaggedItem
+from newtags.utils import parse_tag_input
+from newtags import settings
 
-def get_for_object(self):
+def tags_for_object(self):
     """
     Create a queryset matching all tags associated with the given
     object.
     return Tag.objects.filter(items__content_type__pk=ctype.pk, items__object_id=self.pk)
 
 
-def add_tag(self, tag_name):
+def tag_add(self, tag_name):
     """
     Associates the given object with a tag.
     """
     
     
     
-def update_tags(self, tag_names):
+def tags_update(self, tag_names):
     """
     Update tags associated with an object.
     """
     for tag_name in updated_tag_names:
         if tag_name not in current_tag_names:
             tag, created = Tag.objects.get_or_create(name=tag_name)
-            TaggedItem._default_manager.create(tag=tag, object=self)
+            TaggedItem._default_manager.create(tag=tag, object=self)
+            
+            
+def get_by_model(self, tags):
+    tags = get_tag_list(tags)
+    if not tags:
+        return self.model._default_manager.none()
+    
+    return self.get_intersection_by_model(tags)
+
+
+def get_intersection_by_model(self, tags):
+    """
+    Create a ``QuerySet`` containing instances of the specified
+    model associated with *all* of the given list of tags.
+    """
+    tags = get_tag_list(tags)
+    if not tags:
+        return self.model._default_manager.none()
+
+    objects_id = set([i.id for i in self.model._default_manager.all()])
+    for tag in tags:
+        objects_id = objects_id & set([i.object_id for i in TaggedItem.objects.filter(tag=tag)])
+    return self.model._default_manager.filter(pk__in=objects_id)
+
+

newtags/models.py

 """
-Models and managers for generic tagging.
+Models and managers for generic newtags.
 """
 # Python 2.3 compatibility
 try:
 from django.utils.translation import ugettext_lazy as _
 from django.db.models import Avg, Max, Min, Count
 
-from tagging import settings
-from tagging.utils import calculate_cloud, get_tag_list, get_queryset_and_model, parse_tag_input
-from tagging.utils import LOGARITHMIC
+from newtags import settings
+from newtags.utils import calculate_cloud, get_tag_list, get_queryset_and_model, parse_tag_input
+from newtags.utils import LOGARITHMIC
 import pdb
 qn = connection.ops.quote_name
 
 # Managers #
 ############
 
-class TagManager(models.Manager):
-    def update_tags(self, obj, tag_names):
-        """
-        Update tags associated with an object.
-        """
-        ctype = ContentType.objects.get_for_model(obj)
-        current_tags = list(self.filter(items__content_type__pk=ctype.pk,
-                                        items__object_id=obj.pk))
-        updated_tag_names = parse_tag_input(tag_names)
-        if settings.FORCE_LOWERCASE_TAGS:
-            updated_tag_names = [t.lower() for t in updated_tag_names]
-
-        # Remove tags which no longer apply
-        tags_for_removal = [tag for tag in current_tags \
-                            if tag.name not in updated_tag_names]
-        if len(tags_for_removal):
-            TaggedItem._default_manager.filter(content_type__pk=ctype.pk,
-                                               object_id=obj.pk,
-                                               tag__in=tags_for_removal).delete()
-        # Add new tags
-        current_tag_names = [tag.name for tag in current_tags]
-        for tag_name in updated_tag_names:
-            if tag_name not in current_tag_names:
-                tag, created = self.get_or_create(name=tag_name)
-                TaggedItem._default_manager.create(tag=tag, object=obj)
-
-    def add_tag(self, obj, tag_name):
-        """
-        Associates the given object with a tag.
-        """
-        tag_names = parse_tag_input(tag_name)
-        if not len(tag_names):
-            raise AttributeError(_('No tags were given: "%s".') % tag_name)
-        if len(tag_names) > 1:
-            raise AttributeError(_('Multiple tags were given: "%s".') % tag_name)
-        tag_name = tag_names[0]
-        if settings.FORCE_LOWERCASE_TAGS:
-            tag_name = tag_name.lower()
-        tag, created = self.get_or_create(name=tag_name)
-        ctype = ContentType.objects.get_for_model(obj)
-        TaggedItem._default_manager.get_or_create(
-            tag=tag, content_type=ctype, object_id=obj.pk)
-
-    def get_for_object(self, obj):
-        """
-        Create a queryset matching all tags associated with the given
-        object.
-        """
-        ctype = ContentType.objects.get_for_model(obj)
-        return self.filter(items__content_type__pk=ctype.pk,
-                           items__object_id=obj.pk)
-
-
-    def _get_usage(self, queryset, counts=False, order='-tag__name', min_count=None):
-        content_type = ContentType.objects.get_for_model(queryset.model)
-        queryset = TaggedItem.objects.filter(content_type=content_type).values('tag__name')
-        if counts:
-            queryset = queryset.annotate(count=Count('tag')).order_by('-tag__name')
-        return queryset
-
-
-
-    def usage_for_model(self, model, counts=False, min_count=None, filters=None):
-        """
-        Obtain a list of tags associated with instances of the given
-        Model class.
-
-        If ``counts`` is True, a ``count`` attribute will be added to
-        each tag, indicating how many times it has been used against
-        the Model class in question.
-
-        If ``min_count`` is given, only tags which have a ``count``
-        greater than or equal to ``min_count`` will be returned.
-        Passing a value for ``min_count`` implies ``counts=True``.
-
-        To limit the tags (and counts, if specified) returned to those
-        used by a subset of the Model's instances, pass a dictionary
-        of field lookups to be applied to the given Model as the
-        ``filters`` argument.
-        """
-        if filters is None: filters = {}
-
-        queryset = model._default_manager.filter()
-        for f in filters.items():
-            queryset.query.add_filter(f)
-        return self.usage_for_queryset(queryset, counts, min_count)
-
-
-    def usage_for_queryset(self, queryset, counts=False, min_count=None):
-        """
-        Obtain a list of tags associated with instances of a model
-        contained in the given queryset.
-
-        If ``counts`` is True, a ``count`` attribute will be added to
-        each tag, indicating how many times it has been used against
-        the Model class in question.
-
-        If ``min_count`` is given, only tags which have a ``count``
-        greater than or equal to ``min_count`` will be returned.
-        Passing a value for ``min_count`` implies ``counts=True``.
-        """
-        return self._get_usage(queryset, counts, min_count)
-
-
-    def related_for_model(self, tags, model, counts=False, min_count=None):
-        """
-        Obtain a list of tags related to a given list of tags - that
-        is, other tags used by items which have all the given tags.
-
-        If ``counts`` is True, a ``count`` attribute will be added to
-        each tag, indicating the number of items which have it in
-        addition to the given list of tags.
-
-        If ``min_count`` is given, only tags which have a ``count``
-        greater than or equal to ``min_count`` will be returned.
-        Passing a value for ``min_count`` implies ``counts=True``.
-        """
-
-        tags_id = set([tag.id for tag in tags])
-        tagged_items_id = set([tagged_item.object_id for tagged_item in TaggedItem.objects.filter(tag__id__in=tags_id)])
-        items_id = [item.id for item in model._default_manager.filter(pk__in=tagged_items_id)]
-        newtags_id = set([tagged_item.tag.id for tagged_item in TaggedItem.objects.filter(object_id__in=items_id)])
-        return Tag.objects.filter(pk__in=newtags_id)
-
-
-
-    def cloud_for_model(self, model, steps=4, distribution=LOGARITHMIC,
-                        filters=None, min_count=None):
-        """
-        Obtain a list of tags associated with instances of the given
-        Model, giving each tag a ``count`` attribute indicating how
-        many times it has been used and a ``font_size`` attribute for
-        use in displaying a tag cloud.
-
-        ``steps`` defines the range of font sizes - ``font_size`` will
-        be an integer between 1 and ``steps`` (inclusive).
-
-        ``distribution`` defines the type of font size distribution
-        algorithm which will be used - logarithmic or linear. It must
-        be either ``tagging.utils.LOGARITHMIC`` or
-        ``tagging.utils.LINEAR``.
-
-        To limit the tags displayed in the cloud to those associated
-        with a subset of the Model's instances, pass a dictionary of
-        field lookups to be applied to the given Model as the
-        ``filters`` argument.
-
-        To limit the tags displayed in the cloud to those with a
-        ``count`` greater than or equal to ``min_count``, pass a value
-        for the ``min_count`` argument.
-        """
-        tags = list(self.usage_for_model(model, counts=True, filters=filters,
-                                         min_count=min_count))
-        return calculate_cloud(tags, steps, distribution)
 
 class TaggedItemManager(models.Manager):
     """
     """
     name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
 
-    objects = TagManager()
 
     class Meta:
         ordering = ('name',)

newtags/settings.py

 """
-Convenience module for access of custom tagging application settings,
+Convenience module for access of custom newtags application settings,
 which enforces default settings when the main settings module does not
 contain the appropriate settings.
 """
        * A ``Tag`` ``QuerySet``.
 
     """
-    from tagging.models import Tag
+    from newtags.models import Tag
     if isinstance(tags, Tag):
         return [tags]
     elif isinstance(tags, QuerySet) and tags.model is Tag:
 
     If no matching tag can be found, ``None`` will be returned.
     """
-    from tagging.models import Tag
+    from newtags.models import Tag
     if isinstance(tag, Tag):
         return tag
 
 
     ``distribution`` defines the type of font size distribution
     algorithm which will be used - logarithmic or linear. It must be
-    one of ``tagging.utils.LOGARITHMIC`` or ``tagging.utils.LINEAR``.
+    one of ``newtags.utils.LOGARITHMIC`` or ``newtags.utils.LINEAR``.
     """
     if len(tags) > 0:
         counts = [tag['count'] for tag in tags]