Exemple #1
0
    def save_model(self, request, obj, form, change):
        """
        On save, creates or updates the corresponding catalog in the enterprise-catalog IDA.

        `change` indicates whether or not the object is being updated (as opposed to created).
        """
        catalog_uuid = obj.uuid
        catalog_client = EnterpriseCatalogApiClient(user=request.user)

        if change:
            update_fields = {
                'enterprise_customer': str(obj.enterprise_customer.uuid),
                'title': obj.title,
                'content_filter': obj.content_filter,
                'enabled_course_modes': obj.enabled_course_modes,
                'publish_audit_enrollment_urls':
                obj.publish_audit_enrollment_urls,
            }
            catalog_client.update_enterprise_catalog(catalog_uuid,
                                                     **update_fields)
        else:
            catalog_client.create_enterprise_catalog(
                str(catalog_uuid),
                str(obj.enterprise_customer.uuid),
                obj.title,
                obj.content_filter,
                obj.enabled_course_modes,
                obj.publish_audit_enrollment_urls,
            )

        super(EnterpriseCustomerCatalogAdmin,
              self).save_model(request, obj, form, change)
Exemple #2
0
def update_enterprise_catalog_data(sender, instance, **kwargs):  # pylint: disable=unused-argument
    """
    Send data changes to Enterprise Catalogs to the Enterprise Catalog Service.

    Additionally sends a request to update the catalog's metadata from discovery, and index any relevant content for
    Algolia.
    """
    catalog_uuid = instance.uuid
    try:
        catalog_client = EnterpriseCatalogApiClient()
        if kwargs['created']:
            response = catalog_client.get_enterprise_catalog(
                catalog_uuid=catalog_uuid,
                # Suppress 404 exception on create since we do not expect the catalog
                # to exist yet in enterprise-catalog
                should_raise_exception=False,
            )
        else:
            response = catalog_client.get_enterprise_catalog(
                catalog_uuid=catalog_uuid)
    except NotConnectedToOpenEdX as exc:
        logger.exception('Unable to update Enterprise Catalog {}'.format(
            str(catalog_uuid)),
                         exc_info=exc)
    else:
        if not response:
            # catalog with matching uuid does NOT exist in enterprise-catalog
            # service, so we should create a new catalog
            catalog_client.create_enterprise_catalog(
                str(catalog_uuid),
                str(instance.enterprise_customer.uuid),
                instance.enterprise_customer.name,
                instance.title,
                instance.content_filter,
                instance.enabled_course_modes,
                instance.publish_audit_enrollment_urls,
            )
        else:
            # catalog with matching uuid does exist in enterprise-catalog
            # service, so we should update the existing catalog
            update_fields = {
                'enterprise_customer':
                str(instance.enterprise_customer.uuid),
                'enterprise_customer_name':
                instance.enterprise_customer.name,
                'title':
                instance.title,
                'content_filter':
                instance.content_filter,
                'enabled_course_modes':
                instance.enabled_course_modes,
                'publish_audit_enrollment_urls':
                instance.publish_audit_enrollment_urls,
            }
            catalog_client.update_enterprise_catalog(catalog_uuid,
                                                     **update_fields)
        # Refresh catalog on all creates and updates
        catalog_client.refresh_catalogs([instance])
def update_enterprise_catalog_data(sender, instance, **kwargs):  # pylint: disable=unused-argument
    """
    Send data changes to Enterprise Catalogs to the Enterprise Catalog Service.
    """
    catalog_uuid = instance.uuid
    try:
        catalog_client = EnterpriseCatalogApiClient()
        response = catalog_client.get_enterprise_catalog(catalog_uuid)
    except NotConnectedToOpenEdX as exc:
        logger.exception(
            'Unable to update Enterprise Catalog {}'.format(str(catalog_uuid)),
            exc)
    else:
        if not response:
            # catalog with matching uuid does NOT exist in enterprise-catalog
            # service, so we should create a new catalog
            catalog_client.create_enterprise_catalog(
                str(catalog_uuid),
                str(instance.enterprise_customer.uuid),
                instance.enterprise_customer.name,
                instance.title,
                instance.content_filter,
                instance.enabled_course_modes,
                instance.publish_audit_enrollment_urls,
            )
        else:
            # catalog with matching uuid does exist in enterprise-catalog
            # service, so we should update the existing catalog
            update_fields = {
                'enterprise_customer':
                str(instance.enterprise_customer.uuid),
                'enterprise_customer_name':
                instance.enterprise_customer.name,
                'title':
                instance.title,
                'content_filter':
                instance.content_filter,
                'enabled_course_modes':
                instance.enabled_course_modes,
                'publish_audit_enrollment_urls':
                instance.publish_audit_enrollment_urls,
            }
            catalog_client.update_enterprise_catalog(catalog_uuid,
                                                     **update_fields)
Exemple #4
0
    def handle(self, *args, **options):
        api_username = options['api_user']
        try:
            user = User.objects.get(username=api_username)
        except User.DoesNotExist:
            raise CommandError(
                _('A user with the username {username} was not found.').format(
                    username=api_username))

        client = EnterpriseCatalogApiClient(user=user)

        catalog_uuids_string = options.get('catalog_uuids')
        if catalog_uuids_string:
            catalog_uuids_list = catalog_uuids_string.split(',')
            queryset = EnterpriseCustomerCatalog.objects.filter(
                uuid__in=catalog_uuids_list)
        else:
            queryset = EnterpriseCustomerCatalog.objects.all()

        for enterprise_catalog in queryset:
            LOGGER.info('Migrating Enterprise Catalog {}'.format(
                enterprise_catalog.uuid))
            try:
                response = client.get_enterprise_catalog(
                    enterprise_catalog.uuid)
                if not response:
                    # catalog with matching uuid does NOT exist in enterprise-catalog
                    # service, so we should create a new catalog
                    client.create_enterprise_catalog(
                        str(enterprise_catalog.uuid),
                        str(enterprise_catalog.enterprise_customer.uuid),
                        enterprise_catalog.enterprise_customer.name,
                        enterprise_catalog.title,
                        enterprise_catalog.content_filter,
                        enterprise_catalog.enabled_course_modes,
                        enterprise_catalog.publish_audit_enrollment_urls,
                    )
                else:
                    # catalog with matching uuid does exist in enterprise-catalog
                    # service, so we should update the existing catalog
                    update_fields = {
                        'enterprise_customer':
                        str(enterprise_catalog.enterprise_customer.uuid),
                        'enterprise_customer_name':
                        enterprise_catalog.enterprise_customer.name,
                        'title':
                        enterprise_catalog.title,
                        'content_filter':
                        enterprise_catalog.content_filter,
                        'enabled_course_modes':
                        enterprise_catalog.enabled_course_modes,
                        'publish_audit_enrollment_urls':
                        enterprise_catalog.publish_audit_enrollment_urls,
                    }
                    client.update_enterprise_catalog(
                        str(enterprise_catalog.uuid),
                        **update_fields,
                    )
                LOGGER.info(
                    'Successfully migrated Enterprise Catalog {}'.format(
                        enterprise_catalog.uuid))
            except Exception:  # pylint: disable=broad-except
                LOGGER.exception(
                    'Failed to migrate enterprise catalog {}'.format(
                        enterprise_catalog.uuid))