Skip to content

Commit e955d3a

Browse files
author
Rafael Henter
committed
Fix PEP8
1 parent 1c612bd commit e955d3a

31 files changed

Lines changed: 450 additions & 452 deletions

django_models/__init__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,12 @@
1717

1818
from typing import List
1919

20-
default_app_config = 'django_models.apps.DjangoModelsConfig'
20+
default_app_config = "django_models.apps.DjangoModelsConfig"
2121

2222
from .version import __version__ # noqa
2323
from . import utils # noqa
2424
from . import fields # noqa
2525
from . import forms # noqa
2626
from . import validators # noqa
2727

28-
__all__: List[str] = [
29-
'__version__',
30-
'utils',
31-
'fields',
32-
'forms',
33-
'validators'
34-
]
28+
__all__: List[str] = ["__version__", "utils", "fields", "forms", "validators"]

django_models/apps.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
class DjangoModelsConfig(AppConfig):
55
"""
66
Django app configuration for django_models package.
7-
7+
88
This configuration class is required for Django to properly
99
recognize and load the django_models package as a Django app.
1010
"""
11-
default_auto_field = 'django.db.models.BigAutoField'
12-
name = 'django_models'
13-
verbose_name = 'Django Models Library'
11+
12+
default_auto_field = "django.db.models.BigAutoField"
13+
name = "django_models"
14+
verbose_name = "Django Models Library"

django_models/fields.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import uuid
2-
from typing import Any, Optional
2+
from typing import Any
33

44
from django.core.validators import RegexValidator
55
from django.db import models
@@ -12,7 +12,7 @@ class UUIDPrimaryKeyField(models.UUIDField):
1212
1313
This field automatically configures itself as:
1414
- primary_key=True
15-
- unique=True
15+
- unique=True
1616
- editable=False
1717
1818
It generates a UUID4 value automatically when the model instance is saved
@@ -27,9 +27,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
2727
*args: Variable length argument list passed to parent class
2828
**kwargs: Arbitrary keyword arguments passed to parent class
2929
"""
30-
kwargs['primary_key'] = True
31-
kwargs['unique'] = True
32-
kwargs['editable'] = False
30+
kwargs["primary_key"] = True
31+
kwargs["unique"] = True
32+
kwargs["editable"] = False
3333
super().__init__(*args, **kwargs)
3434

3535
def pre_save(self, model_instance: Model, add: bool) -> uuid.UUID:
@@ -56,13 +56,13 @@ class CharFieldDigitsOnly(models.CharField):
5656
"""
5757
A CharField that only accepts digits and whitespace characters.
5858
59-
This field validates that the input contains only digits (0-9) and
59+
This field validates that the input contains only digits (0-9) and
6060
whitespace characters. It's useful for fields like phone numbers,
6161
postal codes, or other numeric identifiers that may contain spaces.
6262
"""
63+
6364
default_validators = [
6465
RegexValidator(
65-
r'^([\s\d]+)$',
66-
'Only digits and whitespace characters are allowed.'
66+
r"^([\s\d]+)$", "Only digits and whitespace characters are allowed."
6767
)
6868
]

django_models/forms.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Tuple, Union
1+
from typing import Any, Tuple
22
from django.forms import ValidationError
33
from django.forms.fields import CharField
44
from django.utils.translation import gettext_lazy as _
@@ -17,6 +17,7 @@ class InvalidValuesField:
1717
Attributes:
1818
invalid_values: A tuple of values that are considered invalid for this field.
1919
"""
20+
2021
invalid_values: Tuple[str, ...] = ()
2122

