]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/sximporter/importer.py
Unknown user handling changes.
[osqa.git] / forum_modules / sximporter / importer.py
index fb69930f120b83b7141c76966999601ada1fdf69..5d0c99b4ac81ca83aff9b4b476178a3d669e5750 100644 (file)
@@ -1,11 +1,12 @@
 # -*- coding: utf-8 -*-\r
 \r
 from xml.dom import minidom\r
 # -*- coding: utf-8 -*-\r
 \r
 from xml.dom import minidom\r
-from datetime import datetime\r
+from datetime import datetime, timedelta\r
 import time\r
 import re\r
 from django.utils.translation import ugettext as _\r
 from django.template.defaultfilters import slugify\r
 import time\r
 import re\r
 from django.utils.translation import ugettext as _\r
 from django.template.defaultfilters import slugify\r
+from forum.models.utils import dbsafe_encode\r
 from orm import orm\r
 \r
 def getText(el):\r
 from orm import orm\r
 \r
 def getText(el):\r
@@ -29,14 +30,47 @@ def readEl(el):
 def readTable(dump, name):\r
     return [readEl(e) for e in minidom.parseString(dump.read("%s.xml" % name)).getElementsByTagName('row')]\r
 \r
 def readTable(dump, name):\r
     return [readEl(e) for e in minidom.parseString(dump.read("%s.xml" % name)).getElementsByTagName('row')]\r
 \r
+google_accounts_lookup = re.compile(r'^https?://www.google.com/accounts/')\r
+yahoo_accounts_lookup = re.compile(r'^https?://me.yahoo.com/a/')\r
+\r
+openid_lookups = [\r
+    re.compile(r'^https?://www.google.com/profiles/(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://me.yahoo.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://openid.aol.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).myopenid.com/?$'),\r
+    re.compile(r'^https?://flickr.com/(\w+/)*(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://technorati.com/people/technorati/(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).wordpress.com/?$'),\r
+    re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).blogspot.com/?$'),\r
+    re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).livejournal.com/?$'),\r
+    re.compile(r'^https?://claimid.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).pip.verisignlabs.com/?$'),\r
+    re.compile(r'^https?://getopenid.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://[\w\.]+/(\w+/)*(?P<uname>\w+(\.\w+)*)/?$'),\r
+    re.compile(r'^https?://(?P<uname>[\w\.]+)/?$'),\r
+]\r
+\r
+def final_username_attempt(sxu):\r
+    openid = sxu.get('openid', None)\r
+\r
+    if openid:\r
+        if google_accounts_lookup.search(openid):\r
+            return UnknownGoogleUser(sxu.get('id'))\r
+        if yahoo_accounts_lookup.search(openid):\r
+            return UnknownYahooUser(sxu.get('id'))\r
+\r
+        for lookup in openid_lookups:\r
+            if lookup.search(openid):\r
+                return lookup.search(openid).group('uname')\r
+\r
+    return UnknownUser(sxu.get('id'))\r
+\r
 class UnknownUser(object):\r
 class UnknownUser(object):\r
-    counter = 0\r
-    def __init__(self):\r
-        UnknownUser.counter += 1\r
-        self.number = UnknownUser.counter\r
+    def __init__(self, id):\r
+        self._id = id\r
 \r
     def __str__(self):\r
 \r
     def __str__(self):\r
-        return _("Unknown user %(number)d") % {'number': self.number}\r
+        return _("user-%(id)d") % {'id': self._id}\r
 \r
     def __unicode__(self):\r
         return self.__str__()\r
 \r
     def __unicode__(self):\r
         return self.__str__()\r
@@ -44,10 +78,19 @@ class UnknownUser(object):
     def encode(self, *args):\r
         return self.__str__()\r
 \r
     def encode(self, *args):\r
         return self.__str__()\r
 \r
+class UnknownGoogleUser(UnknownUser):\r
+    def __str__(self):\r
+        return _("user-%(id)d (google)") % {'id': self._id}\r
+\r
+class UnknownYahooUser(UnknownUser):\r
+    def __str__(self):\r
+        return _("user-%(id)d (yahoo)") % {'id': self._id}\r
+\r
+\r
 class IdMapper(dict):\r
     def __getitem__(self, key):\r
         key = int(key)\r
 class IdMapper(dict):\r
     def __getitem__(self, key):\r
         key = int(key)\r
