Created
May 6, 2012 08:52
-
-
Save gustabot42/2621142 to your computer and use it in GitHub Desktop.
Django taggit at Kind of tag by proxy
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
# .../apps/taggit-wrap/models.py | |
from django.db import models | |
from django.contrib.contenttypes.models import ContentType | |
from django.utils.translation import ugettext_lazy as _ | |
from taggit.models import GenericTaggedItemBase, TaggedItemBase | |
# Mixin | |
class KindTaggedBase(models.Model): | |
kind_name = '' | |
kind = models.CharField(verbose_name=_('Kind'), max_length=100, null=True) | |
class Meta: | |
abstract = True | |
@classmethod | |
def lookup_kwargs(cls, instance): | |
return { | |
'object_id': instance.pk, | |
'content_type': ContentType.objects.get_for_model(instance), | |
'kind' : cls.kind_name | |
} | |
@classmethod | |
def bulk_lookup_kwargs(cls, instances): | |
# TODO: instances[0], can we assume there are instances. | |
return { | |
'object_id__in': [instance.pk for instance in instances], | |
'content_type': ContentType.objects.get_for_model(instances[0]), | |
'kind' : cls.kind_name | |
} | |
@classmethod | |
def tags_for(cls, model, instance=None): | |
ct = ContentType.objects.get_for_model(model) | |
kwargs = { | |
'%s__content_type' % cls.tag_relname(): ct, | |
'%s__kind' % cls.tag_relname() : cls.kind_name | |
} | |
if instance is not None: | |
kwargs['%s__object_id' % cls.tag_relname()] = instance.pk | |
return cls.tag_model().objects.filter(**kwargs).distinct() | |
# Normal Declaration | |
class KindTaggedItem(KindTaggedBase, GenericTaggedItemBase, TaggedItemBase): | |
class Meta: | |
verbose_name = _("Tagged Item") | |
verbose_name_plural = _("Tagged Items") | |
# 1° Proxy Declaration | |
class KeywordTaggedItem(KindTaggedItem): | |
kind_name = 'keyword' | |
class Meta: | |
verbose_name = _("Keyword Item") | |
verbose_name_plural = _("Keyword Items") | |
proxy = True | |
# 2° Proxy Declaration | |
class BadgeTaggedItem(KindTaggedItem): | |
kind_name = 'badge' | |
class Meta: | |
verbose_name = _("Badge Item") | |
verbose_name_plural = _("Badge Items") | |
proxy = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
don't work :S
http://pastebin.com/bc9zPPvG
http://pastebin.com/755FF5Ag