Skip to content

Commit b4ae270

Browse files
committed
refactor: clean up code quality issues
- Remove unused imports across all modules - Fix line length violations for PEP 8 compliance - Clean up f-string formatting issues - Remove unused variables in tests - Optimize import statements for better organization
1 parent d3c9245 commit b4ae270

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

vditor/configs.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,23 +230,29 @@ def set_configs(self, config_name: str = "default") -> None:
230230
if config_name in configs:
231231
config: Any = configs[config_name]
232232
if not isinstance(config, dict):
233-
error_msg = f'VDITOR_CONFIGS["{config_name}"] setting must be a dictionary type.'
233+
error_msg = (
234+
f'VDITOR_CONFIGS["{config_name}"] setting must be a '
235+
f"dictionary type."
236+
)
234237
logger.error(error_msg)
235238
raise ImproperlyConfigured(error_msg)
236239

237240
logger.debug(
238-
f"Applied configuration '{config_name}' with {len(config)} settings"
241+
f"Applied configuration '{config_name}' with "
242+
f"{len(config)} settings"
239243
)
240244
self.update(config)
241245
else:
242246
available_configs = list(configs.keys())
243247
error_msg = (
244-
f"No configuration named '{config_name}' found in your VDITOR_CONFIGS setting. "
245-
f"Available configurations: {available_configs}"
248+
f"No configuration named '{config_name}' found in your "
249+
f"VDITOR_CONFIGS setting. Available configurations: "
250+
f"{available_configs}"
246251
)
247252
logger.error(error_msg)
248253
raise ImproperlyConfigured(error_msg)
249254
else:
250255
logger.debug(
251-
f"Using default configuration for '{config_name}' (no VDITOR_CONFIGS found)"
256+
f"Using default configuration for '{config_name}' "
257+
f"(no VDITOR_CONFIGS found)"
252258
)

vditor/security.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
import logging
99
import re
1010
from pathlib import Path
11-
from typing import Dict, List, Set
11+
from typing import Dict
1212

1313
from django.conf import settings
14-
from django.utils.html import escape
1514
from django.utils.translation import gettext_lazy as _
1615

1716
logger = logging.getLogger(__name__)

vditor/tests.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_image_upload_success(self):
4747
self.assertEqual(response.status_code, 200)
4848
self.assertEqual(response.json()["code"], 0)
4949
self.assertEqual(response.json()["msg"], "Success!")
50-
50+
5151
# Check that a URL was generated (exact filename will vary due to hash)
5252
response_data = response.json()
5353
self.assertIn("test_image.png", response_data["data"]["succMap"])
@@ -57,10 +57,11 @@ def test_image_upload_success(self):
5757

5858
# Verify that a file was created (filename will have hash prefix)
5959
import glob
60+
6061
file_pattern = os.path.join(settings.MEDIA_ROOT, "*_test_image.png")
6162
matching_files = glob.glob(file_pattern)
6263
self.assertTrue(len(matching_files) > 0, "No uploaded file found")
63-
64+
6465
# Verify file content
6566
if matching_files:
6667
with open(matching_files[0], "rb") as f:
@@ -94,8 +95,9 @@ class VditorWidgetTest(TestCase):
9495
def test_init(self):
9596
# Clear cache to ensure we test actual configuration
9697
from django.core.cache import cache
98+
9799
cache.clear()
98-
100+
99101
with override_settings(LANGUAGE_CODE="en"):
100102
widget = VditorWidget()
101103
self.assertIsInstance(widget.config, dict)
@@ -192,10 +194,12 @@ def test_get_default_config(self):
192194
def test_vditor_config_init_default(self):
193195
# Clear cache to ensure we test actual configuration
194196
from django.core.cache import cache
197+
195198
cache.clear()
196-
199+
197200
# Reset language code to default
198201
from django.test import override_settings
202+
199203
with override_settings(LANGUAGE_CODE="en"):
200204
config = VditorConfig()
201205
self.assertIsInstance(config, dict)
@@ -207,8 +211,9 @@ def test_vditor_config_init_default(self):
207211
def test_vditor_config_init_custom_language(self):
208212
# Clear cache to ensure we test the actual language setting
209213
from django.core.cache import cache
214+
210215
cache.clear()
211-
216+
212217
config = VditorConfig()
213218
self.assertEqual(config["lang"], "fr_FR")
214219

