cells: use get_or_create for ValidityInfo objects (#42823)
This commit is contained in:
parent
2723604596
commit
e88e377679
|
@ -84,7 +84,7 @@ class WcsFormCell(CellBase):
|
|||
def save(self, *args, **kwargs):
|
||||
if 'update_fields' in kwargs:
|
||||
# don't populate the cache
|
||||
return super(WcsFormCell, self).save(*args, **kwargs)
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
def populate_cache():
|
||||
if self.formdef_reference:
|
||||
|
@ -108,23 +108,19 @@ class WcsFormCell(CellBase):
|
|||
self.cached_title = form.get('title')
|
||||
self.cached_url = form.get('url')
|
||||
self.cached_json = form
|
||||
self.save(update_fields=['cached_title', 'cached_url', 'cached_json'])
|
||||
form_found = True
|
||||
break
|
||||
if form_found:
|
||||
self.mark_as_valid()
|
||||
return
|
||||
else:
|
||||
return self.mark_as_invalid('wcs_form_not_found', save=False)
|
||||
return self.mark_as_invalid('wcs_form_not_found')
|
||||
else:
|
||||
return self.mark_as_invalid('wcs_form_not_defined', save=False)
|
||||
return self.mark_as_invalid('wcs_form_not_defined')
|
||||
|
||||
validity_info = populate_cache()
|
||||
result = super(WcsFormCell, self).save(*args, **kwargs)
|
||||
if validity_info:
|
||||
# save validity_info after Cell insert
|
||||
validity_info.content_object = self
|
||||
validity_info.save()
|
||||
return result
|
||||
super().save(*args, **kwargs)
|
||||
populate_cache()
|
||||
|
||||
def get_cell_extra_context(self, context):
|
||||
context = super(WcsFormCell, self).get_cell_extra_context(context)
|
||||
|
@ -194,7 +190,7 @@ class WcsCommonCategoryCell(CellBase):
|
|||
def save(self, *args, **kwargs):
|
||||
if 'update_fields' in kwargs:
|
||||
# don't populate the cache
|
||||
return super(WcsCommonCategoryCell, self).save(*args, **kwargs)
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
def populate_cache():
|
||||
if self.category_reference:
|
||||
|
@ -214,23 +210,19 @@ class WcsCommonCategoryCell(CellBase):
|
|||
self.cached_title = category.get('title')
|
||||
self.cached_description = category.get('description') or ''
|
||||
self.cached_url = category.get('url')
|
||||
self.save(update_fields=['cached_title', 'cached_description', 'cached_url'])
|
||||
category_found = True
|
||||
break
|
||||
if category_found:
|
||||
self.mark_as_valid()
|
||||
return
|
||||
else:
|
||||
return self.mark_as_invalid('wcs_category_not_found', save=False)
|
||||
return self.mark_as_invalid('wcs_category_not_found')
|
||||
else:
|
||||
return self.mark_as_invalid('wcs_category_not_defined', save=False)
|
||||
return self.mark_as_invalid('wcs_category_not_defined')
|
||||
|
||||
validity_info = populate_cache()
|
||||
result = super(WcsCommonCategoryCell, self).save(*args, **kwargs)
|
||||
if validity_info:
|
||||
# save validity_info after Cell insert
|
||||
validity_info.content_object = self
|
||||
validity_info.save()
|
||||
return result
|
||||
super().save(*args, **kwargs)
|
||||
populate_cache()
|
||||
|
||||
def get_additional_label(self):
|
||||
if not self.cached_title:
|
||||
|
|
|
@ -839,10 +839,16 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
|
|||
def get_extra_manager_context(self):
|
||||
return {}
|
||||
|
||||
def mark_as_invalid(self, reason_code, force=True, save=True):
|
||||
validity_info = self.validity_info.all().first()
|
||||
if validity_info is None:
|
||||
validity_info = ValidityInfo(content_object=self)
|
||||
def mark_as_invalid(self, reason_code, force=True):
|
||||
validity_info, created = ValidityInfo.objects.get_or_create(
|
||||
content_type=ContentType.objects.get_for_model(self),
|
||||
object_id=self.pk,
|
||||
defaults={
|
||||
'invalid_reason_code': reason_code,
|
||||
'invalid_since': now(),
|
||||
})
|
||||
if created:
|
||||
return
|
||||
|
||||
if not force and validity_info.invalid_since is not None:
|
||||
# don't overwrite invalid reason already set
|
||||
|
@ -854,17 +860,13 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
|
|||
|
||||
validity_info.invalid_reason_code = reason_code
|
||||
validity_info.invalid_since = now()
|
||||
if save:
|
||||
validity_info.save()
|
||||
return validity_info
|
||||
validity_info.save()
|
||||
|
||||
def mark_as_valid(self, save=True):
|
||||
def mark_as_valid(self):
|
||||
validity_info = self.get_validity_info()
|
||||
if validity_info is None:
|
||||
return
|
||||
if save:
|
||||
validity_info.delete()
|
||||
return validity_info
|
||||
validity_info.delete()
|
||||
|
||||
def get_validity_info(self):
|
||||
if hasattr(self, 'prefetched_validity_info'):
|
||||
|
@ -875,20 +877,19 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
|
|||
|
||||
def set_validity_from_url(
|
||||
self, resp,
|
||||
not_found_code='url_not_found', invalid_code='url_invalid',
|
||||
save=True):
|
||||
not_found_code='url_not_found', invalid_code='url_invalid'):
|
||||
if resp is None:
|
||||
# can not retrieve data, don't report cell as invalid
|
||||
self.mark_as_valid(save=save)
|
||||
self.mark_as_valid()
|
||||
elif resp.status_code == 404:
|
||||
self.mark_as_invalid(not_found_code, save=save)
|
||||
self.mark_as_invalid(not_found_code)
|
||||
elif 400 <= resp.status_code < 500:
|
||||
# 4xx error, cell is invalid
|
||||
self.mark_as_invalid(invalid_code, save=save)
|
||||
self.mark_as_invalid(invalid_code)
|
||||
else:
|
||||
# 2xx or 3xx: cell is valid
|
||||
# 5xx error: can not retrieve data, don't report cell as invalid
|
||||
self.mark_as_valid(save=save)
|
||||
self.mark_as_valid()
|
||||
|
||||
def get_invalid_reason(self):
|
||||
validity_info = self.get_validity_info()
|
||||
|
|
|
@ -187,7 +187,8 @@ def test_form_cell_save_cache(mock_send):
|
|||
assert WcsFormCell.objects.get(id=cell.id).cached_title == 'form title'
|
||||
|
||||
|
||||
def test_form_cell_validity():
|
||||
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
||||
def test_form_cell_validity(mock_send):
|
||||
page = Page.objects.create(title='xxx', slug='test_form_cell_save_cache', template_name='standard')
|
||||
cell = WcsFormCell.objects.create(page=page, placeholder='content', order=0)
|
||||
validity_info = ValidityInfo.objects.latest('pk')
|
||||
|
@ -241,7 +242,8 @@ def test_category_cell_save_cache(mock_send):
|
|||
assert cell.get_additional_label() == 'Test 3'
|
||||
|
||||
|
||||
def test_category_cell_validity():
|
||||
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
||||
def test_category_cell_validity(mock_send):
|
||||
page = Page.objects.create(title='xxx', slug='test_category_cell_save_cache', template_name='standard')
|
||||
cell = WcsCategoryCell.objects.create(page=page, placeholder='content', order=0)
|
||||
validity_info = ValidityInfo.objects.latest('pk')
|
||||
|
|
Loading…
Reference in New Issue