Skip to content

Commit

Permalink
Storage change
Browse files Browse the repository at this point in the history
  • Loading branch information
srugano committed Oct 14, 2024
1 parent 7f6c499 commit 18a4850
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 66 deletions.
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ x-django-env: &django-env
- AZURE_TENANT_ID=
- AZURE_CLIENT_KEY=
- FLOWER_URL=http://flower:5555
- STATIC_FILE_STORAGE=hope_country_report.apps.power_query.storage.DataSetStorage
- STATIC_FILE_STORAGE=django.core.files.storage.FileSystemStorage

services:
backend:
Expand Down
16 changes: 8 additions & 8 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ license = {text = "Private"}
requires-python = ">=3.12"

dependencies = [
"django",
"azure-devops",
"celery",
"django",
"django-admin-cursor-paginator",
"django-admin-extra-buttons",
"django-adminactions",
Expand Down Expand Up @@ -86,7 +86,8 @@ dependencies = [
"pathvalidate",
"django-stubs-ext",
"uwsgi",
"django-smart-env>=0.1.0",
"django-smart-env",
"django-webtest"
]

[tool.pdm.dev-dependencies]
Expand Down
59 changes: 29 additions & 30 deletions src/hope_country_report/apps/core/management/commands/env.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
from typing import TYPE_CHECKING

from typing import Any, Dict
from django.core.management import BaseCommand, CommandError, CommandParser

if TYPE_CHECKING:
from typing import Any

DEVELOP = {
"DEBUG": True,
"SECRET_KEY": "only-development-secret-key",
}
from hope_country_report.config import env


class Command(BaseCommand):
Expand All @@ -25,43 +17,50 @@ def add_arguments(self, parser: "CommandParser") -> None:
help="Only dumps keys, without values",
)
parser.add_argument(
"--develop", action="store_true", help="Get values from teh code not from the current environment"
"--develop", action="store_true", help="Get values from the code, not from the current environment"
)
parser.add_argument(
"--changed", action="store_true", help="Get values from teh code not from the current environment"
"--changed", action="store_true", help="Display only variables that have changed from defaults"
)

parser.add_argument(
"--pattern",
action="store",
dest="pattern",
default="{key}={value} # {help}",
help="Check env for variable availability",
help="Pattern for printing variables",
)
parser.add_argument(
"--check", action="store_true", dest="check", default=False, help="Check env for variable availability"
"--check", action="store_true", dest="check", default=False, help="Check for missing required variables"
)
parser.add_argument(
"--ignore-errors", action="store_true", dest="ignore_errors", default=False, help="Do not fail"
"--ignore-errors", action="store_true", dest="ignore_errors", default=False, help="Ignore missing variables"
)

def handle(self, *args: "Any", **options: "Any") -> None:
from hope_country_report.config import CONFIG, env, EXPLICIT_SET

def handle(self, *args: "Any", **options: "Dict[str, Any]") -> None:
check_failure = False
pattern = options["pattern"]

for k, __ in sorted(CONFIG.items()):
help: str = env.get_help(k)
default = env.get_default(k)
if options["check"]:
if default in EXPLICIT_SET and k not in env.ENVIRON:
self.stderr.write(self.style.ERROR(f"- Missing env variable: {k}"))
if options["check"]:
self.stdout.write(self.style.SUCCESS("Checking for missing required environment variables..."))
missing_vars = env.check_explicit()
if missing_vars:
for var in missing_vars:
self.stderr.write(self.style.ERROR(f"- Missing env variable: {var}"))
check_failure = True
if check_failure and not options["ignore_errors"]:
raise CommandError("One or more required environment variables are missing!")
return

for key, config in sorted(env.config.items()):
help_text = config.get("help", "")
default_value = config.get("default")
# develop_value = config.get("develop")
# explicit = config.get("explicit", False)
if options["develop"]:
value = env.get_develop_value(key)
else:
value: Any = env.get_value(k)
if not options["changed"] or (value != default):
self.stdout.write(pattern.format(key=k, value=value, help=help, default=default))
value = env.get_value(key)

if check_failure and not options["ignore_errors"]:
raise CommandError("Env check command failure!")
if options["changed"] and value == default_value:
continue
self.stdout.write(pattern.format(key=key, value=value, help=help_text, default=default_value))
9 changes: 0 additions & 9 deletions src/hope_country_report/apps/power_query/storage.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import os

from django.core.files.storage import FileSystemStorage

from storages.backends.azure_storage import AzureStorage


class DataSetStorage(FileSystemStorage):
def get_available_name(self, name: str, max_length: int | None = None) -> str:
if self.exists(name):
self.delete(name)
return name


class ReadOnlyStorageMixin:
def delete(self, name):
raise RuntimeError("This storage cannot delete files")
Expand Down
3 changes: 1 addition & 2 deletions src/hope_country_report/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from enum import Enum

from smart_env import SmartEnv

DJANGO_HELP_BASE = "https://docs.djangoproject.com/en/5.0/ref/settings"
Expand Down Expand Up @@ -119,7 +118,7 @@ class Group(Enum):
),
"STATIC_FILE_STORAGE": (
str,
"hope_country_report.apps.power_query.storage.StaticStorage",
"django.core.files.storage.FileSystemStorage",
setting("storages"),
),
"STATIC_ROOT": (str, "/tmp/static/", setting("static-root")),
Expand Down
16 changes: 4 additions & 12 deletions src/hope_country_report/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@
MIGRATION_MODULES = {"hope": None}

STORAGES = {
"default": {
"BACKEND": env("DEFAULT_FILE_STORAGE"),
},
"staticfiles": {
"BACKEND": env("STATIC_FILE_STORAGE"),
},
"media": {
"BACKEND": env("MEDIA_FILE_STORAGE"),
},
"hope": {
"BACKEND": env("HOPE_FILE_STORAGE"),
},
"default": env.storage("DEFAULT_FILE_STORAGE"),
"staticfiles": env.storage("STATIC_FILE_STORAGE"),
"media": env.storage("MEDIA_FILE_STORAGE"),
"hope": env.storage("HOPE_FILE_STORAGE"),
}
INSTALLED_APPS = [
"hope_country_report.web",
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def pytest_configure(config):
ADMINS="",
ALLOWED_HOSTS="*",
AUTHENTICATION_BACKENDS="",
DEFAULT_FILE_STORAGE="hope_country_report.apps.power_query.storage.DataSetStorage",
STATIC_FILE_STORAGE="hope_country_report.apps.power_query.storage.DataSetStorage",
DEFAULT_FILE_STORAGE="django.core.files.storage.FileSystemStorage",
STATIC_FILE_STORAGE="django.core.files.storage.FileSystemStorage",
DJANGO_SETTINGS_MODULE="hope_country_report.config.settings",
CATCH_ALL_EMAIL="",
CELERY_TASK_ALWAYS_EAGER="1",
Expand Down

0 comments on commit 18a4850

Please sign in to comment.