Commits

Nicola Ferruzzi committed 379cff7

Import categories and boards

Comments (0)

Files changed (1)

djangobb/djangobb_forum/management/commands/djangobb_importsmf.py

 from datetime import datetime
 
 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
+from djangobb_forum.models import Category, Forum
 
 class SMFMembers(object):
     pass
             d = {'ID': c.ID_CAT, 'name': c.name, 'order': c.catOrder}
             yield d
             
-    def boards(self):
+    def forums(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 Importer(object):
+    def __init__(self, source):
+        self.source = source
+        
+    def importForums(self):
+        cats = {}
+        for c in self.source.categories():
+            ca = Category(name=c['name'], position=c['order'])
+            ca.save()
+            cats[c['ID']] = ca
+        for f in self.source.forums():
+            fa = Forum(name=f['name'], description=f['description'], category=cats[f['ID_CATEGORY']])
+            fa.save()
+            
+    
 class Command(BaseCommand):
 
     option_list = BaseCommand.option_list + (
                     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', ),          
+        make_option('--import-forums', action='store_true', dest='import_forums', default=False, 
+                    help=u'Import SMF categories and boards', ),          
         )
         
     help = u'Import SMF 1.1.14 database'
             
         if self.engine != None:
             import pprint
+            
             forum = ForumSMF(self.engine, self.prefix)
-
+            importer = Importer(forum)
+            
             if options['show_tables']:
                 print 'Tables:'
                 for t in forum.tablesName():
                 print 'Categories:'
                 for c in forum.categories():
                     pprint.pprint(c)
+                for f in forum.forums():
+                    pprint.pprint(f)
             
-            if options['import_categories']:
-                imp = Importer(self.session)
-                imp.importCategories()
+            if options['import_forums']:
+                importer.importForums()