2223
def clean(self, value: str) -> str:
@@ -49,9 +50,12 @@ class CPFField(InvalidValuesField, CharField):
4950
Attributes:
5051
invalid_values: Tuple of known invalid CPF values that should be rejected
5152
"""
52-
invalid_values: Tuple[str, ...] = ('00000000191',)
5353

54-
def __init__(self, max_length: int = 14, min_length: int = 11, *args: Any, **kwargs: Any) -> None:
54+
invalid_values: Tuple[str, ...] = ("00000000191",)
55+
56+
def __init__(
57+
self, max_length: int = 14, min_length: int = 11, *args: Any, **kwargs: Any
58+
) -> None:
5559
"""
5660
Initialize the CPF field.
5761
@@ -94,13 +98,22 @@ class CNPJField(InvalidValuesField, CharField):
9498
Attributes:
9599
invalid_values: Tuple of known invalid CNPJ values that should be rejected
96100
"""
101+
97102
invalid_values: Tuple[str, ...] = (
98-
'00000000000000', '22222222000191', '33333333000191', '44444444000191',
99-
'55555555000191', '66666666000191', '77777777000191', '88888888000191',
100-
'99999999000191'
103+
"00000000000000",
104+
"22222222000191",
105+
"33333333000191",
106+
"44444444000191",
107+
"55555555000191",
108+
"66666666000191",
109+
"77777777000191",
110+
"88888888000191",
111+
"99999999000191",
101112
)
102113

103-
def __init__(self, min_length: int = 14, max_length: int = 18, *args: Any, **kwargs: Any) -> None:
114+
def __init__(
115+
self, min_length: int = 14, max_length: int = 18, *args: Any, **kwargs: Any
116+
) -> None:
104117
"""
105118
Initialize the CNPJ field.
106119

django_models/models/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
from .history import HistoryModel # noqa
44

55
__all__ = [
6-
'SlugModel', 'TimestampedModel', 'UUIDModel', 'SerializerModel',
7-
'SignalsModel', 'HistoryModel', 'SoftDeleteSignalModel',
6+
"SlugModel",
7+
"TimestampedModel",
8+
"UUIDModel",
9+
"SerializerModel",
10+
"SignalsModel",
11+
"HistoryModel",
12+
"SoftDeleteSignalModel",
813
]

django_models/models/generic.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import partial
2-
from typing import Any, Dict, Type, Optional
2+
from typing import Any, Dict, Type
33
from uuid import UUID
44

55
from django.db import models
@@ -38,7 +38,8 @@ class ActiveModel(models.Model):
3838
Attributes:
3939
is_active (BooleanField): Boolean field indicating if the record is active
4040
"""
41-
is_active = models.BooleanField(_('Active'), default=True)
41+
42+
is_active = models.BooleanField(_("Active"), default=True)
4243

4344
class Meta:
4445
abstract = True
@@ -56,10 +57,11 @@ class CodeModel(models.Model):
5657
Attributes:
5758
code (CharField): Unique 8-character code automatically generated
5859
"""
60+
5961
code = models.CharField(
6062
max_length=32,
6163
default=generate_code,
62-
verbose_name=_('Model code'),
64+
verbose_name=_("Model code"),
6365
unique=True,
6466
)
6567

@@ -85,12 +87,13 @@ def serializer(self) -> Type[ModelSerializer]:
8587
Returns:
8688
A ModelSerializer class configured for this model instance
8789
"""
90+
8891
class SelfSerializer(ModelSerializer):
8992
class Meta:
9093
pass
9194

9295
SelfSerializer.Meta.model = self
93-
SelfSerializer.Meta.fields = '__all__'
96+
SelfSerializer.Meta.fields = "__all__"
9497
return SelfSerializer
9598

9699
def serialize(self) -> Dict[str, Any]:
@@ -126,6 +129,7 @@ class SlugModel(BaseModel):
126129
Attributes:
127130
slug (SlugField): URL-friendly identifier field
128131
"""
132+
129133
slug = models.SlugField(max_length=16)
130134

