Allow the admin to be enabled
This commit is contained in:
parent
703099f02b
commit
eb4e1ad198
5 changed files with 92 additions and 0 deletions
29
yamdl_playground/core/admin.py
Normal file
29
yamdl_playground/core/admin.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
from .models import Page
|
||||||
|
|
||||||
|
class PageAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ["id", "title", "slug"]
|
||||||
|
list_display_links = ["id", "title"]
|
||||||
|
sortable_by = ["title"]
|
||||||
|
ordering = ["title"]
|
||||||
|
|
||||||
|
def has_add_permission(self, request, obj=None):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def has_delete_permission(self, request, obj=None):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def has_change_permission(self, request, obj=None):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_readonly_fields(self, request, obj=None):
|
||||||
|
return [field.name for field in (obj._meta.fields + obj._meta.many_to_many)]
|
||||||
|
|
||||||
|
@admin.options.csrf_protect_m
|
||||||
|
def changeform_view(self, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Override to prevent issues with `db_for_write`
|
||||||
|
"""
|
||||||
|
return self._changeform_view(*args, **kwargs)
|
||||||
|
|
||||||
|
admin.site.register(Page, PageAdmin)
|
|
@ -3,6 +3,7 @@ from django.db import connections
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
import warnings
|
import warnings
|
||||||
from django.utils.html import strip_tags
|
from django.utils.html import strip_tags
|
||||||
|
from django.core.management import call_command
|
||||||
|
|
||||||
|
|
||||||
class CoreConfig(AppConfig):
|
class CoreConfig(AppConfig):
|
||||||
|
@ -19,6 +20,10 @@ class CoreConfig(AppConfig):
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
# Django doesn't like running DB queries during app initialization
|
# Django doesn't like running DB queries during app initialization
|
||||||
warnings.filterwarnings("ignore", module="django.db", category=RuntimeWarning)
|
warnings.filterwarnings("ignore", module="django.db", category=RuntimeWarning)
|
||||||
|
|
||||||
|
# Run migrations, if there are any
|
||||||
|
call_command("migrate", verbosity=0, no_input=True)
|
||||||
|
|
||||||
with connection.cursor() as cursor:
|
with connection.cursor() as cursor:
|
||||||
cursor.execute("CREATE VIRTUAL TABLE search_index USING fts5(body, content='');")
|
cursor.execute("CREATE VIRTUAL TABLE search_index USING fts5(body, content='');")
|
||||||
|
|
||||||
|
|
26
yamdl_playground/core/middleware.py
Normal file
26
yamdl_playground/core/middleware.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
from django.contrib.auth.models import AnonymousUser
|
||||||
|
|
||||||
|
class FakeUser(AnonymousUser):
|
||||||
|
is_authenticated = True
|
||||||
|
is_anonymous = False
|
||||||
|
is_staff = True
|
||||||
|
is_active = True
|
||||||
|
is_superuser = True
|
||||||
|
username = "Fake user"
|
||||||
|
|
||||||
|
def has_module_perms(self, module):
|
||||||
|
# Don't allow access to the 'auth' app
|
||||||
|
# Also stops it showing up in the admin
|
||||||
|
return module not in {"auth"}
|
||||||
|
|
||||||
|
def has_perm(self, perm, obj=None):
|
||||||
|
return True
|
||||||
|
|
||||||
|
class FakeAuthenticationMiddleware:
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
# HACK: Inject our user in to user caches
|
||||||
|
request._cached_user = request._acached_user = FakeUser()
|
||||||
|
return self.get_response(request)
|
|
@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import os
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
@ -188,3 +189,28 @@ LOGGING = {
|
||||||
DEBUG_TOOLBAR_CONFIG = {
|
DEBUG_TOOLBAR_CONFIG = {
|
||||||
"SHOW_TOOLBAR_CALLBACK": "yamdl_playground.core.utils.show_debug_toolbar"
|
"SHOW_TOOLBAR_CALLBACK": "yamdl_playground.core.utils.show_debug_toolbar"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if "ENABLE_ADMIN" in os.environ:
|
||||||
|
INSTALLED_APPS.extend([
|
||||||
|
'django.contrib.admin',
|
||||||
|
'django.contrib.auth',
|
||||||
|
'django.contrib.contenttypes',
|
||||||
|
'django.contrib.messages',
|
||||||
|
'django.contrib.sessions',
|
||||||
|
])
|
||||||
|
|
||||||
|
MIDDLEWARE.extend([
|
||||||
|
'yamdl_playground.core.middleware.FakeAuthenticationMiddleware',
|
||||||
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
|
])
|
||||||
|
|
||||||
|
# To make sure it's definitely working
|
||||||
|
AUTHENTICATION_BACKENDS = []
|
||||||
|
|
||||||
|
TEMPLATES[0]["OPTIONS"]["context_processors"].extend([
|
||||||
|
'django.contrib.auth.context_processors.auth',
|
||||||
|
'django.contrib.messages.context_processors.messages',
|
||||||
|
])
|
||||||
|
|
|
@ -16,6 +16,7 @@ Including another URLconf
|
||||||
"""
|
"""
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from .core import views
|
from .core import views
|
||||||
|
from django.apps import apps
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("search/", views.search),
|
path("search/", views.search),
|
||||||
|
@ -23,3 +24,8 @@ urlpatterns = [
|
||||||
path("__debug__/", include("debug_toolbar.urls")),
|
path("__debug__/", include("debug_toolbar.urls")),
|
||||||
path("cached-content/<slug:slug>/", views.cached_content)
|
path("cached-content/<slug:slug>/", views.cached_content)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if apps.is_installed("django.contrib.admin"):
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
urlpatterns.append(path("admin/", admin.site.urls))
|
||||||
|
|
Loading…
Reference in a new issue