mirror of
https://github.com/yweber/lodel2.git
synced 2026-07-30 01:23:26 +02:00
Code cleaning and adding features to generate models in application only when not testing with the MH
This commit is contained in:
parent
aa6e01bff0
commit
8bea52d740
4 changed files with 77 additions and 41 deletions
|
|
@ -12,12 +12,16 @@ from EditorialModel.exceptions import *
|
||||||
|
|
||||||
#django.conf.settings.configure(DEBUG=True)
|
#django.conf.settings.configure(DEBUG=True)
|
||||||
|
|
||||||
## @package EditorialModel.migrationhandler.django
|
|
||||||
# @brief A migration handler for django ORM
|
|
||||||
#
|
|
||||||
# Create django models according to the editorial model
|
|
||||||
|
|
||||||
##
|
## @brief Create a django model
|
||||||
|
# @param name str : The django model name
|
||||||
|
# @param fields dict : A dict that contains fields name and type ( str => DjangoField )
|
||||||
|
# @param app_label str : The name of the applications that will have those models
|
||||||
|
# @param module str : The module name this model will belong to
|
||||||
|
# @param options dict : Dict of options (name => value)
|
||||||
|
# @param admin_opts dict : Dict of options for admin part of this model
|
||||||
|
# @param parent_class str : Parent class name
|
||||||
|
# @return A dynamically created django model
|
||||||
# @source https://code.djangoproject.com/wiki/DynamicModels
|
# @source https://code.djangoproject.com/wiki/DynamicModels
|
||||||
#
|
#
|
||||||
def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None, parent_class=None):
|
def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None, parent_class=None):
|
||||||
|
|
@ -56,11 +60,13 @@ def create_model(name, fields=None, app_label='', module='', options=None, admin
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
|
## @package EditorialModel.migrationhandler.django
|
||||||
|
# @brief A migration handler for django ORM
|
||||||
|
#
|
||||||
|
# Create django models according to the editorial model
|
||||||
|
|
||||||
class DjangoMigrationHandler(object):
|
class DjangoMigrationHandler(object):
|
||||||
|
|
||||||
app_label = 'lodel'
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param app_name str : The django application name for models generation
|
# @param app_name str : The django application name for models generation
|
||||||
# @param debug bool : Set to True to be in debug mode
|
# @param debug bool : Set to True to be in debug mode
|
||||||
|
|
@ -68,6 +74,7 @@ class DjangoMigrationHandler(object):
|
||||||
self.models = {}
|
self.models = {}
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
self.app_name = app_name
|
self.app_name = app_name
|
||||||
|
|
||||||
## @brief Record a change in the EditorialModel and indicate wether or not it is possible to make it
|
## @brief Record a change in the EditorialModel and indicate wether or not it is possible to make it
|
||||||
# @note The states ( initial_state and new_state ) contains only fields that changes
|
# @note The states ( initial_state and new_state ) contains only fields that changes
|
||||||
# @param em model : The EditorialModel.model object to provide the global context
|
# @param em model : The EditorialModel.model object to provide the global context
|
||||||
|
|
@ -77,13 +84,28 @@ class DjangoMigrationHandler(object):
|
||||||
# @throw EditorialModel.exceptions.MigrationHandlerChangeError if the change was refused
|
# @throw EditorialModel.exceptions.MigrationHandlerChangeError if the change was refused
|
||||||
def register_change(self, em, uid, initial_state, new_state):
|
def register_change(self, em, uid, initial_state, new_state):
|
||||||
|
|
||||||
|
#Starting django
|
||||||
|
os.environ['LODEL_MIGRATION_HANDLER_TESTS'] = 'YES'
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
|
||||||
django.setup()
|
django.setup()
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.core.management import call_command as django_cmd
|
from django.core.management import call_command as django_cmd
|
||||||
|
|
||||||
|
if self.debug:
|
||||||
|
self.dump_migration(uid, initial_state, new_state)
|
||||||
|
|
||||||
|
#Generation django models
|
||||||
|
self.em_to_models(em)
|
||||||
|
try:
|
||||||
|
#Calling makemigrations to see if the migration is valid
|
||||||
|
django_cmd('makemigrations', self.app_name, dry_run=True, intercative=True, merge=True, noinput=True)
|
||||||
|
except django.core.management.base.CommandError as e:
|
||||||
|
raise MigrationHandlerChangeError(str(e))
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
## @brief Print a debug message representing a migration
|
||||||
|
def dump_migration(self, uid, initial_state, new_state):
|
||||||
if self.debug:
|
if self.debug:
|
||||||
print("\n##############")
|
print("\n##############")
|
||||||
print("DummyMigrationHandler debug. Changes for component with uid %d :" % uid)
|
print("DummyMigrationHandler debug. Changes for component with uid %d :" % uid)
|
||||||
|
|
@ -106,21 +128,11 @@ class DjangoMigrationHandler(object):
|
||||||
str_chg += " deletion "
|
str_chg += " deletion "
|
||||||
print(str_chg)
|
print(str_chg)
|
||||||
print("##############\n")
|
print("##############\n")
|
||||||
|
|
||||||
self.em_to_models(em,self.app_name, self.app_name+'models')
|
|
||||||
try:
|
|
||||||
#ret = django_cmd('makemigrations', self.app_name, interactive=False, noinput=True, dryrun=True, traceback=True)
|
|
||||||
#django_cmd('makemigrations', self.app_name, dry-run=True, intercative=True, noinput=True)
|
|
||||||
django_cmd('makemigrations', self.app_name, dry_run=True, intercative=True, merge=True, noinput=True)
|
|
||||||
except django.core.management.base.CommandError as e:
|
|
||||||
raise MigrationHandlerChangeError(str(e))
|
|
||||||
|
|
||||||
return True
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
## @brief Not usefull ?
|
||||||
def register_model_state(self, em, state_hash):
|
def register_model_state(self, em, state_hash):
|
||||||
print('OHOHOH !!! i\'ve been called')
|
print('OHOHOH !!! i\'ve been called')
|
||||||
#ret = django_cmd('makemigrations', '--noinput', '--traceback', self.app_name)
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
## @brief Return the models save method
|
## @brief Return the models save method
|
||||||
|
|
@ -147,20 +159,22 @@ class DjangoMigrationHandler(object):
|
||||||
return save
|
return save
|
||||||
|
|
||||||
## @brief Create django models from an EditorialModel.model object
|
## @brief Create django models from an EditorialModel.model object
|
||||||
# @param me EditorialModel.model.Model : The editorial model instance
|
# @param edMod EditorialModel.model.Model : The editorial model instance
|
||||||
# @return a dict with all the models
|
# @return a dict with all the models
|
||||||
# @todo Handle fieldgroups
|
# @todo Handle fieldgroups
|
||||||
# @todo write and use a function to forge models name from EmClasses and EmTypes names
|
# @todo write and use a function to forge models name from EmClasses and EmTypes names
|
||||||
# @note There is a problem with the related_name for superiors fk : The related name cannot be subordinates, it has to be the subordinates em_type name
|
# @note There is a problem with the related_name for superiors fk : The related name cannot be subordinates, it has to be the subordinates em_type name
|
||||||
def em_to_models(self, me, app_label, module_name):
|
def em_to_models(self, edMod):
|
||||||
|
|
||||||
|
module_name = self.app_name+'models'
|
||||||
|
|
||||||
#Purging django models cache
|
#Purging django models cache
|
||||||
if app_label in django_cache.all_models:
|
if self.app_name in django_cache.all_models:
|
||||||
for modname in django_cache.all_models[app_label]:
|
for modname in django_cache.all_models[self.app_name]:
|
||||||
del(django_cache.all_models[app_label][modname])
|
del(django_cache.all_models[self.app_name][modname])
|
||||||
#del(django_cache.all_models[app_label])
|
#del(django_cache.all_models[self.app_name])
|
||||||
|
|
||||||
#django_cache.clear_cache()
|
#This cache at instance level seems to be useless...
|
||||||
del(self.models)
|
del(self.models)
|
||||||
self.models = {}
|
self.models = {}
|
||||||
|
|
||||||
|
|
@ -179,11 +193,11 @@ class DjangoMigrationHandler(object):
|
||||||
}
|
}
|
||||||
|
|
||||||
#Creating the base model document
|
#Creating the base model document
|
||||||
document_model = create_model('document', document_attrs, app_label, module_name)
|
document_model = create_model('document', document_attrs, self.app_name, module_name)
|
||||||
|
|
||||||
django_models = {'doc' : document_model, 'classes':{}, 'types':{} }
|
django_models = {'doc' : document_model, 'classes':{}, 'types':{} }
|
||||||
|
|
||||||
classes = me.classes()
|
classes = edMod.classes()
|
||||||
|
|
||||||
#Creating the EmClasses models with document inheritance
|
#Creating the EmClasses models with document inheritance
|
||||||
for emclass in classes:
|
for emclass in classes:
|
||||||
|
|
@ -198,7 +212,7 @@ class DjangoMigrationHandler(object):
|
||||||
emclass_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
|
emclass_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
|
||||||
#print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
|
#print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
|
||||||
print("Model for class %s created"%emclass.uniq_name)
|
print("Model for class %s created"%emclass.uniq_name)
|
||||||
django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, app_label, module_name, parent_class=django_models['doc'])
|
django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, self.app_name, module_name, parent_class=django_models['doc'])
|
||||||
|
|
||||||
#Creating the EmTypes models with EmClass inherithance
|
#Creating the EmTypes models with EmClass inherithance
|
||||||
for emtype in emclass.types():
|
for emtype in emclass.types():
|
||||||
|
|
@ -213,12 +227,11 @@ class DjangoMigrationHandler(object):
|
||||||
for nature, superior in emtype.superiors().items():
|
for nature, superior in emtype.superiors().items():
|
||||||
emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
|
emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
|
||||||
|
|
||||||
#print("Model for type %s created with fields : "%emtype.uniq_name, emtype_fields)
|
if self.debug:
|
||||||
print("Model for type %s created"%emtype.uniq_name)
|
print("Model for type %s created"%emtype.uniq_name)
|
||||||
django_models['types'][emtype.uniq_name] = create_model(emtype.uniq_name, emtype_fields, app_label, module_name, parent_class=django_models['classes'][emclass.uniq_name])
|
django_models['types'][emtype.uniq_name] = create_model(emtype.uniq_name, emtype_fields, self.app_name, module_name, parent_class=django_models['classes'][emclass.uniq_name])
|
||||||
|
|
||||||
self.models=django_models
|
self.models=django_models
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import os
|
||||||
from Lodel.settings.defaults import *
|
from Lodel.settings.defaults import *
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|
@ -7,6 +8,9 @@ if DEBUG:
|
||||||
else:
|
else:
|
||||||
from Lodel.settings.production import *
|
from Lodel.settings.production import *
|
||||||
|
|
||||||
|
if 'LODEL_MIGRATION_HANDLER_TESTS' in os.environ:
|
||||||
|
from Lodel.settings.migrations import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from Lodel.settings.locale import *
|
from Lodel.settings.locale import *
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ INSTALLED_APPS = (
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'LodelTestInstance',
|
||||||
)
|
)
|
||||||
|
|
||||||
MIDDLEWARE_CLASSES = (
|
MIDDLEWARE_CLASSES = (
|
||||||
|
|
@ -77,12 +78,25 @@ WSGI_APPLICATION = 'Lodel.wsgi.application'
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
|
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
|
||||||
|
|
||||||
|
DATABASES = {
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.db.backends.mysql',
|
||||||
|
'NAME': 'lodel2',
|
||||||
|
'USER': 'lodel',
|
||||||
|
'PASSWORD': 'bruno',
|
||||||
|
'HOST': 'localhost',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
"""
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/1.7/topics/i18n/
|
# https://docs.djangoproject.com/en/1.7/topics/i18n/
|
||||||
|
|
@ -102,3 +116,6 @@ USE_TZ = True
|
||||||
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
|
|
||||||
|
# Lodel configurations
|
||||||
|
LODEL_MIGRATION_HANDLER_TESTS = True
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from EditorialModel.migrationhandler.django import DjangoMigrationHandler
|
from EditorialModel.migrationhandler.django import DjangoMigrationHandler
|
||||||
|
|
@ -6,10 +7,11 @@ from EditorialModel.model import Model
|
||||||
from EditorialModel.backend.json_backend import EmBackendJson
|
from EditorialModel.backend.json_backend import EmBackendJson
|
||||||
|
|
||||||
|
|
||||||
me = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler = DummyMigrationHandler(True))
|
if not settings.LODEL_MIGRATION_HANDLER_TESTS:
|
||||||
|
me = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler = DummyMigrationHandler(True))
|
||||||
dmh = DjangoMigrationHandler()
|
dmh = DjangoMigrationHandler()
|
||||||
|
models = dmh.me_to_models(me,'LodelTestInstance', 'LodelTestInstance.models')
|
||||||
models = dmh.me_to_models(me,'LodelTestInstance', 'LodelTestInstance.models')
|
elif settings.DEBUG:
|
||||||
|
print("Making migrations tests, don't generate the models in the models.py file but within the migrations handler check process")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue