Skip to content
Merged
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
5 changes: 3 additions & 2 deletions license_library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,10 @@ def __str__(self):
return f"{self.short_name} ({self.key})"

def save(self, *args, **kwargs):
"""Clear the licensing cache on License object changes."""
"""Clear the licensing cache entry of the License Dataspace on changes."""
super().save(*args, **kwargs)
caches["licensing"].clear()
# Delete only this Dataspace entry: `clear()` would flush the whole Redis database.
caches["licensing"].delete(str(self.dataspace.uuid))

def clean(self, from_api=False):
if self.is_active is False and self.spdx_license_key:
Expand Down
16 changes: 16 additions & 0 deletions license_library/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from collections import OrderedDict

from django.core.cache import caches
from django.test import TestCase

from license_expression import LicenseSymbol
Expand Down Expand Up @@ -498,6 +499,21 @@ def test_license_model_get_all_urls(self):
]
self.assertEqual(expected, self.license1.get_all_urls())

def test_license_model_save_deletes_dataspace_licensing_cache_entry(self):
licensing_cache = caches["licensing"]
dataspace_key = str(self.dataspace.uuid)
other_dataspace_key = str(self.other_dataspace.uuid)
licensing_cache.set(dataspace_key, "licensing")
licensing_cache.set(other_dataspace_key, "other_licensing")
caches["default"].set("session", "data")
self.addCleanup(caches["default"].delete, "session")

self.license1.save()

self.assertIsNone(licensing_cache.get(dataspace_key))
self.assertEqual("other_licensing", licensing_cache.get(other_dataspace_key))
self.assertEqual("data", caches["default"].get("session"))


class LicenseChoiceModelTestCase(TestCase):
def setUp(self):
Expand Down
Loading