From 62bb15317c46f2c1444444d3a4eb205674dc38a2 Mon Sep 17 00:00:00 2001 From: thomas girod Date: Tue, 6 Aug 2024 11:42:10 +0200 Subject: [PATCH] T2 ruff rule --- core/apps.py | 5 ++--- core/management/commands/install_xapian.py | 14 +++++++----- core/management/commands/markdown.py | 2 +- core/models.py | 22 +++++++------------ core/views/user.py | 3 ++- forum/views.py | 3 ++- .../migrations/0002_auto_20190827_2251.py | 6 +---- pyproject.toml | 1 + .../delete_all_forum_user_messages.py | 4 ++-- rootplace/views.py | 3 ++- sith/settings.py | 5 +++-- 11 files changed, 33 insertions(+), 35 deletions(-) diff --git a/core/apps.py b/core/apps.py index 50ea2cee..5f1b9fc3 100644 --- a/core/apps.py +++ b/core/apps.py @@ -20,8 +20,7 @@ # Place - Suite 330, Boston, MA 02111-1307, USA. # # - -import sys +import logging from django.apps import AppConfig from django.core.cache import cache @@ -41,7 +40,7 @@ class SithConfig(AppConfig): def clear_cached_memberships(**kwargs): Forum._club_memberships = {} - print("Connecting signals!", file=sys.stderr) + logging.getLogger("django").info("Connecting signals!") request_started.connect( clear_cached_memberships, weak=False, diff --git a/core/management/commands/install_xapian.py b/core/management/commands/install_xapian.py index e91491be..8fb9c5b3 100644 --- a/core/management/commands/install_xapian.py +++ b/core/management/commands/install_xapian.py @@ -48,20 +48,24 @@ class Command(BaseCommand): def handle(self, *args, force: bool, **options): if not os.environ.get("VIRTUAL_ENV", None): - print("No virtual environment detected, this command can't be used") + self.stdout.write( + "No virtual environment detected, this command can't be used" + ) return desired = self._desired_version() if desired == self._current_version(): if not force: - print( + self.stdout.write( f"Version {desired} is already installed, use --force to re-install" ) return - print(f"Version {desired} is already installed, re-installing") - print(f"Installing xapian version {desired} at {os.environ['VIRTUAL_ENV']}") + self.stdout.write(f"Version {desired} is already installed, re-installing") + self.stdout.write( + f"Installing xapian version {desired} at {os.environ['VIRTUAL_ENV']}" + ) subprocess.run( [str(Path(__file__).parent / "install_xapian.sh"), desired], env=dict(os.environ), ).check_returncode() - print("Installation success") + self.stdout.write("Installation success") diff --git a/core/management/commands/markdown.py b/core/management/commands/markdown.py index 8b43e5ac..f9379373 100644 --- a/core/management/commands/markdown.py +++ b/core/management/commands/markdown.py @@ -35,4 +35,4 @@ class Command(BaseCommand): root_path = settings.BASE_DIR with open(root_path / "core/fixtures/SYNTAX.md", "r") as md: result = markdown(md.read()) - print(result, end="") + self.stdout.write(result) diff --git a/core/models.py b/core/models.py index ef85aded..3803e3ca 100644 --- a/core/models.py +++ b/core/models.py @@ -24,6 +24,7 @@ from __future__ import annotations import importlib +import logging import os import unicodedata from datetime import date, timedelta @@ -1085,19 +1086,15 @@ class SithFile(models.Model): # file storage parent_path = "." + self.parent.get_full_path() parent_full_path = settings.MEDIA_ROOT + parent_path - print("Parent full path: %s" % parent_full_path) os.makedirs(parent_full_path, exist_ok=True) old_path = self.file.name # Should be relative: "./users/skia/bleh.jpg" new_path = "." + self.get_full_path() - print("Old path: %s " % old_path) - print("New path: %s " % new_path) try: # Make this atomic, so that a FS problem rolls back the DB change with transaction.atomic(): # Set the new filesystem path self.file.name = new_path self.save() - print("New file path: %s " % self.file.path) # Really move at the FS level if os.path.exists(parent_full_path): os.rename( @@ -1108,25 +1105,22 @@ class SithFile(models.Model): # problem, and that can be solved with a simple shell # command: `find . -type d -empty -delete` except Exception as e: - print("This file likely had a problem. Here is the exception:") - print(repr(e)) - print("-" * 80) + logging.error(e) def _check_path_consistence(self): file_path = str(self.file) file_full_path = settings.MEDIA_ROOT + file_path db_path = ".%s" % self.get_full_path() if not os.path.exists(file_full_path): - print("%s: WARNING: real file does not exists!" % self.id) - print("file path: %s" % file_path, end="") - print(" db path: %s" % db_path) + print("%s: WARNING: real file does not exists!" % self.id) # noqa T201 + print("file path: %s" % file_path, end="") # noqa T201 + print(" db path: %s" % db_path) # noqa T201 return False if file_path != db_path: - print("%s: " % self.id, end="") - print("file path: %s" % file_path, end="") - print(" db path: %s" % db_path) + print("%s: " % self.id, end="") # noqa T201 + print("file path: %s" % file_path, end="") # noqa T201 + print(" db path: %s" % db_path) # noqa T201 return False - print("%s OK (%s)" % (self.id, file_path)) return True def _check_fs(self): diff --git a/core/views/user.py b/core/views/user.py index 86043f9f..d50671c8 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -22,6 +22,7 @@ # # import itertools +import logging # This file contains all the views that concern the user model from datetime import date, timedelta @@ -801,7 +802,7 @@ class UserAccountView(UserAccountBase): product__eticket=None ).all() except Exception as e: - print(repr(e)) + logging.error(e) return kwargs diff --git a/forum/views.py b/forum/views.py index ca815c61..bbb2ba32 100644 --- a/forum/views.py +++ b/forum/views.py @@ -21,6 +21,7 @@ # Place - Suite 330, Boston, MA 02111-1307, USA. # # +import logging import math from functools import partial @@ -424,7 +425,7 @@ class ForumMessageCreateView(CanCreateMixin, CreateView): ) init["message"] += "\n\n" except Exception as e: - print(repr(e)) + logging.error(e) return init def form_valid(self, form): diff --git a/pedagogy/migrations/0002_auto_20190827_2251.py b/pedagogy/migrations/0002_auto_20190827_2251.py index 41babcc1..2b1f4b36 100644 --- a/pedagogy/migrations/0002_auto_20190827_2251.py +++ b/pedagogy/migrations/0002_auto_20190827_2251.py @@ -36,11 +36,7 @@ def remove_multiples_comments_from_same_user(apps, schema_editor): .order_by("-publish_date") .first() ) - for comment in ( - user.uv_comments.filter(uv__id=uv["uv"]).exclude(pk=last.pk).all() - ): - print("removing : %s" % (comment,)) - comment.delete() + user.uv_comments.filter(uv__id=uv["uv"]).exclude(pk=last.pk).delete() class Migration(migrations.Migration): diff --git a/pyproject.toml b/pyproject.toml index ecfdfc71..eaef9542 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,6 +103,7 @@ select = [ "FBT", # boolean trap "UP008", # Use super() instead of super(__class__, self) "UP009", # utf-8 encoding declaration is unnecessary + "T2", # print statements ] ignore = [ diff --git a/rootplace/management/commands/delete_all_forum_user_messages.py b/rootplace/management/commands/delete_all_forum_user_messages.py index 478ccd33..45153608 100644 --- a/rootplace/management/commands/delete_all_forum_user_messages.py +++ b/rootplace/management/commands/delete_all_forum_user_messages.py @@ -40,7 +40,7 @@ class Command(BaseCommand): user = User.objects.filter(id=options["user_id"]).first() if user is None: - print("User with ID %s not found" % (options["user_id"],)) + self.stderr.write("User with ID %s not found" % (options["user_id"],)) exit(1) confirm = input( @@ -49,7 +49,7 @@ class Command(BaseCommand): ) if not confirm.lower().startswith("y"): - print("Operation aborted") + self.stderr.write("Operation aborted") exit(1) delete_all_forum_user_messages(user, User.objects.get(id=0), verbose=True) diff --git a/rootplace/views.py b/rootplace/views.py index 65f94b56..7d999aae 100644 --- a/rootplace/views.py +++ b/rootplace/views.py @@ -21,6 +21,7 @@ # Place - Suite 330, Boston, MA 02111-1307, USA. # # +import logging from ajax_select.fields import AutoCompleteSelectField from django import forms @@ -146,7 +147,7 @@ def delete_all_forum_user_messages(user, moderator, *, verbose=False): continue if verbose: - print(message) + logging.getLogger("django").info(message) ForumMessageMeta(message=message, user=moderator, action="DELETE").save() diff --git a/sith/settings.py b/sith/settings.py index c6ff45a0..967539f9 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -34,6 +34,7 @@ https://docs.djangoproject.com/en/1.8/ref/settings/ """ import binascii +import logging import os import sys from pathlib import Path @@ -680,9 +681,9 @@ TOXIC_DOMAINS_PROVIDERS = [ try: from .settings_custom import * - print("Custom settings imported", file=sys.stderr) + logging.getLogger("django").info("Custom settings imported") except: - print("Custom settings failed", file=sys.stderr) + logging.getLogger("django").warning("Custom settings failed") if DEBUG: INSTALLED_APPS += ("debug_toolbar",)