From 2228968f3d51a3d686adb2839bf43e018432f941 Mon Sep 17 00:00:00 2001 From: SVN-Git Migration Date: Thu, 8 Oct 2015 11:51:45 -0700 Subject: Imported Upstream version 0.1+svn102 --- tagging/models.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tagging/models.py (limited to 'tagging/models.py') diff --git a/tagging/models.py b/tagging/models.py new file mode 100644 index 0000000..199e5eb --- /dev/null +++ b/tagging/models.py @@ -0,0 +1,53 @@ +""" +Models for generic tagging. +""" +from django.db import models +from django.contrib.contenttypes import generic +from django.contrib.contenttypes.models import ContentType + +from tagging.managers import TagManager, TaggedItemManager +from tagging.validators import isTag + +class Tag(models.Model): + """ + A basic tag. + """ + name = models.CharField(max_length=50, unique=True, db_index=True, validator_list=[isTag]) + + objects = TagManager() + + class Meta: + db_table = 'tag' + verbose_name = 'Tag' + verbose_name_plural = 'Tags' + ordering = ('name',) + + class Admin: + pass + + def __unicode__(self): + return self.name + +class TaggedItem(models.Model): + """ + Holds the relationship between a tag and the item being tagged. + """ + tag = models.ForeignKey(Tag, related_name='items') + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + object = generic.GenericForeignKey('content_type', 'object_id') + + objects = TaggedItemManager() + + class Meta: + db_table = 'tagged_item' + verbose_name = 'Tagged Item' + verbose_name_plural = 'Tagged Items' + # Enforce unique tag association per object + unique_together = (('tag', 'content_type', 'object_id'),) + + class Admin: + pass + + def __unicode__(self): + return u'%s [%s]' % (self.object, self.tag) -- cgit v1.2.3