forked from eduNEXT/eox-tenant
/
proxies.py
198 lines (160 loc) · 6.11 KB
/
proxies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
Tenant wise proxies that allows to override the platform models.
"""
import json
import logging
from itertools import chain
import six
from django.conf import settings
from django.core.cache import cache
from eox_tenant.edxapp_wrapper.site_configuration_module import get_site_configuration_models
from eox_tenant.models import Microsite, TenantConfig, TenantOrganization
from eox_tenant.utils import clean_serializable_values
SiteConfigurationModels = get_site_configuration_models()
TENANT_ALL_ORGS_CACHE_KEY = "tenant.all_orgs_list"
EOX_TENANT_CACHE_KEY_TIMEOUT = getattr(
settings,
"EOX_TENANT_CACHE_KEY_TIMEOUT",
300
)
TENANT_MICROSITES_ITERATOR_KEY = "tenant-microsites-iterator"
logger = logging.getLogger(__name__)
class TenantSiteConfigProxy(SiteConfigurationModels.SiteConfiguration):
"""
This a is Proxy model for SiteConfiguration from <openedx.core.djangoapps.site_configuration.models>.
This allows to add or override methods using as base the SiteConfiguration model.
More information in https://docs.djangoproject.com/en/3.0/topics/db/models/#proxy-models
"""
cached_site_values = None
class Meta:
""" Set as a proxy model. """
proxy = True
def __str__(self):
key = getattr(settings, "EDNX_TENANT_KEY", "No tenant is active at the moment")
return "<Tenant proxy as site_configuration: {}>".format(key)
@property
def enabled(self):
"""
Return True if EDNX_TENANT_KEY is in the current settings.
"""
if getattr(settings, 'EDNX_TENANT_KEY', None):
return True
return False
@enabled.setter
def enabled(self, value):
"""
We ignore the setter since this is a read proxy.
"""
def get_value(self, name, default=None):
"""
Return Configuration value from the Tenant loaded in the settings object
as if this was a SiteConfiguration class.
"""
try:
return getattr(settings, name, default)
except AttributeError as error:
logger.exception("Invalid data at the TenantConfigProxy get_value. \n [%s]", error)
return default
@property
def values(self):
"""
Returns a serializable subset of the loaded settings.
"""
if self.enabled:
return vars(settings._wrapped) # pylint: disable=protected-access
return {}
@values.setter
def values(self, value):
"""
We ignore the setter since this is a read proxy.
"""
@property
def site_values(self):
"""
Returns a serializable subset of the loaded settings. This version works with juniper releases.
"""
if self.cached_site_values:
return self.cached_site_values
self.cached_site_values = clean_serializable_values(self.values)
return self.cached_site_values
@site_values.setter
def site_values(self, value):
"""
We ignore the setter since this is a read proxy. This version works with juniper releases.
"""
def save(self, *args, **kwargs):
"""
Don't allow to save TenantSiteConfigProxy model in database.
"""
@classmethod
def get_all_orgs(cls):
"""
This returns a set of orgs that are considered within all microsites and TenantConfig.
This can be used, for example, to do filtering
"""
# Check the cache first
org_filter_set = cache.get(TENANT_ALL_ORGS_CACHE_KEY)
if org_filter_set:
return org_filter_set
org_filter_set = set(TenantOrganization.objects.all().values_list("name", flat=True))
cls.set_key_to_cache(TENANT_ALL_ORGS_CACHE_KEY, org_filter_set)
return org_filter_set
@classmethod
def get_value_for_org(cls, org, val_name, default=None):
"""
Returns a configuration value for a microsite or TenantConfig which has an org_filter that matches
what is passed in.
"""
return cls.__get_value_for_org(org, val_name, default)
@classmethod
def set_key_to_cache(cls, key, value):
"""
Stores a key value pair in a cache scoped to the thread
"""
cache.set(
key,
value,
EOX_TENANT_CACHE_KEY_TIMEOUT
)
@classmethod
def __get_value_for_org(cls, org, val_name, default=None):
"""
Optimized method, that returns a value for the given org and val_name, from the
TenantConfig or Microsite model.
"""
cache_key = "org-value-{}-{}".format(org, val_name)
cached_value = cache.get(cache_key)
if cached_value:
return cached_value
result = TenantConfig.get_value_for_org(org, val_name)
if result:
cls.set_key_to_cache(cache_key, result)
return result
result = Microsite.get_value_for_org(org, val_name)
result = result if result else default
cls.set_key_to_cache(cache_key, result)
return result
@classmethod
def pre_load_values_by_org(cls, val_name):
"""
Save in cache the values for all the organizations in TenantConfig and Microsite models.
"""
pre_load_value_key = "eox-tenant-pre-load-{}-key".format(val_name)
if cache.get(pre_load_value_key):
return
tenant_config = TenantConfig.objects.values_list("lms_configs", flat=True)
microsite_config = Microsite.objects.values_list("values", flat=True)
for config in chain(microsite_config, tenant_config):
try:
if isinstance(config, six.string_types):
config = json.loads(config)
org_filter = config.get("course_org_filter", [])
result = config.get(val_name)
except AttributeError:
continue
if isinstance(org_filter, six.string_types):
org_filter = [org_filter]
for org in org_filter:
key = "org-value-{}-{}".format(org, val_name)
cls.set_key_to_cache(key, result)
cls.set_key_to_cache(pre_load_value_key, True)