Browse Source

Merge pull request #8213 from rtibbles/clear_device_settings

Fix deprovision cache issue
release-v0.14.x
Richard Tibbles 3 months ago
committed by GitHub
parent
commit
5e2982eee5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      kolibri/core/device/models.py
  2. 40
      kolibri/core/device/test/test_device_settings.py

17
kolibri/core/device/models.py

@ -4,6 +4,7 @@ from uuid import uuid4
from django.conf import settings
from django.db import models
from django.db.models import QuerySet
from morango.models import UUIDField
from .utils import LANDING_PAGE_LEARN
@ -36,7 +37,13 @@ class DevicePermissions(models.Model):
DEVICE_SETTINGS_CACHE_KEY = "device_settings_cache_key"
class DeviceSettingsManager(models.Manager):
class DeviceSettingsQuerySet(QuerySet):
def delete(self, **kwargs):
cache.delete(DEVICE_SETTINGS_CACHE_KEY)
return super(DeviceSettingsQuerySet, self).delete(**kwargs)
class DeviceSettingsManager(models.Manager.from_queryset(DeviceSettingsQuerySet)):
def get(self, **kwargs):
if DEVICE_SETTINGS_CACHE_KEY not in cache:
model = super(DeviceSettingsManager, self).get(**kwargs)
@ -90,8 +97,14 @@ class DeviceSettings(models.Model):
def save(self, *args, **kwargs):
self.pk = 1
self.full_clean()
super(DeviceSettings, self).save(*args, **kwargs)
out = super(DeviceSettings, self).save(*args, **kwargs)
cache.set(DEVICE_SETTINGS_CACHE_KEY, self, 600)
return out
def delete(self, *args, **kwargs):
out = super(DeviceSettings, self).delete(*args, **kwargs)
cache.delete(DEVICE_SETTINGS_CACHE_KEY)
return out
CONTENT_CACHE_KEY_CACHE_KEY = "content_cache_key"

40
kolibri/core/device/test/test_device_settings.py

@ -0,0 +1,40 @@
from django.core.exceptions import ValidationError
from django.test import TestCase
from kolibri.core.device.models import DeviceSettings
from kolibri.core.utils.cache import process_cache as cache
class DeviceSettingsTestCase(TestCase):
def test_singleton(self):
cache.clear()
DeviceSettings.objects.create()
with self.assertRaises(ValidationError):
DeviceSettings.objects.create()
def test_get_setting(self):
cache.clear()
ds = DeviceSettings.objects.create()
ds2 = DeviceSettings.objects.get()
self.assertEqual(ds, ds2)
def test_delete_setting(self):
cache.clear()
ds = DeviceSettings.objects.create()
ds.delete()
with self.assertRaises(DeviceSettings.DoesNotExist):
DeviceSettings.objects.get()
def test_delete_setting_queryset(self):
cache.clear()
DeviceSettings.objects.create()
DeviceSettings.objects.all().delete()
with self.assertRaises(DeviceSettings.DoesNotExist):
DeviceSettings.objects.get()
def test_delete_setting_manager(self):
cache.clear()
DeviceSettings.objects.create()
DeviceSettings.objects.delete()
with self.assertRaises(DeviceSettings.DoesNotExist):
DeviceSettings.objects.get()
Loading…
Cancel
Save