Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- update release notes
- invert `skip_registry`. It behaves now backward compatible but has a
  literal value for allowing the search
  • Loading branch information
devkral committed Jan 13, 2025
1 parent 8ff29f6 commit 85bf9d0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
16 changes: 12 additions & 4 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,30 @@ hide:
- Add `through_registry` to ManyToMany.
- Add `no_copy` to models MetaInfo.
- Add `ModelCollisionError` exception.
- Add keyword only hook function `real_add_to_registry`. It can be used to customize the `add_to_registry` behaviour.

### Changed

- `create_edgy_model` has now `__type_kwargs__` which contains a dict of keyword arguments provided to `__new__` of type.
- RelatedField uses now `no_copy`.
- `add_to_registry` returns the type which was actually added to registry instead of None.
- Through models use now `no_copy` when autogenerated.
- BREAKING: instead of silent replacing models with the same `__name__` now an error is raised.
- Through models use now `no_copy` when autogenerated. This way they don't land in copied registries but are autogenerated again.
- Instead of silent replacing models with the same `__name__` now an error is raised.
- `skip_registry` has now also an allowed literal value: `"allow_search"`. It enables the search of the registry but doesn't register the model.

### Fixed

- Copying registries and models is working now.
- Fix deleting (clearing cache) of BaseForeignKey target.
- Creating two models with the same name did lead to silent replacements.
- Invalidating causes schema errors.
- ManyToMany and ForeignKey fields in connection with tenancy.
- Invalidating caused schema errors.
- ManyToMany and ForeignKey fields didn't worked when referencing tenant models.
- ManyToMany fields didn't worked when specified on tenant models.

### BREAKING

- Instead of silent replacing models with the same `__name__` now an error is raised.
- The return value of `add_to_registry` changed. If you customize the function you need to return now the actual model added to the registry.

## 0.24.2

Expand Down
11 changes: 9 additions & 2 deletions edgy/contrib/multi_tenancy/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ def __new__(
bases: tuple[type, ...],
attrs: Any,
on_conflict: Literal["error", "replace", "keep"] = "error",
skip_registry: bool = False,
skip_registry: Union[bool, Literal["allow_search"]] = False,
meta_info_class: type[TenantMeta] = TenantMeta,
**kwargs: Any,
) -> Any:
database: Union[Literal["keep"], None, Database, bool] = attrs.get("database", "keep")
new_model = super().__new__(
cls, name, bases, attrs, meta_info_class=TenantMeta, skip_registry=True, **kwargs
cls,
name,
bases,
attrs,
skip_registry="allow_search",
meta_info_class=meta_info_class,
**kwargs,
)
if new_model.meta.is_tenant is None:
new_model.meta.is_tenant = _check_model_inherited_tenancy(bases)
Expand Down
8 changes: 3 additions & 5 deletions edgy/core/db/models/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def __new__(
bases: tuple[type, ...],
attrs: dict[str, Any],
meta_info_class: type[MetaInfo] = MetaInfo,
skip_registry: bool = False,
skip_registry: Union[bool, Literal["allow_search"]] = False,
on_conflict: Literal["error", "replace", "keep"] = "error",
**kwargs: Any,
) -> Any:
Expand Down Expand Up @@ -784,10 +784,8 @@ def __new__(
tablename = f"{name.lower()}s"
meta.tablename = tablename
meta.model = new_class
# Now find a registry and add it to the meta. This isn't affected by skip_registry.
# Use Meta: registry = False for disabling the search.
# The registry will be updated by add_to_registry.
if meta.registry is None:
# Now find a registry and add it to the meta.
if meta.registry is None and skip_registry is not True:
registry: Union[Registry, None, Literal[False]] = get_model_registry(bases, meta_class)
meta.registry = registry or None
# don't add automatically to registry. Useful for subclasses which modify the registry itself.
Expand Down
16 changes: 14 additions & 2 deletions tests/metaclass/test_meta_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,24 @@ class Meta:
)


def test_no_raises_ModelCollisionError():
class User(edgy.StrictModel, skip_registry=True):
@pytest.mark.parametrize("value", [True, "allow_search"])
def test_no_raises_ModelCollisionError_and_set_correctly(value):
class BaseUser(edgy.StrictModel):
name = edgy.CharField(max_length=255)

class Meta:
registry = models
abstract = True

class User(BaseUser, skip_registry=value):
pass

if value is True:
assert BaseUser.meta.registry is models
assert User.meta.registry is None
else:
assert BaseUser.meta.registry is models
assert User.meta.registry is models


def test_raises_ForeignKeyBadConfigured():
Expand Down

0 comments on commit 85bf9d0

Please sign in to comment.