-        return super(IdMapper, self).get(key, key)\r
+        return super(IdMapper, self).get(key, 1)\r
 \r
     def __setitem__(self, key, value):\r
         super(IdMapper, self).__setitem__(int(key), int(value))\r
 \r
     def __setitem__(self, key, value):\r
         super(IdMapper, self).__setitem__(int(key), int(value))\r
@@ -77,19 +120,19 @@ def userimport(dump, options):
             uidmapper[-1] = 1\r
             create = False\r
         else:\r
             uidmapper[-1] = 1\r
             create = False\r
         else:\r
-            username = sxu.get('displayname', sxu.get('displaynamecleaned', sxu.get('realname', UnknownUser())))\r
+            username = sxu.get('displayname', sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu))))\r
 \r
             if not isinstance(username, UnknownUser) and username in user_by_name:\r
 \r
             if not isinstance(username, UnknownUser) and username in user_by_name:\r
-                if options.get('mergesimilar', False) and sxu.get('email', 'INVALID') == user_by_name[username].email:\r
-                    osqau = user_by_name[username]\r
-                    create = False\r
-                    uidmapper[sxu.get('id')] = osqau.id\r
-                else:\r
-                    inc = 1\r
-                    while ("%s %d" % (username, inc)) in user_by_name:\r
-                        inc += 1\r
+                #if options.get('mergesimilar', False) and sxu.get('email', 'INVALID') == user_by_name[username].email:\r
+                #    osqau = user_by_name[username]\r
+                #    create = False\r
+                #    uidmapper[sxu.get('id')] = osqau.id\r
+                #else:\r
+                inc = 1\r
+                while ("%s %d" % (username, inc)) in user_by_name:\r
+                    inc += 1\r
 \r
 \r
-                    username = "%s %d" % (username, inc)\r
+                username = "%s %d" % (username, inc)\r
 \r
         sxbadges = sxu.get('badgesummary', None)\r
         badges = {'1':'0','2':'0','3':'0'}\r
 \r
         sxbadges = sxu.get('badgesummary', None)\r
         badges = {'1':'0','2':'0','3':'0'}\r
@@ -107,6 +150,7 @@ def userimport(dump, options):
                 is_staff     = sxu.get('usertypeid') == '4',\r
                 is_active    = True,\r
                 date_joined  = readTime(sxu.get('creationdate')),\r
                 is_staff     = sxu.get('usertypeid') == '4',\r
                 is_active    = True,\r
                 date_joined  = readTime(sxu.get('creationdate')),\r
+                last_seen    = readTime(sxu.get('lastaccessdate')),\r
                 about         = sxu.get('aboutme', ''),\r
                 date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None,\r
                 email_isvalid = int(sxu.get('usertypeid')) > 2,\r
                 about         = sxu.get('aboutme', ''),\r
                 date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None,\r
                 email_isvalid = int(sxu.get('usertypeid')) > 2,\r
@@ -120,10 +164,28 @@ def userimport(dump, options):
 \r
             osqau.save()\r
 \r
 \r
             osqau.save()\r
 \r
-            s = orm.SubscriptionSettings(user=osqau)\r
-            s.save()\r
+            user_joins = orm.Action(\r
+                action_type = "userjoins",\r
+                action_date = osqau.date_joined,\r
+                user = osqau\r
+            )\r
+            user_joins.save()\r
+\r
+            rep = orm.ActionRepute(\r
+                value = 1,\r
+                user = osqau,\r
+                date = osqau.date_joined,\r
+                action = user_joins\r
+            )\r
+            rep.save()            \r
+\r
+            try:\r
+                orm.SubscriptionSettings.objects.get(user=osqau)\r
+            except:\r
+                s = orm.SubscriptionSettings(user=osqau)\r
+                s.save()\r
 \r
 \r
-            user_by_name[osqau.username] = osqau\r
+            uidmapper[osqau.id] = osqau.id\r
         else:\r
             new_about = sxu.get('aboutme', None)\r
             if new_about and osqau.about != new_about:\r
         else:\r
             new_about = sxu.get('aboutme', None)\r
             if new_about and osqau.about != new_about:\r
@@ -142,6 +204,7 @@ def userimport(dump, options):
             merged_users.append(osqau.id)\r
             osqau.save()\r
 \r
             merged_users.append(osqau.id)\r
             osqau.save()\r
 \r
