Exemple #1
0
def track_license_changes_task(self,
                               license_uuids,
                               event_name,
                               properties=None):
    """
    Calls ``track_license_changes()`` on some chunks of licenses.

    Args:
        license_uuid (list): List of license uuids
        event_name (str): Name of the event in the format of:
            edx.server.license-manager.license-lifecycle.<new-status>, see constants.SegmentEvents
        properties: (dict): Additional properties to track for each event,
            overrides fields from get_license_tracking_properties
    Returns:
        None
    """
    properties = properties or {}
    # We chunk these up as to not fetch too many license records from the DB in a single query.
    for uuid_str_chunk in chunks(license_uuids, 10):
        license_uuid_chunk = [
            uuid.UUID(uuid_str) for uuid_str in uuid_str_chunk
        ]
        licenses = License.objects.filter(uuid__in=license_uuid_chunk)
        track_license_changes(licenses, event_name, properties)
        logger.info(
            'Task {} tracked license changes for license uuids {}'.format(
                self.request.id,
                license_uuid_chunk,
            ))
Exemple #2
0
def test_track_license_changes_with_properties(mock_track_event, _):
    licenses = LicenseFactory.create_batch(5)
    track_license_changes(licenses, SegmentEvents.LICENSE_CREATED,
                          {'counter': 2})
    assert mock_track_event.call_count == 5
    mock_track_event.assert_called_with(None, SegmentEvents.LICENSE_CREATED,
                                        {'counter': 2})
Exemple #3
0
def _renew_all_licenses(original_licenses, future_plan, is_auto_renewed):
    """
    We assume at this point that the future plan has at least as many licenses
    as the number of licenses in the original plan.  Does a bulk update of
    the renewed licenses.
    """
    future_licenses = []

    for original_license, future_license in zip(original_licenses, future_plan.licenses.all()):
        future_license.status = original_license.status
        future_license.user_email = original_license.user_email
        future_license.lms_user_id = original_license.lms_user_id
        future_license.activation_key = original_license.activation_key
        future_license.assigned_date = localized_utcnow()
        if original_license.status == ACTIVATED:
            future_license.activation_date = future_plan.start_date

        future_licenses.append(future_license)

        original_license.renewed_to = future_license
    License.bulk_update(
        future_licenses,
        ['status', 'user_email', 'lms_user_id', 'activation_date', 'assigned_date'],
    )
    License.bulk_update(
        original_licenses,
        ['renewed_to'],
    )

    event_utils.track_license_changes(future_licenses, SegmentEvents.LICENSE_RENEWED, {
        'is_auto_renewed': is_auto_renewed
    })
Exemple #4
0
def dispatch_license_expiration_event(sender, **kwargs):  # pylint: disable=unused-argument
    """
    Post save hook to handle tracking license lifecycle events:
    Sends an expiration event for all linked licenses when a top level subscription plan is marked as
    expired and individual license WASN'T renewed.
    """
    # if we updated the expiration_processed field and it's true now:
    subscription_plan_obj = kwargs['instance']
    update_fields = kwargs.get('update_fields', None)

    if subscription_plan_obj and update_fields and 'expiration_processed' in update_fields:
        expired_licenses = [lcs for lcs in subscription_plan_obj.licenses.all() if not lcs.renewed_to]
        track_license_changes(expired_licenses, SegmentEvents.LICENSE_EXPIRED)
Exemple #5
0
    def bulk_create(cls, license_objects, batch_size=LICENSE_BULK_OPERATION_BATCH_SIZE):
        """
        django-simple-history functions by saving history using a post_save signal every time that
        an object with history is saved. However, for certain bulk operations, such as bulk_create, bulk_update,
        and queryset updates, signals are not sent, and the history is not saved automatically.
        However, django-simple-history provides utility functions to work around this.

        https://django-simple-history.readthedocs.io/en/2.12.0/common_issues.html#bulk-creating-and-queryset-updating
        """
        bulk_create_with_history(license_objects, cls, batch_size=batch_size)

        # Since bulk_create does not call post_save, handle tracking events manually:
        track_license_changes(license_objects, SegmentEvents.LICENSE_CREATED)