No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

django.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # -*- coding: utf-8 -*-
  2. #from django.conf import settings
  3. #settings.configure(DEBUG=True)
  4. import os
  5. import sys
  6. from django.db import models
  7. import django
  8. from django.db.models.loading import cache as django_cache
  9. from EditorialModel.exceptions import *
  10. #django.conf.settings.configure(DEBUG=True)
  11. ## @brief Create a django model
  12. # @param name str : The django model name
  13. # @param fields dict : A dict that contains fields name and type ( str => DjangoField )
  14. # @param app_label str : The name of the applications that will have those models
  15. # @param module str : The module name this model will belong to
  16. # @param options dict : Dict of options (name => value)
  17. # @param admin_opts dict : Dict of options for admin part of this model
  18. # @param parent_class str : Parent class name
  19. # @return A dynamically created django model
  20. # @source https://code.djangoproject.com/wiki/DynamicModels
  21. #
  22. def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None, parent_class=None):
  23. class Meta:
  24. # Using type('Meta', ...) gives a dictproxy error during model creation
  25. pass
  26. if app_label:
  27. # app_label must be set using the Meta inner class
  28. setattr(Meta, 'app_label', app_label)
  29. # Update Meta with any options that were provided
  30. if options is not None:
  31. for key, value in options.iteritems():
  32. setattr(Meta, key, value)
  33. # Set up a dictionary to simulate declarations within a class
  34. attrs = {'__module__': module, 'Meta':Meta}
  35. # Add in any fields that were provided
  36. if fields:
  37. attrs.update(fields)
  38. # Create the class, which automatically triggers ModelBase processing
  39. if parent_class is None:
  40. parent_class = models.Model
  41. model = type(name, (parent_class,), attrs)
  42. # Create an Admin class if admin options were provided
  43. if admin_opts is not None:
  44. class Admin(admin.ModelAdmin):
  45. pass
  46. for key, value in admin_opts:
  47. setattr(Admin, key, value)
  48. admin.site.register(model, Admin)
  49. return model
  50. ## @package EditorialModel.migrationhandler.django
  51. # @brief A migration handler for django ORM
  52. #
  53. # Create django models according to the editorial model
  54. class DjangoMigrationHandler(object):
  55. ##
  56. # @param app_name str : The django application name for models generation
  57. # @param debug bool : Set to True to be in debug mode
  58. def __init__(self, app_name, debug=False):
  59. self.models = {}
  60. self.debug = debug
  61. self.app_name = app_name
  62. ## @brief Record a change in the EditorialModel and indicate wether or not it is possible to make it
  63. # @note The states ( initial_state and new_state ) contains only fields that changes
  64. #
  65. # @note Migration is not applied by this method. This method only checks if the new em is valid
  66. #
  67. # @param em model : The EditorialModel.model object to provide the global context
  68. # @param uid int : The uid of the change EmComponent
  69. # @param initial_state dict | None : dict with field name as key and field value as value. Representing the original state. None mean creation of a new component.
  70. # @param new_state dict | None : dict with field name as key and field value as value. Representing the new state. None mean component deletion
  71. # @throw EditorialModel.exceptions.MigrationHandlerChangeError if the change was refused
  72. def register_change(self, em, uid, initial_state, new_state):
  73. #Starting django
  74. os.environ['LODEL_MIGRATION_HANDLER_TESTS'] = 'YES'
  75. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
  76. django.setup()
  77. from django.contrib import admin
  78. from django.core.management import call_command as django_cmd
  79. if self.debug:
  80. self.dump_migration(uid, initial_state, new_state)
  81. #Generation django models
  82. self.em_to_models(em)
  83. try:
  84. #Calling makemigrations to see if the migration is valid
  85. #django_cmd('makemigrations', self.app_name, dry_run=True, intercative=True, merge=True, noinput=True)
  86. django_cmd('makemigrations', self.app_name, dry_run=True, intercative=True, noinput=True)
  87. except django.core.management.base.CommandError as e:
  88. raise MigrationHandlerChangeError(str(e))
  89. return True
  90. ## @brief Print a debug message representing a migration
  91. def dump_migration(self, uid, initial_state, new_state):
  92. if self.debug:
  93. print("\n##############")
  94. print("DummyMigrationHandler debug. Changes for component with uid %d :" % uid)
  95. if initial_state is None:
  96. print("Component creation (uid = %d): \n\t" % uid, new_state)
  97. elif new_state is None:
  98. print("Component deletion (uid = %d): \n\t" % uid, initial_state)
  99. else:
  100. field_list = set(initial_state.keys()).union(set(new_state.keys()))
  101. for field_name in field_list:
  102. str_chg = "\t%s " % field_name
  103. if field_name in initial_state:
  104. str_chg += "'" + str(initial_state[field_name]) + "'"
  105. else:
  106. str_chg += " creating "
  107. str_chg += " => "
  108. if field_name in new_state:
  109. str_chg += "'" + str(new_state[field_name]) + "'"
  110. else:
  111. str_chg += " deletion "
  112. print(str_chg)
  113. print("##############\n")
  114. pass
  115. ## @brief Not usefull ?
  116. # @note Maybe we can (have to?) use it to actually do migrations
  117. def register_model_state(self, em, state_hash):
  118. print('OHOHOH !!! i\'ve been called')
  119. pass
  120. ## @brief Return the models save method
  121. #
  122. # The save function of our class and type models is used to set unconditionnaly the
  123. # classtype, class_name and type_name models property
  124. #
  125. # @param classname str: The classname used to call the super().save
  126. # @param level str: Wich type of model are we doing. Possible values are 'class' and 'type'
  127. # @param datas list : List of property => value to set in the save function
  128. # @return The wanted save function
  129. def get_save_fun(self, classname, level, datas):
  130. if level == 'class':
  131. def save(self, *args, **kwargs):
  132. self.classtype = datas['classtype']
  133. self.class_name = datas['class_name']
  134. super(classname, self).save(*args, **kwargs)
  135. elif level == 'type':
  136. def save(self, *args, **kwargs):
  137. self.type_name = datas['type_name']
  138. super(classname, self).save(*args, **kwargs)
  139. return save
  140. ## @brief Create django models from an EditorialModel.model object
  141. # @param edMod EditorialModel.model.Model : The editorial model instance
  142. # @return a dict with all the models
  143. # @todo Handle fieldgroups
  144. # @todo write and use a function to forge models name from EmClasses and EmTypes names
  145. # @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
  146. def em_to_models(self, edMod):
  147. module_name = self.app_name+'models'
  148. #Purging django models cache
  149. if self.app_name in django_cache.all_models:
  150. for modname in django_cache.all_models[self.app_name]:
  151. del(django_cache.all_models[self.app_name][modname])
  152. #del(django_cache.all_models[self.app_name])
  153. #This cache at instance level seems to be useless...
  154. del(self.models)
  155. self.models = {}
  156. app_name = self.app_name
  157. #Creating the document model
  158. document_attrs = {
  159. 'lodel_id' : models.AutoField(primary_key=True),
  160. 'classtype': models.CharField(max_length=16, editable=False),
  161. 'class_name': models.CharField(max_length=16, editable=False),
  162. 'type_name': models.CharField(max_length=16, editable=False),
  163. 'string' : models.CharField(max_length=255),
  164. 'date_update': models.DateTimeField(auto_now=True, auto_now_add=True),
  165. 'date_create': models.DateTimeField(auto_now_add=True),
  166. 'rank' : models.IntegerField(),
  167. 'help_text': models.CharField(max_length=255),
  168. }
  169. #Creating the base model document
  170. document_model = create_model('document', document_attrs, self.app_name, module_name)
  171. django_models = {'doc' : document_model, 'classes':{}, 'types':{} }
  172. classes = edMod.classes()
  173. #Creating the EmClasses models with document inheritance
  174. for emclass in classes:
  175. emclass_fields = {
  176. 'save' : self.get_save_fun(emclass.uniq_name, 'class', { 'classtype':emclass.classtype, 'class_name':emclass.uniq_name})
  177. }
  178. #Addding non optionnal fields
  179. for emfield in emclass.fields():
  180. if not emfield.optional:
  181. # !!! Replace with fieldtype 2 django converter
  182. emclass_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
  183. #print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
  184. print("Model for class %s created"%emclass.uniq_name)
  185. django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, self.app_name, module_name, parent_class=django_models['doc'])
  186. #Creating the EmTypes models with EmClass inherithance
  187. for emtype in emclass.types():
  188. emtype_fields = {
  189. 'save': self.get_save_fun(emtype.uniq_name, 'type', { 'type_name':emtype.uniq_name }),
  190. }
  191. #Adding selected optionnal fields
  192. for emfield in emtype.selected_fields():
  193. emtype_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
  194. #Adding superiors foreign key
  195. for nature, superior in emtype.superiors().items():
  196. emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
  197. if self.debug:
  198. print("Model for type %s created"%emtype.uniq_name)
  199. 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])
  200. self.models=django_models
  201. pass