Added auxilary info reader and drafts for closet command

This commit is contained in:
Oracle 2025-08-09 11:23:37 +02:00
parent e8660437a5
commit ba4c7fb772
2 changed files with 72 additions and 1 deletions

View File

@ -3,7 +3,7 @@ from django.conf import settings
from django.apps import apps from django.apps import apps
import os, pathlib, importlib, types import os, pathlib, importlib, types
import tomllib
class AsyncronConfig(AppConfig): class AsyncronConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = 'django.db.models.BigAutoField'
@ -19,6 +19,7 @@ class AsyncronConfig(AppConfig):
#if settings.DEBUG: #if settings.DEBUG:
# os.environ['PYTHONASYNCIODEBUG'] = "1" # os.environ['PYTHONASYNCIODEBUG'] = "1"
self.load_model_auxilaries()
self.load_extensions() self.load_extensions()
#Init the asyncron worker for this process #Init the asyncron worker for this process
@ -40,6 +41,45 @@ class AsyncronConfig(AppConfig):
loader = importlib.machinery.SourceFileLoader( f"{app.name}.{name}", str(import_file) ) loader = importlib.machinery.SourceFileLoader( f"{app.name}.{name}", str(import_file) )
loader.exec_module( types.ModuleType(loader.name) ) loader.exec_module( types.ModuleType(loader.name) )
def load_model_auxilaries( self ):
"""
Loads auxilary (passive) data from the models.toml file of each app.
Data is either a keyword argument of a field, or an allowed Meta class value.
There should be no database defining values set the toml file:
- ModelName.Meta.verbose_name -> Allowed
- ModelName.Meta.unique_together -> Bad Idea / Undefined Behaviour
- ModelName.help_text.a_field_name -> Allowed (string)
- ModelName.null.a_field_name -> Bad Idea / Undefined Behaviour
"""
from .base.models import BaseModel
for app in apps.get_app_configs():
app_dir = pathlib.Path(app.path)
toml_file = app_dir / "models.toml"
if not toml_file.exists(): continue
with open(toml_file, 'rb') as f:
model_auxilaries = tomllib.load(f)
for model in app.get_models():
if not issubclass( model, BaseModel ): continue
if model.__name__ not in model_auxilaries: continue
aux = model_auxilaries[model.__name__]
if 'Meta' in aux:
for k, v in aux.pop('Meta').items():
setattr(model._meta, k, v)
for field_kwarg, field_nv in aux.items():
for field_name, field_kwarg_value in field_nv.items():
if not hasattr(model, field_name): continue
field = getattr(model, field_name).field
if not getattr(field, field_kwarg, None): #Set only as default or empty replacements
setattr(field, field_kwarg, field_kwarg_value)
def load_extensions( self ): def load_extensions( self ):
from .base.models import BaseModel from .base.models import BaseModel

View File

@ -0,0 +1,31 @@
##
#
# Command: python manage.py startasyncron
#
##
import traceback, logging
import asyncio
import time
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
class Command( BaseCommand ):
help = "Asyncron's Closet Creation and Management tool"
def handle( self, *args, **kwargs ):
print( self, args, kwargs )
def add_arguments( self, parser ):
subparsers = parser.add_subparsers(title = "list of sub-commands")
# create the parser for the "a" command
show_parser = subparsers.add_parser('show', help = 'show [app_name]')
show_parser.add_argument('app_name', default = "")
# create the parser for the "b" command
create_parser = subparsers.add_parser('create', help='create app_name')
create_parser.add_argument('app_name')