+        user_by_name[osqau.username] = osqau\r
 \r
         openid = sxu.get('openid', None)\r
         if openid and openidre.match(openid):\r
 \r
         openid = sxu.get('openid', None)\r
         if openid and openidre.match(openid):\r
@@ -163,7 +226,7 @@ def tagsimport(dump, uidmap):
             id = int(sxtag['id']),\r
             name = sxtag['name'],\r
             used_count = int(sxtag['count']),\r
             id = int(sxtag['id']),\r
             name = sxtag['name'],\r
             used_count = int(sxtag['count']),\r
-            created_by_id = uidmap[sxtag['userid']],\r
+            created_by_id = uidmap[sxtag.get('userid', 1)],\r
         )\r
         otag.save()\r
 \r
         )\r
         otag.save()\r
 \r
@@ -173,6 +236,7 @@ def tagsimport(dump, uidmap):
 \r
 def postimport(dump, uidmap, tagmap):\r
     history = {}\r
 \r
 def postimport(dump, uidmap, tagmap):\r
     history = {}\r
+    accepted = {}\r
     all = {}\r
 \r
     for h in readTable(dump, "PostHistory"):\r
     all = {}\r
 \r
     for h in readTable(dump, "PostHistory"):\r
@@ -184,34 +248,68 @@ def postimport(dump, uidmap, tagmap):
     posts = readTable(dump, "Posts")\r
 \r
     for sxpost in posts:\r
     posts = readTable(dump, "Posts")\r
 \r
     for sxpost in posts:\r
-        accepted = {}\r
+        nodetype = (sxpost.get('posttypeid') == '1') and "nodetype" or "answer"\r
 \r
 \r
-        postclass = sxpost.get('posttypeid') == '1' and orm.Question or orm.Answer\r
-\r
-        post = postclass(\r
+        post = orm.Node(\r
+            node_type = nodetype,\r
             id = sxpost['id'],\r
             added_at = readTime(sxpost['creationdate']),\r
             body = sxpost['body'],\r
             score = sxpost.get('score', 0),\r
             id = sxpost['id'],\r
             added_at = readTime(sxpost['creationdate']),\r
             body = sxpost['body'],\r
             score = sxpost.get('score', 0),\r
-            vote_up_count = 0,\r
-            vote_down_count = 0\r
+            author_id = sxpost.get('deletiondate', None) and 1 or uidmap[sxpost['owneruserid']]\r
         )\r
 \r
         )\r
 \r
-        if sxpost.get('deletiondate', None):\r
-            post.deleted = True\r
-            post.deleted_at = readTime(sxpost['deletiondate'])\r
-            post.author_id = 1\r
-        else:\r
-            post.author_id = uidmap[sxpost['owneruserid']]\r
+        post.save()\r
+\r
+        create_action = orm.Action(\r
+            action_type = (nodetype == "nodetype") and "ask" or "answer",\r
+            user_id = post.author_id,\r
+            node = post,\r
+            action_date = post.added_at\r
+        )\r
+\r
+        create_action.save()\r
+\r
+        #if sxpost.get('deletiondate', None):\r
+        #    delete_action = orm.Action(\r
+        #        action_type = "delete",\r
+        #        user_id = 1,\r
+        #        node = post,\r
+        #        action_date = readTime(sxpost['deletiondate'])\r
+        #    )\r
+\r
+        #    delete_action.save()\r
+        #    post.deleted = delete_action\r
 \r
         if sxpost.get('lasteditoruserid', None):\r
 \r
         if sxpost.get('lasteditoruserid', None):\r
-            post.last_edited_by_id = uidmap[sxpost.get('lasteditoruserid')]\r
-            post.last_edited_at = readTime(sxpost['lasteditdate'])\r
+            revise_action = orm.Action(\r
+                action_type = "revise",\r
+                user_id = uidmap[sxpost.get('lasteditoruserid')],\r
+                node = post,\r
+                action_date = readTime(sxpost['lasteditdate']),\r
+            )\r
+\r
+            revise_action.save()\r
+            post.last_edited = revise_action\r
 \r
         if sxpost.get('communityowneddate', None):\r
             post.wiki = True\r
 \r
         if sxpost.get('communityowneddate', None):\r
             post.wiki = True\r
