def dispatch_license_create_events(sender, **kwargs): # pylint: disable=unused-argument """ Post creation hook to handle tracking license lifecycle events that could have been created in a variety of states. """ license_obj = kwargs['instance'] is_new_license = kwargs.get('created', False) if not is_new_license: # Update events are handled by more explicit tracking events around the codebase # to map them more easily with the events we want to track. return event_properties = get_license_tracking_properties(license_obj) # We always send a creation event. track_event(license_obj.lms_user_id, SegmentEvents.LICENSE_CREATED, event_properties) # If the license has extra statuses on creation that would normally fire events, # then programmatically fire events for those also if license_obj.status == ASSIGNED: track_event(license_obj.lms_user_id, SegmentEvents.LICENSE_ASSIGNED, event_properties) if license_obj.status == ACTIVATED: track_event(license_obj.lms_user_id, SegmentEvents.LICENSE_ACTIVATED, event_properties)
def revoke(self): """ Performs all field updates required to revoke a License """ self.status = REVOKED self.revoked_date = localized_utcnow() self.save() event_properties = get_license_tracking_properties(self) track_event(self.lms_user_id, SegmentEvents.LICENSE_REVOKED, event_properties)
def test_get_license_tracking_properties(): test_email = '*****@*****.**' assigned_license = LicenseFactory.create( subscription_plan=SubscriptionPlanFactory.create(), lms_user_id=5, user_email=test_email, status=ASSIGNED, auto_applied=True) flat_data = get_license_tracking_properties(assigned_license) assert flat_data['license_uuid'] == str(assigned_license.uuid) assert flat_data['license_activation_key'] == str( assigned_license.activation_key) assert flat_data['previous_license_uuid'] == '' assert flat_data['assigned_date'] == _iso_8601_format_string( assigned_license.assigned_date) assert flat_data['activation_date'] == _iso_8601_format_string( assigned_license.activation_date) assert flat_data['assigned_lms_user_id'] == assigned_license.lms_user_id assert flat_data['assigned_email'] == test_email assert flat_data['auto_applied'] is True assert flat_data['enterprise_customer_uuid'] \ == str(assigned_license.subscription_plan.customer_agreement.enterprise_customer_uuid) assert flat_data['enterprise_customer_slug'] \ == assigned_license.subscription_plan.customer_agreement.enterprise_customer_slug assert flat_data['expiration_processed'] \ == assigned_license.subscription_plan.expiration_processed assert flat_data['customer_agreement_uuid'] \ == str(assigned_license.subscription_plan.customer_agreement.uuid) assert flat_data['enterprise_customer_name'] \ == assigned_license.subscription_plan.customer_agreement.enterprise_customer_name # Check that all the data is a basic type that can be serialized so it will be clean in segment: for k, v in flat_data.items(): assert isinstance(k, str) assert (isinstance(v, str) or isinstance(v, int) or isinstance(v, bool))
def dispatch_license_delete_event(sender, **kwargs): # pylint: disable=unused-argument license_obj = kwargs['instance'] event_properties = get_license_tracking_properties(license_obj) track_event(license_obj.lms_user_id, SegmentEvents.LICENSE_DELETED, event_properties)
def handle(self, *args, **options): ready_for_retirement_date = localized_utcnow() - timedelta( DAYS_TO_RETIRE) expired_licenses_for_retirement = License.get_licenses_exceeding_purge_duration( 'subscription_plan__expiration_date', ) # Scrub all piii on licenses whose subscription expired over 90 days ago, and mark the licenses as revoked for expired_license in expired_licenses_for_retirement: original_lms_user_id = expired_license.lms_user_id # record event data BEFORE we clear the license data: event_properties = get_license_tracking_properties(expired_license) expired_license.clear_pii() expired_license.status = REVOKED expired_license.revoked_date = localized_utcnow() expired_license.save() event_properties = get_license_tracking_properties(expired_license) track_event(original_lms_user_id, SegmentEvents.LICENSE_REVOKED, event_properties) # Clear historical pii after removing pii from the license itself expired_license.clear_historical_pii() expired_license_uuids = sorted([ expired_license.uuid for expired_license in expired_licenses_for_retirement ]) message = 'Retired {} expired licenses with uuids: {}'.format( len(expired_license_uuids), expired_license_uuids) logger.info(message) revoked_licenses_for_retirement = License.get_licenses_exceeding_purge_duration( 'revoked_date', status=REVOKED, ) # Scrub all pii on the revoked licenses, but they should stay revoked and keep their other info as we currently # add an unassigned license to the subscription's license pool whenever one is revoked. for revoked_license in revoked_licenses_for_retirement: revoked_license.clear_pii() revoked_license.save() # Clear historical pii after removing pii from the license itself revoked_license.clear_historical_pii() revoked_license_uuids = sorted([ revoked_license.uuid for revoked_license in revoked_licenses_for_retirement ]) message = 'Retired {} revoked licenses with uuids: {}'.format( len(revoked_license_uuids), revoked_license_uuids) logger.info(message) # Any license that was assigned but not activated before the associated agreement's # ``unactivated_license_duration`` elapsed should have its data scrubbed. assigned_licenses_for_retirement = License.get_licenses_exceeding_purge_duration( 'assigned_date', status=ASSIGNED, ) # We place previously assigned licenses that are now retired back into the unassigned license pool, so we scrub # all data on them. for assigned_license in assigned_licenses_for_retirement: assigned_license.reset_to_unassigned() assigned_license.save() # Clear historical pii after removing pii from the license itself assigned_license.clear_historical_pii() assigned_license_uuids = sorted([ assigned_license.uuid for assigned_license in assigned_licenses_for_retirement ], ) message = 'Retired {} assigned licenses that exceeded their inactivation duration with uuids: {}'.format( len(assigned_license_uuids), assigned_license_uuids, ) logger.info(message)