131135
class Meta:
@@ -143,7 +147,10 @@ class SortOrderModel(models.Model):
143147
Attributes:
144148
sort_order (PositiveIntegerField): Field for manual ordering of instances
145149
"""
146-
sort_order = models.PositiveIntegerField(default=0, blank=False, null=False, verbose_name=_("Sort"))
150+
151+
sort_order = models.PositiveIntegerField(
152+
default=0, blank=False, null=False, verbose_name=_("Sort")
153+
)
147154

148155
class Meta:
149156
abstract = True
@@ -163,12 +170,13 @@ def _get_next_sort_order(self) -> int:
163170
return self.sort_order
164171

165172
# Get the highest sort_order value from existing instances
166-
last_instance = type(self).objects.order_by('-sort_order').first()
173+
last_instance = type(self).objects.order_by("-sort_order").first()
167174
if last_instance and last_instance.sort_order:
168175
return last_instance.sort_order + 1
169176

170177
return 1
171178

179+
172180
class TimestampedModel(models.Model):
173181
"""
174182
Abstract model that adds automatic timestamp fields for creation and updates.
@@ -181,12 +189,11 @@ class TimestampedModel(models.Model):
181189
created_at (DateTimeField): Timestamp when the instance was created
182190
updated_at (DateTimeField): Timestamp when the instance was last updated
183191
"""
192+
184193
created_at = models.DateTimeField(
185-
db_index=True, auto_now_add=True, verbose_name=_('Created at')
186-
)
187-
updated_at = models.DateTimeField(
188-
auto_now=True, verbose_name=_('Updated at')
194+
db_index=True, auto_now_add=True, verbose_name=_("Created at")
189195
)
196+
updated_at = models.DateTimeField(auto_now=True, verbose_name=_("Updated at"))
190197

191198
class Meta:
192199
abstract = True
@@ -203,6 +210,7 @@ class UUIDModel(BaseModel):
203210
Attributes:
204211
id (UUIDPrimaryKeyField): UUID primary key field
205212
"""
213+
206214
id = UUIDPrimaryKeyField()
207215

208216
class Meta:

django_models/models/history.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@
55

66
class HistoryModel(SignalsModel, TimestampedModel):
77
history_model = None
8-
history_parent_field_name = 'parent'
8+
history_parent_field_name = "parent"
99

1010
def __init__(self, *args, **kwargs):
1111
if not self.history_model:
1212
raise HistoryModelNotSetError(
13-
"You should set the history_model attribute of {}".format(type(self).__name__)
13+
"You should set the history_model attribute of {}".format(
14+
type(self).__name__
15+
)
1416
)
1517

1618
super().__init__(*args, **kwargs)
1719

1820
def save_history(self):
1921
data = {self.history_parent_field_name: self}
2022

21-
history_model_fields = tuple(field.name for field in self.history_model._meta.fields)
23+
history_model_fields = tuple(
24+
field.name for field in self.history_model._meta.fields
25+
)
2226

2327
for field in self._meta.fields:
24-
if field.name in ('id', 'created_at', 'updated_at'):
28+
if field.name in ("id", "created_at", "updated_at"):
2529
continue
2630

2731
if field.name in history_model_fields:

django_models/models/managers.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ def delete(self):
2424

2525
def hard_delete(self):
2626
"""Delete the records in the current QuerySet."""
27-
self._not_support_combined_queries('delete')
28-
assert not self.query.is_sliced, \
29-
"Cannot use 'limit' or 'offset' with delete."
27+
self._not_support_combined_queries("delete")
28+
assert not self.query.is_sliced, "Cannot use 'limit' or 'offset' with delete."
3029

3130
if self._fields is not None:
3231
raise TypeError("Cannot call delete() after .values() or .values_list()")
@@ -55,15 +54,15 @@ def restore(self):
5554
return super().update(deleted_at=None, is_deleted=False)
5655

5756
def __repr__(self):
58-
data = list(self[:REPR_OUTPUT_SIZE + 1])
57+
data = list(self[: REPR_OUTPUT_SIZE + 1])
5958
if len(data) > REPR_OUTPUT_SIZE:
6059
data[-1] = "...(remaining elements truncated)..."
61-
return '<QuerySet %r>' % data
60+
return "<QuerySet %r>" % data
6261

6362

6463
class SoftDeleteSignalsManager(SignalsManager):
6564
def __init__(self, *args, **kwargs):
66-
self.show_deleted = kwargs.pop('show_deleted', False)
65+
self.show_deleted = kwargs.pop("show_deleted", False)
6766
super().__init__(*args, **kwargs)
6867

6968
def get_queryset(self):
@@ -81,7 +80,7 @@ def restore(self):
8180
return self.get_queryset().restore()
8281

8382
def filter(self, *args, **kwargs):
84-
if 'is_deleted' in kwargs:
83+
if "is_deleted" in kwargs:
8584
qs = SoftDeleteQuerySet(self.model, using=self._db)
8685
return qs.filter(*args, **kwargs)
8786
return super().filter(*args, **kwargs)

0 commit comments

Comments
 (0)