forked from unioslo/mreg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrating away from postgres CI fields.
*Caveats:* - Assumes fields in LCI fields are all already in lowercase. No efforts are currently made to manipulate these fields. This is fixable if required. - Email fields are migrated to models.EmailField. Case insensitivity is not preserved. - Needs (much) more testing. Implementation details: - Creates two LowerCase-fields to replace the LCI-fields. - Uses a manager to hook into the calls to objects.get, objects.filter, and objects.exclude (the latter is currently unused). - Uses a mixin for views to overload get_object for relevant detail views. - Overloaded get_queryset() usage to check for exists are handled manually, should be cleaned up.
- Loading branch information
Showing
17 changed files
with
315 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 3.2.18 on 2023-06-29 09:26 | ||
|
||
from django.db import migrations | ||
import hostpolicy.models | ||
import mreg.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('hostpolicy', '0003_hostpolicyrole_labels'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='hostpolicyatom', | ||
name='name', | ||
field=mreg.fields.LowerCaseCharField(max_length=64, unique=True, validators=[hostpolicy.models._validate_atom_name]), | ||
), | ||
migrations.AlterField( | ||
model_name='hostpolicyrole', | ||
name='name', | ||
field=mreg.fields.LowerCaseCharField(max_length=64, unique=True, validators=[hostpolicy.models._validate_role_name]), | ||
), | ||
] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.db import models | ||
|
||
from .fields import LowerCaseCharField | ||
|
||
|
||
class LowerCaseManager(models.Manager): | ||
"""A manager that lowercases all values of LowerCaseCharFields in filter/exclude/get calls.""" | ||
|
||
@property | ||
def lowercase_fields(self): | ||
if not hasattr(self, "_lowercase_fields_cache"): | ||
self._lowercase_fields_cache = [ | ||
field.name | ||
for field in self.model._meta.get_fields() | ||
if isinstance(field, LowerCaseCharField) | ||
] | ||
return self._lowercase_fields_cache | ||
|
||
def _lowercase_fields(self, **kwargs): | ||
lower_kwargs = {} | ||
for key, value in kwargs.items(): | ||
field_name = key.split("__")[0] | ||
if field_name in self.lowercase_fields and isinstance(value, str): | ||
value = value.lower() | ||
lower_kwargs[key] = value | ||
return lower_kwargs | ||
|
||
def filter(self, **kwargs): | ||
return super().filter(**self._lowercase_fields(**kwargs)) | ||
|
||
def exclude(self, **kwargs): | ||
return super().exclude(**self._lowercase_fields(**kwargs)) | ||
|
||
def get(self, **kwargs): | ||
return super().get(**self._lowercase_fields(**kwargs)) | ||
|
||
|
||
def lower_case_manager_factory(base_manager): | ||
class LowerCaseBaseManager(base_manager, LowerCaseManager): | ||
pass | ||
|
||
return LowerCaseBaseManager |
Oops, something went wrong.