Commits

hideki nara  committed ff07db9

creating fixtures for specified app_lable.model_object_name_regex_pattern

  • Participants
  • Parent commits cb65280

Comments (0)

Files changed (1)

File src/mandb/management/commands/db.py

             default='',
             help=u'SQL table name'),
 
+        make_option('--m',
+            action='store',
+            dest='model',
+            default='',
+            help=u'model class signature (app_label.model_name_regex)'),
+
         make_option('--d',
             action='store',
             dest='database',
             default='default',
             help=u'index for settings.DATABASES[database]'),
         
+        make_option('--dir',
+            action='store',
+            dest='directory',
+            default='.fixtures',
+            help=u'directory to save output files'),
+        
     )
 
     def sql(self, statement ):
         for i in self.sql("drop table %s" % option['table']):
             print r[0],i
 
+    def handle_describe(self,*args,**option):
+        for i in self.sql("desc %s" % option['table']):
+            print i
+
     def handle_count(self,*args,**option):
         ''' count record number
         '''
         param = settings.DATABASES[ options['database'] ]
         print "mysql -u %(USER)s --password=%(PASSWORD)s %(NAME)s" % param
 
+    def handle_model_list(self,console=True,*args,**options):
+        ''' モデル一覧
+        '''
+        (app_label,model_name ) = options['model'].split('.') 
+
+        import re
+        from django.db import models as django_models
+        ret = [ m for m in django_models.get_models(
+                    django_models.get_app( app_label  ) )
+#              if model_name in ('*','all',m._meta.object_name)
+                if re.search(r"%s" % model_name, m._meta.object_name )
+            ]
+
+        if console:
+            for r in ret:
+                print r
+        return ret 
+         
+
+    def handle_fixture(self,*args,**options):
+        ''' 指定したモデルのfixtureを作る
+        '''
+        import os
+
+        mcs = self.handle_model_list(console=False,*args,**options)
+        if len(mcs) < 1 or mcs[0] == None:
+            print "no models"
+            print "check --c=model_list --a=app.*"
+            return
+
+        if os.path.isdir(options['directory'] ) == False:
+            os.makedirs(options['directory'] )   
+
+        from django.core import serializers
+        for mc in mcs :
+            for obj in mc.objects.all():
+                fname= os.path.join(options['directory'],
+                        "fixture.%s.%d.json" % (obj._meta.db_table,obj.id) )
+                f= open(fname,"w") 
+                f.write( 
+                    serializers.serialize("json",[obj], 
+#                        ensure_ascii=False,
+                        indent=2).encode('utf8') 
+                )   
+                f.close()    
+
     def handle(self, *args, **options):
         '''  handler
         '''