
    xh2                        S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	J
r
JrJrJrJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJrJrJrJr  S SKJr  S SKJ r J!r!  S SK"J#r#  S SK$J%r%  S SK&J'r'J(r(  S SK)J*r*  S SK+J,r,  SSK-J.r.  SSK/J0r0   " S S\5      r1 " S S\\5      r2 " S S\\5      r3 " S S\5      r4 " S S\5      r5 " S S \Rl                  5      r7g!)"    )	SocialApp)settings)MaxValueValidator)models)CountFOuterRefQSubquerySum)Coalesceslugify)gettext_lazy)SharedBaseModel)OrganizationBaseOrganizationInvitationBaseOrganizationOwnerBaseOrganizationUserBase)
OrgManager)owner_changed
user_added)DebugInformationFile)clear_metrics_cache) IssueEventProjectHourlyStatistic&TransactionEventProjectHourlyStatistic)DebugSymbolBundle)MonitorCheck   )OrganizationUserRole)OrganizationSlugFieldc                       \ rS rSrSS jrSrg)OrganizationManager    c                 T   U n[        5       n[        5       n[        5       nU(       ai  [        R                  (       aT  [        [        S5      [        S5      S9n[        [        S5      [        S5      S9n[        [        S5      [        S5      S9n[	        [
        R                  R                  [        [        S5      S9U5      R                  S5      R                  [        S	5      S
9R                  S5      R                  5       [        R                  " 5       S9n[	        [        R                  R                  [        [        S5      S9U5      R                  S5      R                  [        S	5      S
9R                  S5      R                  5       [        R                  " 5       S9n[	        [        R                  R                  [        [        S5      S9[        U5      5      R                  S5      R                  [!        S5      S9R                  S5      R                  5       [        R"                  " 5       S9n[	        [$        R                  R                  [        [        S5      S9[        U5      5      R                  S5      R                  [        S5      S9R                  S5      R                  5       [        R                  " 5       S9n	[	        [&        R                  R                  [        [        S5      S9U5      R                  S5      R                  [        S5      S9R                  S5      R                  5       [        R                  " 5       S9n
UR                  [)        US5      [)        US5      [)        US5      [)        [        R*                  " [)        U	S5      [)        U
S5      -   [        R,                  " 5       S9S-  S[        R                  " 5       S9S9R                  [/        S5      [/        S5      -   [/        S5      -   [/        S5      -   S9$ )N1stripe_primary_subscription__current_period_start/stripe_primary_subscription__current_period_end)created__gtecreated__lt)	date__gtedate__lt)start_check__gtestart_check__ltpk)project__organizationr/   count)	sum_countr1   )output_field)monitor__organizationr3   )check_countr4   )organizationr5   file__blob__size)
total_sizer7   r   g    .Ag        )issue_event_counttransaction_countuptime_check_event_count	file_sizer8   r9   r:   r;   )total_event_count)r
   r   BILLING_ENABLEDr	   r   r   objectsfiltervaluesannotater   order_byr   BigIntegerFieldr   r   r   IntegerFieldr   r   r   ExpressionWrapper
FloatFieldr   )selfcurrent_periodquerysetsubscription_filterevent_subscription_filterchecks_subscription_filterissue_event_subquerytransaction_subqueryuptime_check_subquerydebugsymbol_size_subquerydebuginfo_size_subquerys              &/code/apps/organizations_ext/models.pywith_event_counts%OrganizationManager.with_event_counts!   s   c$%C!%&S"h66"#%G %%VW	# )*"#VW!"ST)% *+!)G" !)E!	*&  (,44;;7) V' Xg,   V XZ//1 
&  (2::AA79R V+,XGX-VK XZ//1	 
 !)  ''7,- V' X!$K   V XZ,,.!
( %-%%,,x~.%& V X12   V XZ//1%
!( #+ ((//7# V' X12   V XZ//1#
"   &';Q?&';Q?%-.CQ%G (( !:A>"#:A>? "(!2!2!4  #335 ! 
& ( 34#$%*+, n  	
'	
     N)T)__name__
__module____qualname____firstlineno__rS   __static_attributes__rV   rU   rR   r#   r#       s    O
rU   r#   c            
         ^  \ rS rSr\R
                  " SS9r\" SSSSS\" S5      S9r	\R                  " SS	S
9r\R                  " S\" S5      /SS9r\R                  " SSS
9r\R                  " SSS
9r\R"                  " SSS9r\R&                  " S\R(                  SSSS9r\" 5       rU 4S jrU 4S jrS r\R8                  4S jr\S 5       r\S 5       r S r!S r"S r#Sr$U =r%$ ) Organization   T)primary_key   Fnamez:The name in all lowercase, suitable for URL identification)
max_lengthblankeditablepopulate_fromunique	help_textz Used for throttling at org level)defaultrg   r   d   z_Probability (in percent) on how many events are throttled. Used for throttling at project level)rh   
validatorsrg   z.Allow any organization member to join any teamz7Default for whether projects should script IP Addresses   )rb   rc   zstripe.StripeSubscription+)	on_deleterc   nullrelated_namec                 t   > SnU R                   (       d  Sn[        TU ]  " U0 UD6  U(       a  [        5         g g )NFT)r.   supersaver   )rG   argskwargsnew	__class__s       rR   rr   Organization.save   s3    wwCd%f%! rU   c                 :   > [         TU ]  " U0 UD6  [        5         g N)rq   deleter   rG   rs   rt   rv   s      rR   rz   Organization.delete   s    ''rU   c                 8    / SQn[        U5      nX2;   a  US-   $ U$ )N)loginregisterappprofileorganizationsr   issuesperformance_healthz	rest-authapiacceptstripeadminstatus_page	__debug__z-1r   )rG   contentreserved_wordsslugs       rR   slugify_functionOrganization.slugify_function   s*    
$ w!$;rU   c                 D   U R                   R                  5       R                  5       nUS:X  a  [        R                  nU R
                  R                  R                  XUS9nUS:X  a#  U R                  R                  R                  XS9  [        R                  " XS9  U$ )zN
Adds a new user and if the first user makes the user an admin and
the owner.
r   )userr5   role)r5   organization_user)senderr   )usersallr0   r    OWNER_org_user_modelr>   create_org_owner_modelr   send)rG   r   r   users_countorg_users        rR   add_userOrganization.add_user   s    
 jjnn&,,.!'--D''//66t 7 
 !!!))00! 1 
 	t/rU   c                 P    U R                   R                  [        R                  S9$ )N)(organizations_ext_organizationuser__role)r   r?   r    r   rG   s    rR   ownersOrganization.owners  s'    zz  5I5O5O ! 
 	
rU   c                 Z    U R                   R                  R                  nUR                  $ )z,Used to identify billing contact for stripe.)ownerr   r   email)rG   billing_contacts     rR   r   Organization.email  s%     **66;;$$$rU   c                 T    U R                   R                  US9nUR                  5       $ )N)r   )organization_usersget
get_scopes)rG   r   r   s      rR   get_user_scopesOrganization.get_user_scopes  s*    **..D.9""$$rU   c                     U R                   R                  nXR                   l        U R                   R                  5         [        R                  " XUS9  g)z'
Changes ownership of an organization.
)r   oldru   N)r   r   rr   r   r   )rG   	new_owner	old_owners      rR   change_ownerOrganization.change_owner  s;     JJ00	'0

