1
|
|
|
# pylint: disable=no-self-use, too-few-public-methods |
2
|
|
|
|
3
|
|
|
from django.conf import settings |
4
|
|
|
from django.contrib import messages |
5
|
|
|
from django.contrib.sites.models import Site |
6
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections |
7
|
|
|
from django.db.migrations.executor import MigrationExecutor |
8
|
|
|
from django.db.utils import OperationalError, ProgrammingError |
9
|
|
|
from django.http import HttpResponseRedirect |
10
|
|
|
from django.urls import reverse |
11
|
|
|
from django.utils.deprecation import MiddlewareMixin |
12
|
|
|
from django.utils.safestring import mark_safe |
13
|
|
|
from django.utils.translation import gettext_lazy as _ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class CheckDBStructureExistsMiddleware(MiddlewareMixin): |
17
|
|
|
def process_request(self, request): |
18
|
|
|
if request.path == "/init-db/": |
19
|
|
|
return None |
20
|
|
|
try: |
21
|
|
|
Site.objects.get(pk=settings.SITE_ID) |
22
|
|
|
except (OperationalError, ProgrammingError): |
23
|
|
|
# Redirect to Setup view |
24
|
|
|
return HttpResponseRedirect(reverse("init-db")) |
25
|
|
|
return None |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class CheckSettingsMiddleware(MiddlewareMixin): |
29
|
|
|
def process_request(self, request): |
30
|
|
|
doc_url = "https://kiwitcms.readthedocs.io/en/latest/admin.html#configure-kiwi-s-base-url" |
31
|
|
|
if request.path == "/init-db/": |
32
|
|
|
return |
33
|
|
|
site = Site.objects.get(pk=settings.SITE_ID) |
34
|
|
|
|
35
|
|
|
if site.domain == "127.0.0.1:8000": |
36
|
|
|
messages.add_message( |
37
|
|
|
request, |
38
|
|
|
messages.ERROR, |
39
|
|
|
mark_safe( # nosec:B308:B703 |
40
|
|
|
_( |
41
|
|
|
"Base URL is not configured! " |
42
|
|
|
'See <a href="%(doc_url)s">documentation</a> and ' |
43
|
|
|
'<a href="%(admin_url)s">change it</a>' |
44
|
|
|
) |
45
|
|
|
% { |
46
|
|
|
"doc_url": doc_url, |
47
|
|
|
"admin_url": reverse("admin:sites_site_change", args=[site.pk]), |
48
|
|
|
} |
49
|
|
|
), |
50
|
|
|
) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class CheckUnappliedMigrationsMiddleware(MiddlewareMixin): |
54
|
|
|
def process_request(self, request): |
55
|
|
|
if request.path == "/init-db/": |
56
|
|
|
return |
57
|
|
|
doc_url = ( |
58
|
|
|
"https://kiwitcms.readthedocs.io/en/latest/" |
59
|
|
|
"installing_docker.html#initial-configuration-of-running-container" |
60
|
|
|
) |
61
|
|
|
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) |
62
|
|
|
plan = executor.migration_plan(executor.loader.graph.leaf_nodes()) |
63
|
|
|
if plan: |
64
|
|
|
messages.add_message( |
65
|
|
|
request, |
66
|
|
|
messages.ERROR, |
67
|
|
|
mark_safe( # nosec:B308:B703 |
68
|
|
|
_( |
69
|
|
|
"You have %(unapplied_migration_count)s unapplied migration(s). " |
70
|
|
|
'See <a href="%(doc_url)s">documentation</a>' |
71
|
|
|
) |
72
|
|
|
% { |
73
|
|
|
"unapplied_migration_count": len(plan), |
74
|
|
|
"doc_url": doc_url, |
75
|
|
|
} |
76
|
|
|
), |
77
|
|
|
) |
78
|
|
|
|