-            post.wikified_at = readTime(sxpost['communityowneddate'])\r
 \r
 \r
+            wikify_action = orm.Action(\r
+                action_type = "wikify",\r
+                user_id = 1,\r
+                node = post,\r
+                action_date = readTime(sxpost['communityowneddate'])\r
+            )\r
+\r
+            wikify_action.save()\r
+\r
+\r
+        if sxpost.get('lastactivityuserid', None):\r
+            post.last_activity_by_id = uidmap[sxpost['lastactivityuserid']]\r
+            post.last_activity_at = readTime(sxpost['lastactivitydate'])\r
+\r
+            \r
         if sxpost.get('posttypeid') == '1': #question\r
             post.node_type = "question"\r
             post.title = sxpost['title']\r
         if sxpost.get('posttypeid') == '1': #question\r
             post.node_type = "question"\r
             post.title = sxpost['title']\r
@@ -219,34 +317,49 @@ def postimport(dump, uidmap, tagmap):
             tagnames = sxpost['tags'].replace(u'ö', '-').replace(u'é', '').replace(u'à', '')\r
             post.tagnames = tagnames\r
 \r
             tagnames = sxpost['tags'].replace(u'ö', '-').replace(u'é', '').replace(u'à', '')\r
             post.tagnames = tagnames\r
 \r
-            post.view_count = sxpost.get('viewcount', 0)\r
-            post.favourite_count = sxpost.get('favoritecount', 0)\r
-            post.answer_count = sxpost.get('answercount', 0)\r
+            post.extra_count = sxpost.get('viewcount', 0)\r
 \r
 \r
-            if sxpost.get('lastactivityuserid', None):\r
-                post.last_activity_by_id = uidmap[sxpost['lastactivityuserid']]\r
-                post.last_activity_at = readTime(sxpost['lastactivitydate'])\r
+            #if sxpost.get('closeddate', None):\r
+            #    post.marked = True\r
+            #\r
+            #    close_action = orm.Action(\r
+            #        action_type = "close",\r
+            #        user_id = 1,\r
+            #        node = post,\r
+            #        action_date = datetime.now() - timedelta(days=7)\r
+            #    )\r
+            #\r
+            #    close_action.save()\r
+            #    post.extra_action = close_action\r
 \r
 \r
-            if sxpost.get('closeddate', None):\r
-                post.closed = True\r
-                post.closed_by_id = 1\r
-                post.closed_at = datetime.now()\r
+            #if sxpost.get('acceptedanswerid', None):\r
+            #    accepted[int(sxpost.get('acceptedanswerid'))] = post\r
 \r
 \r
-            if sxpost.get('acceptedanswerid', None):\r
-                accepted[int(sxpost.get('acceptedanswerid'))] = post\r
+            #post.save()\r
 \r
         else:\r
 \r
         else:\r
-            post.node_type = "answer"\r
             post.parent_id = sxpost['parentid']\r
 \r
             post.parent_id = sxpost['parentid']\r
 \r
-            if int(post.id) in accepted:\r
-                question = accepted[int(post.id)]\r
-                question.accepted_answer_id = post\r
-                question.save()\r
+            #if int(post.id) in accepted:\r
+                #post.marked = True\r
+\r
+                #accept_action = orm.Action(\r
+                #    action_type = "acceptanswer",\r
+                #    user_id = accepted[int(post.id)].author_id,\r
+                #    node = post,\r
+                #    action_date = datetime.now() - timedelta(days=7)\r
+                #)\r
+\r
+                #accept_action.save()\r
 \r
 \r
-                post.accepted = True\r
-                post.accepted_at = datetime.now()\r
-                post.accepted_by_id = question.author_id\r
+\r
+                #post.accepted_at = datetime.now()\r
+                #post.accepted_by_id = accepted[int(post.id)].author_id\r
+\r
+                #accepted[int(post.id)].extra_ref = post\r
+                #accepted[int(post.id)].save()\r
+\r
+        post.save()\r
 \r
         all[int(post.id)] = post\r
 \r
 \r
         all[int(post.id)] = post\r
 \r
@@ -263,21 +376,38 @@ def comment_import(dump, uidmap, posts):
             id = currid,\r
             node_type = "comment",\r
             added_at = readTime(sxc['creationdate']),\r
             id = currid,\r
             node_type = "comment",\r
             added_at = readTime(sxc['creationdate']),\r
