Skip to content

Commit

Permalink
Merge pull request #76 from vintasoftware/feat/setup-ruff
Browse files Browse the repository at this point in the history
Set up `ruff`
  • Loading branch information
pamella authored May 23, 2024
2 parents ef6ebd9 + 492b722 commit 70e728d
Show file tree
Hide file tree
Showing 27 changed files with 351 additions and 366 deletions.
9 changes: 0 additions & 9 deletions .isort.cfg

This file was deleted.

17 changes: 10 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ repos:
- id: debug-statements
- id: detect-private-key

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.4
hooks:
# Run the linter.
- id: ruff
args: [--fix]
# Run the formatter.
- id: ruff-format

- repo: local
hooks:
- id: isort
name: isort-local
entry: isort
language: python
types: [python]
exclude: .+/(settings|migrations)/.+
pass_filenames: true
- id: eslint
name: eslint-local
entry: npm run lint
Expand Down
36 changes: 36 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
exclude = [
"env",
"venv",
".pyenv",
".pytest_cache",
"*/__pycache__/*",
"*/migrations/*",
"docs",
]

line-length = 100

target-version = "py38"

[format]
quote-style = "double"
indent-style = "space"
line-ending = "auto"

[lint]
extend-select = ["I"]

[lint.isort.sections]
django = ["django"]
drf = ["rest_framework"]

[lint.isort]
section-order = [
"future",
"standard-library",
"django",
"drf",
"third-party",
"first-party",
"local-folder",
]
6 changes: 4 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import pytest

import django
from django.conf import settings

import pytest

# We manually designate which settings we will be using in an environment variable
# This is similar to what occurs in the `manage.py`
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings")


# `pytest` automatically calls this function once when tests are run.
Expand Down
2 changes: 1 addition & 1 deletion drf_rw_serializers/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class DrfRwSerializersConfig(AppConfig):
Configuration for the drf_rw_serializers Django application.
"""

name = 'drf_rw_serializers'
name = "drf_rw_serializers"
45 changes: 14 additions & 31 deletions drf_rw_serializers/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from rest_framework import generics, mixins

from .mixins import CreateModelMixin, UpdateModelMixin, ListModelMixin, RetrieveModelMixin
from .mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin


class GenericAPIView(generics.GenericAPIView):

def get_serializer_class(self):
"""
Return the class to use for the serializer.
Expand All @@ -16,13 +15,12 @@ def get_serializer_class(self):
(Eg. admins get full serialization, others get basic serialization)
"""
assert (
self.serializer_class is not None or
getattr(self, 'read_serializer_class', None) is not None
self.serializer_class is not None
or getattr(self, "read_serializer_class", None) is not None
), (
"'%s' should either include one of `serializer_class` and `read_serializer_class` "
"attribute, or override one of the `get_serializer_class()`, "
"`get_read_serializer_class()` method."
% self.__class__.__name__
"`get_read_serializer_class()` method." % self.__class__.__name__
)

return self.serializer_class
Expand All @@ -32,7 +30,7 @@ def get_read_serializer(self, *args, **kwargs):
Return the serializer instance that should be used for serializing output.
"""
serializer_class = self.get_read_serializer_class()
kwargs['context'] = self.get_serializer_context()
kwargs["context"] = self.get_serializer_context()
return serializer_class(*args, **kwargs)

def get_read_serializer_class(self):
Expand All @@ -43,7 +41,7 @@ def get_read_serializer_class(self):
serializations depending on the incoming request.
(Eg. admins get full serialization, others get basic serialization)
"""
if getattr(self, 'read_serializer_class', None) is None:
if getattr(self, "read_serializer_class", None) is None:
return self.get_serializer_class()

return self.read_serializer_class
Expand All @@ -54,7 +52,7 @@ def get_write_serializer(self, *args, **kwargs):
and deserializing input.
"""
serializer_class = self.get_write_serializer_class()
kwargs['context'] = self.get_serializer_context()
kwargs["context"] = self.get_serializer_context()
return serializer_class(*args, **kwargs)