$

$9ErU   c                 H    U R                   R                  R                  U:H  $ )zC
Returns True is user is the organization's owner, otherwise false
)r   r   r   )rG   r   s     rR   is_ownerOrganization.is_owner)  s     zz++00D88rU   rV   )&rW   rX   rY   rZ   r   	AutoFieldidr!   _r   BooleanFieldis_accepting_eventsPositiveSmallIntegerFieldr   event_throttle_rateopen_membershipscrub_ip_addresses	CharFieldstripe_customer_id
ForeignKeySET_NULLstripe_primary_subscriptionr#   r>   rr   rz   r   r    MEMBERr   propertyr   r   r   r   r   r[   __classcell__rv   s   @rR   r]   r]      sB   			d	+B PQD !-- B !::%c*+s
 )) PO  ,,K  ))RtD"("3"3#//# "#G"0 #7"="= ( 
 

 % %
%F9 9rU   r]   c                     ^  \ rS rSr\R
                  " SSS\R                  SS9r\R                  " \	R                  S9r\R                  " SSSS9r " S	 S
\R                  5      rU 4S jrS rS rS r\S 5       r\S 5       rSrU =r$ )OrganizationUseri0  z
users.UserT"organizations_ext_organizationuser)rc   rn   rm   ro   )choiceszEmail for pending invite)rc   rn   rg   c                       \ rS rSrSrSrg)OrganizationUser.Metai=  ))r   r5   )r   r5   rV   N)rW   rX   rY   rZ   unique_togetherr[   rV   rU   rR   Metar   =  s    OrU   r   c                 ^   > U R                   (       a  [        TU ]  " U0 UD6$ U R                  $ ry   )r   rq   __str__r   r{   s      rR   r   OrganizationUser.__str__@  s(    997?D3F33zzrU   c                 h    U R                   (       a  U R                   R                  $ U R                  $ ry   )r   r   r   s    rR   	get_emailOrganizationUser.get_emailE  s!    9999??"zzrU   c                 >    U R                  5       R                  5       $ ry   )get_role_displaylowerr   s    rR   get_roleOrganizationUser.get_roleJ  s    $$&,,..rU   c                 L    [         R                  " U R                  5      nUS   $ )Nscopes)r    r   r   )rG   r   s     rR   r   OrganizationUser.get_scopesM  s     #,,TYY7H~rU   c                     U R                   S L $ ry   )user_idr   s    rR   pendingOrganizationUser.pendingQ  s    ||t##rU   c                 $    U R                   (       + $ )zNon pending means active)r   r   s    rR   	is_activeOrganizationUser.is_activeU  s     <<rU   rV   )rW   rX   rY   rZ   r   r   CASCADEr   r   r    r   r   
EmailFieldr   r   r   r   r   r   r   r   r   r   r[   r   r   s   @rR   r   r   0  s    ..9D ++4H4P4PQD)CEP$)) P

