Skip to content

Commit

Permalink
some cleanup, deprecate only the extra add_to_registry positional arg…
Browse files Browse the repository at this point in the history
…uments
  • Loading branch information
devkral committed Jan 10, 2025
1 parent 7fffd1c commit a21f0aa
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
8 changes: 4 additions & 4 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ hide:

### Added

- M2M-Field `create_through_model` allows now the keyword only argument `replace_related_field`.
- `add_to_registry` has now an additional keyword `replace_related_field_m2m` for seperate controlling the `create_through_model` registration logic.
- ManyToManyField `create_through_model` method allows now the keyword only argument `replace_related_field`.
- `add_to_registry` has now an additional keyword-only argument `replace_related_field_m2m` for extra controlling the `create_through_model` registration logic.
- Passing a tuple or list of types to `replace_related_field` is now allowed.

### Changed

- `add_to_registry` has at most one positional argument. Such an interface was intended but not enforced.
- `add_to_registry` deprecates passing arguments as positional arguments except the first one (registry).
- `create_edgy_model` passes through additional keyword arguments to the edgy model class.

### Fixed

- Copying registries is working now.


## 0.24.2

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion edgy/core/connection/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _set_content_type(
__bases__=(with_content_type,),
)
elif real_content_type.meta.registry is None:
real_content_type.add_to_registry(self, "ContentType")
real_content_type.add_to_registry(self, name="ContentType")
self.content_type = real_content_type

def callback(model_class: type["BaseModelType"]) -> None:
Expand Down
5 changes: 4 additions & 1 deletion edgy/core/db/fields/many_to_many.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ def create_through_model(
self,
*,
replace_related_field: Union[
bool, type["BaseModelType"], tuple[type["BaseModelType"], ...]
bool,
type["BaseModelType"],
tuple[type["BaseModelType"], ...],
list[type["BaseModelType"]],
] = False,
) -> None:
"""
Expand Down
60 changes: 49 additions & 11 deletions edgy/core/db/models/mixins/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ class _EmptyClass: ...


def _check_replace_related_field(
replace_related_field: Union[bool, type["BaseModelType"], tuple[type["BaseModelType"], ...]],
replace_related_field: Union[
bool, type["BaseModelType"], tuple[type["BaseModelType"], ...], list[type["BaseModelType"]]
],
model: type["BaseModelType"],
) -> bool:
if isinstance(replace_related_field, bool):
return replace_related_field
if not isinstance(replace_related_field, tuple):
if not isinstance(replace_related_field, (tuple, list)):
replace_related_field = (replace_related_field,)
return any(refmodel is model for refmodel in replace_related_field)

Expand All @@ -74,7 +76,9 @@ def _set_related_field(
foreign_key_name: str,
related_name: str,
source: type["BaseModelType"],
replace_related_field: Union[bool, type["BaseModelType"], tuple[type["BaseModelType"], ...]],
replace_related_field: Union[
bool, type["BaseModelType"], tuple[type["BaseModelType"], ...], list[type["BaseModelType"]]
],
) -> None:
if replace_related_field is not True and related_name in target.meta.fields:
# is already correctly set, required for migrate of model_apps with registry set
Expand Down Expand Up @@ -115,7 +119,7 @@ def _set_related_name_for_foreign_keys(
meta: "MetaInfo",
model_class: type["BaseModelType"],
replace_related_field: Union[
bool, type["BaseModelType"], tuple[type["BaseModelType"], ...]
bool, type["BaseModelType"], tuple[type["BaseModelType"], ...], list[type["BaseModelType"]]
] = False,
) -> None:
"""
Expand Down Expand Up @@ -164,16 +168,49 @@ class DatabaseMixin:
def add_to_registry(
cls: type["BaseModelType"],
registry: "Registry",
*,
*args: Any,
name: str = "",
database: Union[bool, "Database", Literal["keep"]] = "keep",
replace_related_field: Union[
bool, type["BaseModelType"], tuple[type["BaseModelType"], ...]
bool,
type["BaseModelType"],
tuple[type["BaseModelType"], ...],
list[type["BaseModelType"]],
] = False,
replace_related_field_m2m: Union[
bool, type["BaseModelType"], tuple[type["BaseModelType"], ...], None
bool,
type["BaseModelType"],
tuple[type["BaseModelType"], ...],
list[type["BaseModelType"]],
None,
] = None,
) -> None:
if args:
warnings.warn(
(
"Positional extra arguments except registry are deprecated for `add_to_registry`. "
"Please provide the arguments as keyword arguments"
),
DeprecationWarning,
stacklevel=2,
)
# supports only up to 3, the last one was added later
len_args = len(args)
if len_args >= 4:
raise Exception("Not supported, please use keyword arguments")
if len_args == 3:
kw_args: dict = {
"name": args[0],
"database": args[1],
"replace_related_field": args[2],
}
elif len_args == 2:
kw_args = {"name": args[0], "database": args[1]}
elif len_args == 1:
kw_args = {"name": args[0]}

cls.add_to_registry(registry, **kw_args)
return
# when called if registry is not set
cls.meta.registry = registry
if database is True:
Expand Down Expand Up @@ -264,15 +301,16 @@ def copy_edgy_model(
type["Model"],
type(cls.__name__, cls.__bases__, attrs, skip_registry=True, **kwargs),
)
if getattr(cls, "database", None) is not None:
# should also allow masking database with None
if hasattr(cls, "database"):
_copy.database = cls.database
targets: list[type[BaseModelType]] = []
replaceable_models: list[type[BaseModelType]] = [cls]
for field_name in list(_copy.meta.fields):
src_field = cls.meta.fields[field_name]
if not isinstance(src_field, (BaseManyToManyForeignKeyField, BaseForeignKeyField)):
continue
# we use the target of source
targets.append(src_field.target)
replaceable_models.append(src_field.target)

if src_field.target_registry is cls.meta.registry:
del _copy.meta.fields[field_name].target_registry
Expand Down Expand Up @@ -304,7 +342,7 @@ def copy_edgy_model(
# replace when old class otherwise old references can lead to issues
_copy.add_to_registry(
registry,
replace_related_field=(cls, *targets),
replace_related_field=replaceable_models,
database="keep"
if cls.meta.registry is False or cls.database is not cls.meta.registry.database
else True,
Expand Down

0 comments on commit a21f0aa

Please sign in to comment.