-            author_id = uidmap[sxc['userid']],\r
+            author_id = uidmap[sxc.get('userid', 1)],\r
             body = sxc['text'],\r
             parent_id = sxc.get('postid'),\r
             body = sxc['text'],\r
             parent_id = sxc.get('postid'),\r
-            vote_up_count = 0,\r
-            vote_down_count = 0\r
         )\r
 \r
         if sxc.get('deletiondate', None):\r
         )\r
 \r
         if sxc.get('deletiondate', None):\r
-            oc.deleted = True\r
-            oc.deleted_at = readTime(sxc['deletiondate'])\r
-            oc.deleted_by_id = uidmap[sxc['deletionuserid']]\r
+            delete_action = orm.Action(\r
+                action_type = "delete",\r
+                user_id = uidmap[sxc['deletionuserid']],\r
+                action_date = readTime(sxc['deletiondate'])\r
+            )\r
+\r
             oc.author_id = uidmap[sxc['deletionuserid']]\r
             oc.author_id = uidmap[sxc['deletionuserid']]\r
+            oc.save()\r
+\r
+            delete_action.node = oc\r
+            delete_action.save()\r
+\r
+            oc.deleted = delete_action\r
         else:\r
         else:\r
-            oc.author_id = uidmap[sxc['userid']]\r
+            oc.author_id = uidmap[sxc.get('userid', 1)]\r
+            oc.save()\r
+\r
+        create_action = orm.Action(\r
+            action_type = "comment",\r
+            user_id = oc.author_id,\r
+            node = oc,\r
+            action_date = oc.added_at\r
+        )\r
 \r
 \r
+        create_action.save()\r
+        oc.save()\r
 \r
         posts[oc.id] = oc\r
         mapping[int(sxc['id'])] = int(oc.id)\r
 \r
         posts[oc.id] = oc\r
         mapping[int(sxc['id'])] = int(oc.id)\r
@@ -285,12 +415,10 @@ def comment_import(dump, uidmap, posts):
     return posts, mapping\r
 \r
 \r
     return posts, mapping\r
 \r
 \r
-def save_posts(posts, tagmap):\r
+def add_tags_to_posts(posts, tagmap):\r
     for post in posts.values():\r
     for post in posts.values():\r
-        post.save()\r
-\r
         if post.node_type == "question":\r
         if post.node_type == "question":\r
-            tags = filter(lambda t: t is not None, [tagmap.get(n, None) for n in post.tagnames.split()])\r
+            tags = [tag for tag in [tagmap.get(name.strip()) for name in post.tagnames.split(u' ') if name] if tag]\r
             post.tagnames = " ".join([t.name for t in tags]).strip()\r
             post.tags = tags\r
 \r
             post.tagnames = " ".join([t.name for t in tags]).strip()\r
             post.tags = tags\r
 \r
@@ -313,64 +441,182 @@ def create_and_activate_revision(post):
     post.active_revision_id = rev.id\r
     post.save()\r
 \r
     post.active_revision_id = rev.id\r
     post.save()\r
 \r
-\r
 def post_vote_import(dump, uidmap, posts):\r
     votes = readTable(dump, "Posts2Votes")\r
 def post_vote_import(dump, uidmap, posts):\r
     votes = readTable(dump, "Posts2Votes")\r
+    close_reasons = dict([(r['id'], r['name']) for r in readTable(dump, "CloseReasons")])\r
+\r
+    user2vote = []\r
 \r
     for sxv in votes:\r
 \r
     for sxv in votes:\r
