<s> import django <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . contenttypes . generic import GenericForeignKey <EOL> from django . db import models , IntegrityError , transaction <EOL> from django . template . defaultfilters import slugify as default_slugify <EOL> from django . utils . translation import ugettext_lazy as _ , ugettext <EOL> class TagBase ( models . Model ) : <EOL> name = models . CharField ( verbose_name = _ ( '<STR_LIT:Name>' ) , max_length = <NUM_LIT:100> ) <EOL> slug = models . SlugField ( verbose_name = _ ( '<STR_LIT>' ) , unique = True , max_length = <NUM_LIT:100> ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class Meta : <EOL> abstract = True <EOL> def save ( self , * args , ** kwargs ) : <EOL> if not self . pk and not self . slug : <EOL> self . slug = self . slugify ( self . name ) <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> from django . db import router <EOL> using = kwargs . get ( "<STR_LIT>" ) or router . db_for_write ( <EOL> type ( self ) , instance = self ) <EOL> kwargs [ "<STR_LIT>" ] = using <EOL> trans_kwargs = { "<STR_LIT>" : using } <EOL> else : <EOL> trans_kwargs = { } <EOL> i = <NUM_LIT:0> <EOL> while True : <EOL> i += <NUM_LIT:1> <EOL> try : <EOL> sid = transaction . savepoint ( ** trans_kwargs ) <EOL> res = super ( TagBase , self ) . save ( * args , ** kwargs ) <EOL> transaction . savepoint_commit ( sid , ** trans_kwargs ) <EOL> return res <EOL> except IntegrityError : <EOL> transaction . savepoint_rollback ( sid , ** trans_kwargs ) <EOL> self . slug = self . slugify ( self . name , i ) <EOL> else : <EOL> return super ( TagBase , self ) . save ( * args , ** kwargs ) <EOL> def slugify ( self , tag , i = None ) : <EOL> slug = default_slugify ( tag ) <EOL> if i is not None : <EOL> slug += "<STR_LIT>" % i <EOL> return slug <EOL> class Tag ( TagBase ) : <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class ItemBase ( models . Model ) : <EOL> def __unicode__ ( self ) : <EOL> return ugettext ( "<STR_LIT>" ) % { <EOL> "<STR_LIT:object>" : self . content_object , <EOL> "<STR_LIT>" : self . tag <EOL> } <EOL> class Meta : <EOL> abstract = True <EOL> @ classmethod <EOL> def tag_model ( cls ) : <EOL> return cls . _meta . get_field_by_name ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . rel . to <EOL> @ classmethod <EOL> def tag_relname ( cls ) : <EOL> return cls . _meta . get_field_by_name ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . rel . related_name <EOL> @ classmethod <EOL> def lookup_kwargs ( cls , instance ) : <EOL> return { <EOL> '<STR_LIT>' : instance <EOL> } <EOL> @ classmethod <EOL> def bulk_lookup_kwargs ( cls , instances ) : <EOL> return { <EOL> "<STR_LIT>" : instances , <EOL> } <EOL> class TaggedItemBase ( ItemBase ) : <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> tag = models . ForeignKey ( Tag , related_name = "<STR_LIT>" ) <EOL> else : <EOL> tag = models . ForeignKey ( Tag , related_name = "<STR_LIT>" ) <EOL> class Meta : <EOL> abstract = True <EOL> @ classmethod <EOL> def tags_for ( cls , model , instance = None ) : <EOL> if instance is not None : <EOL> return cls . tag_model ( ) . objects . filter ( ** { <EOL> '<STR_LIT>' % cls . tag_relname ( ) : instance <EOL> } ) <EOL> return cls . tag_model ( ) . objects . filter ( ** { <EOL> '<STR_LIT>' % cls . tag_relname ( ) : False <EOL> } ) . distinct ( ) <EOL> class GenericTaggedItemBase ( ItemBase ) : <EOL> object_id = models . IntegerField ( verbose_name = _ ( '<STR_LIT>' ) , db_index = True ) <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> content_type = models . ForeignKey ( <EOL> ContentType , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> related_name = "<STR_LIT>" <EOL> ) <EOL> else : <EOL> content_type = models . ForeignKey ( <EOL> ContentType , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> related_name = "<STR_LIT>" <EOL> ) <EOL> content_object = GenericForeignKey ( ) <EOL> class Meta : <EOL> abstract = True <EOL> @ classmethod <EOL> def lookup_kwargs ( cls , instance ) : <EOL> return { <EOL> '<STR_LIT>' : instance . pk , <EOL> '<STR_LIT>' : ContentType . objects . get_for_model ( instance ) <EOL> } <EOL> @ classmethod <EOL> def bulk_lookup_kwargs ( cls , instances ) : <EOL> return { <EOL> "<STR_LIT>" : [ instance . pk for instance in instances ] , <EOL> "<STR_LIT>" : ContentType . objects . get_for_model ( instances [ <NUM_LIT:0> ] ) , <EOL> } <EOL> @ classmethod <EOL> def tags_for ( cls , model , instance = None ) : <EOL> ct = ContentType . objects . get_for_model ( model ) <EOL> kwargs = { <EOL> "<STR_LIT>" % cls . tag_relname ( ) : ct <EOL> } <EOL> if instance is not None : <EOL> kwargs [ "<STR_LIT>" % cls . tag_relname ( ) ] = instance . pk <EOL> return cls . tag_model ( ) . objects . filter ( ** kwargs ) . distinct ( ) <EOL> class TaggedItem ( GenericTaggedItemBase , TaggedItemBase ) : <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) </s>
