]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/sximporter/importer.py
#SPLUNK-91, modifying the permanent link tool, adding an option to copy to the clipbo...
[osqa.git] / forum_modules / sximporter / importer.py
index 8887f8096a6083b4fecaa305b7ad9ad8f316c758..98c9dbd64872a63fdc73ee9d9d8407b7539c4a36 100644 (file)
@@ -6,7 +6,6 @@ import re
 import os
 import gc
 from django.utils.translation import ugettext as _
 import os
 import gc
 from django.utils.translation import ugettext as _
-from orm import orm
 
 from django.utils.encoding import force_unicode
 
 
 from django.utils.encoding import force_unicode
 
@@ -22,6 +21,33 @@ from zlib import compress, decompress
 from xml.sax import make_parser
 from xml.sax.handler import ContentHandler
 
 from xml.sax import make_parser
 from xml.sax.handler import ContentHandler
 
+def create_orm():
+    from django.conf import settings
+    from south.orm import FakeORM
+
+    get_migration_number_re = re.compile(r'^((\d+)_.*)\.py$')
+
+    migrations_folder = os.path.join(settings.SITE_SRC_ROOT, 'forum/migrations')
+
+    highest_number = 0
+    highest_file = None
+
+    for f in os.listdir(migrations_folder):
+        if os.path.isfile(os.path.join(migrations_folder, f)):
+            m = get_migration_number_re.match(f)
+
+            if m:
+                found = int(m.group(2))
+
+                if found > highest_number:
+                    highest_number = found
+                    highest_file = m.group(1)
+
+    mod = __import__('forum.migrations.%s' % highest_file, globals(), locals(), ['forum.migrations'])
+    return FakeORM(getattr(mod, 'Migration'), "forum")
+
+orm = create_orm()
+
 class SXTableHandler(ContentHandler):
     def __init__(self, fname, callback):
         self.in_row = False
 class SXTableHandler(ContentHandler):
     def __init__(self, fname, callback):
         self.in_row = False
@@ -148,9 +174,13 @@ class UnknownYahooUser(UnknownUser):
 
 
 class IdMapper(dict):
 
 
 class IdMapper(dict):
+
+    def __init__(self):
+        self.default = 1
+
     def __getitem__(self, key):
         key = int(key)
     def __getitem__(self, key):
         key = int(key)
-        return super(IdMapper, self).get(key, 1)
+        return super(IdMapper, self).get(key, self.default)
 
     def __setitem__(self, key, value):
         super(IdMapper, self).__setitem__(int(key), int(value))
 
     def __setitem__(self, key, value):
         super(IdMapper, self).__setitem__(int(key), int(value))
@@ -174,15 +204,22 @@ def userimport(path, options):
     #check for empty values
     if not owneruid:
         owneruid = None
     #check for empty values
     if not owneruid:
         owneruid = None
+    else:
+        owneruid = int(owneruid)
 
     def callback(sxu):
         create = True
 
     def callback(sxu):
         create = True
+        set_mapper_defaults = False
 
         if sxu.get('id') == '-1':
             return
         #print "\n".join(["%s : %s" % i for i in sxu.items()])
 
 
         if sxu.get('id') == '-1':
             return
         #print "\n".join(["%s : %s" % i for i in sxu.items()])
 
-        if int(sxu.get('id')) == int(owneruid):
+        if (owneruid and (int(sxu.get('id')) == owneruid)) or (
+            (not owneruid) and len(uidmapper)):
+
+            set_mapper_defaults = True
+
             if authenticated_user:
                 osqau = orm.User.objects.get(id=authenticated_user.id)
 
             if authenticated_user:
                 osqau = orm.User.objects.get(id=authenticated_user.id)
 
@@ -190,12 +227,7 @@ def userimport(path, options):
                     openids.add(assoc.key)
 
                 uidmapper[owneruid] = osqau.id
                     openids.add(assoc.key)
 
                 uidmapper[owneruid] = osqau.id
-                uidmapper[-1] = osqau.id
                 create = False
                 create = False
-            else:
-                uidmapper[owneruid] = int(owneruid)
-                uidmapper[-1] = int(owneruid)
-
 
         sxbadges = sxu.get('badgesummary', None)
         badges = {'1':'0', '2':'0', '3':'0'}
 
         sxbadges = sxu.get('badgesummary', None)
         badges = {'1':'0', '2':'0', '3':'0'}
@@ -294,6 +326,10 @@ def userimport(path, options):
             #merged_users.append(osqau.id)
             osqau.save()
 
             #merged_users.append(osqau.id)
             osqau.save()
 
+        if set_mapper_defaults:
+            uidmapper[-1] = osqau.id
+            uidmapper.default = osqau.id
+
         usernames.append(osqau.username)
 
         openid = sxu.get('openid', None)
         usernames.append(osqau.username)
 
         openid = sxu.get('openid', None)
@@ -310,8 +346,8 @@ def userimport(path, options):
 
     readTable(path, "Users", callback)
 
 
     readTable(path, "Users", callback)
 
-    if uidmapper[-1] == -1:
-        uidmapper[-1] = 1
+    #if uidmapper[-1] == -1:
+    #    uidmapper[-1] = 1
 
     return uidmapper
 
 
     return uidmapper
 
@@ -732,6 +768,7 @@ def badges_import(dump, uidmap, post_list):
 
         osqaa.save()
         badge.awarded_count += 1
 
         osqaa.save()
         badge.awarded_count += 1
+
         user_badge_count[user_id] += 1
 
     readTable(dump, "Users2Badges", callback)
         user_badge_count[user_id] += 1
 
     readTable(dump, "Users2Badges", callback)