-        if sxv['votetypeid'] in ('2', '3'):\r
-            ov = orm.Vote(\r
-                node_id = sxv['postid'],\r
+        action = orm.Action(\r
+            user_id=uidmap[sxv['userid']],\r
+            action_date = readTime(sxv['creationdate']),\r
+        )\r
+\r
+        node = posts.get(int(sxv['postid']), None)\r
+        if not node: continue\r
+        action.node = node\r
+\r
+        if sxv['votetypeid'] == '1':\r
+            answer = node\r
+            question = posts.get(int(answer.parent_id), None)\r
+\r
+            action.action_type = "acceptanswer"\r
+            action.save()\r
+\r
+            answer.marked = True\r
+            answer.extra_action = action\r
+\r
+            question.extra_ref_id = answer.id\r
+\r
+            answer.save()\r
+            question.save()\r
+\r
+        elif sxv['votetypeid'] in ('2', '3'):\r
+            if not (action.node.id, action.user_id) in user2vote:\r
+                user2vote.append((action.node.id, action.user_id))\r
+\r
+                action.action_type = (sxv['votetypeid'] == '2') and "voteup" or "votedown"\r
+                action.save()\r
+\r
+                ov = orm.Vote(\r
+                    node_id = action.node.id,\r
+                    user_id = action.user_id,\r
+                    voted_at = action.action_date,\r
+                    value = sxv['votetypeid'] == '2' and 1 or -1,\r
+                    action = action\r
+                )\r
+                ov.save()\r
+            else:\r
+                action.action_type = "unknown"\r
+                action.save()\r
+\r
+        elif sxv['votetypeid'] in ('4', '12', '13'):\r
+            action.action_type = "flag"\r
+            action.save()\r
+\r
+            of = orm.Flag(\r
+                node = action.node,\r
+                user_id = action.user_id,\r
+                flagged_at = action.action_date,\r
+                reason = '',\r
+                action = action\r
+            )\r
+\r
+            of.save()\r
+\r
+        elif sxv['votetypeid'] == '5':\r
+            action.action_type = "favorite"\r
+            action.save()\r
+\r
+        elif sxv['votetypeid'] == '6':\r
+            action.action_type = "close"\r
+            action.extra = dbsafe_encode(close_reasons[sxv['comment']])\r
+            action.save()\r
+\r
+            node.marked = True\r
+            node.extra_action = action\r
+            node.save()\r
+\r
+        elif sxv['votetypeid'] == '7':\r
+            action.action_type = "unknown"\r
+            action.save()\r
+            \r
+            node.marked = False\r
+            node.extra_action = None\r
+            node.save()\r
+\r
+        elif sxv['votetypeid'] == '10':\r
+            action.action_type = "delete"\r
+            action.save()\r
+\r
+            node.deleted = action\r
+            node.save()\r
+\r
+        elif sxv['votetypeid'] == '11':\r
+            action.action_type = "unknown"\r
+            action.save()\r
+\r
+            node.deleted = None\r
+            node.save()\r
+\r
+        else:\r
+            action.action_type = "unknown"\r
+            action.save()\r
+\r
+\r
+        if sxv.get('targetrepchange', None):\r
+            rep = orm.ActionRepute(\r
+                action = action,\r
+                date = action.action_date,\r
+                user_id = uidmap[sxv['targetuserid']],\r
+                value = int(sxv['targetrepchange'])\r
+            )\r
+\r
+            rep.save()\r
+\r
+        if sxv.get('voterrepchange', None):\r
+            rep = orm.ActionRepute(\r
+                action = action,\r
+                date = action.action_date,\r
                 user_id = uidmap[sxv['userid']],\r
                 user_id = uidmap[sxv['userid']],\r
-                voted_at = readTime(sxv['creationdate']),\r
-                vote = sxv['votetypeid'] == '2' and 1 or -1,\r
+                value = int(sxv['voterrepchange'])\r
             )\r
 \r
             )\r
 \r
-            if sxv['votetypeid'] == '2':\r
-                posts[int(sxv['postid'])].vote_up_count += 1\r
-            else:\r
-                posts[int(sxv['postid'])].vote_down_count += 1\r
+            rep.save()\r
 \r
 \r
-            ov.save()\r
 \r
 def comment_vote_import(dump, uidmap, comments, posts):\r
     votes = readTable(dump, "Comments2Votes")\r
 \r
 def comment_vote_import(dump, uidmap, comments, posts):\r
     votes = readTable(dump, "Comments2Votes")\r
+    user2vote = []\r
 \r
     for sxv in votes:\r
 \r
     for sxv in votes:\r
-        if sxv['votetypeid'] in ('2', '3'):\r
-            ov = orm.Vote(\r
-                node_id = comments[int(sxv['postcommentid'])],\r
-                user_id = uidmap[sxv['userid']],\r
-                voted_at = readTime(sxv['creationdate']),\r
-                vote = sxv['votetypeid'] == '2' and 1 or -1,\r
-            )\r
+        if sxv['votetypeid'] == "2":\r
+            comment_id = comments[int(sxv['postcommentid'])]\r
+            user_id = uidmap[sxv['userid']]\r
 \r
 \r
