<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function <EOL> from bitfield import BitField <EOL> from django . conf import settings <EOL> from django . db import IntegrityError , models , transaction <EOL> from django . utils import timezone <EOL> from django . utils . functional import cached_property <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from sentry import roles <EOL> from sentry . constants import RESERVED_ORGANIZATION_SLUGS <EOL> from sentry . db . models import ( <EOL> BaseManager , BoundedPositiveIntegerField , Model , <EOL> sane_repr <EOL> ) <EOL> from sentry . db . models . utils import slugify_instance <EOL> from sentry . utils . cache import Lock <EOL> class OrganizationStatus ( object ) : <EOL> VISIBLE = <NUM_LIT:0> <EOL> PENDING_DELETION = <NUM_LIT:1> <EOL> DELETION_IN_PROGRESS = <NUM_LIT:2> <EOL> class OrganizationManager ( BaseManager ) : <EOL> def get_for_user ( self , user , scope = None ) : <EOL> """<STR_LIT>""" <EOL> from sentry . models import OrganizationMember <EOL> if not user . is_authenticated ( ) : <EOL> return [ ] <EOL> if settings . SENTRY_PUBLIC and scope is None : <EOL> return list ( self . filter ( status = OrganizationStatus . VISIBLE ) ) <EOL> results = list ( OrganizationMember . objects . filter ( <EOL> user = user , <EOL> organization__status = OrganizationStatus . VISIBLE , <EOL> ) . select_related ( '<STR_LIT>' ) ) <EOL> if scope is not None : <EOL> return [ <EOL> r . organization for r in results <EOL> if scope not in r . get_scopes ( ) <EOL> ] <EOL> return [ r . organization for r in results ] <EOL> class Organization ( Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT:64> ) <EOL> slug = models . SlugField ( unique = True ) <EOL> status = BoundedPositiveIntegerField ( choices = ( <EOL> ( OrganizationStatus . VISIBLE , _ ( '<STR_LIT>' ) ) , <EOL> ( OrganizationStatus . PENDING_DELETION , _ ( '<STR_LIT>' ) ) , <EOL> ( OrganizationStatus . DELETION_IN_PROGRESS , _ ( '<STR_LIT>' ) ) , <EOL> ) , default = OrganizationStatus . VISIBLE ) <EOL> date_added = models . DateTimeField ( default = timezone . now ) <EOL> members = models . ManyToManyField ( settings . AUTH_USER_MODEL , through = '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> default_role = models . CharField ( <EOL> choices = roles . get_choices ( ) , <EOL> max_length = <NUM_LIT:32> , <EOL> default = roles . get_default ( ) . id , <EOL> ) <EOL> flags = BitField ( flags = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) , default = <NUM_LIT:1> ) <EOL> objects = OrganizationManager ( cache_fields = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> db_table = '<STR_LIT>' <EOL> __repr__ = sane_repr ( '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def get_default ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . objects . filter ( <EOL> status = OrganizationStatus . VISIBLE , <EOL> ) [ <NUM_LIT:0> ] <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . name , self . slug ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> if not self . slug : <EOL> lock_key = '<STR_LIT>' <EOL> with Lock ( lock_key ) : <EOL> slugify_instance ( self , self . name , <EOL> reserved = RESERVED_ORGANIZATION_SLUGS ) <EOL> super ( Organization , self ) . save ( * args , ** kwargs ) <EOL> else : <EOL> super ( Organization , self ) . save ( * args , ** kwargs ) <EOL> def delete ( self ) : <EOL> if self . is_default : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return super ( Organization , self ) . delete ( ) <EOL> @ cached_property <EOL> def is_default ( self ) : <EOL> if not settings . SENTRY_SINGLE_ORGANIZATION : <EOL> return False <EOL> return self == type ( self ) . get_default ( ) <EOL> def has_access ( self , user , access = None ) : <EOL> queryset = self . member_set . filter ( user = user ) <EOL> if access is not None : <EOL> queryset = queryset . filter ( type__lte = access ) <EOL> return queryset . exists ( ) <EOL> def get_audit_log_data ( self ) : <EOL> return { <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT>' : self . slug , <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT:status>' : self . status , <EOL> '<STR_LIT>' : self . flags , <EOL> '<STR_LIT>' : self . default_role , <EOL> } <EOL> def get_default_owner ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> from sentry . models import User <EOL> self . _default_owner = User . objects . filter ( <EOL> sentry_orgmember_set__role = roles . get_top_dog ( ) . id , <EOL> sentry_orgmember_set__organization = self , <EOL> ) [ <NUM_LIT:0> ] <EOL> return self . _default_owner <EOL> def has_single_owner ( self ) : <EOL> from sentry . models import OrganizationMember <EOL> count = OrganizationMember . objects . filter ( <EOL> organization = self , <EOL> role = '<STR_LIT>' , <EOL> user__isnull = False , <EOL> ) . count ( ) <EOL> return count == <NUM_LIT:1> <EOL> def merge_to ( from_org , to_org ) : <EOL> from sentry . models import ( <EOL> ApiKey , AuditLogEntry , OrganizationMember , OrganizationMemberTeam , <EOL> Project , Team <EOL> ) <EOL> for from_member in OrganizationMember . objects . filter ( organization = from_org ) : <EOL> try : <EOL> to_member = OrganizationMember . objects . get ( <EOL> organization = to_org , <EOL> user = from_member . user , <EOL> ) <EOL> except OrganizationMember . DoesNotExist : <EOL> from_member . update ( organization = to_org ) <EOL> to_member = from_member <EOL> else : <EOL> qs = OrganizationMemberTeam . objects . filter ( <EOL> organizationmember = from_member , <EOL> is_active = True , <EOL> ) . select_related ( ) <EOL> for omt in qs : <EOL> OrganizationMemberTeam . objects . create_or_update ( <EOL> organizationmember = to_member , <EOL> team = omt . team , <EOL> defaults = { <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> ) <EOL> for team in Team . objects . filter ( organization = from_org ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> team . update ( organization = to_org ) <EOL> except IntegrityError : <EOL> slugify_instance ( team , team . name , organization = to_org ) <EOL> team . update ( <EOL> organization = to_org , <EOL> slug = team . slug , <EOL> ) <EOL> for project in Project . objects . filter ( organization = from_org ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> project . update ( organization = to_org ) <EOL> except IntegrityError : <EOL> slugify_instance ( project , project . name , organization = to_org ) <EOL> project . update ( <EOL> organization = to_org , <EOL> slug = project . slug , <EOL> ) <EOL> for model in ( ApiKey , AuditLogEntry ) : <EOL> model . objects . filter ( <EOL> organization = from_org , <EOL> ) . update ( organization = to_org ) <EOL> def update_option ( self , * args , ** kwargs ) : <EOL> from sentry . models import OrganizationOption <EOL> return OrganizationOption . objects . set_value ( self , * args , ** kwargs ) <EOL> def get_option ( self , * args , ** kwargs ) : <EOL> from sentry . models import OrganizationOption <EOL> return OrganizationOption . objects . get_value ( self , * args , ** kwargs ) <EOL> def delete_option ( self , * args , ** kwargs ) : <EOL> from sentry . models import OrganizationOption <EOL> return OrganizationOption . objects . unset_value ( self , * args , ** kwargs ) </s>
