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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import django
  5. from django.db import models
  6. from django.db.models.loading import cache as django_cache
  7. from django.core.exceptions import ValidationError
  8. from django.contrib import admin
  9. from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
  10. from EditorialModel.classtypes import EmNature
  11. from EditorialModel.exceptions import *
  12. ## @package EditorialModel::migrationhandler::django
  13. # @deprecated Broken since EmField and fieldtypes changed
  14. ## @brief Create a django model
  15. # @param name str : The django model name
  16. # @param fields dict : A dict that contains fields name and type ( str => DjangoField )
  17. # @param app_label str : The name of the applications that will have those models
  18. # @param module str : The module name this model will belong to
  19. # @param options dict : Dict of options (name => value)
  20. # @param admin_opts dict : Dict of options for admin part of this model
  21. # @param parent_class str : Parent class name
  22. # @return A dynamically created django model
  23. # @note Source : https://code.djangoproject.com/wiki/DynamicModels
  24. #
  25. def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None, parent_class=None):
  26. class Meta:
  27. # Using type('Meta', ...) gives a dictproxy error during model creation
  28. pass
  29. if app_label:
  30. # app_label must be set using the Meta inner class
  31. setattr(Meta, 'app_label', app_label)
  32. # Update Meta with any options that were provided
  33. if options is not None:
  34. for key, value in options.iteritems():
  35. setattr(Meta, key, value)
  36. # Set up a dictionary to simulate declarations within a class
  37. attrs = {'__module__': module, 'Meta': Meta}
  38. # Add in any fields that were provided
  39. if fields:
  40. attrs.update(fields)
  41. # Create the class, which automatically triggers ModelBase processing
  42. if parent_class is None:
  43. parent_class = models.Model
  44. model = type(name, (parent_class,), attrs)
  45. # Create an Admin class if admin options were provided
  46. if admin_opts is not None:
  47. class Admin(admin.ModelAdmin):
  48. pass
  49. for key, value in admin_opts:
  50. setattr(Admin, key, value)
  51. admin.site.register(model, Admin)
  52. return model
  53. ## @package EditorialModel.migrationhandler.django
  54. # @brief A migration handler for django ORM
  55. #
  56. # Create django models according to the editorial model
  57. class DjangoMigrationHandler(DummyMigrationHandler):
  58. ## @brief Instanciate a new DjangoMigrationHandler
  59. # @param app_name str : The django application name for models generation
  60. # @param debug bool : Set to True to be in debug mode
  61. # @param dryrun bool : If true don't do any migration, only simulate them
  62. def __init__(self, app_name, debug=False, dryrun=False):
  63. self.debug = debug
  64. self.app_name = app_name
  65. self.dryrun = dryrun
  66. ## @brief Record a change in the EditorialModel and indicate wether or not it is possible to make it
  67. # @note The states ( initial_state and new_state ) contains only fields that changes
  68. #
  69. # @note Migration is not applied by this method. This method only checks if the new em is valid
  70. #
  71. # @param em model : The EditorialModel.model object to provide the global context
  72. # @param uid int : The uid of the change EmComponent
  73. # @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.
  74. # @param new_state dict | None : dict with field name as key and field value as value. Representing the new state. None mean component deletion
  75. # @throw EditorialModel.exceptions.MigrationHandlerChangeError if the change was refused
  76. # @todo Some tests about strating django in this method
  77. # @todo Rename in something like "validate_change"
  78. #
  79. # @warning broken because of : https://code.djangoproject.com/ticket/24735 you have to patch django/core/management/commands/makemigrations.py w/django/core/management/commands/makemigrations.py
  80. def register_change(self, em, uid, initial_state, new_state):
  81. #Starting django
  82. os.environ['LODEL_MIGRATION_HANDLER_TESTS'] = 'YES'
  83. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
  84. django.setup()
  85. from django.contrib import admin
  86. from django.core.management import call_command as django_cmd
  87. if self.debug:
  88. self.dump_migration(uid, initial_state, new_state)
  89. #Generation django models
  90. self.em_to_models(em)
  91. try:
  92. #Calling makemigrations to see if the migration is valid
  93. #django_cmd('makemigrations', self.app_name, dry_run=True, interactive=False, merge=True)
  94. django_cmd('makemigrations', self.app_name, dry_run=True, interactive=False)
  95. except django.core.management.base.CommandError as e:
  96. raise MigrationHandlerChangeError(str(e))
  97. return True
  98. ## @brief Print a debug message representing a migration
  99. # @param uid int : The EmComponent uid
  100. # @param initial_state dict | None : dict representing the fields that are changing
  101. # @param new_state dict | None : dict represnting the new fields states
  102. def dump_migration(self, uid, initial_state, new_state):
  103. if self.debug:
  104. print("\n##############")
  105. print("DummyMigrationHandler debug. Changes for component with uid %d :" % uid)
  106. if initial_state is None:
  107. print("Component creation (uid = %d): \n\t" % uid, new_state)
  108. elif new_state is None:
  109. print("Component deletion (uid = %d): \n\t" % uid, initial_state)
  110. else:
  111. field_list = set(initial_state.keys()).union(set(new_state.keys()))
  112. for field_name in field_list:
  113. str_chg = "\t%s " % field_name
  114. if field_name in initial_state:
  115. str_chg += "'" + str(initial_state[field_name]) + "'"
  116. else:
  117. str_chg += " creating "
  118. str_chg += " => "
  119. if field_name in new_state:
  120. str_chg += "'" + str(new_state[field_name]) + "'"
  121. else:
  122. str_chg += " deletion "
  123. print(str_chg)
  124. print("##############\n")
  125. pass
  126. ## @brief Register a new model state and update the data representation given the new state
  127. # @param em model : The EditorialModel to migrate
  128. # @param state_hash str : Note usefull (for the moment ?)
  129. # @todo Rename this method in something like "model_migrate"
  130. def register_model_state(self, em, state_hash):
  131. if self.dryrun:
  132. return
  133. if self.debug:
  134. print("Applying editorial model change")
  135. #Starting django
  136. os.environ['LODEL_MIGRATION_HANDLER_TESTS'] = 'YES'
  137. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
  138. django.setup()
  139. from django.contrib import admin
  140. from django.core.management import call_command as django_cmd
  141. #Generation django models
  142. self.em_to_models(em)
  143. try:
  144. #Calling makemigrations
  145. django_cmd('makemigrations', self.app_name, interactive=False)
  146. except django.core.management.base.CommandError as e:
  147. raise MigrationHandlerChangeError(str(e))
  148. try:
  149. #Calling migrate to update the database schema
  150. django_cmd('migrate', self.app_name, interactive=False, noinput=True)
  151. except django.core.management.base.CommandError as e:
  152. raise MigrationHandlerChangeError("Unable to migrate to new model state : %s" % e)
  153. pass
  154. ## @brief Return the models save method
  155. #
  156. # The save function of our class and type models is used to set unconditionnaly the
  157. # classtype, class_name and type_name models property
  158. #
  159. # @param classname str: The classname used to call the super().save
  160. # @param level str: Wich type of model are we doing. Possible values are 'class' and 'type'
  161. # @param datas list : List of property => value to set in the save function
  162. # @return The wanted save function
  163. @staticmethod
  164. def set_save_fun(class_obj, level, datas):
  165. if level == 'class':
  166. def save(self, *args, **kwargs):
  167. #Sets the classtype and class name
  168. self.classtype = datas['classtype']
  169. self.class_name = datas['class_name']
  170. super(class_obj, self).save(*args, **kwargs)
  171. elif level == 'type':
  172. def save(self, *args, **kwargs):
  173. #Set the type name
  174. self.type_name = datas['type_name']
  175. super(class_obj, self).save(*args, **kwargs)
  176. class_obj.save = save
  177. return class_obj
  178. ## @brief Create django models from an EditorialModel.model object
  179. # @param edMod EditorialModel.model.Model : The editorial model instance
  180. # @return a dict with all the models
  181. # @todo Handle fieldgroups
  182. # @todo write and use a function to forge models name from EmClasses and EmTypes names
  183. # @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
  184. def em_to_models(self, edMod):
  185. module_name = self.app_name + '.models'
  186. #Purging django models cache
  187. if self.app_name in django_cache.all_models:
  188. for modname in django_cache.all_models[self.app_name]:
  189. del(django_cache.all_models[self.app_name][modname])
  190. #del(django_cache.all_models[self.app_name])
  191. app_name = self.app_name
  192. #Creating the document model
  193. document_attrs = {
  194. 'lodel_id': models.AutoField(primary_key=True),
  195. 'classtype': models.CharField(max_length=16, editable=False),
  196. 'class_name': models.CharField(max_length=76, editable=False),
  197. 'type_name': models.CharField(max_length=76, editable=False),
  198. 'string': models.CharField(max_length=255),
  199. 'date_update': models.DateTimeField(auto_now=True, auto_now_add=True),
  200. 'date_create': models.DateTimeField(auto_now_add=True),
  201. 'rank': models.IntegerField(),
  202. 'help_text': models.CharField(max_length=255),
  203. }
  204. #Creating the base model document
  205. document_model = create_model('document', document_attrs, self.app_name, module_name)
  206. django_models = {'doc': document_model, 'classes': {}, 'types': {}}
  207. classes = edMod.classes()
  208. def object_repr(self):
  209. return self.string
  210. #Creating the EmClasses models with document inheritance
  211. for emclass in classes:
  212. emclass_fields = {
  213. 'save': None, #will be set later using self.set_save_fun
  214. '__str__': object_repr,
  215. }
  216. #Addding non optionnal fields
  217. for emfield in emclass.fields():
  218. if not emfield.optional:
  219. # !!! Replace with fieldtype 2 django converter
  220. #emclass_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
  221. emclass_fields[emfield.uniq_name] = self.field_to_django(emfield, emclass)
  222. #print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
  223. if self.debug:
  224. print("Model for class %s created" % emclass.uniq_name)
  225. django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, self.app_name, module_name, parent_class=django_models['doc'])
  226. self.set_save_fun(django_models['classes'][emclass.uniq_name], 'class', {'classtype': emclass.classtype, 'class_name': emclass.uniq_name})
  227. #Creating the EmTypes models with EmClass inherithance
  228. for emtype in emclass.types():
  229. emtype_fields = {
  230. 'save':None,
  231. '__str__': object_repr,
  232. }
  233. #Adding selected optionnal fields
  234. for emfield in emtype.selected_fields():
  235. #emtype_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
  236. emtype_fields[emfield.uniq_name] = self.field_to_django(emfield, emtype)
  237. #Adding superiors foreign key
  238. for nature, superiors_list in emtype.superiors().items():
  239. for superior in superiors_list:
  240. emtype_fields[nature+'_'+superior.uniq_name] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, blank=True, default=None, null=True)
  241. # Method to get the parent that is not None
  242. emtype_fields['sup_field_list'] = [ nature + '_' + superior.uniq_name for superior in superiors_list ]
  243. def get_sup(self):
  244. for field in [ getattr(self, fname) for fname in self.sup_field_list ]:
  245. if not (field is None):
  246. return field
  247. return None
  248. #Adding a method to get the superior by nature
  249. emtype_fields[nature] = get_sup
  250. # Adding a dummy function to non present nature
  251. def dummyNone(self): return None
  252. for nat in [ nat for nat in EmNature.getall() if nat not in emtype_fields]:
  253. emtype_fields[nat] = dummyNone
  254. if self.debug:
  255. print("Model for type %s created" % emtype.uniq_name)
  256. 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], admin_opts=dict())
  257. self.set_save_fun(django_models['types'][emtype.uniq_name], 'type', {'type_name': emtype.uniq_name})
  258. return django_models
  259. ## @brief Return a good django field type given a field
  260. # @param f EmField : an EmField object
  261. # @param assoc_comp EmComponent : The associated component (type or class)
  262. # @return A django field instance
  263. # @note The manytomany fields created with the rel2type field has no related_name associated to it
  264. def field_to_django(self, f, assoc_comp):
  265. #Building the args dictionnary for django field instanciation
  266. args = dict()
  267. args['null'] = f.nullable
  268. if not (f.default is None):
  269. args['default'] = f.default
  270. v_fun = f.validation_function(raise_e=ValidationError)
  271. if v_fun:
  272. args['validators'] = [v_fun]
  273. if f.uniq:
  274. args['unique'] = True
  275. # Field instanciation
  276. if f.ftype == 'char': # varchar field
  277. args['max_length'] = f.max_length
  278. return models.CharField(**args)
  279. elif f.ftype == 'int': # integer field
  280. return models.IntegerField(**args)
  281. elif f.ftype == 'text': # text field
  282. return models.TextField(**args)
  283. elif f.ftype == 'datetime': # Datetime field
  284. args['auto_now'] = f.now_on_update
  285. args['auto_now_add'] = f.now_on_create
  286. return models.DateTimeField(**args)
  287. elif f.ftype == 'bool': # Boolean field
  288. if args['null']:
  289. return models.NullBooleanField(**args)
  290. del(args['null'])
  291. return models.BooleanField(**args)
  292. elif f.ftype == 'rel2type': # Relation to type
  293. if assoc_comp is None:
  294. raise RuntimeError("Rel2type field in a rel2type table is not allowed")
  295. #create first a throught model if there is data field associated with the relation
  296. kwargs = dict()
  297. relf_l = f.get_related_fields()
  298. if len(relf_l) > 0:
  299. through_fields = {}
  300. #The two FK of the through model
  301. through_fields[assoc_comp.name] = models.ForeignKey(assoc_comp.uniq_name)
  302. rtype = f.get_related_type()
  303. through_fields[rtype.name] = models.ForeignKey(rtype.uniq_name)
  304. for relf in relf_l:
  305. through_fields[relf.name] = self.field_to_django(relf, None)
  306. #through_model_name = f.uniq_name+assoc_comp.uniq_name+'to'+rtype.uniq_name
  307. through_model_name = f.name + assoc_comp.name + 'to' + rtype.name
  308. module_name = self.app_name + '.models'
  309. #model created
  310. through_model = create_model(through_model_name, through_fields, self.app_name, module_name)
  311. kwargs['through'] = through_model_name
  312. return models.ManyToManyField(f.get_related_type().uniq_name, **kwargs)
  313. else: # Unknow data type
  314. raise NotImplemented("The conversion to django fields is not yet implemented for %s field type" % f.ftype)