-            if sxv['votetypeid'] == '2':\r
-                posts[comments[int(sxv['postcommentid'])]].vote_up_count += 1\r
-            else:\r
-                posts[comments[int(sxv['postcommentid'])]].vote_down_count += 1\r
+            if not (comment_id, user_id) in user2vote:\r
+                user2vote.append((comment_id, user_id))\r
+\r
+                action = orm.Action(\r
+                    action_type = "voteupcomment",\r
+                    user_id = user_id,\r
+                    action_date = readTime(sxv['creationdate']),\r
+                    node_id = comment_id\r
+                )\r
+                action.save()\r
+\r
+                ov = orm.Vote(\r
+                    node_id = comment_id,\r
+                    user_id = user_id,\r
+                    voted_at = action.action_date,\r
+                    value = 1,\r
+                    action = action\r
+                )\r
 \r
 \r
-            ov.save()\r
+                ov.save()\r
 \r
 \r
+                posts[int(action.node_id)].score += 1\r
+                posts[int(action.node_id)].save()\r
 \r
 \r
 \r
 \r
-def badges_import(dump, uidmap):\r
+\r
+def badges_import(dump, uidmap, post_list):\r
     node_ctype = orm['contenttypes.contenttype'].objects.get(name='node')\r
     node_ctype = orm['contenttypes.contenttype'].objects.get(name='node')\r
-    obadges = dict([(b.slug, b) for b in orm.Badge.objects.all()])\r
+    obadges = dict([(b.cls, b) for b in orm.Badge.objects.all()])\r
     sxbadges = dict([(int(b['id']), b) for b in readTable(dump, "Badges")])\r
     sxbadges = dict([(int(b['id']), b) for b in readTable(dump, "Badges")])\r
+    user_badge_count = {}\r
 \r
     sx_to_osqa = {}\r
 \r
     for id, sxb in sxbadges.items():\r
 \r
     sx_to_osqa = {}\r
 \r
     for id, sxb in sxbadges.items():\r
-        slug = slugify(sxb['name'].replace('&', 'and'))\r
-        if slug in obadges:\r
-            sx_to_osqa[id] = obadges[slug]\r
+        cls = "".join(sxb['name'].replace('&', 'And').split(' '))\r
+\r
+        if cls in obadges:\r
+            sx_to_osqa[id] = obadges[cls]\r
         else:\r
             osqab = orm.Badge(\r
         else:\r
             osqab = orm.Badge(\r
-                name = sxb['name'],\r
-                slug = slugify(sxb['name']),\r
-                description = sxb['description'],\r
-                multiple = sxb.get('single', 'false') == 'false',\r
+                cls = cls,\r
                 awarded_count = 0,\r
                 type = sxb['class']                \r
             )\r
                 awarded_count = 0,\r
                 type = sxb['class']                \r
             )\r
@@ -382,21 +628,33 @@ def badges_import(dump, uidmap):
 \r
     for sxa in sxawards:\r
         badge = sx_to_osqa[int(sxa['badgeid'])]\r
 \r
     for sxa in sxawards:\r
         badge = sx_to_osqa[int(sxa['badgeid'])]\r
+\r
+        user_id = uidmap[sxa['userid']]\r
+        if not user_badge_count.get(user_id, None):\r
+            user_badge_count[user_id] = 0\r
+\r
+        action = orm.Action(\r
+            action_type = "award",\r
+            user_id = user_id,\r
+            action_date = readTime(sxa['date'])\r
+        )\r
+\r
+        action.save()\r
+\r
         osqaa = orm.Award(\r
             user_id = uidmap[sxa['userid']],\r
             badge = badge,\r
         osqaa = orm.Award(\r
             user_id = uidmap[sxa['userid']],\r
             badge = badge,\r
-            content_type = node_ctype,\r
-            object_id = 1\r
+            node = post_list[user_badge_count[user_id]],\r
+            awarded_at = action.action_date,\r
+            action = action\r
         )\r
 \r
         )\r
 \r
-        osqaawards.append(osqaa)\r
+        osqaa.save()\r
         badge.awarded_count += 1\r
         badge.awarded_count += 1\r
+        user_badge_count[user_id] += 1\r
 \r
 \r
