PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/appsavocabulary/management/commands/vocabulary_backup.py

https://gitlab.com/Seiris/annot
Python | 173 lines | 161 code | 7 blank | 5 comment | 3 complexity | 15cab60df57f2cafe55fded304fc5f32 MD5 | raw file
  1. # import form django
  2. from django.core import management
  3. from django.core.management.base import BaseCommand, CommandError
  4. # import python
  5. import datetime
  6. import glob
  7. import importlib
  8. import inspect
  9. import json
  10. # import annot
  11. from appsavocabulary.models import SysAdminVocabulary
  12. class Command( BaseCommand ):
  13. args = "<apponmmm_source apponmmm_source ...>"
  14. help = "Backup controlled vocabulary."
  15. def handle(self, *args, **options):
  16. # initiate
  17. b_flag = False
  18. queryset = SysAdminVocabulary.objects.all() # get queryset
  19. ls_queryset = []
  20. ls_apponfilter = []
  21. # load filter_vocabulary_getupdate.txt
  22. self.stdout.write("Load vocabulary_getupdate filter" )
  23. with open( "appsavocabulary/filter_vocabulary.txt", 'r' ) as f:
  24. for s_line in f:
  25. s_line = s_line.strip()
  26. if ( s_line != "" ):
  27. ls_apponfilter.append(s_line)
  28. self.stdout.write( "Filter: " + str(ls_apponfilter) )
  29. # treat argument specified vocabulary
  30. if ( len( args ) > 0 ):
  31. # get vocabularies app
  32. ls_appon = list( args )
  33. for obj_n in queryset:
  34. # check if vocabulary app in vocabulary table exist
  35. if ( str( obj_n.app ) in ls_appon):
  36. ls_queryset.append( str( obj_n.app ) ) # populate queryset list
  37. # treat all vocabularies
  38. else:
  39. # get vocabularies app
  40. ls_appon = glob.glob( "appon*" )
  41. ls_appon.sort()
  42. # reset queryset
  43. self.stdout.write( "Reset vocabulary table" )
  44. for obj_n in queryset:
  45. # check if vocabulary app in vocabulary table exist
  46. if ( str( obj_n.app ) in ls_appon):
  47. ls_queryset.append( str( obj_n.app ) ) # populate queryset list
  48. else:
  49. obj_n.delete() # delete vocabulary out of table
  50. # process vocabulary app
  51. for s_appon in ls_appon:
  52. # python manage.py vocabulary_getupdate s_appon
  53. management.call_command( "vocabulary_loadupdate", s_appon, )
  54. # begin backup handle
  55. self.stdout.write( "\nProcessing vocabulary backup: "+s_appon )
  56. # check process filter
  57. if ( s_appon in ls_apponfilter ):
  58. # don't process this vocabulary
  59. self.stdout.write( "WARNING: vocabulary will not be backuped because it is listed in appsavocabulary/filter_vocabulary.txt: "+s_appon )
  60. else:
  61. # load latest database version
  62. try:
  63. m_appon = importlib.import_module( s_appon+".models" )
  64. for s_name, obj in inspect.getmembers( m_appon ):
  65. if inspect.isclass( obj ):
  66. s_apponmodels = s_name
  67. c_apponmodels = obj # eqivalent to json model
  68. lo_record = c_apponmodels.objects.all( )
  69. self.stdout.write( "Load database version: "+s_apponmodels )
  70. except ImportError:
  71. raise CommandError( "Annot vocabulary app %s does not exist." % s_appon )
  72. # find latest json file backup version
  73. self.stdout.write( "Search latest json file version..." )
  74. obj_n = SysAdminVocabulary.objects.get( app=s_appon )
  75. s_vocabulary = obj_n.vocabulary
  76. s_version_latest = obj_n.version_latest
  77. s_regexfixture = s_appon+"/fixtures/*_"+s_vocabulary+"_"+s_version_latest+"_backup.json"
  78. ls_backuppathfile = glob.glob( s_regexfixture )
  79. i_latest = 0
  80. s_backuplatest = None
  81. for s_backuppathfile in ls_backuppathfile:
  82. s_backupfile = s_backuppathfile.split('/')[-1]
  83. s_date = s_backupfile.split('_')[0]
  84. i_date = int( s_date )
  85. if ( i_latest < i_date ):
  86. s_backuplatest = s_backuppathfile
  87. i_latest = i_date
  88. # generate future backup path file name
  89. s_today = str(datetime.date.today())
  90. s_today = s_today.replace( '-','' )
  91. s_backuptoday = s_appon+"/fixtures/"+s_today+"_"+s_vocabulary+"_"+s_version_latest+"_backup.json"
  92. # if backup found
  93. if not ( s_backuplatest == None ):
  94. # load latest json file backup version
  95. self.stdout.write( "Load latest json file version: "+s_backuplatest )
  96. f_backuplatest = open( s_backuplatest, 'r' ) # open file handle
  97. ld_json = json.load( f_backuplatest )
  98. # compare database content to latest back up contnet
  99. self.stdout.write( "Compare database conetent to lates backup content.")
  100. # check every database version term if it is a member of the latest json file version
  101. for o_record in lo_record :
  102. b_record = False
  103. s_record_term_name = o_record.term_name
  104. s_record_term_id = o_record.term_id
  105. s_record_annot_id = o_record.annot_id
  106. s_record_term_source_version_responsible = o_record.term_source_version_responsible
  107. s_record_term_source_version_update = str( o_record.term_source_version_update )
  108. s_record_term_source_version = o_record.term_source_version
  109. s_record_term_ok = o_record.term_ok
  110. for d_json in ld_json :
  111. # annot_id
  112. s_file_annot_id = d_json["pk"]
  113. if ( s_file_annot_id == s_record_annot_id ):
  114. # term_name
  115. if not ( s_record_term_name == d_json["fields"]["term_name"] ):
  116. b_flag = True
  117. # term_id
  118. if not ( s_record_term_id == d_json["fields"]["term_id"] ):
  119. b_flag = True
  120. # term_source_version_responsible
  121. if not ( s_record_term_source_version_responsible == d_json["fields"]["term_source_version_responsible"] ):
  122. b_flag = True
  123. # term_source_version_update
  124. if not ( s_record_term_source_version_update == d_json["fields"]["term_source_version_update"] ):
  125. b_flag = True
  126. # term_source_version
  127. if not ( s_record_term_source_version == d_json["fields"]["term_source_version"] ):
  128. b_flag = True
  129. # term_ok
  130. if not ( s_record_term_ok == d_json["fields"]["term_ok"] ):
  131. b_flag = True
  132. # record ok
  133. b_record = True
  134. break
  135. if not ( b_record ):
  136. b_flag = True
  137. # check every latest json file version if it is a member of the database version
  138. for d_json in ld_json :
  139. s_file_annot_id = d_json["pk"]
  140. try:
  141. o_record = c_apponmodels.objects.get( annot_id=s_file_annot_id )
  142. except:
  143. b_flag = True
  144. # if no backup file found
  145. else:
  146. self.stdout.write( "No earlyer backup." )
  147. b_flag = True
  148. # generate backupfile
  149. self.stdout.write( "Backup needed? "+str(b_flag) )
  150. if ( b_flag ):
  151. # python manage.py dumpdata s_backuptoday
  152. self.stdout.write( "Generate backup file: "+s_backuptoday )
  153. f_stream = open( s_backuptoday, 'w' ) # encoding="utf-8"
  154. management.call_command( "dumpdata", s_appon, indent=4, format='json',stdout=f_stream )
  155. f_stream.close()
  156. else:
  157. self.stdout.write( "Latest backup version is equivalent to the database version. Backup skiped.")