From adc8573134f53e3af796140ca740dba3b1d74115 Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 14 Dec 2021 08:39:08 +0100 Subject: [PATCH] Code cleanup --- dpaste/__init__.py | 4 +++- dpaste/apps.py | 7 ++++--- dpaste/highlight.py | 4 +++- dpaste/management/commands/cleanup_snippets.py | 5 ++++- dpaste/settings/tests.py | 1 - dpaste/tests/test_api.py | 15 +++++++++++---- dpaste/tests/test_snippet.py | 4 +++- dpaste/urls/dpaste_api.py | 6 +++++- dpaste/views.py | 6 +----- 9 files changed, 34 insertions(+), 18 deletions(-) diff --git a/dpaste/__init__.py b/dpaste/__init__.py index c979bf2..6a97310 100644 --- a/dpaste/__init__.py +++ b/dpaste/__init__.py @@ -1,7 +1,9 @@ VERSION = (3, 5) __version__ = "{major}.{minor}{rest}".format( - major=VERSION[0], minor=VERSION[1], rest="".join(str(i) for i in VERSION[2:]), + major=VERSION[0], + minor=VERSION[1], + rest="".join(str(i) for i in VERSION[2:]), ) default_app_config = "dpaste.apps.dpasteAppConfig" diff --git a/dpaste/apps.py b/dpaste/apps.py index a56f07d..4e045d5 100644 --- a/dpaste/apps.py +++ b/dpaste/apps.py @@ -1,11 +1,12 @@ +from logging import getLogger + from django.apps import AppConfig, apps from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ -from logging import getLogger - log = getLogger(__file__) + class dpasteAppConfig(AppConfig): name = "dpaste" verbose_name = "dpaste" @@ -133,8 +134,8 @@ class dpasteAppConfig(AppConfig): If the Highlight Class is not given, PygmentsHighlighter is used. """ from dpaste.highlight import ( - PlainTextHighlighter, MarkdownHighlighter, + PlainTextHighlighter, RestructuredTextHighlighter, ) diff --git a/dpaste/highlight.py b/dpaste/highlight.py index 18aae8d..cd3ba7c 100644 --- a/dpaste/highlight.py +++ b/dpaste/highlight.py @@ -77,7 +77,9 @@ class MarkdownHighlighter(PlainTextHighlighter): return mark_safe( misaka.html( - code_string, extensions=self.extensions, render_flags=self.render_flags, + code_string, + extensions=self.extensions, + render_flags=self.render_flags, ) ) diff --git a/dpaste/management/commands/cleanup_snippets.py b/dpaste/management/commands/cleanup_snippets.py index d2721d6..06fc754 100644 --- a/dpaste/management/commands/cleanup_snippets.py +++ b/dpaste/management/commands/cleanup_snippets.py @@ -14,7 +14,10 @@ class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument( - "--dry-run", action="store_true", dest="dry_run", help="Don't do anything.", + "--dry-run", + action="store_true", + dest="dry_run", + help="Don't do anything.", ), def handle(self, *args, **options): diff --git a/dpaste/settings/tests.py b/dpaste/settings/tests.py index 22817cf..b3fbbc1 100644 --- a/dpaste/settings/tests.py +++ b/dpaste/settings/tests.py @@ -1,7 +1,6 @@ """ Settings for the testsuite runs. """ -import django from .base import * # noqa diff --git a/dpaste/tests/test_api.py b/dpaste/tests/test_api.py index 30fa3d2..a3bcd52 100644 --- a/dpaste/tests/test_api.py +++ b/dpaste/tests/test_api.py @@ -145,7 +145,8 @@ class SnippetAPITestCase(TestCase): def test_expire_choices_invalid_given(self): # A expire choice that does not exist returns a BadRequest response = self.client.post( - self.api_url, {"content": u"Hello Wörld.\n\tGood Bye", "expires": "foobar"}, + self.api_url, + {"content": u"Hello Wörld.\n\tGood Bye", "expires": "foobar"}, ) self.assertEqual(response.status_code, 400) self.assertEqual(Snippet.objects.count(), 0) @@ -166,7 +167,8 @@ class SnippetAPITestCase(TestCase): def test_valid_expiration_choices_never(self): response = self.client.post( - self.api_url, {"content": u"Hello Wörld.\n\tGood Bye", "expires": "never"}, + self.api_url, + {"content": u"Hello Wörld.\n\tGood Bye", "expires": "never"}, ) self.assertEqual(response.status_code, 200) self.assertEqual(Snippet.objects.count(), 1) @@ -174,7 +176,8 @@ class SnippetAPITestCase(TestCase): def test_valid_expiration_choices_hour(self): response = self.client.post( - self.api_url, {"content": u"Hello Wörld.\n\tGood Bye", "expires": 3600}, + self.api_url, + {"content": u"Hello Wörld.\n\tGood Bye", "expires": 3600}, ) self.assertEqual(response.status_code, 200) self.assertEqual(Snippet.objects.count(), 1) @@ -204,7 +207,11 @@ class SnippetAPITestCase(TestCase): """ response = self.client.post( self.api_url, - {"content": u"Hello Wörld.\n\tGood Bye", "lexer": "", "filename": "",}, + { + "content": u"Hello Wörld.\n\tGood Bye", + "lexer": "", + "filename": "", + }, ) self.assertEqual(response.status_code, 400) diff --git a/dpaste/tests/test_snippet.py b/dpaste/tests/test_snippet.py index c176155..f13c054 100644 --- a/dpaste/tests/test_snippet.py +++ b/dpaste/tests/test_snippet.py @@ -375,7 +375,9 @@ class SnippetTestCase(TestCase): management.call_command("cleanup_snippets") self.assertEqual(Snippet.objects.count(), 1) - def test_delete_management_snippet_never_expires_not_get_deleted(self,): + def test_delete_management_snippet_never_expires_not_get_deleted( + self, + ): """ Snippets without an expiration date wont get deleted automatically. """ diff --git a/dpaste/urls/dpaste_api.py b/dpaste/urls/dpaste_api.py index e8c3036..7146e9e 100644 --- a/dpaste/urls/dpaste_api.py +++ b/dpaste/urls/dpaste_api.py @@ -4,5 +4,9 @@ from django.views.decorators.csrf import csrf_exempt from ..views import APIView urlpatterns = [ - re_path(r"^api/$", csrf_exempt(APIView.as_view()), name="dpaste_api_create_snippet",) + re_path( + r"^api/$", + csrf_exempt(APIView.as_view()), + name="dpaste_api_create_snippet", + ) ] diff --git a/dpaste/views.py b/dpaste/views.py index 6531971..2e5fdff 100644 --- a/dpaste/views.py +++ b/dpaste/views.py @@ -1,6 +1,5 @@ import datetime import difflib -import ipaddress import json from django.apps import apps @@ -21,16 +20,12 @@ from django.views.generic.base import TemplateView, View from django.views.generic.detail import DetailView from pygments.lexers import get_lexer_for_filename from pygments.util import ClassNotFound -from ratelimit.decorators import ratelimit -from ratelimit.exceptions import Ratelimited from dpaste import highlight from dpaste.forms import SnippetForm, get_expire_values from dpaste.highlight import PygmentsHighlighter from dpaste.models import Snippet -from django.conf import settings - config = apps.get_app_config("dpaste") @@ -343,6 +338,7 @@ class APIView(View): # handle them here. # ----------------------------------------------------------------------------- + def handler404(request, exception=None, template_name="dpaste/404.html"): context = {} context.update(config.extra_template_context)