/ $ $    rU   r   c                       \ rS rSrSrSrg)OrganizationOwneri[  z+Only usage is for billing contact currentlyrV   NrW   rX   rY   rZ   __doc__r[   rV   rU   rR   r   r   [  s    5rU   r   c                       \ rS rSrSrSrg)OrganizationInvitationi_  z*Required to exist for django-organizationsrV   Nr   rV   rU   rR   r   r   _  s    4rU   r   c                       \ rS rSrSr\R                  " \\R                  S9r	\R                  " \\R                  S9rSrg)OrganizationSocialAppic  z
Associate organization with social app, for authentication purposes.
Example: If Foo org has social app FooGoogle, then any user logging in via FooGoogle
OAuth must be automatically assigned to the Foo org.
)rm   rV   N)rW   rX   rY   rZ   r   r   r   r]   r   r5   OneToOneFieldr   
social_appr[   rV   rU   rR   r   r   c  s5     $$\V^^LL%%i6>>JJrU   r   N)8allauth.socialaccount.modelsr   django.confr   django.core.validatorsr   	django.dbr   django.db.modelsr   r   r	   r
   r   r   django.db.models.functionsr   django.utils.textr   django.utils.translationr   r   organizations.abstractr   organizations.baser   r   r   r   organizations.managersr   organizations.signalsr   r   apps.difs.modelsr   apps.observability.utilsr   apps.projects.modelsr   r   apps.sourcecode.modelsr   apps.uptime.modelsr   	constantsr    fieldsr!   r#   r]   r   r   r   Modelr   rV   rU   rR   <module>r	     s    2   4  A A / % 6 2  . ; 1 8 5 + + )P
* P
fz9?$4 z9z( (< ( V6- 657 5KFLL KrU   