Skip to content

Commit

Permalink
chore(sample_project): cleanup manage.py and sample_project/settings.py
Browse files Browse the repository at this point in the history
The `./manage.py` script still contained a fix that was superseeded by
using `apis-override-select2js` long ago, sow we drop that fix.
Additionaly, we point DJANGO_SETTINGS_MODULE to `sample_project`, which
was still pointing to `apis` which was dropped. And we format the whole
file to look more similar to the default Django generated `manage.py`.

The `sample_project/settings.py` now drops the hardcoded secret key and
regenerates one every execution (or uses one from the environment). We
also removed some unused apps from INSTALLED_APPS and unused entries
from MIDDLEWARE and TEMPLATES.
  • Loading branch information
b1rger committed May 28, 2024
1 parent 5ff57cd commit d980613
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 52 deletions.
54 changes: 9 additions & 45 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apis.settings.settings_test_ci")

def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sample_project.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -12,48 +16,8 @@
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)

# TODO : Check at some time if the bug in dal_select2 has been fixed, and if so, remove this function here
def work_around_dal_select2_bug():
"""
Due to a bug in dal_select2, this workaround function here goes into the offending file and replaces the
relevant code segment with a work-around segment.
This function ensures that this replacement is done each time django is run and also modifies only the file
which is loaded via the respective library within the virtual environment of the current python interpreter.
"""

import dal_select2

file_to_fix = dal_select2.__file__.replace(
"/__init__.py", "/static/autocomplete_light/select2.js"
)

try:
with open(file_to_fix, "r") as f:
lines = f.readlines()

for i, line in enumerate(lines):
if (
line == " processResults: function (data, page) {\n"
and lines[i + 1]
== " if (element.attr('data-tags')) {\n"
and lines[i + 2]
== " $.each(data.results, function(index, value) {\n"
and lines[i + 3]
== " value.id = value.text;\n"
):
lines[i + 3] = " value.id = value.id;\n"

with open(file_to_fix, "w") as f:
f.write("".join(lines))

except FileNotFoundError:
raise Exception(
"Could not find select2.js file to inject bug workaround into.\n"
+ "Maybe the dal_select library has changed and this workaround is not necessary anymore?"
)

work_around_dal_select2_bug()

execute_from_command_line(sys.argv)
if __name__ == "__main__":
main()
12 changes: 5 additions & 7 deletions sample_project/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
SECRET_KEY = "a+nkut46lzzg_=ul)zrs29$u_6^*)2by2mjmwn)tqlgw)_at&l"
import os
from django.core.management.utils import get_random_secret_key

SECRET_KEY = os.environ.get("SECRET_KEY", get_random_secret_key())
DEBUG = True

ALLOWED_HOSTS = []
Expand Down Expand Up @@ -28,10 +31,9 @@
"dal_select2",
# api
"rest_framework",
"rest_framework.authtoken",
# for swagger ui generation
"drf_spectacular",
"simple_history",
# our ontology
"sample_project",
]

Expand All @@ -43,8 +45,6 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"allow_cidr.middleware.AllowCIDRMiddleware",
"reversion.middleware.RevisionMiddleware",
]

ROOT_URLCONF = "apis_core.urls"
Expand All @@ -60,8 +60,6 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
# we need this for accessing `basetemplate`
"apis_core.context_processors.custom_context_processors.list_apis_settings",
],
},
},
Expand Down

0 comments on commit d980613

Please sign in to comment.