<s> from __future__ import print_function , absolute_import <EOL> from django . db import IntegrityError , transaction <EOL> from django . db . models import Q <EOL> from django . utils import timezone <EOL> from sentry . models import ( <EOL> OnboardingTask , <EOL> OnboardingTaskStatus , <EOL> OrganizationOnboardingTask , <EOL> OrganizationOption <EOL> ) <EOL> from sentry . plugins import IssueTrackingPlugin , NotificationPlugin <EOL> from sentry . signals import ( <EOL> event_processed , <EOL> project_created , <EOL> first_event_pending , <EOL> first_event_received , <EOL> member_invited , <EOL> member_joined , <EOL> plugin_enabled , <EOL> issue_tracker_used , <EOL> ) <EOL> from sentry . utils . javascript import has_sourcemap <EOL> def check_for_onboarding_complete ( organization ) : <EOL> if OrganizationOption . objects . filter ( organization = organization , key = "<STR_LIT>" ) . exists ( ) : <EOL> return <EOL> completed = set ( OrganizationOnboardingTask . objects . filter ( Q ( organization = organization ) & ( Q ( status = OnboardingTaskStatus . COMPLETE ) | Q ( status = OnboardingTaskStatus . SKIPPED ) ) ) . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> if completed >= OnboardingTask . REQUIRED_ONBOARDING_TASKS : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOption . objects . create ( <EOL> organization = organization , <EOL> key = "<STR_LIT>" , <EOL> value = { '<STR_LIT>' : timezone . now ( ) } <EOL> ) <EOL> except IntegrityError : <EOL> pass <EOL> @ project_created . connect ( weak = False ) <EOL> def record_new_project ( project , user , ** kwargs ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . FIRST_PROJECT , <EOL> user = user , <EOL> status = OnboardingTaskStatus . COMPLETE , <EOL> project_id = project . id , <EOL> date_completed = timezone . now ( ) , <EOL> ) <EOL> except IntegrityError : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . SECOND_PLATFORM , <EOL> user = user , <EOL> status = OnboardingTaskStatus . PENDING , <EOL> project_id = project . id , <EOL> date_completed = timezone . now ( ) , <EOL> ) <EOL> except IntegrityError : <EOL> pass <EOL> @ first_event_pending . connect ( weak = False ) <EOL> def record_raven_installed ( project , user , ** kwargs ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . FIRST_EVENT , <EOL> status = OnboardingTaskStatus . PENDING , <EOL> user = user , <EOL> project_id = project . id , <EOL> date_completed = timezone . now ( ) <EOL> ) <EOL> except IntegrityError : <EOL> pass <EOL> @ first_event_received . connect ( weak = False ) <EOL> def record_first_event ( project , group , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> rows_affected , created = OrganizationOnboardingTask . objects . create_or_update ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . FIRST_EVENT , <EOL> status = OnboardingTaskStatus . PENDING , <EOL> values = { <EOL> '<STR_LIT:status>' : OnboardingTaskStatus . COMPLETE , <EOL> '<STR_LIT>' : project . id , <EOL> '<STR_LIT>' : project . first_event , <EOL> '<STR_LIT:data>' : { '<STR_LIT>' : group . platform } , <EOL> } <EOL> ) <EOL> if not rows_affected and not created : <EOL> try : <EOL> oot = OrganizationOnboardingTask . objects . filter ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . FIRST_EVENT <EOL> ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return <EOL> if oot . project_id != project . id and oot . data . get ( '<STR_LIT>' , group . platform ) != group . platform : <EOL> OrganizationOnboardingTask . objects . create_or_update ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . SECOND_PLATFORM , <EOL> status = OnboardingTaskStatus . PENDING , <EOL> values = { <EOL> '<STR_LIT:status>' : OnboardingTaskStatus . COMPLETE , <EOL> '<STR_LIT>' : project . id , <EOL> '<STR_LIT>' : project . first_event , <EOL> '<STR_LIT:data>' : { '<STR_LIT>' : group . platform } , <EOL> } <EOL> ) <EOL> @ member_invited . connect ( weak = False ) <EOL> def record_member_invited ( member , user , ** kwargs ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = member . organization , <EOL> task = OnboardingTask . INVITE_MEMBER , <EOL> user = user , <EOL> status = OnboardingTaskStatus . PENDING , <EOL> date_completed = timezone . now ( ) , <EOL> data = { '<STR_LIT>' : member . id } <EOL> ) <EOL> except IntegrityError : <EOL> pass <EOL> @ member_joined . connect ( weak = False ) <EOL> def record_member_joined ( member , ** kwargs ) : <EOL> rows_affected , created = OrganizationOnboardingTask . objects . create_or_update ( <EOL> organization = member . organization , <EOL> task = OnboardingTask . INVITE_MEMBER , <EOL> status = OnboardingTaskStatus . PENDING , <EOL> values = { <EOL> '<STR_LIT:status>' : OnboardingTaskStatus . COMPLETE , <EOL> '<STR_LIT>' : timezone . now ( ) , <EOL> '<STR_LIT:data>' : { '<STR_LIT>' : member . id } <EOL> } <EOL> ) <EOL> if created or rows_affected : <EOL> check_for_onboarding_complete ( organization = member . organization ) <EOL> @ event_processed . connect ( weak = False ) <EOL> def record_release_received ( project , group , event , ** kwargs ) : <EOL> if event . get_tag ( '<STR_LIT>' ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . RELEASE_TRACKING , <EOL> status = OnboardingTaskStatus . COMPLETE , <EOL> project_id = project . id , <EOL> date_completed = timezone . now ( ) <EOL> ) <EOL> check_for_onboarding_complete ( project . organization ) <EOL> except IntegrityError : <EOL> pass <EOL> @ event_processed . connect ( weak = False ) <EOL> def record_user_context_received ( project , group , event , ** kwargs ) : <EOL> if event . data . get ( '<STR_LIT>' ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . USER_CONTEXT , <EOL> status = OnboardingTaskStatus . COMPLETE , <EOL> project_id = project . id , <EOL> date_completed = timezone . now ( ) <EOL> ) <EOL> check_for_onboarding_complete ( project . organization ) <EOL> except IntegrityError : <EOL> pass <EOL> @ event_processed . connect ( weak = False ) <EOL> def record_sourcemaps_received ( project , group , event , ** kwargs ) : <EOL> if has_sourcemap ( event ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . SOURCEMAPS , <EOL> status = OnboardingTaskStatus . COMPLETE , <EOL> project_id = project . id , <EOL> date_completed = timezone . now ( ) <EOL> ) <EOL> check_for_onboarding_complete ( project . organization ) <EOL> except IntegrityError : <EOL> pass <EOL> @ plugin_enabled . connect ( weak = False ) <EOL> def record_plugin_enabled ( plugin , project , user , ** kwargs ) : <EOL> if isinstance ( plugin , IssueTrackingPlugin ) : <EOL> task = OnboardingTask . ISSUE_TRACKER <EOL> status = OnboardingTaskStatus . PENDING <EOL> elif isinstance ( plugin , NotificationPlugin ) : <EOL> task = OnboardingTask . NOTIFICATION_SERVICE <EOL> status = OnboardingTaskStatus . COMPLETE <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> OrganizationOnboardingTask . objects . create ( <EOL> organization = project . organization , <EOL> task = task , <EOL> status = status , <EOL> user = user , <EOL> project_id = project . id , <EOL> date_completed = timezone . now ( ) , <EOL> data = { '<STR_LIT>' : plugin . slug } <EOL> ) <EOL> check_for_onboarding_complete ( project . organization ) <EOL> except IntegrityError : <EOL> pass <EOL> @ issue_tracker_used . connect ( weak = False ) <EOL> def record_issue_tracker_used ( plugin , project , user , ** kwargs ) : <EOL> rows_affected , created = OrganizationOnboardingTask . objects . create_or_update ( <EOL> organization = project . organization , <EOL> task = OnboardingTask . ISSUE_TRACKER , <EOL> status = OnboardingTaskStatus . PENDING , <EOL> values = { <EOL> '<STR_LIT:status>' : OnboardingTaskStatus . COMPLETE , <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : project . id , <EOL> '<STR_LIT>' : timezone . now ( ) , <EOL> '<STR_LIT:data>' : { '<STR_LIT>' : plugin . slug } <EOL> } <EOL> ) <EOL> if rows_affected or created : <EOL> check_for_onboarding_complete ( project . organization ) </s>
