Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ruff code style checker #159

Merged
merged 7 commits into from
Feb 22, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Format whole project with ruff
Bensge committed Feb 22, 2025
commit 654bfa30e7b5f10786ecd2403648dbd35aa0b853
2 changes: 1 addition & 1 deletion Spybot2/asgi.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,6 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Spybot2.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Spybot2.settings")

application = get_asgi_application()
154 changes: 81 additions & 73 deletions Spybot2/settings.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from datetime import timedelta

from pathlib import Path
import environ
import os
@@ -24,71 +24,78 @@
DEBUG=(bool, False)
)
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

RECORDER_ENABLED = env.bool('RECORDER_ENABLED', True)
RECORDER_ENABLED = env.bool("RECORDER_ENABLED", True)

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
SECRET_KEY = env("SECRET_KEY")

# TS DATA
TS_USER = env('TS_USER')
TS_PASSWORD = env('TS_PASSWORD')
TS_IP = env('TS_IP')
TS_PORT = env('TS_PORT')
SERVER_IP = env('SERVER_IP')
TS_USER = env("TS_USER")
TS_PASSWORD = env("TS_PASSWORD")
TS_IP = env("TS_IP")
TS_PORT = env("TS_PORT")
SERVER_IP = env("SERVER_IP")

# Steam API Key
STEAM_API_KEY = env('STEAM_API_KEY')
STEAM_API_KEY = env("STEAM_API_KEY")

# Sentry SDK
SENTRY_ENABLED = env.bool('SENTRY_ENABLED', False)
SENTRY_DSN = env('SENTRY_DSN', default="")
SENTRY_ENABLED = env.bool("SENTRY_ENABLED", False)
SENTRY_DSN = env("SENTRY_DSN", default="")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DEBUG', False)

ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct', '192.168.59.100', '192.168.59.100:20001']
DEBUG = env.bool("DEBUG", False)

ALLOWED_HOSTS = [
SERVER_IP,
TS_IP,
"localhost",
"127.0.0.1",
"spybot.localhost.direct",
"192.168.59.100",
"192.168.59.100:20001",
]

CSRF_TRUSTED_ORIGINS = [f"https://{SERVER_IP}"]

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

CSRF_COOKIE_SECURE = not env.bool('INSECURE_COOKIES', False)
SESSION_COOKIE_SECURE = not env.bool('INSECURE_COOKIES', False)
CSRF_COOKIE_SECURE = not env.bool("INSECURE_COOKIES", False)
SESSION_COOKIE_SECURE = not env.bool("INSECURE_COOKIES", False)


# Application definition

INSTALLED_APPS = [
'spybot',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.forms',
'django_bootstrap5',
"spybot",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.forms",
"django_bootstrap5",
]

ROOT_URLCONF = 'Spybot2.urls'
ROOT_URLCONF = "Spybot2.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'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',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"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",
],
},
},
@@ -105,17 +112,17 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

WSGI_APPLICATION = 'Spybot2.wsgi.application'
WSGI_APPLICATION = "Spybot2.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
'default': {
**env.db_url('DB_URL'),
'CONN_MAX_AGE': 3600,
'CONN_HEALTH_CHECKS': True,
"default": {
**env.db_url("DB_URL"),
"CONN_MAX_AGE": 3600,
"CONN_HEALTH_CHECKS": True,
},
}

@@ -125,39 +132,39 @@

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",
},
]

AUTH_USER_MODEL = 'spybot.MergedUser'
AUTH_USER_MODEL = "spybot.MergedUser"

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'spybot.auth.backend.link_backend.LinkAuthBackend',
"django.contrib.auth.backends.ModelBackend",
"spybot.auth.backend.link_backend.LinkAuthBackend",
]

LOGIN_URL = '/login'
LOGIN_URL = "/login"

# Passkeys
FIDO_SERVER_NAME = "Spybot local"
#KEY_ATTACHMENT = passkeys.Attachment.CROSS_PLATFORM
# KEY_ATTACHMENT = passkeys.Attachment.CROSS_PLATFORM


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

@@ -167,8 +174,8 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = 'static/'
STATIC_ROOT = 'spybot_static/'
STATIC_URL = "static/"
STATIC_ROOT = "spybot_static/"

