feat: add update-by-domain
Action and Enforce JSON Field Validations in TenantConfig API
#237
+208
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces two key improvements to the
TenantConfigViewSet
:update-by-domain
action: Allows updating aTenantConfig
by searching for it using its domainroute__domain
, rather than the default lookup fields (id
orexternal_key
).TenantConfigSerializer
: Ensures that thelms_configs
,studio_configs
,theming_configs
, andmeta
fields must always be dictionaries.Motivation
Why Add
update-by-domain
action?In Control Center, an automated process is required to create new subscriptions and correctly link them to their respective Open edX sites. To achieve this, Control Center must be able to update the
external_key
of the site (TenantConfig) so that it matches theexternal_key
assigned to the tenant in Control Center.This alignment is crucial because Control Center relies on the
external_key
to establish connections with the Open edX site and enable the necessary functionalities for managing the site.However, in this process, Control Center only has access to the site's domain (
route__domain
), not itsid
orexternal_key
. Therefore, this newupdate-by-domain
action allows Control Center to update the TenantConfig using the domain as the lookup field, ensuring that the correct site is modified while maintaining compatibility with existing API consumers.Why Strengthen JSON Field Validation?
The existing serializer used
serializers.JSONField()
for storing configurations (lms_configs
,studio_configs
,theming_configs
, andmeta
). However, DRF’sJSONField
does not enforce any specific type, meaning these fields could accept any valid JSON value (e.g., strings, numbers, lists). This led to unexpected behavior where incorrect data types were stored without validation.To address this, custom field validators were added to enforce that these fields must always be dictionaries. This ensures data integrity and prevents errors caused by unexpected input types.
Changes Introduced
update-by-domain
ActionPATCH
request can now be made to:TenantConfig
is found usingroute__domain
, it is updated and returned.TenantConfig
is found, a404 Not Found
response is returned.domain
query parameter is missing, a400 Bad Request
response is returned.How to Test
Update a TenantConfig by its
domain
Expected:
200 OK
with updated TenantConfig.Backward Compatibility
update-by-domain
action does not modify existing API behavior.