# Path: supertagging/models.py
# class SuperTag(models.Model):
#     calais_id = models.CharField(_("Calais ID"), max_length=255, unique=True)
#     substitute = models.ForeignKey("self", null=True, blank=True, 
#         related_name="substitute_tagsubstitute",
#         verbose_name=_("Substitute"),
#         help_text=_("""Tag to use instead of this one. This will also update
#             all items and relation items from this tag to the substitute tag."""))
#     name = models.CharField(_("Name"), max_length=150)
#     slug = models.SlugField(_("Slug"), max_length=150)
#     stype = models.CharField(_("Type"), max_length=100)
#     properties = PickledObjectField(_("Properties"), null=True, blank=True)
#     enabled = models.BooleanField(_("Enabled"), default=True, 
#         help_text=_("""If False, will cause the tag to be disabled 
#             and all assoicated items and relation items will be removed."""))
#     
#     if st_settings.INCLUDE_DISPLAY_FIELDS:
#         # Extra fields that optionally can be used for display.
#         from django.core.files.storage import get_storage_class
#         IMAGE_STORAGE = get_storage_class(st_settings.DEFAULT_STORAGE)
#         # Create the display fields
#         display_name = models.CharField(_("Display Name"), max_length=150, 
#             null=True, blank=True)
#         description = models.TextField(_("Description"), blank=True, 
#             null=True, help_text=_('Tag Description.'))
#         icon = models.ImageField(_("Icon"), upload_to="supertagging/icons/", 
#             blank=True, null=True, storage=IMAGE_STORAGE(), 
#             help_text=_('Tag Icon.'))
#         related = models.ManyToManyField("self", blank=True, null=True,
#             verbose_name=_("Related Tags"), 
#             help_text=_("Assign related tags manually."))
# 
#     objects = SuperTagManager()
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.name, self.stype)
#         
#     def get_name(self):
#         if self.has_display_fields():
#             return self.display_name or self.name
#         return self.name
#         
#     def has_display_fields(self):
#         if st_settings.INCLUDE_DISPLAY_FIELDS:
#             return True
#         return False
#         
#     def render(self, template=None, suffix=None):
#         return render_item(None, self.stype, template, suffix,
#             template_path="supertagging/render/tags",
#             context={'obj': self})
#             
#     class Meta:
#         ordering = ('name',)
#         
#     def save(self, *args, **kwargs):      
#         super(SuperTag, self).save(*args, **kwargs)
#         # If display fields are available and FREEBASE_RETRIEVE_DESCRIPTIONS is True
#         # and the description field is empty, try to get a description from Freebase
#         if self.has_display_fields() and st_settings.FREEBASE_RETRIEVE_DESCRIPTIONS and not self.description:
#             self.description = retrieve_freebase_desc(self.name, self.stype)
#             
#         # If tag is set to be disabled and REMOVE_REL_ON_DISABLE is True, 
#         # remove all Tagged Items and Tagged Relation Items
#         if not self.enabled and st_settings.REMOVE_REL_ON_DISABLE:
#             SuperTaggedItem.objects.filter(tag__pk=self.pk).delete()
#             SuperTaggedRelationItem.objects.filter(relation__tag__pk=self.pk).delete()
#             
#         # If a substitute is supplied, change all SuperTaggedItem's and 
#         # SuperTagRelation's to have this new tag
#         if self.substitute and st_settings.SUBSTITUTE_TAG_UPDATE:
#             items = self.supertaggeditem_set.all()
#             relations = self.supertagrelation_set.all()  
#             items.update(tag=self.substitute)
#             relations.update(tag=self.substitute)
# 
# class SuperTaggedItem(models.Model):
#     tag = models.ForeignKey(SuperTag)
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
#     content_object = generic.GenericForeignKey('content_type', 'object_id')
#     field = models.CharField(max_length=100)
#     process_type = models.CharField(max_length=20, null=True, blank=True)
#     relevance = models.IntegerField(null=True, blank=True)
#     instances = PickledObjectField(null=True, blank=True)
#     item_date = models.DateTimeField(null=True, blank=True)
#     ignore = models.BooleanField(default=False)
#     
#     objects = SuperTaggedItemManager()
#     
#     class Meta:
#         ordering = ('-relevance',)
#     
#     def __unicode__(self):
#         return u'%s of %s' % (self.tag, unicode(self.content_object))
#     
#     def render(self, template=None, suffix=None):
#         return render_item(self, None, template, suffix,
#             template_path="supertagging/render/tagged_items", 
#             context={'obj': self.content_object, 'content': self})


from django.contrib.contenttypes.models import ContentType
from django.db import models
from supertagging.models import SuperTag, SuperTaggedItem
"""
Custom managers for Django models registered with the supertagging
application.
"""

class ModelTagManager(models.Manager):
    """
    A manager for retrieving tags for a particular model.
    """
    def get_query_set(self):
        ctype = ContentType.objects.get_for_model(self.model)