STATICFILES_DIRS = [
BASE_DIR / "frontend/output/",
@@ -177,34 +184,35 @@
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Celery
CELERY_BROKER_URL = "redis://redis:6379"
CELERY_RESULT_BACKEND = "redis://redis:6379"

# Celery beat
CELERY_BEAT_SCHEDULE = {
'record_hourly_activity': {
'task': 'spybot.tasks.record_hourly_activity',
'schedule': crontab(minute=59), # every hour
'args': (),
'options': {},
"record_hourly_activity": {
"task": "spybot.tasks.record_hourly_activity",
"schedule": crontab(minute=59), # every hour
"args": (),
"options": {},
},
'end_of_week_awards': {
'task': 'spybot.tasks.end_of_week_awards',
'schedule': crontab(minute=59, hour=23, day_of_week='sunday'), # every Sunday evening
'args': (),
'options': {},
"end_of_week_awards": {
"task": "spybot.tasks.end_of_week_awards",
"schedule": crontab(
minute=59, hour=23, day_of_week="sunday"
), # every Sunday evening
"args": (),
"options": {},
},
}



# Testing

TEST_RUNNER = 'spybot.tests.runner_testcontainers_db.TestContainerRunner'
TEST_OUTPUT_FILE_NAME = 'tests_result.xml'
TEST_RUNNER = "spybot.tests.runner_testcontainers_db.TestContainerRunner"
TEST_OUTPUT_FILE_NAME = "tests_result.xml"

# logging
# LOGGING = {
@@ -225,8 +233,8 @@
# }

BOOTSTRAP5 = {
'include_jquery': False,
'javascript_in_head': False,
"include_jquery": False,
"javascript_in_head": False,
}

if SENTRY_ENABLED:
@@ -239,4 +247,4 @@
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)
)
5 changes: 3 additions & 2 deletions Spybot2/urls.py
Original file line number Diff line number Diff line change
@@ -13,10 +13,11 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('', include('spybot.urls')),
path('admin/', admin.site.urls),
path("", include("spybot.urls")),
path("admin/", admin.site.urls),
]
2 changes: 1 addition & 1 deletion Spybot2/wsgi.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Spybot2.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Spybot2.settings")

application = get_wsgi_application()
5 changes: 3 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Spybot2.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Spybot2.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
@@ -18,5 +19,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == '__main__':
if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ dependencies = [
dev-dependencies = [
"unittest-xml-reporting ~=3.2",
"testcontainers[postgres]>=4.9.0",
"ruff>=0.9.7",
]

[tool.setuptools]
15 changes: 8 additions & 7 deletions spybot/admin.py
Original file line number Diff line number Diff line change
@@ -11,14 +11,14 @@
# Register your models here.
@admin.register(NewsEvent)
class NewsEventAdmin(ModelAdmin):
list_display = ('text', 'website_link', 'date')
list_display = ("text", "website_link", "date")


@admin.action(description="Merge selected users")
def merge_users_action(admin: ModelAdmin, request: HttpRequest, queryset: QuerySet):
users: List[TSUser] = list(queryset)
if len(users) < 2:
messages.error(request, 'Need at least two users to merge')
messages.error(request, "Need at least two users to merge")
return

print("Merging users", users)
@@ -43,7 +43,7 @@ def merge_users_action(admin: ModelAdmin, request: HttpRequest, queryset: QueryS
@admin.register(TSUser)
class TSUserAdmin(ModelAdmin):
actions = [merge_users_action]
list_display = ('id', 'name', 'merged_user', 'last_login_time')
list_display = ("id", "name", "merged_user", "last_login_time")
list_max_show_all = 2000

def has_delete_permission(self, request, obj=None):
@@ -59,20 +59,21 @@ class SteamIDInlineAdmin(admin.TabularInline):

@admin.register(MergedUser)
class MergedUserAdmin(ModelAdmin):
list_display = ('id', 'name', 'merged_user_names', 'obsolete', 'number_of_tsusers')
list_display = ("id", "name", "merged_user_names", "obsolete", "number_of_tsusers")
inlines = [
SteamIDInlineAdmin,
]

def get_queryset(self, request):
query_set = super(MergedUserAdmin, self).get_queryset(request)
query_set = query_set.annotate(number_of_tsusers=models.Count('tsusers'))
query_set = query_set.order_by('-number_of_tsusers')
query_set = query_set.annotate(number_of_tsusers=models.Count("tsusers"))
query_set = query_set.order_by("-number_of_tsusers")
return query_set

def number_of_tsusers(self, mu: MergedUser):
return mu.number_of_tsusers
number_of_tsusers.admin_order_field = 'number_of_tsusers'

number_of_tsusers.admin_order_field = "number_of_tsusers"

def has_delete_permission(self, request, obj=None):
return False
Loading