<s> from datetime import datetime <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . contrib . auth . management import create_superuser <EOL> from django . contrib . auth import models as auth_app <EOL> from django . db . models . signals import post_save , post_syncdb <EOL> from django . utils . importlib import import_module <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . contenttypes import generic <EOL> from django . core . urlresolvers import reverse <EOL> from timezones . fields import TimeZoneField , MAX_TIMEZONE_LENGTH <EOL> from timezones . utils import adjust_datetime_to_timezone <EOL> User = auth_app . User <EOL> post_syncdb . disconnect ( <EOL> create_superuser , <EOL> sender = auth_app , <EOL> dispatch_uid = "<STR_LIT>" <EOL> ) <EOL> TIME_ZONE = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ServiceApp ( models . Model ) : <EOL> module_name = models . CharField ( max_length = <NUM_LIT:255> , db_index = True ) <EOL> enable = models . NullBooleanField ( null = True , blank = True , default = True ) <EOL> _module = None <EOL> @ property <EOL> def module ( self ) : <EOL> if not self . _module : <EOL> self . _module = import_module ( "<STR_LIT>" % ( self . module_name , ) ) <EOL> return self . _module <EOL> @ property <EOL> def display_name ( self ) : <EOL> return self . module . ServiceHandler . display_name <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . module . ServiceHandler . display_name , ) <EOL> @ property <EOL> def auth_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> app_settings = getattr ( settings , '<STR_LIT>' , { } ) . get ( <EOL> self . module_name , { } ) . get ( '<STR_LIT>' , None ) <EOL> if app_settings : <EOL> return app_settings <EOL> else : <EOL> raise Exception ( <EOL> '<STR_LIT>' % ( self . module_name , ) <EOL> ) <EOL> def get_app_by_name ( module_name ) : <EOL> """<STR_LIT>""" <EOL> app = ServiceApp . objects . get_or_create ( module_name = module_name ) [ <NUM_LIT:0> ] <EOL> return app <EOL> class UserService ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> user = models . ForeignKey ( User , db_index = True ) <EOL> app = models . ForeignKey ( ServiceApp , db_index = True ) <EOL> setup = models . NullBooleanField ( null = True , blank = True , default = False ) <EOL> share = models . NullBooleanField ( null = True , blank = True , default = False ) <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> modified = models . DateTimeField ( auto_now = True ) <EOL> public = models . NullBooleanField ( null = True , blank = True , default = False ) <EOL> auth_type = models . ForeignKey ( ContentType , blank = True , null = True ) <EOL> auth_object_id = models . PositiveIntegerField ( blank = True , null = True ) <EOL> auth = generic . GenericForeignKey ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> _handler = None <EOL> class Meta : <EOL> unique_together = ( "<STR_LIT:user>" , "<STR_LIT>" , ) <EOL> @ property <EOL> def form_template_path ( self ) : <EOL> if self . handler . custom_form : <EOL> path = "<STR_LIT>" % ( self . template_name , ) <EOL> elif self . handler . oauth_form : <EOL> path = "<STR_LIT>" <EOL> else : <EOL> path = "<STR_LIT>" <EOL> return path <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . app , self . user , ) <EOL> def get_auth_url ( self ) : <EOL> return reverse ( '<STR_LIT>' % ( self . app . module_name . replace ( '<STR_LIT:.>' , '<STR_LIT:->' ) , ) ) <EOL> @ property <EOL> def class_name ( self ) : <EOL> return self . app . module_name . replace ( '<STR_LIT:.>' , '<STR_LIT:->' ) <EOL> @ property <EOL> def template_name ( self ) : <EOL> return self . app . module_name . replace ( '<STR_LIT:.>' , '<STR_LIT:/>' ) <EOL> @ property <EOL> def handler ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _handler : <EOL> return self . _handler <EOL> else : <EOL> self . _handler = self . app . module . ServiceHandler ( self ) <EOL> try : <EOL> self . _handler . setup ( ) <EOL> except : <EOL> pass <EOL> return self . _handler <EOL> class BaseAuth ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> user_services = generic . GenericRelation ( <EOL> UserService , <EOL> content_type_field = '<STR_LIT>' , <EOL> object_id_field = '<STR_LIT>' <EOL> ) <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> modified = models . DateTimeField ( auto_now = True ) <EOL> class Auth ( BaseAuth ) : <EOL> """<STR_LIT>""" <EOL> username = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> secret = models . CharField ( max_length = <NUM_LIT:255> , blank = True , null = True ) <EOL> def __unicode__ ( self ) : <EOL> if self . user_services . count ( ) > <NUM_LIT:0> : <EOL> return u'<STR_LIT>' % ( self . user_services . all ( ) [ <NUM_LIT:0> ] , ) <EOL> else : <EOL> return u'<STR_LIT>' <EOL> class RequestToken ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> oauth_token = models . CharField ( max_length = <NUM_LIT:255> , blank = True , null = True ) <EOL> oauth_token_secret = models . CharField ( max_length = <NUM_LIT:255> , blank = True , null = True ) <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> oauth_verify = models . CharField ( max_length = <NUM_LIT:255> , blank = True , null = True ) <EOL> def __unicode__ ( self ) : <EOL> if self . oauth_set . count ( ) > <NUM_LIT:0> and self . oauth_set . all ( ) [ <NUM_LIT:0> ] . user_services . count ( ) > <NUM_LIT:0> : <EOL> return u'<STR_LIT>' % ( self . oauth_set . all ( ) [ <NUM_LIT:0> ] . user_services . all ( ) [ <NUM_LIT:0> ] , ) <EOL> else : <EOL> return u'<STR_LIT>' <EOL> class AccessToken ( RequestToken ) : <EOL> """<STR_LIT>""" <EOL> def __unicode__ ( self ) : <EOL> if self . oauth_set . count ( ) > <NUM_LIT:0> and self . oauth_set . all ( ) [ <NUM_LIT:0> ] . user_services . count ( ) > <NUM_LIT:0> : <EOL> return u'<STR_LIT>' % ( self . oauth_set . all ( ) [ <NUM_LIT:0> ] . user_services . all ( ) [ <NUM_LIT:0> ] , ) <EOL> else : <EOL> return u'<STR_LIT>' <EOL> class OAuth ( BaseAuth ) : <EOL> """<STR_LIT>""" <EOL> request_token = models . ForeignKey ( RequestToken , null = True , blank = True ) <EOL> access_token = models . ForeignKey ( AccessToken , related_name = "<STR_LIT>" , null = True , blank = True ) <EOL> def __unicode__ ( self ) : <EOL> if self . user_services . count ( ) : <EOL> return u'<STR_LIT>' % ( self . user_services . all ( ) [ <NUM_LIT:0> ] , ) <EOL> else : <EOL> return u'<STR_LIT>' <EOL> class UserProfile ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> user = models . ForeignKey ( User ) <EOL> timezone = TimeZoneField ( ) <EOL> def user_save_handler ( sender , ** kwargs ) : <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> up = UserProfile ( ) <EOL> up . user = kwargs [ '<STR_LIT>' ] <EOL> up . timezone = TIME_ZONE <EOL> up . save ( ) <EOL> post_save . connect ( user_save_handler , User ) <EOL> try : <EOL> from south . modelsinspector import add_introspection_rules <EOL> add_introspection_rules ( <EOL> rules = [ ( <EOL> ( TimeZoneField , ) , <EOL> [ ] , <EOL> { "<STR_LIT:max_length>" : [ "<STR_LIT:max_length>" , { "<STR_LIT:default>" : MAX_TIMEZONE_LENGTH } ] , } <EOL> ) ] , <EOL> patterns = [ '<STR_LIT>' ] <EOL> ) <EOL> except ImportError : <EOL> pass <EOL> class ServiceItem ( object ) : <EOL> def __init__ ( self ) : <EOL> self . created = None <EOL> self . title = None <EOL> self . body = None <EOL> self . location = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> self . service = None <EOL> self . link_back = None <EOL> @ property <EOL> def class_name ( self ) : <EOL> return self . service and self . service . class_name or '<STR_LIT>' <EOL> def has_location ( self ) : <EOL> return self . location . has_key ( '<STR_LIT>' ) is not False and self . location . has_key ( '<STR_LIT>' ) is not False <EOL> @ property <EOL> def created_local ( self ) : <EOL> if hasattr ( self . service . user , '<STR_LIT>' ) : <EOL> return adjust_datetime_to_timezone ( <EOL> self . created , <EOL> '<STR_LIT>' , <EOL> unicode ( self . service . user . get_profile ( ) . timezone ) <EOL> ) <EOL> else : <EOL> return adjust_datetime_to_timezone ( self . created , '<STR_LIT>' , TIME_ZONE ) </s>
