Skip to content

Commit

Permalink
set charfield as default pk to avoid errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasag7 committed Dec 4, 2023
2 parents 43365cd + 3858d9f commit b0c2b50
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 209 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: test

'on': [push, pull_request, workflow_dispatch]
"on": [push, pull_request, workflow_dispatch]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10']
django-version: ['3.2', '4.0', '4.1']
python-version: ["3.8", "3.9", "3.10", "3.11"]
django-version: ["3.2", "4.1", "4.2"]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/psf/black
rev: 23.9.1
rev: 23.10.1
hooks:
- id: black
language_version: python3
Expand Down
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build:
- asdf global poetry latest
- poetry config virtualenvs.create false
post_install:
- poetry install -E docs --no-dev
- . "$(pwd | rev | sed 's/stuokcehc/svne/' | rev)/bin/activate" && poetry install --only main --only docs

# Build documentation in the docs/ directory with Sphinx
sphinx:
Expand Down
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ We follow [Semantic Versions](https://semver.org/) starting at the `0.14.0` rele

## {{ Next Version }}

### Bug Fixes
### Features

## 1.5.0 (2023-11-08)

### Bug Fixes

- Fixes querying with multiple eav kwargs [#395](https://github.com/jazzband/django-eav2/issues/395)

## 1.5.0 (2023-09-07)

### Features

- Support for many type of primary key (UUIDField, BigAutoField)
- Support for natural key use for some models for serialization (EnumValue, EnumGroup, Attribute, Value)
- Add support for Django 4.2
- Add support for Python 3.11

## 1.4.0 (2023-07-07)

Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def setup(app):
# -- Options for intersphinx extension ---------------------------------------

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'https://docs.python.org/': None}
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}

# -- Autodoc configuration ---------------------------------------------------

Expand Down
72 changes: 71 additions & 1 deletion eav/logic/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,96 @@


class EnumValueManager(models.Manager):
"""
Custom manager for `EnumValue` model.
This manager adds utility methods specific to the `EnumValue` model.
"""

def get_by_natural_key(self, value):
"""
Retrieves an EnumValue instance using its `value` as a natural key.
Args:
value (str): The value of the EnumValue instance.
Returns:
EnumValue: The instance matching the provided value.
"""
return self.get(value=value)


class EnumGroupManager(models.Manager):
"""
Custom manager for `EnumGroup` model.
This manager adds utility methods specific to the `EnumGroup` model.
"""

def get_by_natural_key(self, name):
"""
Retrieves an EnumGroup instance using its `name` as a natural key.
Args:
name (str): The name of the EnumGroup instance.
Returns:
EnumGroup: The instance matching the provided name.
"""
return self.get(name=name)


class AttributeManager(models.Manager):
"""
Custom manager for `Attribute` model.
This manager adds utility methods specific to the `Attribute` model.
"""

def get_by_natural_key(self, name, slug):
"""
Retrieves an Attribute instance using its `name` and `slug` as natural keys.
Args:
name (str): The name of the Attribute instance.
slug (str): The slug of the Attribute instance.
Returns:
Attribute: The instance matching the provided name and slug.
"""
return self.get(name=name, slug=slug)


class ValueManager(models.Manager):
"""
Custom manager for `Value` model.
This manager adds utility methods specific to the `Value` model.
"""

def get_by_natural_key(self, attribute, entity_id, entity_uuid):
"""
Retrieve a Value instance using multiple natural keys.
This method utilizes a combination of an `attribute` (defined by its
name and slug), `entity_id`, and `entity_uuid` to retrieve a unique
Value instance.
Args:
attribute (tuple): A tuple containing the name and slug of the
Attribute instance.
entity_id (int): The ID of the associated entity.
entity_uuid (str): The UUID of the associated entity.
Returns:
Value: The instance matching the provided keys.
"""
from eav.models import Attribute

attribute = Attribute.objects.get(name=attribute[0], slug=attribute[1])

return self.get(
attribute=attribute, entity_id=entity_id, entity_uuid=entity_uuid
attribute=attribute,
entity_id=entity_id,
entity_uuid=entity_uuid,
)
54 changes: 40 additions & 14 deletions eav/logic/object_pk.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import uuid

from functools import partial
from django.db import models
from typing import Type

from django.conf import settings
from django.db import models

#: Constants
_DEFAULT_CHARFIELD_LEN: int = 40

_FIELD_MAPPING = {
"django.db.models.UUIDField": partial(
models.UUIDField,
primary_key=True,
editable=False,
default=uuid.uuid4,
),
"django.db.models.CharField": partial(
models.CharField,
primary_key=True,
editable=False,
max_length=_DEFAULT_CHARFIELD_LEN,
),
}


def get_pk_format() -> Type[models.Field]:
"""
Get the primary key field format based on the Django settings.
This function returns a field factory function that corresponds to the
primary key format specified in Django settings. If the primary key
format is not recognized, it defaults to using BigAutoField.
Returns:
Type[models.Field]: A field factory function that can be used to
create the primary key field instance.
"""
field_factory = _FIELD_MAPPING.get(
settings.EAV2_PRIMARY_KEY_FIELD,
partial(models.BigAutoField, primary_key=True, editable=False),
)

def get_pk_format():
if settings.EAV2_PRIMARY_KEY_FIELD == "django.db.models.UUIDField":
PrimaryField = partial(
models.UUIDField, primary_key=True, editable=False, default=uuid.uuid4
)
elif settings.EAV2_PRIMARY_KEY_FIELD == "django.db.models.CharField":
PrimaryField = partial(
models.CharField, primary_key=True, editable=False, max_length=40
)
else:
PrimaryField = partial(models.BigAutoField, primary_key=True, editable=False)
return PrimaryField()
# Create and return the field instance
return field_factory()

This file was deleted.

42 changes: 42 additions & 0 deletions eav/migrations/0010_dynamic_pk_type_for_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 4.2.6 on 2023-12-04 08:31

from django.db import migrations, models


class Migration(migrations.Migration):
"""Migration to set CharField as default primary key for all models."""

dependencies = [
('eav', '0009_enchance_naming'),
]

operations = [
migrations.AlterField(
model_name='attribute',
name='id',
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
migrations.AlterField(
model_name='enumgroup',
name='id',
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
migrations.AlterField(
model_name='enumvalue',
name='id',
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
migrations.AlterField(
model_name='value',
name='id',
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
]
Loading

0 comments on commit b0c2b50

Please sign in to comment.