Skip to content

Commit

Permalink
Move arches applications declaration to AppConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Jul 10, 2024
1 parent eeda1b0 commit f569e09
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 117 deletions.
3 changes: 2 additions & 1 deletion arches/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from arches.app.const import ExtensionType
from arches.app.utils.module_importer import get_class_from_modulename
from arches.app.utils.thumbnail_factory import ThumbnailGeneratorInstance
from arches.app.models.fields.i18n import I18n_TextField, I18n_JSONField
from arches.app.utils import import_class_from_string
from django.contrib.gis.db import models
Expand Down Expand Up @@ -409,6 +408,8 @@ def save(self, *args, **kwargs):
super(File, self).save(*args, **kwargs)

def make_thumbnail(self, kwargs_from_save_call, force=False):
from arches.app.utils.thumbnail_factory import ThumbnailGeneratorInstance

try:
if ThumbnailGeneratorInstance and (force or self.thumbnail_data is None):
self.thumbnail_data = ThumbnailGeneratorInstance.get_thumbnail_data(
Expand Down
3 changes: 2 additions & 1 deletion arches/app/utils/module_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from arches.app.const import ExtensionType
from arches.app.models.system_settings import settings
from arches.settings_utils import list_arches_app_names


def get_module(path, modulename=""):
Expand All @@ -15,7 +16,7 @@ def get_directories(extension_type: ExtensionType):
core_root_dir = core_root_dir.replace("search_components", "search.components")

core_and_arches_app_dirs = [core_root_dir]
for arches_app in settings.ARCHES_APPLICATIONS:
for arches_app in list_arches_app_names():
core_and_arches_app_dirs.append(f"{arches_app}.{extension_type.value}")
core_and_arches_app_dirs.append(
f"{arches_app}.pkg.extensions.{extension_type.value}"
Expand Down
8 changes: 3 additions & 5 deletions arches/app/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from arches.app.search.components.base import SearchFilterFactory
from arches.app.datatypes.datatypes import DataTypeFactory, EDTFDataType
from arches.app.search.search_engine_factory import SearchEngineFactory
from arches.settings_utils import list_arches_app_paths

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -122,12 +123,9 @@ def get(self, request):
os.path.join(settings.APP_ROOT, "locale", user_language + ".json")
)

for arches_application_name in settings.ARCHES_APPLICATIONS:
application_path = os.path.split(
sys.modules[arches_application_name].__spec__.origin
)[0]
for arches_app_path in list_arches_app_paths():
language_file_path.append(
os.path.join(application_path, "locale", user_language + ".json")
os.path.join(arches_app_path, "locale", user_language + ".json")
)

language_file_path.append(
Expand Down
12 changes: 12 additions & 0 deletions arches/install/arches-templates/project_name/apps.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.apps import AppConfig

from arches.settings_utils import inject_arches_applications_directories


class {{ project_name_title_case }}Config(AppConfig):
name = "{{ project_name }}"
verbose_name = "{{ project_name_title_case }}"
is_arches_application = True

def ready(self):
inject_arches_applications_directories()
14 changes: 1 addition & 13 deletions arches/install/arches-templates/project_name/settings.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ INSTALLED_APPS = (
"{{ project_name }}",
)

ARCHES_APPLICATIONS = ()

MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
Expand All @@ -170,14 +168,12 @@ MIDDLEWARE = [
STATICFILES_DIRS = build_staticfiles_dirs(
root_dir=ROOT_DIR,
app_root=APP_ROOT,
arches_applications=ARCHES_APPLICATIONS,
)

TEMPLATES = build_templates_config(
root_dir=ROOT_DIR,
debug=DEBUG,
app_root=APP_ROOT,
arches_applications=ARCHES_APPLICATIONS,
)

ALLOWED_HOSTS = []
Expand Down Expand Up @@ -423,12 +419,4 @@ except ImportError as e:

# returns an output that can be read by NODEJS
if __name__ == "__main__":
transmit_webpack_django_config(
root_dir=ROOT_DIR,
app_root=APP_ROOT,
arches_applications=ARCHES_APPLICATIONS,
public_server_address=PUBLIC_SERVER_ADDRESS,
static_url=STATIC_URL,
webpack_development_server_port=WEBPACK_DEVELOPMENT_SERVER_PORT,
)

transmit_webpack_django_config(**locals())
2 changes: 1 addition & 1 deletion arches/install/arches-templates/project_name/urls.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ urlpatterns = [

# Only handle i18n routing in active project. This will still handle the routes provided by Arches core and Arches applications,
# but handling i18n routes in multiple places causes application errors.
if settings.APP_NAME != "Arches" and settings.APP_NAME not in settings.ARCHES_APPLICATIONS:
if settings.ROOT_URLCONF == __name__:
if settings.SHOW_LANGUAGE_SWITCH is True:
urlpatterns = i18n_patterns(*urlpatterns)

Expand Down
2 changes: 0 additions & 2 deletions arches/install/arches-templates/tests/test_settings.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ TEST_ROOT = os.path.normpath(os.path.join(ROOT_DIR, "..", "tests"))

ROOT_URLCONF = '{{ project_name }}.urls'

ARCHES_APPLICATIONS = ()

MIN_ARCHES_VERSION = arches.__version__
MAX_ARCHES_VERSION = arches.__version__

Expand Down
5 changes: 4 additions & 1 deletion arches/install/arches_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def handle(self, options):
options["arches_next_minor_version"] = ".".join(
[str(arches.VERSION[0]), str(arches.VERSION[1] + 1), "0"]
)
options["project_name_title_case"] = project_name.title()

super(ArchesProjectCommand, self).handle(
"project", project_name, target, **options
Expand All @@ -132,6 +133,7 @@ def handle(self, options):
)

for relative_file_path in [
os.path.join(project_name, "apps.py"),
".coveragerc",
"pyproject.toml",
".pre-commit-config.yaml",
Expand All @@ -141,7 +143,8 @@ def handle(self, options):
file.close()

updated_file_data = (
file_data.replace("{{ project_name }}", project_name)
file_data.replace("{{ project_name_title_case }}", project_name.title())
.replace("{{ project_name }}", project_name)
.replace(
"{{ arches_semantic_version }}", options["arches_semantic_version"]
)
Expand Down
5 changes: 4 additions & 1 deletion arches/install/arches_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def handle(self, options):
options["arches_next_minor_version"] = ".".join(
[str(arches.VERSION[0]), str(arches.VERSION[1] + 1), "0"]
)
options["project_name_title_case"] = project_name.title()

super(ArchesCommand, self).handle("project", project_name, target, **options)

Expand All @@ -60,6 +61,7 @@ def handle(self, options):
)

for relative_file_path in [
os.path.join(project_name, "apps.py"),
".coveragerc",
"pyproject.toml",
".pre-commit-config.yaml",
Expand All @@ -69,7 +71,8 @@ def handle(self, options):
file.close()

updated_file_data = (
file_data.replace("{{ project_name }}", project_name)
file_data.replace("{{ project_name_title_case }}", project_name.title())
.replace("{{ project_name }}", project_name)
.replace(
"{{ arches_semantic_version }}", options["arches_semantic_version"]
)
Expand Down
29 changes: 28 additions & 1 deletion arches/management/commands/updateproject.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import arches
import os
import shutil
import warnings

from django.core.management.base import BaseCommand
from django.core import management
Expand Down Expand Up @@ -174,6 +175,28 @@ def update_to_v7_6(self):
os.path.join(dirpath, filename[:-7] + ".py"),
)

if os.path.isfile(os.path.join(settings.APP_ROOT, "apps.py")):
warnings.warn(
"Existing apps.py detected. Manually add is_arches_application=True.",
UserWarning,
)
else:
self.stdout.write("Copying apps.py to project root")
shutil.copy2(
os.path.join(
settings.ROOT_DIR,
"install",
"arches-templates",
"project_name",
"apps.py-tpl",
),
settings.APP_ROOT,
)
os.rename(
os.path.join(settings.APP_ROOT, "apps.py-tpl"),
os.path.join(settings.APP_ROOT, "apps.py"),
)

if not os.path.isfile(
os.path.join(settings.APP_ROOT, "src", "declarations.d.ts")
):
Expand Down Expand Up @@ -230,6 +253,7 @@ def update_to_v7_6(self):

path_to_project = os.path.join(settings.APP_ROOT, "..")
for relative_file_path in [
os.path.join(settings.APP_NAME, "apps.py"),
"gettext.config.js",
".coveragerc",
".gitignore",
Expand All @@ -245,7 +269,10 @@ def update_to_v7_6(self):
file.close()

updated_file_data = (
file_data.replace("{{ project_name }}", settings.APP_NAME)
file_data.replace(
"{{ project_name_title_case }}", settings.APP_NAME.title()
)
.replace("{{ project_name }}", settings.APP_NAME)
.replace("{{ arches_semantic_version }}", arches_semantic_version)
.replace(
"{{ arches_next_minor_version }}", arches_next_minor_version
Expand Down
19 changes: 2 additions & 17 deletions arches/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@
from datetime import datetime, timedelta
from contextlib import suppress


try:
from settings_utils import *
except ModuleNotFoundError:
try:
from .settings_utils import *
except ModuleNotFoundError:
pass
from arches.settings_utils import *

try:
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -374,8 +367,6 @@
"django_celery_results",
)

ARCHES_APPLICATIONS = ()

MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
Expand Down Expand Up @@ -879,10 +870,4 @@ def JSON_LD_FIX_DATA_FUNCTION(data, jsdata, model):

# returns an output that can be read by NODEJS
if __name__ == "__main__":
transmit_webpack_django_config(
root_dir=ROOT_DIR,
app_root=APP_ROOT,
public_server_address=PUBLIC_SERVER_ADDRESS,
static_url=STATIC_URL,
webpack_development_server_port=WEBPACK_DEVELOPMENT_SERVER_PORT,
)
transmit_webpack_django_config(**locals())
Loading

0 comments on commit f569e09

Please sign in to comment.