@@ -225,8 +230,9 @@ def test_vditor_config_init_custom_config(self):
225230
def test_vditor_config_improperly_configured_not_dict(self):
226231
# Clear cache to ensure we test the actual configuration loading
227232
from django.core.cache import cache
233+
228234
cache.clear()
229-
235+
230236
with self.assertRaisesMessage(
231237
ImproperlyConfigured, "VDITOR_CONFIGS setting must be a dictionary type."
232238
):
@@ -236,11 +242,12 @@ def test_vditor_config_improperly_configured_not_dict(self):
236242
def test_vditor_config_improperly_configured_config_not_found(self):
237243
# Clear cache to ensure we test the actual configuration loading
238244
from django.core.cache import cache
245+
239246
cache.clear()
240-
247+
241248
with self.assertRaises(ImproperlyConfigured) as cm:
242249
VditorConfig(config_name="non_existent")
243-
250+
244251
error_msg = str(cm.exception)
245252
self.assertIn("No configuration named 'non_existent' found", error_msg)
246253
self.assertIn("Available configurations:", error_msg)
@@ -267,60 +274,62 @@ def test_vditor_text_form_field_widget(self):
267274

268275
class VditorSecurityTest(TestCase):
269276
"""Test security features."""
270-
277+
271278
def test_filename_validation(self):
272279
from vditor.views import _validate_filename_security
273-
280+
274281
# Valid filename
275282
self.assertTrue(_validate_filename_security("test.png")[0])
276-
283+
277284
# Invalid filenames
278285
self.assertFalse(_validate_filename_security("../test.png")[0])
279286
self.assertFalse(_validate_filename_security("test<>.png")[0])
280287
self.assertFalse(_validate_filename_security("CON.png")[0])
281288
self.assertFalse(_validate_filename_security("test~.png")[0])
282-
289+
283290
def test_file_validation_size(self):
284291
from django.core.files.uploadedfile import SimpleUploadedFile
285292
from vditor.views import _validate_uploaded_file
286-
293+
287294
# Too small file
288295
small_file = SimpleUploadedFile("test.png", b"x", content_type="image/png")
289296
is_valid, error = _validate_uploaded_file(small_file)
290297
self.assertFalse(is_valid)
291298
self.assertIn("too small", error)
292-
299+
293300
# Valid file
294-
valid_file = SimpleUploadedFile("test.png", b"x" * 100, content_type="image/png")
301+
valid_file = SimpleUploadedFile(
302+
"test.png", b"x" * 100, content_type="image/png"
303+
)
295304
is_valid, error = _validate_uploaded_file(valid_file)
296305
self.assertTrue(is_valid)
297306

298307

299308
class VditorCacheTest(TestCase):
300309
"""Test caching functionality."""
301-
310+
302311
def test_config_caching(self):
303312
from vditor.cache_utils import ConfigCache
304-
313+
305314
# Test setting and getting config
306315
test_config = {"test": "value"}
307316
ConfigCache.set_config("test_config", test_config)
308-
317+
309318
retrieved_config = ConfigCache.get_config("test_config")
310319
self.assertEqual(retrieved_config, test_config)
311-
320+
312321
# Test invalidation
313322
ConfigCache.invalidate_config("test_config")
314323
self.assertIsNone(ConfigCache.get_config("test_config"))
315-
324+
316325
def test_media_cache(self):
317326
from vditor.cache_utils import MediaCache
318-
327+
319328
# Test media hash generation
320329
hash1 = MediaCache.get_media_hash()
321330
hash2 = MediaCache.get_media_hash()
322331
self.assertEqual(hash1, hash2) # Should be cached
323-
332+
324333
# Test invalidation
325334
MediaCache.invalidate_media()
326335
hash3 = MediaCache.get_media_hash()

vditor/views.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import hashlib
22
import logging
3-
import mimetypes
43
import os
5-
import uuid
64
from pathlib import Path
7-
from typing import Any, Dict
85

96
from django.conf import settings
107
from django.core.files.uploadedfile import UploadedFile
118
from django.http import HttpRequest, JsonResponse
12-
from django.utils.decorators import method_decorator
13-
from django.views.decorators.cache import cache_control, cache_page
9+
from django.views.decorators.cache import cache_control
1410
from django.views.decorators.csrf import csrf_exempt
15-
from django.views.decorators.gzip import gzip_page
1611
from django.views.decorators.http import require_http_methods
1712
from django.views.decorators.vary import vary_on_headers
1813
from werkzeug.utils import secure_filename
@@ -254,7 +249,8 @@ def vditor_images_upload_view(request: HttpRequest) -> JsonResponse:
254249
file_path = upload_path / unique_filename
255250

256251
logger.info(
257-
f"Processing upload: {original_filename} -> {unique_filename} (hash: {content_hash})"
252+
f"Processing upload: {original_filename} -> {unique_filename} "
253+
f"(hash: {content_hash})"
258254
)
259255

260256
# Check if file already exists (deduplication)

vditor_app/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from django.urls import reverse
44
from django.views import generic
5-
from django.shortcuts import render
65

76
from . import forms
87
from .models import VditorTest

0 commit comments

Comments
 (0)