-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] Removed default values from fallback fields
Fallback fields should not have default field set to the fallback value as it would require users to manually update those fields when the fallback value is changed.
- Loading branch information
Showing
16 changed files
with
241 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...dius/migrations/0037_alter_organizationradiussettings_allowed_mobile_prefixes_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Generated by Django 4.2.14 on 2024-08-07 16:38 | ||
|
||
from django.db import migrations | ||
|
||
import openwisp_radius.base.validators | ||
import openwisp_utils.fields | ||
|
||
from .. import settings as app_settings | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("openwisp_radius", "0036_organizationradiussettings_mac_addr_roaming_enabled"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="organizationradiussettings", | ||
name="allowed_mobile_prefixes", | ||
field=openwisp_utils.fields.FallbackTextField( | ||
blank=True, | ||
default=None, | ||
fallback=','.join(app_settings.ALLOWED_MOBILE_PREFIXES), | ||
help_text=( | ||
"Comma separated list of international mobile prefixes" | ||
" allowed to register via the user registration API." | ||
), | ||
null=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="organizationradiussettings", | ||
name="freeradius_allowed_hosts", | ||
field=openwisp_utils.fields.FallbackTextField( | ||
blank=True, | ||
default=None, | ||
fallback=','.join(app_settings.FREERADIUS_ALLOWED_HOSTS), | ||
help_text=( | ||
"Comma separated list of IP addresses allowed to access" | ||
" freeradius API" | ||
), | ||
null=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="organizationradiussettings", | ||
name="password_reset_url", | ||
field=openwisp_utils.fields.FallbackCharField( | ||
blank=True, | ||
default=None, | ||
fallback=app_settings.DEFAULT_PASSWORD_RESET_URL, | ||
help_text="Enter the URL where users can reset their password", | ||
max_length=200, | ||
null=True, | ||
validators=[ | ||
openwisp_radius.base.validators.password_reset_url_validator | ||
], | ||
verbose_name="Password reset URL", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from django.db import migrations | ||
|
||
from openwisp_utils.fields import FallbackMixin | ||
|
||
from ..utils import load_model | ||
|
||
|
||
def clean_fallback_fields(apps, schema_editor): | ||
""" | ||
This data migration is necessary to clean up the database | ||
from unnecessary stored fallback values. In the previous | ||
implementation, the fallback value was stored in the database. | ||
However, this was not the intended behavior and was a bug. This migration | ||
sets the fallback fields to None when the value in the database | ||
is the same as the fallback value, effectively removing the | ||
unnecessary data from the database. | ||
""" | ||
OrganizationRadiusSettings = load_model('OrganizationRadiusSettings') | ||
fallback_fields = [] | ||
fallback_field_names = [] | ||
|
||
for field in OrganizationRadiusSettings._meta.get_fields(): | ||
if isinstance(field, FallbackMixin): | ||
fallback_fields.append(field) | ||
fallback_field_names.append(field.name) | ||
updated_settings = [] | ||
for radius_settings in OrganizationRadiusSettings.objects.iterator(): | ||
changed = False | ||
for field in fallback_fields: | ||
if getattr(radius_settings, field.name) == field.fallback: | ||
setattr(radius_settings, field.name, None) | ||
changed = True | ||
if changed: | ||
updated_settings.append(radius_settings) | ||
|
||
if len(updated_settings) > 100: | ||
OrganizationRadiusSettings.objects.bulk_update( | ||
updated_settings, fields=[field.name for field in fallback_fields] | ||
) | ||
updated_settings = [] | ||
|
||
if updated_settings: | ||
OrganizationRadiusSettings.objects.bulk_update( | ||
updated_settings, fields=[field.name for field in fallback_fields] | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
( | ||
'openwisp_radius', | ||
'0037_alter_organizationradiussettings_allowed_mobile_prefixes_and_more', | ||
), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
clean_fallback_fields, reverse_code=migrations.RunPython.noop | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.