Skip to content

Commit

Permalink
Merge pull request #31 from em1208/package-updates
Browse files Browse the repository at this point in the history
Upgraded dependencies
  • Loading branch information
em1208 committed Mar 19, 2024
2 parents a9a117f + 892cc54 commit e733285
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 252 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- '3.9'
- '3.10'
- '3.11'
- '3.12'

steps:
- uses: actions/checkout@v3
Expand All @@ -26,7 +27,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install poetry
run: python -m pip install poetry==1.2.2
run: python -m pip install poetry==1.8.2

- name: Install dependencies
run: |
Expand Down
16 changes: 7 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ repos:
- id: check-toml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.3
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
additional_dependencies:
- flake8-tidy-imports
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
9 changes: 2 additions & 7 deletions adrf/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ def api_view(http_method_names=None):
Decorator that converts a function-based view into an APIView subclass.
Takes a list of allowed methods for the view as an argument.
"""
http_method_names = (
["GET"] if (http_method_names is None) else http_method_names
)
http_method_names = ["GET"] if (http_method_names is None) else http_method_names

def decorator(func):

WrappedAPIView = type(
"WrappedAPIView", (APIView,), {"__doc__": func.__doc__}
)
WrappedAPIView = type("WrappedAPIView", (APIView,), {"__doc__": func.__doc__})

# Note, the above allows us to set the docstring.
# It is the equivalent of:
Expand Down
7 changes: 1 addition & 6 deletions adrf/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,10 @@ def _authenticate(self):
exceptions.APIException: If an exception occurs during authentication.
"""
self._authenticator, self.user, self.auth = None, None, None

for authenticator in self.authenticators:

try:
if asyncio.iscoroutinefunction(authenticator.authenticate):
user_auth_tuple = async_to_sync(
authenticator.authenticate
)(self)
user_auth_tuple = async_to_sync(authenticator.authenticate)(self)
else:
user_auth_tuple = authenticator.authenticate(self)
except exceptions.APIException:
Expand Down
49 changes: 16 additions & 33 deletions adrf/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
from rest_framework.serializers import ListSerializer as DRFListSerializer
from rest_framework.serializers import ModelSerializer as DRFModelSerializer
from rest_framework.serializers import Serializer as DRFSerializer
from rest_framework.serializers import \
SerializerMetaclass as DRFSerializerMetaclass
from rest_framework.serializers import (
model_meta, raise_errors_on_nested_writes
)
from rest_framework.serializers import SerializerMetaclass as DRFSerializerMetaclass
from rest_framework.serializers import model_meta, raise_errors_on_nested_writes
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList


