1. Nicola Ferruzzi
  2. DjangoBB_SMF

Commits

Nicola Ferruzzi  committed f2d5880

Use a class for each kind of forums, the api is based on dictionary and generators

  • Participants
  • Parent commits 61554a8
  • Branches default

Comments (0)

Files changed (1)

File djangobb/djangobb_forum/management/commands/djangobb_importsmf.py

View file
  • Ignore whitespace
 from django.core.management.base import BaseCommand, CommandError
 from django.contrib.auth.models import User
 from djangobb_forum.models import Ban
+from djangobb_forum.models import Category
 
 class SMFMembers(object):
     pass
 class SMFCategories(object):
     pass
 
-class Command(BaseCommand):
 
-    option_list = BaseCommand.option_list + (
-        make_option('--engine', action='store', dest='engine', default=False, 
-                    help=u'SMF DB (ie. mysql://user:password@host/database)'),
-        make_option('--prefix', action='store', dest='smf', default=False, 
-                    help=u'SMF DB prefix (ie. smf_)', ),
-        make_option('--show-tables', action='store_true', dest='show_tables', default=False, 
-                    help=u'Show SMF tables', ),
-        make_option('--show-columns', action='store', dest='show_columns', default=False, 
-                    help=u'Show columns of an SMF table (ie. members)', ),
-        make_option('--show-members', action='store_true', dest='show_members', default=False, 
-                    help=u'Show SMF users', ),        
-        make_option('--show-categories', action='store_true', dest='show_categories', default=False, 
-                    help=u'Show SMF categories', ),                
+class ForumSMF(object):
+    
+    def __init__(self, engine_url, prefix):
+        self.engine_url = engine_url
+        self.prefix = prefix
+        self.connectToSMF()
         
-        )
-        
-    help = u'Import SMF 1.1.14 database'
-
-    def handle(self, *args, **options):
-        self.engine = "mysql://nferruzzi:pippo@localhost/playfields?charset=utf8"
-        self.prefix = "smf_"
-        
-        if options['engine']:
-            print 'Engine', options['engine']
-            try:
-                import sqlalchemy
-                import sqlalchemy.orm
-            except ImportError:
-                raise CommandError, 'Import SMF requires sqlalchemy module'
-            self.engine = options['engine']
-        elif options['smf']:
-            print 'Prefix', options['prefix']
-            self.prefix = options['prefix']
-            
-        if self.engine != None:
-            self.connectToSMF()
-            if options['show_tables']:
-                self.showTables()
-            if options['show_columns']:
-                self.showColumns(options['show_columns'])
-            self.createSession()
-            if options['show_members']:
-                self.showMembers()
-            if options['show_categories']:
-                self.showCategories()
-            
     def connectToSMF(self):
-        import sqlalchemy
-        self.engine = sqlalchemy.create_engine(self.engine)
+        import sqlalchemy.orm
+        self.engine = sqlalchemy.create_engine(self.engine_url)
         self.meta = sqlalchemy.MetaData(self.engine, True)
 
     def createSession(self):
             })
             
         sqlalchemy.orm.mapper(SMFCategories, self.tcategories)
-        session = sqlalchemy.orm.sessionmaker(self.engine)
-        self.session = session()
+        self.sessionMaker = sqlalchemy.orm.sessionmaker(self.engine)
+        self.session = self.sessionMaker()
+        
+    def tablesName(self):
+        for k in self.meta.tables.keys():
+            yield k
 
-    def showTables(self):
-        print 'Tables:'
-        for k in self.meta.tables.keys():
-            print '\t',k
+    def columnsName(self, table):
+        table = self.prefix + table
+        for k in self.meta.tables[table].c.keys():
+            yield k
 
-    def showColumns(self, table):
-        table = self.prefix + table
-        print 'Columns of:', table
-        for k in self.meta.tables[table].c.keys():
-            print '\t',k
+    def members(self):
+        for m in self.session.query(SMFMembers).all():            
+            d = {'ID': m.ID_MEMBER, 
+                 'name': m.realName,
+                 'nickname': m.memberName,
+                 'email': m.emailAddress,
+                 'password':None,
+                 'signature': m.signature,
+                 'isAdmin': m.ID_GROUP == 1,
+                 }
+            yield d
+            
+    def categories(self):
+        for c in self.session.query(SMFCategories).all():
+            d = {'ID': c.ID_CAT, 'name': c.name, 'order': c.catOrder}
+            yield d
+            
+    def boards(self):
+        for c in self.session.query(SMFCategories).all():
+            for b in c.boards:
+                d = {'ID': b.ID_BOARD, 'ID_CATEGORY': b.ID_CAT, 'name': b.name, 'order': b.boardOrder, 'description': b.description}
+                yield d
+                
+            
+class Command(BaseCommand):
 
-    def showMembers(self):
-        print 'Members:'
-        for u in self.session.query(SMFMembers).all():
-            if u.ID_GROUP == 1:
-                print '[ADMIN]',
-            print '\t', u.ID_MEMBER, u.memberName, '->', u.realName    
+    option_list = BaseCommand.option_list + (
+        make_option('--engine', action='store', dest='engine', default=False, 
+                    help=u'SMF DB (ie. mysql://user:password@host/database)'),
+        make_option('--prefix', action='store', dest='smf', default=False, 
+                    help=u'SMF DB prefix (ie. smf_)', ),
+        make_option('--show-tables', action='store_true', dest='show_tables', default=False, 
+                    help=u'Show SMF tables', ),
+        make_option('--show-columns', action='store', dest='show_columns', default=False, 
+                    help=u'Show columns of an SMF table (ie. members)', ),
+        make_option('--show-members', action='store_true', dest='show_members', default=False, 
+                    help=u'Show SMF users', ),        
+        make_option('--show-categories', action='store_true', dest='show_categories', default=False, 
+                    help=u'Show SMF categories', ),                
+        make_option('--import-categories', action='store_true', dest='import_categories', default=False, 
+                    help=u'Import SMF categories', ),          
+        )
+        
+    help = u'Import SMF 1.1.14 database'
+
+    def handle(self, *args, **options):
+        self.engine = "mysql://nferruzzi:pippo@localhost/playfields?charset=utf8"
+        self.prefix = "smf_"
+        
+        if options['engine']:
+            print 'Engine', options['engine']
+            try:
+                import sqlalchemy
+                import sqlalchemy.orm
+            except ImportError:
+                raise CommandError, 'Import SMF requires sqlalchemy module'
+            self.engine = options['engine']
+        elif options['smf']:
+            print 'Prefix', options['prefix']
+            self.prefix = options['prefix']
             
-    def showCategories(self):
-        print 'Categories:'
-        for c in self.session.query(SMFCategories).all():
-            print '\t', c.ID_CAT, c.name
-            for b in c.boards:
-                print '\t\t', b.name
-        
+        if self.engine != None:
+            import pprint
+            forum = ForumSMF(self.engine, self.prefix)
+
+            if options['show_tables']:
+                print 'Tables:'
+                for t in forum.tablesName():
+                    pprint.pprint(t)
+                                
+            if options['show_columns']:
+                print 'Columns:'
+                for c in forum.columnsName(options['show_columns']):
+                    pprint.pprint(c)
+
+            forum.createSession()
+            
+            if options['show_members']:
+                print 'Members:'
+                for m in forum.members():
+                    pprint.pprint(m)
+                
+            if options['show_categories']:
+                print 'Categories:'
+                for c in forum.categories():
+                    pprint.pprint(c)
+            
+            if options['import_categories']:
+                imp = Importer(self.session)
+                imp.importCategories()
+    
+