def create(self, validated_data): content_filter = validated_data.pop('content_filter') catalog_query, _ = CatalogQuery.objects.get_or_create( content_filter_hash=get_content_filter_hash(content_filter), defaults={'content_filter': content_filter}, ) try: catalog = EnterpriseCatalog.objects.create( **validated_data, catalog_query=catalog_query ) except IntegrityError as exc: message = ( 'Encountered the following error in the create serializer: %s | ' 'content_filter: %s | ' 'catalog_query id: %s | ' 'validated_data: %s' ) logger.error(message, exc, content_filter, catalog_query.id, validated_data) raise async_task = update_catalog_metadata_task.delay(catalog_query_id=catalog.catalog_query.id) message = ( 'Spinning off update_catalog_metadata_task (%s) from create serializer ' 'to update content_metadata for catalog %s' ) logger.info(message, async_task.task_id, catalog) return catalog
def clean_content_filter(self): content_filter = self.cleaned_data['content_filter'] self.validate_content_filter_fields(content_filter) content_filter_hash = get_content_filter_hash(content_filter) if CatalogQuery.objects.filter(content_filter_hash=content_filter_hash).exists(): raise ValidationError('Catalog Query with this Content filter already exists.') return content_filter
def test_new_uuid_old_filter_saves_query_with_new_uuid(self): old_filter = {'key': ['course:testing']} CatalogQuery.objects.create( content_filter=old_filter, content_filter_hash=get_content_filter_hash(old_filter) ) new_uuid = uuid4() result = find_and_modify_catalog_query(old_filter, new_uuid) self.assertEqual((result.content_filter, result.uuid), (old_filter, new_uuid))
def setUp(self): super().setUp() self.old_uuid = uuid4() self.old_filter = {'key': ['arglblargl']} self.old_catalog_query = CatalogQuery.objects.create( content_filter=self.old_filter, content_filter_hash=get_content_filter_hash(self.old_filter), uuid=self.old_uuid )
def test_old_uuid_new_filter_saves_query_with_new_filter(self): old_uuid = uuid4() old_filter = {'key': ['plpplplpl']} new_filter = {'key': ['roger']} CatalogQuery.objects.create( content_filter=old_filter, content_filter_hash=get_content_filter_hash(old_filter), uuid=old_uuid ) result = find_and_modify_catalog_query(new_filter, old_uuid) self.assertEqual((result.content_filter, result.uuid), (new_filter, old_uuid))
def update(self, instance, validated_data): default_content_filter = None if instance.catalog_query: default_content_filter = instance.catalog_query.content_filter content_filter = validated_data.get('content_filter', default_content_filter) instance.catalog_query, _ = CatalogQuery.objects.get_or_create( content_filter_hash=get_content_filter_hash(content_filter), defaults={'content_filter': content_filter}, ) async_task = update_catalog_metadata_task.delay(catalog_query_id=instance.catalog_query.id) message = ( 'Spinning off update_catalog_metadata_task (%s) from update serializer ' 'to update content_metadata for catalog %s' ) logger.info(message, async_task.task_id, instance) return super().update(instance, validated_data)
def save(self, *args, **kwargs): # pylint: disable=arguments-differ self.content_filter_hash = get_content_filter_hash(self.content_filter) super().save(*args, **kwargs)
def save(self, *args, **kwargs): self.content_filter_hash = get_content_filter_hash(self.content_filter) super().save(*args, **kwargs)