def get_write_serializer_class(self):
Expand All @@ -65,20 +63,18 @@ def get_write_serializer_class(self):
serializations depending on the incoming request.
(Eg. admins can send extra fields, others cannot)
"""
if getattr(self, 'write_serializer_class', None) is None:
if getattr(self, "write_serializer_class", None) is None:
return self.get_serializer_class()

return self.write_serializer_class


class CreateAPIView(CreateModelMixin, GenericAPIView):

def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)


class UpdateAPIView(UpdateModelMixin, GenericAPIView):

def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)

Expand All @@ -87,43 +83,32 @@ def patch(self, request, *args, **kwargs):


class ListAPIView(ListModelMixin, GenericAPIView):

def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)


class RetrieveAPIView(RetrieveModelMixin, GenericAPIView):

def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)


class ListCreateAPIView(ListModelMixin,
CreateModelMixin,
GenericAPIView):

class ListCreateAPIView(ListModelMixin, CreateModelMixin, GenericAPIView):
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)


class RetrieveDestroyAPIView(RetrieveModelMixin,
mixins.DestroyModelMixin,
GenericAPIView):

class RetrieveDestroyAPIView(RetrieveModelMixin, mixins.DestroyModelMixin, GenericAPIView):
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)


class RetrieveUpdateAPIView(RetrieveModelMixin,
UpdateModelMixin,
GenericAPIView):

class RetrieveUpdateAPIView(RetrieveModelMixin, UpdateModelMixin, GenericAPIView):
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

Expand All @@ -134,11 +119,9 @@ def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)


class RetrieveUpdateDestroyAPIView(RetrieveModelMixin,
UpdateModelMixin,
mixins.DestroyModelMixin,
GenericAPIView):

class RetrieveUpdateDestroyAPIView(
RetrieveModelMixin, UpdateModelMixin, mixins.DestroyModelMixin, GenericAPIView
):
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

Expand Down
12 changes: 3 additions & 9 deletions drf_rw_serializers/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@


class UpdateModelMixin(mixins.UpdateModelMixin):

def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
partial = kwargs.pop("partial", False)
instance = self.get_object()
write_serializer = self.get_write_serializer(
instance, data=request.data, partial=partial)
write_serializer = self.get_write_serializer(instance, data=request.data, partial=partial)
write_serializer.is_valid(raise_exception=True)
self.perform_update(write_serializer)

# pylint: disable=protected-access
if getattr(instance, '_prefetched_objects_cache', None) is not None:

if getattr(instance, "_prefetched_objects_cache", None) is not None:
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache = {}
Expand All @@ -27,7 +24,6 @@ def update(self, request, *args, **kwargs):


class CreateModelMixin(mixins.CreateModelMixin):

def create(self, request, *args, **kwargs):
write_serializer = self.get_write_serializer(data=request.data)
write_serializer.is_valid(raise_exception=True)
Expand All @@ -40,7 +36,6 @@ def create(self, request, *args, **kwargs):


class ListModelMixin(mixins.ListModelMixin):

def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())

Expand All @@ -54,7 +49,6 @@ def list(self, request, *args, **kwargs):


class RetrieveModelMixin(mixins.RetrieveModelMixin):

def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_read_serializer(instance)
Expand Down
25 changes: 12 additions & 13 deletions drf_rw_serializers/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
# -*- coding: utf-8 -*-

from rest_framework import viewsets, mixins
from rest_framework import mixins, viewsets

from .generics import GenericAPIView
from .mixins import CreateModelMixin, UpdateModelMixin, ListModelMixin, RetrieveModelMixin
from .mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin


class GenericViewSet(GenericAPIView,
viewsets.GenericViewSet):
class GenericViewSet(GenericAPIView, viewsets.GenericViewSet):
pass


class ModelViewSet(CreateModelMixin,
RetrieveModelMixin,
UpdateModelMixin,
mixins.DestroyModelMixin,
ListModelMixin,
GenericViewSet):
class ModelViewSet(
CreateModelMixin,
RetrieveModelMixin,
UpdateModelMixin,
mixins.DestroyModelMixin,
ListModelMixin,
GenericViewSet,
):
pass


class ReadOnlyModelViewSet(RetrieveModelMixin,
ListModelMixin,
GenericViewSet):
class ReadOnlyModelViewSet(RetrieveModelMixin, ListModelMixin, GenericViewSet):
pass
2 changes: 1 addition & 1 deletion example_app/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
default_app_config = 'example_app.apps.ExampleAppConfig' # pylint: disable=invalid-name
default_app_config = "example_app.apps.ExampleAppConfig" # pylint: disable=invalid-name
2 changes: 1 addition & 1 deletion example_app/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class ExampleAppConfig(AppConfig):
Configuration for the example_app Django application.
"""

name = 'example_app'
name = "example_app"
12 changes: 5 additions & 7 deletions example_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ class Order(models.Model):

@property
def total_price(self):
return sum([
o.meal.price * o.quantity
for o in self.ordered_meals.select_related('meal').all()
])
return sum(
[o.meal.price * o.quantity for o in self.ordered_meals.select_related("meal").all()]
)


class OrderedMeal(models.Model):
order = models.ForeignKey(
'Order', related_name="ordered_meals", on_delete=models.CASCADE)
meal = models.ForeignKey('Meal', on_delete=models.CASCADE)
order = models.ForeignKey("Order", related_name="ordered_meals", on_delete=models.CASCADE)
meal = models.ForeignKey("Meal", on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)


Expand Down
Loading

0 comments on commit 70e728d

Please sign in to comment.