Skip to content

Commit

Permalink
Merge pull request #101 from browniebroke/lint/enable-more-ruff-rules
Browse files Browse the repository at this point in the history
Modernize a bit the codebase with Ruff
  • Loading branch information
browniebroke authored Oct 16, 2024
2 parents 4a01c06 + c9ca309 commit cefa815
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 31 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
rev: v0.6.9
hooks:
- id: ruff-format
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
5 changes: 3 additions & 2 deletions drf_excel/fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import contextlib
import datetime
import json
from collections.abc import Iterable
from decimal import Decimal
from typing import Any, Callable, Iterable, Union
from typing import Any, Callable, Union

from django.utils.dateparse import parse_date, parse_datetime, parse_time
from openpyxl.cell import Cell
Expand All @@ -29,7 +30,7 @@
from drf_excel.utilities import XLSXStyle, get_setting, sanitize_value, set_cell_style


class XLSXField(object):
class XLSXField:
sanitize = True

def __init__(
Expand Down
7 changes: 4 additions & 3 deletions drf_excel/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework.response import Response


class XLSXFileMixin(object):
class XLSXFileMixin:
"""
Mixin which allows the override of the filename being
passed back to the user when the spreadsheet is downloaded.
Expand All @@ -26,7 +26,8 @@ def finalize_response(self, request, response, *args, **kwargs):
isinstance(response, Response)
and response.accepted_renderer.format == "xlsx"
):
response["content-disposition"] = "attachment; filename={}".format(
escape_uri_path(self.get_filename(request=request, *args, **kwargs)),
filename = self.get_filename(request, *args, **kwargs)
response["content-disposition"] = (
f"attachment; filename={escape_uri_path(filename)}"
)
return response
5 changes: 2 additions & 3 deletions drf_excel/renderers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from collections.abc import Iterable, MutableMapping
from tempfile import TemporaryFile
from typing import Dict

from django.utils.functional import Promise
from openpyxl import Workbook
Expand Down Expand Up @@ -57,7 +56,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
Render `data` into XLSX workbook, returning a workbook.
"""
if data is None:
return bytes()
return b""

if not self._check_validation_data(data):
return json.dumps(data)
Expand Down Expand Up @@ -315,7 +314,7 @@ def _get_label(parent_label, label_sep, obj):

return _header_dict

def _flatten_data(self, data, parent_key="", key_sep=".") -> Dict[str, XLSXField]:
def _flatten_data(self, data, parent_key="", key_sep=".") -> dict[str, XLSXField]:
items = []
for k, v in data.items():
new_key = f"{parent_key}{key_sep}{k}" if parent_key else k
Expand Down
2 changes: 1 addition & 1 deletion drf_excel/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ESCAPE_CHARS = ("=", "-", "+", "@", "\t", "\r", "\n")


class XLSXStyle(object):
class XLSXStyle:
# Class that holds all parts of a style, but without being an actual NamedStyle

def __init__(self, style_dict=None):
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ DJANGO_SETTINGS_MODULE = "tests.settings"
[tool.coverage.run]
plugins = ["django_coverage_plugin"]
include = ["drf_excel/*"]

[tool.ruff]
target-version = "py39"

[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"UP", # pyupgrade
"I", # isort
]
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import io
from typing import Union, Callable
from typing import Callable, Union

import pytest
from openpyxl.reader.excel import load_workbook
Expand Down
20 changes: 10 additions & 10 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
from decimal import Decimal
import datetime as dt
from decimal import Decimal
from types import SimpleNamespace

import pytest
from openpyxl.cell import Cell
from openpyxl.worksheet.worksheet import Worksheet
from rest_framework.fields import (
BooleanField,
CharField,
IntegerField,
FloatField,
DecimalField,
DateTimeField,
DateField,
TimeField,
DateTimeField,
DecimalField,
FloatField,
IntegerField,
ListField,
BooleanField,
TimeField,
)

from drf_excel.fields import (
XLSXField,
XLSXNumberField,
XLSXBooleanField,
XLSXDateField,
XLSXField,
XLSXListField,
XLSXBooleanField,
XLSXNumberField,
)
from drf_excel.utilities import XLSXStyle

Expand Down
7 changes: 3 additions & 4 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import datetime as dt
from types import SimpleNamespace

import pytest
import datetime as dt

from openpyxl.cell import Cell
from openpyxl.styles import Font, PatternFill, Alignment, Border, Color, Side
from openpyxl.styles import Alignment, Border, Color, Font, PatternFill, Side
from openpyxl.worksheet.worksheet import Worksheet

from drf_excel.utilities import (
get_setting,
XLSXStyle,
get_attribute,
get_setting,
sanitize_value,
set_cell_style,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_viewset_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rest_framework.test import APIClient
from time_machine import TimeMachineFixture

from tests.testapp.models import ExampleModel, AllFieldsModel, Tag
from tests.testapp.models import AllFieldsModel, ExampleModel, Tag

pytestmark = pytest.mark.django_db

Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from .models import ExampleModel, AllFieldsModel
from .models import AllFieldsModel, ExampleModel


class ExampleSerializer(serializers.ModelSerializer):
Expand Down
5 changes: 3 additions & 2 deletions tests/testapp/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from rest_framework.viewsets import ReadOnlyModelViewSet

from drf_excel.mixins import XLSXFileMixin
from drf_excel.renderers import XLSXRenderer

from .models import ExampleModel, AllFieldsModel
from .serializers import ExampleSerializer, AllFieldsSerializer
from .models import AllFieldsModel, ExampleModel
from .serializers import AllFieldsSerializer, ExampleSerializer


class ExampleViewSet(XLSXFileMixin, ReadOnlyModelViewSet):
Expand Down
2 changes: 1 addition & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import routers

from .testapp.views import ExampleViewSet, AllFieldsViewSet
from .testapp.views import AllFieldsViewSet, ExampleViewSet

router = routers.SimpleRouter()
router.register(r"examples", ExampleViewSet)
Expand Down

0 comments on commit cefa815

Please sign in to comment.