Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: updating catalog post save signal process of updating catalogs in catalog service #2142

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions enterprise/api_client/enterprise_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class EnterpriseCatalogApiClient(UserAPIClient):

API_BASE_URL = urljoin(f"{settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL}/", "api/v1/")
ENTERPRISE_CATALOG_ENDPOINT = 'enterprise-catalogs'
CATALOG_QUERY_ENDPOINT = 'catalog-queries'
GET_CONTENT_METADATA_ENDPOINT = ENTERPRISE_CATALOG_ENDPOINT + '/{}/get_content_metadata'
REFRESH_CATALOG_ENDPOINT = ENTERPRISE_CATALOG_ENDPOINT + '/{}/refresh_metadata'
CATALOG_DIFF_ENDPOINT = ENTERPRISE_CATALOG_ENDPOINT + '/{}/generate_diff'
Expand All @@ -33,6 +34,7 @@ class EnterpriseCatalogApiClient(UserAPIClient):
"/{}/content-metadata/" + "{}"
APPEND_SLASH = True
GET_CONTENT_METADATA_PAGE_SIZE = getattr(settings, 'ENTERPRISE_CATALOG_GET_CONTENT_METADATA_PAGE_SIZE', 50)
GET_QUERY_BY_HASH_ENDPOINT = CATALOG_QUERY_ENDPOINT + '/get_query_by_hash'

def __init__(self, user=None):
user = user if user else utils.get_enterprise_worker_user()
Expand Down Expand Up @@ -353,6 +355,20 @@ def get_content_metadata_content_identifier(self, enterprise_uuid, content_id):
)
return {}

@UserAPIClient.refresh_token
def get_catalog_query_by_hash(self, hash):
"""
Placeholder
"""
try:
api_url = self.get_api_url(f"{self.GET_QUERY_BY_HASH_ENDPOINT}").rstrip('/') + f'?hash={hash}'
response = self.client.get(api_url) # type: ignore
response.raise_for_status()
return response.json()
except Exception as exc:
print(exc)
pass


class NoAuthEnterpriseCatalogClient(NoAuthAPIClient):
"""
Expand Down
25 changes: 25 additions & 0 deletions enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import collections
import datetime
import hashlib
import itertools
import json
from decimal import Decimal
Expand Down Expand Up @@ -2418,6 +2419,13 @@ def delete(self, *args, **kwargs):
)


@cached_property
def get_content_filter_hash(self):
content_filter_sorted_keys = json.dumps(self.content_filter, sort_keys=True).encode()
content_filter_hash = hashlib.md5(content_filter_sorted_keys).hexdigest()
return content_filter_hash


class BulkCatalogQueryUpdateCommandConfiguration(ConfigurationModel):
"""
Manages configuration for a run of the cert_generation management command.
Expand Down Expand Up @@ -2748,6 +2756,11 @@ def get_program_enrollment_url(self, program_uuid):

return utils.update_query_parameters(url, {'catalog': self.uuid})

def get_content_filter_hash(self):
content_filter_sorted_keys = json.dumps(self.content_filter, sort_keys=True).encode()
content_filter_hash = hashlib.md5(content_filter_sorted_keys).hexdigest()
return content_filter_hash

def save(self, *args, **kwargs):
"""
Saves this ``EnterpriseCatalogQuery``.
Expand All @@ -2758,6 +2771,18 @@ def save(self, *args, **kwargs):
if self.enterprise_catalog_query:
content_filter_from_query = self.enterprise_catalog_query.content_filter
self.content_filter = content_filter_from_query
else:
try:
old_catalog = EnterpriseCustomerCatalog.objects.get(pk=self.pk)
if self.content_filter != old_catalog.content_filter:
new_content_hash = self.get_content_filter_hash()
client = EnterpriseCatalogApiClient()
print(client.user)
queries_by_hash = client.get_catalog_query_by_hash(new_content_hash)
print(queries_by_hash)
except EnterpriseCustomerCatalog.DoesNotExist:
pass

super().save(*args, **kwargs)


Expand Down
Loading