Expand All @@ -38,22 +35,20 @@ def many_init(cls, *args, **kwargs):
list_kwargs["max_length"] = max_length
if min_length is not None:
list_kwargs["min_length"] = min_length
list_kwargs.update({
key: value
for key, value in kwargs.items()
if key in LIST_SERIALIZER_KWARGS
})
meta = getattr(cls, "Meta", None)
list_serializer_class = getattr(
meta, "list_serializer_class", ListSerializer
list_kwargs.update(
{
key: value
for key, value in kwargs.items()
if key in LIST_SERIALIZER_KWARGS
}
)
meta = getattr(cls, "Meta", None)
list_serializer_class = getattr(meta, "list_serializer_class", ListSerializer)
return list_serializer_class(*args, **list_kwargs)

@async_property
async def adata(self):
if hasattr(self, "initial_data") and not hasattr(
self, "_validated_data"
):
if hasattr(self, "initial_data") and not hasattr(self, "_validated_data"):
msg = (
"When a serializer is passed a `data` keyword argument you "
"must call `.is_valid()` before attempting to access the "
Expand All @@ -64,9 +59,7 @@ async def adata(self):
raise AssertionError(msg)

if not hasattr(self, "_data"):
if self.instance is not None and not getattr(
self, "_errors", None
):
if self.instance is not None and not getattr(self, "_errors", None):
self._data = await self.ato_representation(self.instance)
elif hasattr(self, "_validated_data") and not getattr(
self, "_errors", None
Expand All @@ -78,9 +71,7 @@ async def adata(self):
return self._data

async def ato_representation(self, instance):
raise NotImplementedError(
"`ato_representation()` must be implemented."
)
raise NotImplementedError("`ato_representation()` must be implemented.")

async def aupdate(self, instance, validated_data):
raise NotImplementedError("`aupdate()` must be implemented.")
Expand Down Expand Up @@ -162,14 +153,11 @@ async def ato_representation(self, instance):
) + [DRFModelSerializer.serializer_choice_field]

check_for_none = (
attribute.pk
if isinstance(attribute, models.Model)
else attribute
attribute.pk if isinstance(attribute, models.Model) else attribute
)
if check_for_none is None:
ret[field.field_name] = None
else:

if is_drf_field:
repr = field.to_representation(attribute)
else:
Expand All @@ -192,10 +180,7 @@ async def ato_representation(self, data):
data = data.all()

if isinstance(data, models.query.QuerySet):
return [
await self.child.ato_representation(item)
async for item in data
]
return [await self.child.ato_representation(item) async for item in data]
else:
return [await self.child.ato_representation(item) for item in data]

Expand Down Expand Up @@ -262,9 +247,7 @@ async def acreate(self, validated_data):
many_to_many[field_name] = validated_data.pop(field_name)

try:
instance = await ModelClass._default_manager.acreate(
**validated_data
)
instance = await ModelClass._default_manager.acreate(**validated_data)
except TypeError:
tb = traceback.format_exc()
msg = (
Expand Down
85 changes: 45 additions & 40 deletions adrf/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def _encode_data(self, data, format=None, content_type=None):
"""

if data is None:
return ('', content_type)
return ("", content_type)

assert format is None or content_type is None, (
'You may not set both `format` and `content_type`.'
)
assert (
format is None or content_type is None
), "You may not set both `format` and `content_type`."

if content_type:
# Content type specified explicitly, treat data as a raw bytestring
Expand All @@ -62,7 +62,7 @@ def _encode_data(self, data, format=None, content_type=None):
"Set TEST_REQUEST_RENDERER_CLASSES to enable "
"extra request formats.".format(
format,
', '.join(["'" + fmt + "'" for fmt in self.renderer_classes])
", ".join(["'" + fmt + "'" for fmt in self.renderer_classes]),
)
)

Expand All @@ -73,9 +73,7 @@ def _encode_data(self, data, format=None, content_type=None):
# Determine the content-type header from the renderer
content_type = renderer.media_type
if renderer.charset:
content_type = "{}; charset={}".format(
content_type, renderer.charset
)
content_type = "{}; charset={}".format(content_type, renderer.charset)

# Coerce text to bytes if required.
if isinstance(ret, str):
Expand All @@ -85,45 +83,51 @@ def _encode_data(self, data, format=None, content_type=None):

def get(self, path, data=None, **extra):
r = {
'QUERY_STRING': urlencode(data or {}, doseq=True),
"QUERY_STRING": urlencode(data or {}, doseq=True),
}
if not data and '?' in path:
if not data and "?" in path:
# Fix to support old behavior where you have the arguments in the
# url. See #1461.
query_string = force_bytes(path.split('?')[1])
query_string = query_string.decode('iso-8859-1')
r['QUERY_STRING'] = query_string
query_string = force_bytes(path.split("?")[1])
query_string = query_string.decode("iso-8859-1")
r["QUERY_STRING"] = query_string
r.update(extra)
return self.generic('GET', path, **r)
return self.generic("GET", path, **r)

def post(self, path, data=None, format=None, content_type=None, **extra):
data, content_type = self._encode_data(data, format, content_type)
return self.generic('POST', path, data, content_type, **extra)
return self.generic("POST", path, data, content_type, **extra)

def put(self, path, data=None, format=None, content_type=None, **extra):
data, content_type = self._encode_data(data, format, content_type)
return self.generic('PUT', path, data, content_type, **extra)
return self.generic("PUT", path, data, content_type, **extra)

def patch(self, path, data=None, format=None, content_type=None, **extra):
data, content_type = self._encode_data(data, format, content_type)
return self.generic('PATCH', path, data, content_type, **extra)
return self.generic("PATCH", path, data, content_type, **extra)

def delete(self, path, data=None, format=None, content_type=None, **extra):
data, content_type = self._encode_data(data, format, content_type)
return self.generic('DELETE', path, data, content_type, **extra)
return self.generic("DELETE", path, data, content_type, **extra)

def options(self, path, data=None, format=None, content_type=None, **extra):
data, content_type = self._encode_data(data, format, content_type)
return self.generic('OPTIONS', path, data, content_type, **extra)

def generic(self, method, path, data='',
content_type='application/octet-stream', secure=False, **extra):
return self.generic("OPTIONS", path, data, content_type, **extra)

def generic(
self,
method,
path,
data="",
content_type="application/octet-stream",
secure=False,
**extra,
):
# Include the CONTENT_TYPE, regardless of whether or not data is empty.
if content_type is not None:
extra['CONTENT_TYPE'] = str(content_type)
extra["CONTENT_TYPE"] = str(content_type)

return super().generic(
method, path, data, content_type, secure, **extra)
return super().generic(method, path, data, content_type, secure, **extra)

def request(self, **kwargs):
request = super().request(**kwargs)
Expand All @@ -138,6 +142,7 @@ class AsyncAPIClient(DjangoAsyncClient, AsyncAPIRequestFactory):
Does not currently support "follow" on its methods.
"""

def __init__(self, enforce_csrf_checks=False, **defaults):
super().__init__(**defaults)
self.handler = AsyncForceAuthClientHandler(enforce_csrf_checks)
Expand Down Expand Up @@ -168,34 +173,34 @@ def get(self, path, data=None, **extra):
response = super().get(path, data=data, **extra)
return response

def post(self, path, data=None, format=None, content_type=None,
**extra):
def post(self, path, data=None, format=None, content_type=None, **extra):
response = super().post(
path, data=data, format=format, content_type=content_type, **extra)
path, data=data, format=format, content_type=content_type, **extra
)
return response

def put(self, path, data=None, format=None, content_type=None,
**extra):
def put(self, path, data=None, format=None, content_type=None, **extra):
response = super().put(
path, data=data, format=format, content_type=content_type, **extra)
path, data=data, format=format, content_type=content_type, **extra
)
return response

def patch(self, path, data=None, format=None, content_type=None,
**extra):
def patch(self, path, data=None, format=None, content_type=None, **extra):
response = super().patch(
path, data=data, format=format, content_type=content_type, **extra)
path, data=data, format=format, content_type=content_type, **extra
)
return response

def delete(self, path, data=None, format=None, content_type=None,
**extra):
def delete(self, path, data=None, format=None, content_type=None, **extra):
response = super().delete(
path, data=data, format=format, content_type=content_type, **extra)
path, data=data, format=format, content_type=content_type, **extra
)
return response

def options(self, path, data=None, format=None, content_type=None,
**extra):
def options(self, path, data=None, format=None, content_type=None, **extra):
response = super().options(
path, data=data, format=format, content_type=content_type, **extra)
path, data=data, format=format, content_type=content_type, **extra
)
return response

def logout(self):
Expand Down

0 comments on commit e733285

Please sign in to comment.