Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 MVMTaggedBase(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()
- class MVMTaggedItem(MVMTaggedBase, GenericTaggedItemBase, TaggedItemBase):
- class Meta:
- verbose_name = _("Tagged Item")
- verbose_name_plural = _("Tagged Items")
- class ActorTaggedItem(MVMTaggedItem):
- kind_name = 'actor'
- class Meta:
- verbose_name = _("Actor Item")
- verbose_name_plural = _("Actor Items")
- proxy = True
- app_label = 'mvm'
- class IdentityTaggedItem(MVMTaggedItem):
- kind_name = 'identity'
- class Meta:
- verbose_name = _("Identity Item")
- verbose_name_plural = _("Identity Items")
- proxy = True
- app_label = 'mvm'
- class RoleTaggedItem(MVMTaggedItem):
- kind_name = 'role'
- class Meta:
- verbose_name = _("Role Item")
- verbose_name_plural = _("Role Items")
- proxy = True
- app_label = 'mvm'
- class PlaceTaggedItem(MVMTaggedItem):
- kind_name = 'place'
- class Meta:
- verbose_name = _("Place Item")
- verbose_name_plural = _("Place Items")
- proxy = True
- app_label = 'mvm'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement