<s> import django <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from django . db import models , connection <EOL> from django . db . models . fields . related import create_many_related_manager , ManyToManyRel <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . compat import User <EOL> class RelationshipStatusManager ( models . Manager ) : <EOL> def following ( self ) : <EOL> return self . get ( from_slug = '<STR_LIT>' ) <EOL> def blocking ( self ) : <EOL> return self . get ( from_slug = '<STR_LIT>' ) <EOL> def by_slug ( self , status_slug ) : <EOL> return self . get ( <EOL> models . Q ( from_slug = status_slug ) | <EOL> models . Q ( to_slug = status_slug ) | <EOL> models . Q ( symmetrical_slug = status_slug ) <EOL> ) <EOL> class RelationshipStatus ( models . Model ) : <EOL> name = models . CharField ( _ ( '<STR_LIT:name>' ) , max_length = <NUM_LIT:100> ) <EOL> verb = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> ) <EOL> from_slug = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> to_slug = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> symmetrical_slug = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> login_required = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> private = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> objects = RelationshipStatusManager ( ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT:name>' , ) <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class Relationship ( models . Model ) : <EOL> from_user = models . ForeignKey ( User , <EOL> related_name = '<STR_LIT>' , verbose_name = _ ( '<STR_LIT>' ) ) <EOL> to_user = models . ForeignKey ( User , <EOL> related_name = '<STR_LIT>' , verbose_name = _ ( '<STR_LIT>' ) ) <EOL> status = models . ForeignKey ( RelationshipStatus , verbose_name = _ ( '<STR_LIT:status>' ) ) <EOL> created = models . DateTimeField ( _ ( '<STR_LIT>' ) , auto_now_add = True ) <EOL> weight = models . FloatField ( _ ( '<STR_LIT>' ) , default = <NUM_LIT:1.0> , blank = True , null = True ) <EOL> site = models . ForeignKey ( Site , default = settings . SITE_ID , <EOL> verbose_name = _ ( '<STR_LIT>' ) , related_name = '<STR_LIT>' ) <EOL> class Meta : <EOL> unique_together = ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' ) , ) <EOL> ordering = ( '<STR_LIT>' , ) <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return ( _ ( '<STR_LIT>' ) <EOL> % { '<STR_LIT>' : self . from_user . username , <EOL> '<STR_LIT>' : self . to_user . username } ) <EOL> field = models . ManyToManyField ( User , through = Relationship , <EOL> symmetrical = False , related_name = '<STR_LIT>' ) <EOL> class RelationshipManager ( User . _default_manager . __class__ ) : <EOL> def __init__ ( self , instance = None , * args , ** kwargs ) : <EOL> super ( RelationshipManager , self ) . __init__ ( * args , ** kwargs ) <EOL> self . instance = instance <EOL> def add ( self , user , status = None , symmetrical = False ) : <EOL> """<STR_LIT>""" <EOL> if not status : <EOL> status = RelationshipStatus . objects . following ( ) <EOL> relationship , created = Relationship . objects . get_or_create ( <EOL> from_user = self . instance , <EOL> to_user = user , <EOL> status = status , <EOL> site = Site . objects . get_current ( ) <EOL> ) <EOL> if symmetrical : <EOL> return ( relationship , user . relationships . add ( self . instance , status , False ) ) <EOL> else : <EOL> return relationship <EOL> def remove ( self , user , status = None , symmetrical = False ) : <EOL> """<STR_LIT>""" <EOL> if not status : <EOL> status = RelationshipStatus . objects . following ( ) <EOL> res = Relationship . objects . filter ( <EOL> from_user = self . instance , <EOL> to_user = user , <EOL> status = status , <EOL> site__pk = settings . SITE_ID <EOL> ) . delete ( ) <EOL> if symmetrical : <EOL> return ( res , user . relationships . remove ( self . instance , status , False ) ) <EOL> else : <EOL> return res <EOL> def _get_from_query ( self , status ) : <EOL> return dict ( <EOL> to_users__from_user = self . instance , <EOL> to_users__status = status , <EOL> to_users__site__pk = settings . SITE_ID , <EOL> ) <EOL> def _get_to_query ( self , status ) : <EOL> return dict ( <EOL> from_users__to_user = self . instance , <EOL> from_users__status = status , <EOL> from_users__site__pk = settings . SITE_ID <EOL> ) <EOL> def get_relationships ( self , status , symmetrical = False ) : <EOL> """<STR_LIT>""" <EOL> query = self . _get_from_query ( status ) <EOL> if symmetrical : <EOL> query . update ( self . _get_to_query ( status ) ) <EOL> return User . objects . filter ( ** query ) <EOL> def get_related_to ( self , status ) : <EOL> """<STR_LIT>""" <EOL> return User . objects . filter ( ** self . _get_to_query ( status ) ) <EOL> def only_to ( self , status ) : <EOL> """<STR_LIT>""" <EOL> from_relationships = self . get_relationships ( status ) <EOL> to_relationships = self . get_related_to ( status ) <EOL> return to_relationships . exclude ( pk__in = from_relationships . values_list ( '<STR_LIT>' ) ) <EOL> def only_from ( self , status ) : <EOL> """<STR_LIT>""" <EOL> from_relationships = self . get_relationships ( status ) <EOL> to_relationships = self . get_related_to ( status ) <EOL> return from_relationships . exclude ( pk__in = to_relationships . values_list ( '<STR_LIT>' ) ) <EOL> def exists ( self , user , status = None , symmetrical = False ) : <EOL> """<STR_LIT>""" <EOL> query = dict ( <EOL> to_users__from_user = self . instance , <EOL> to_users__to_user = user , <EOL> to_users__site__pk = settings . SITE_ID , <EOL> ) <EOL> if status : <EOL> query . update ( to_users__status = status ) <EOL> if symmetrical : <EOL> query . update ( <EOL> from_users__to_user = self . instance , <EOL> from_users__from_user = user , <EOL> from_users__site__pk = settings . SITE_ID <EOL> ) <EOL> if status : <EOL> query . update ( from_users__status = status ) <EOL> return User . objects . filter ( ** query ) . exists ( ) <EOL> def following ( self ) : <EOL> return self . get_relationships ( RelationshipStatus . objects . following ( ) ) <EOL> def followers ( self ) : <EOL> return self . get_related_to ( RelationshipStatus . objects . following ( ) ) <EOL> def blocking ( self ) : <EOL> return self . get_relationships ( RelationshipStatus . objects . blocking ( ) ) <EOL> def blockers ( self ) : <EOL> return self . get_related_to ( RelationshipStatus . objects . blocking ( ) ) <EOL> def friends ( self ) : <EOL> return self . get_relationships ( RelationshipStatus . objects . following ( ) , True ) <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> RelatedManager = create_many_related_manager ( RelationshipManager , Relationship ) <EOL> class RelationshipsDescriptor ( object ) : <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> qn = connection . ops . quote_name <EOL> manager = RelatedManager ( <EOL> model = User , <EOL> core_filters = { '<STR_LIT>' : instance . _get_pk_val ( ) } , <EOL> instance = instance , <EOL> symmetrical = False , <EOL> join_table = qn ( '<STR_LIT>' ) , <EOL> source_col_name = qn ( '<STR_LIT>' ) , <EOL> target_col_name = qn ( '<STR_LIT>' ) , <EOL> ) <EOL> return manager <EOL> elif django . VERSION > ( <NUM_LIT:1> , <NUM_LIT:2> ) and django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> fake_rel = ManyToManyRel ( <EOL> to = User , <EOL> through = Relationship ) <EOL> RelatedManager = create_many_related_manager ( RelationshipManager , fake_rel ) <EOL> class RelationshipsDescriptor ( object ) : <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> manager = RelatedManager ( <EOL> model = User , <EOL> core_filters = { '<STR_LIT>' : instance . _get_pk_val ( ) } , <EOL> instance = instance , <EOL> symmetrical = False , <EOL> source_field_name = '<STR_LIT>' , <EOL> target_field_name = '<STR_LIT>' <EOL> ) <EOL> return manager <EOL> else : <EOL> fake_rel = ManyToManyRel ( <EOL> to = User , <EOL> through = Relationship ) <EOL> RelatedManager = create_many_related_manager ( RelationshipManager , fake_rel ) <EOL> class RelationshipsDescriptor ( object ) : <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> manager = RelatedManager ( <EOL> model = User , <EOL> query_field_name = '<STR_LIT>' , <EOL> instance = instance , <EOL> symmetrical = False , <EOL> source_field_name = '<STR_LIT>' , <EOL> target_field_name = '<STR_LIT>' , <EOL> through = Relationship , <EOL> ) <EOL> return manager <EOL> field . contribute_to_class ( User , '<STR_LIT>' ) <EOL> setattr ( User , '<STR_LIT>' , RelationshipsDescriptor ( ) ) </s>