# Path: supertagging/models.py
# class SuperTag(models.Model):
#     calais_id = models.CharField(_("Calais ID"), max_length=255, unique=True)
#     substitute = models.ForeignKey("self", null=True, blank=True, 
#         related_name="substitute_tagsubstitute",
#         verbose_name=_("Substitute"),
#         help_text=_("""Tag to use instead of this one. This will also update
#             all items and relation items from this tag to the substitute tag."""))
#     name = models.CharField(_("Name"), max_length=150)
#     slug = models.SlugField(_("Slug"), max_length=150)
#     stype = models.CharField(_("Type"), max_length=100)
#     properties = PickledObjectField(_("Properties"), null=True, blank=True)
#     enabled = models.BooleanField(_("Enabled"), default=True, 
#         help_text=_("""If False, will cause the tag to be disabled 
#             and all assoicated items and relation items will be removed."""))
#     
#     if st_settings.INCLUDE_DISPLAY_FIELDS:
#         # Extra fields that optionally can be used for display.
#         from django.core.files.storage import get_storage_class
#         IMAGE_STORAGE = get_storage_class(st_settings.DEFAULT_STORAGE)
#         # Create the display fields
#         display_name = models.CharField(_("Display Name"), max_length=150, 
#             null=True, blank=True)
#         description = models.TextField(_("Description"), blank=True, 
#             null=True, help_text=_('Tag Description.'))
#         icon = models.ImageField(_("Icon"), upload_to="supertagging/icons/", 
#             blank=True, null=True, storage=IMAGE_STORAGE(), 
#             help_text=_('Tag Icon.'))
#         related = models.ManyToManyField("self", blank=True, null=True,
#             verbose_name=_("Related Tags"), 
#             help_text=_("Assign related tags manually."))
# 
#     objects = SuperTagManager()
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.name, self.stype)
#         
#     def get_name(self):
#         if self.has_display_fields():
#             return self.display_name or self.name
#         return self.name
#         
#     def has_display_fields(self):
#         if st_settings.INCLUDE_DISPLAY_FIELDS:
#             return True
#         return False
#         
#     def render(self, template=None, suffix=None):
#         return render_item(None, self.stype, template, suffix,
#             template_path="supertagging/render/tags",
#             context={'obj': self})
#             
#     class Meta:
#         ordering = ('name',)
#         
#     def save(self, *args, **kwargs):      
#         super(SuperTag, self).save(*args, **kwargs)
#         # If display fields are available and FREEBASE_RETRIEVE_DESCRIPTIONS is True
#         # and the description field is empty, try to get a description from Freebase
#         if self.has_display_fields() and st_settings.FREEBASE_RETRIEVE_DESCRIPTIONS and not self.description:
#             self.description = retrieve_freebase_desc(self.name, self.stype)
#             
#         # If tag is set to be disabled and REMOVE_REL_ON_DISABLE is True, 
#         # remove all Tagged Items and Tagged Relation Items
#         if not self.enabled and st_settings.REMOVE_REL_ON_DISABLE:
#             SuperTaggedItem.objects.filter(tag__pk=self.pk).delete()
#             SuperTaggedRelationItem.objects.filter(relation__tag__pk=self.pk).delete()
#             
#         # If a substitute is supplied, change all SuperTaggedItem's and 
#         # SuperTagRelation's to have this new tag
#         if self.substitute and st_settings.SUBSTITUTE_TAG_UPDATE:
#             items = self.supertaggeditem_set.all()
#             relations = self.supertagrelation_set.all()  
#             items.update(tag=self.substitute)
#             relations.update(tag=self.substitute)
# 
# class SuperTaggedItem(models.Model):
#     tag = models.ForeignKey(SuperTag)
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
#     content_object = generic.GenericForeignKey('content_type', 'object_id')
#     field = models.CharField(max_length=100)
#     process_type = models.CharField(max_length=20, null=True, blank=True)
#     relevance = models.IntegerField(null=True, blank=True)
#     instances = PickledObjectField(null=True, blank=True)
#     item_date = models.DateTimeField(null=True, blank=True)
#     ignore = models.BooleanField(default=False)
#     
#     objects = SuperTaggedItemManager()
#     
#     class Meta:
#         ordering = ('-relevance',)
#     
#     def __unicode__(self):
#         return u'%s of %s' % (self.tag, unicode(self.content_object))
#     
#     def render(self, template=None, suffix=None):
#         return render_item(self, None, template, suffix,
#             template_path="supertagging/render/tagged_items", 
#             context={'obj': self.content_object, 'content': self})


# Path: supertagging/managers.py
from django.contrib.contenttypes.models import ContentType
from django.db import models
from supertagging.models import SuperTag, SuperTaggedItem

"""
Custom managers for Django models registered with the supertagging
application.
"""

class ModelTagManager(models.Manager):
    """
    A manager for retrieving tags for a particular model.
    """
    def get_query_set(self):
        ctype = ContentType.objects.get_for_model(self.model)

        return SuperTag.objects.filter(