Skip to content

Commit

Permalink
🚨 (lint): Code linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dd committed Jun 20, 2024
1 parent cf8ffa3 commit 4a625bd
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 47 deletions.
3 changes: 2 additions & 1 deletion meringue/api/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from meringue.api.utils import render_error_details


try:
from rest_framework_simplejwt.exceptions import DetailDictMixin
except ImportError:
Expand All @@ -24,7 +25,7 @@ def exception_handler(exc, context):
if response is None:
return response

if isinstance(exc, (Http404, PermissionDenied)):
if isinstance(exc, Http404 | PermissionDenied):
# django Http404 and PermissionDenied errors are substituted for drf errors,
# in `rest_framework.views.exception_handler` method.
response.data = render_error_details(response.data["detail"])
Expand Down
1 change: 1 addition & 0 deletions meringue/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework.exceptions import ErrorDetail


try:
from rest_framework_simplejwt.exceptions import DetailDictMixin
except ImportError:
Expand Down
3 changes: 2 additions & 1 deletion meringue/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ def __getattr__(self, attr: str) -> Any:
"""

if attr not in self.defaults:
raise AttributeError("Invalid setting key: '%s'" % attr)
msg = f"Invalid setting key: '{attr}'"
raise AttributeError(msg)

if attr in self.deprecated_params:
warnings.warn(self.deprecated_params[attr], DeprecationWarning, stacklevel=2)
Expand Down
4 changes: 1 addition & 3 deletions meringue/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ def correction_sorting(self):
The selection for updating sorting can be pre-limited by filtering the list.
"""
items = []
sorting = 0
for item in self:
for sorting, item in enumerate(self):
if item.sorting != sorting:
item.sorting = sorting
items.append(item)
sorting += 1

return self.bulk_update(items, ["sorting"])

Expand Down
12 changes: 7 additions & 5 deletions meringue/protected/fields.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models.fields.files import FileField
from django.db.models.fields.files import FieldFile
from django.db.models.fields.files import FileField
from django.db.models.fields.files import ImageField
from django.db.models.fields.files import ImageFieldFile
from django.urls import reverse

from meringue.conf import m_settings


try:
from django_hosts.resolvers import reverse as hosts_reverse
except ImportError:
hosts_reverse = None

from meringue.conf import m_settings


class ProtectedFileMixin:
@property
Expand Down Expand Up @@ -86,7 +88,7 @@ def __init__(

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
kwargs['view_name'] = self.m_protected_view_name
kwargs["view_name"] = self.m_protected_view_name
return name, path, args, kwargs


Expand Down Expand Up @@ -122,5 +124,5 @@ def __init__(

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
kwargs['view_name'] = self.m_protected_view_name
kwargs["view_name"] = self.m_protected_view_name
return name, path, args, kwargs
4 changes: 3 additions & 1 deletion meringue/protected/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytest
from io import BytesIO
from unittest.mock import patch

from django.core.files.uploadedfile import UploadedFile

import pytest
from faker import Faker


Expand All @@ -14,10 +14,12 @@
def file_uploaded():
return UploadedFile(BytesIO(faker.binary()), name=faker.file_name())


@pytest.fixture
def image_uploaded():
return UploadedFile(BytesIO(faker.image()), name=faker.file_name())


@pytest.fixture
def mocked_has_perm_true(request):
with patch(
Expand Down
2 changes: 1 addition & 1 deletion meringue/protected/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from io import BytesIO

import django
from django.contrib.contenttypes.models import ContentType
from django.core.files.uploadedfile import UploadedFile
from django.test import override_settings

import pytest
from faker import Faker

from test_project.models import ProtectedModel
Expand Down
13 changes: 7 additions & 6 deletions meringue/protected/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import mimetypes
from pathlib import Path
from urllib.parse import quote
from urllib.parse import urljoin

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.http import FileResponse
Expand Down Expand Up @@ -51,7 +49,10 @@ def x_accel_redirect_view(request, cid, field, pk, disp="inline"):
response["X-Accel-Redirect"] = redirect_url
return response

return FileResponse(
open(file.path, "rb"),
as_attachment = disp == "attachment",
)
with open(file.path, "rb") as tmp_file:
response = FileResponse(
tmp_file,
as_attachment=disp == "attachment",
)

return response
4 changes: 2 additions & 2 deletions meringue/thumbnail/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def _dummyimage(job_chain: str) -> str:
height=size[1],
)

return "%simages/noise.png" % settings.STATIC_URL
# return "%simages/none.gif" % settings.STATIC_URL
return f"{settings.STATIC_URL}images/noise.png"
# return f"{settings.STATIC_URL}images/none.gif"


def get_thumbnail(
Expand Down
8 changes: 6 additions & 2 deletions test_project/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ class ProtectedModel(models.Model):
view_name="sub-x_accel_redirect_view", host_name="sub", disposition="inline"
)
image_hosts = ProtectedImageField(view_name="sub-x_accel_redirect_view", host_name="sub")
file_getter = ProtectedFileField(view_name="x_accel_redirect_view", nginx_location_getter=_test_getter)
image_getter = ProtectedImageField(view_name="x_accel_redirect_view", nginx_location_getter=_test_getter)
file_getter = ProtectedFileField(
view_name="x_accel_redirect_view", nginx_location_getter=_test_getter
)
image_getter = ProtectedImageField(
view_name="x_accel_redirect_view", nginx_location_getter=_test_getter
)
file_orig = models.FileField()
image_orig = models.ImageField()
50 changes: 25 additions & 25 deletions test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
# DEBUG = False

INSTALLED_APPS = [
# 'django.contrib.admin',
# "django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
# 'django.contrib.sessions',
# 'django.contrib.messages',
# 'django.contrib.staticfiles',
# "django.contrib.sessions",
# "django.contrib.messages",
# "django.contrib.staticfiles",
"modeltranslation",
"test_project",
"rest_framework",
Expand All @@ -24,34 +24,34 @@
]

MIDDLEWARE = [
# 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
# 'django.contrib.messages.middleware.MessageMiddleware',
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
# "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
# "django.middleware.common.CommonMiddleware",
# "django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
# "django.contrib.messages.middleware.MessageMiddleware",
# "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "test_project.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
# 'DIRS': [],
# 'APP_DIRS': True,
# 'OPTIONS': {
# 'context_processors': [
# 'django.template.context_processors.debug',
# 'django.template.context_processors.request',
# 'django.contrib.auth.context_processors.auth',
# 'django.contrib.messages.context_processors.messages',
# "DIRS": [],
# "APP_DIRS": True,
# "OPTIONS": {
# "context_processors": [
# "django.template.context_processors.debug",
# "django.template.context_processors.request",
# "django.contrib.auth.context_processors.auth",
# "django.contrib.messages.context_processors.messages",
# ],
# },
},
]

# WSGI_APPLICATION = 'tests.wsgi.application'
# WSGI_APPLICATION = "tests.wsgi.application"


# Database
Expand All @@ -71,16 +71,16 @@

# AUTH_PASSWORD_VALIDATORS = [
# {
# 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
# "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
# },
# {
# 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
# "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
# },
# {
# 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
# "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
# },
# {
# 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
# "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
# },
# ]

Expand All @@ -99,7 +99,7 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

# STATIC_URL = 'static/'
# STATIC_URL = "static/"

STATIC_ROOT = BASE_DIR / "static"
STATIC_URL = "static/"
Expand Down

0 comments on commit 4a625bd

Please sign in to comment.