Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renovations #8

Merged
merged 8 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@ on:
branches:
- master
pull_request:
branches:
- master
jobs:
Lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pre-commit/[email protected]
Build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: python -m pip install -U build
- run: python -m build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/*
Test:
runs-on: ubuntu-20.04
strategy:
matrix:
Expand All @@ -17,9 +30,9 @@ jobs:
- python-version: '3.10'
- python-version: '3.11'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: 'Set up Python ${{ matrix.python-version }}'
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '${{ matrix.python-version }}'
cache: pip
Expand Down
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
hooks:
- id: ruff
args:
- --fix
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-merge-conflict
- id: check-yaml
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: trailing-whitespace
- id: mixed-line-ending
args:
- --fix=lf
- repo: https://github.com/crate-ci/typos
rev: v1.18.2
hooks:
- id: typos
1 change: 0 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@
* Feature: Warn when some values of an enum won't fit in the backing database field
* Packaging: PEP-396 compliant `__version__` attribute
* Packaging: Tests are now packaged with the source distribution

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The MIT License
The MIT License

Copyright © 2013-2020 Hirshorn Zuckerman Design Group, Inc
Copyright © 2022 Aarni Koskela
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ Django Rest Framework integration
# models.py
from enumfields import EnumField
from enum import Enum

class Color(Enum):
RED = 'r'
GREEN = 'g'
BLUE = 'b'

class MyModel(models.Model):
color = EnumField(Color, max_length=1)

Expand Down
7 changes: 7 additions & 0 deletions enumfields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
from .fields import EnumField, EnumIntegerField

__version__ = "3.0.2"

__all__ = [
"Enum",
"EnumField",
"EnumIntegerField",
"IntEnum",
]
5 changes: 5 additions & 0 deletions enumfields/drf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
from .fields import EnumField
from .serializers import EnumSupportSerializerMixin

__all__ = [
"EnumField",
"EnumSupportSerializerMixin",
]
1 change: 0 additions & 1 deletion enumfields/drf/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.utils.encoding import force_str

from rest_framework.fields import ChoiceField


Expand Down
3 changes: 2 additions & 1 deletion enumfields/drf/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework.fields import CharField, ChoiceField, IntegerField

from enumfields.drf.fields import EnumField as EnumSerializerField
from enumfields.fields import EnumFieldMixin
from rest_framework.fields import CharField, ChoiceField, IntegerField


class EnumSupportSerializerMixin:
Expand Down
4 changes: 2 additions & 2 deletions enumfields/enums.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sys
import inspect
from enum import _EnumDict
import sys
from enum import Enum as BaseEnum
from enum import EnumMeta as BaseEnumMeta
from enum import _EnumDict

from django.utils.encoding import force_str

Expand Down
23 changes: 23 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,26 @@ path = "enumfields/__init__.py"
include = [
"/enumfields",
]

[tool.hatch.build.targets.wheel]
packages = ["enumfields"]

[tool.ruff]
line-length = 120

[tool.ruff.lint]
extend-select = [
"C9",
"E",
"F",
"I",
"W",
]

[tool.ruff.lint.mccabe]
max-complexity = 10

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "tests.settings"
norecursedirs = [".git", ".tox", ".eggs", ".cache", "htmlcov", "venv*"]
doctest_optionflags = ["NORMALIZE_WHITESPACE", "IGNORE_EXCEPTION_DETAIL", "ALLOW_UNICODE"]
28 changes: 0 additions & 28 deletions setup.cfg

This file was deleted.

60 changes: 60 additions & 0 deletions tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 5.0.4 on 2024-04-09 06:18

from django.db import migrations, models

import enumfields.fields
import tests.enums


class Migration(migrations.Migration):
initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="MyModel",
fields=[
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("color", enumfields.fields.EnumField(enum=tests.enums.Color, max_length=1)),
(
"color_not_editable",
enumfields.fields.EnumField(editable=False, enum=tests.enums.Color, max_length=1, null=True),
),
("taste", enumfields.fields.EnumField(default=1, enum=tests.enums.Taste, max_length=10)),
(
"taste_not_editable",
enumfields.fields.EnumField(default=1, editable=False, enum=tests.enums.Taste, max_length=10),
),
(
"taste_null_default",
enumfields.fields.EnumField(
blank=True, default=None, enum=tests.enums.Taste, max_length=10, null=True
),
),
("taste_int", enumfields.fields.EnumIntegerField(default=1, enum=tests.enums.Taste)),
(
"default_none",
enumfields.fields.EnumIntegerField(blank=True, default=None, enum=tests.enums.Taste, null=True),
),
("nullable", enumfields.fields.EnumIntegerField(blank=True, enum=tests.enums.Taste, null=True)),
("random_code", models.TextField(blank=True, null=True)),
("zero", enumfields.fields.EnumIntegerField(default=0, enum=tests.enums.ZeroEnum)),
("zero2", enumfields.fields.EnumIntegerField(blank=True, default=0, enum=tests.enums.ZeroEnum)),
(
"int_enum",
enumfields.fields.EnumIntegerField(
blank=True, default=None, enum=tests.enums.IntegerEnum, null=True
),
),
(
"int_enum_not_editable",
enumfields.fields.EnumIntegerField(default=0, editable=False, enum=tests.enums.IntegerEnum),
),
(
"labeled_enum",
enumfields.fields.EnumField(blank=True, enum=tests.enums.LabeledEnum, max_length=10, null=True),
),
],
),
]
Empty file added tests/migrations/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'django.contrib.sessions',
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.messages',
'tests',
)

Expand Down Expand Up @@ -42,9 +43,12 @@
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.request',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
3 changes: 2 additions & 1 deletion tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enumfields import EnumField
from django.db import models

from enumfields import EnumField
from tests.enums import LabeledEnum


Expand Down
1 change: 1 addition & 0 deletions tests/test_django_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from django.urls import reverse

from enumfields import EnumIntegerField

from .enums import Color, IntegerEnum, Taste, ZeroEnum
Expand Down
9 changes: 4 additions & 5 deletions tests/test_django_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.db import connection

import pytest
from django.db import connection

from .enums import Color, IntegerEnum, LabeledEnum, Taste, ZeroEnum
from .models import MyModel
Expand Down Expand Up @@ -95,9 +94,9 @@ def test_int_enum():
def test_serialization():
from django.core.serializers.python import Serializer as PythonSerializer
m = MyModel(color=Color.RED, taste=Taste.SALTY)
ser = PythonSerializer()
ser.serialize([m])
fields = ser.getvalue()[0]["fields"]
serializer = PythonSerializer()
serializer.serialize([m])
fields = serializer.getvalue()[0]["fields"]
assert fields["color"] == m.color.value
assert fields["taste"] == m.taste.value

Expand Down
6 changes: 5 additions & 1 deletion tests/test_enums.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from django.core.exceptions import ValidationError
from django.forms import BaseForm

import pytest
from enumfields import Enum, EnumField

from .enums import Color, IntegerEnum
Expand Down Expand Up @@ -72,3 +72,7 @@ def test_programmatic():
'Green': 'g',
'Blue': 'b',
})
assert Foople.Red == Foople('r')
assert Foople.Red.label == 'Red'
assert Foople.Red.value == 'r'
assert Foople.Red.name == 'Red'
3 changes: 1 addition & 2 deletions tests/test_form_fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest
from django.db.models import BLANK_CHOICE_DASH
from django.forms.models import model_to_dict, modelform_factory

import pytest

from .enums import Color, ZeroEnum
from .models import MyModel

Expand Down
3 changes: 2 additions & 1 deletion tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import uuid

import pytest
from rest_framework import serializers

from enumfields.drf import EnumField
from enumfields.drf.serializers import EnumSupportSerializerMixin
from rest_framework import serializers

from .enums import Color, IntegerEnum, Taste
from .models import MyModel
Expand Down
2 changes: 1 addition & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf import settings
from django.urls import re_path
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import re_path

admin.autodiscover()

Expand Down