-    for b in sx_to_osqa.values():\r
-        b.save()\r
-\r
-    for a in osqaawards:\r
-        a.save()\r
+    for badge in obadges.values():\r
+        badge.save()\r
 \r
 \r
 def reset_sequences():\r
 \r
 \r
 def reset_sequences():\r
@@ -411,12 +669,10 @@ def sximport(dump, options):
     tagmap = tagsimport(dump, uidmap)\r
     posts = postimport(dump, uidmap, tagmap)\r
     posts, comments = comment_import(dump, uidmap, posts)\r
     tagmap = tagsimport(dump, uidmap)\r
     posts = postimport(dump, uidmap, tagmap)\r
     posts, comments = comment_import(dump, uidmap, posts)\r
-    save_posts(posts, tagmap)\r
+    add_tags_to_posts(posts, tagmap)\r
     post_vote_import(dump, uidmap, posts)\r
     comment_vote_import(dump, uidmap, comments, posts)\r
     post_vote_import(dump, uidmap, posts)\r
     comment_vote_import(dump, uidmap, comments, posts)\r
-    for post in posts.values():\r
-        post.save()\r
-    badges_import(dump, uidmap)\r
+    badges_import(dump, uidmap, posts.values())\r
 \r
     from south.db import db\r
     db.commit_transaction()\r
 \r
     from south.db import db\r
     db.commit_transaction()\r
@@ -426,31 +682,30 @@ def sximport(dump, options):
     \r
     \r
 PG_SEQUENCE_RESETS = """\r
     \r
     \r
 PG_SEQUENCE_RESETS = """\r
+SELECT setval('"auth_user_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user";\r
 SELECT setval('"auth_user_groups_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_groups";\r
 SELECT setval('"auth_user_user_permissions_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_user_permissions";\r
 SELECT setval('"auth_user_groups_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_groups";\r
 SELECT setval('"auth_user_user_permissions_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_user_permissions";\r
-SELECT setval('"activity_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "activity";\r
-SELECT setval('"forum_subscriptionsettings_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_subscriptionsettings";\r
-SELECT setval('"forum_validationhash_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_validationhash";\r
-SELECT setval('"forum_authkeyuserassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_authkeyuserassociation";\r
-SELECT setval('"tag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "tag";\r
-SELECT setval('"forum_markedtag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_markedtag";\r
-SELECT setval('"forum_node_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node";\r
-SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
-SELECT setval('"forum_noderevision_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_noderevision";\r
-SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
-SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
-SELECT setval('"favorite_question_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "favorite_question";\r
-SELECT setval('"forum_questionsubscription_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_questionsubscription";\r
-SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
-SELECT setval('"vote_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "vote";\r
-SELECT setval('"flagged_item_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "flagged_item";\r
-SELECT setval('"badge_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "badge";\r
-SELECT setval('"award_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "award";\r
-SELECT setval('"repute_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "repute";\r
-SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
-SELECT setval('"forum_keyvalue_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_keyvalue";\r
-SELECT setval('"forum_openidnonce_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidnonce";\r
-SELECT setval('"forum_openidassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidassociation";\r
+SELECT setval('"forum_keyvalue_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_keyvalue";\r
+SELECT setval('"forum_action_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_action";\r
+SELECT setval('"forum_actionrepute_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_actionrepute";\r
+SELECT setval('"forum_subscriptionsettings_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_subscriptionsettings";\r
+SELECT setval('"forum_validationhash_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_validationhash";\r
+SELECT setval('"forum_authkeyuserassociation_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_authkeyuserassociation";\r
+SELECT setval('"forum_tag_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_tag";\r
+SELECT setval('"forum_markedtag_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_markedtag";\r
+SELECT setval('"forum_node_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_node";\r
+SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_node_tags";\r
+SELECT setval('"forum_noderevision_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_noderevision";\r
+SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_node_tags";\r
+SELECT setval('"forum_questionsubscription_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_questionsubscription";\r
+SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_node_tags";\r
+SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_node_tags";\r
+SELECT setval('"forum_vote_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_vote";\r
+SELECT setval('"forum_flag_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_flag";\r
+SELECT setval('"forum_badge_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_badge";\r
+SELECT setval('"forum_award_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_award";\r
+SELECT setval('"forum_openidnonce_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_openidnonce";\r
+SELECT setval('"forum_openidassociation_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "forum_openidassociation";\r
 """\r
 \r
 \r
 """\r
 \r
 \r