]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/sximporter/importer.py
Some fixes in the importer.
[osqa.git] / forum_modules / sximporter / importer.py
1 # -*- coding: utf-8 -*-\r
2 \r
3 from xml.dom import minidom\r
4 from datetime import datetime, timedelta\r
5 import time\r
6 import re\r
7 from django.utils.translation import ugettext as _\r
8 from django.template.defaultfilters import slugify\r
9 from forum.models.utils import dbsafe_encode\r
10 from orm import orm\r
11 \r
12 def getText(el):\r
13     rc = ""\r
14     for node in el.childNodes:\r
15         if node.nodeType == node.TEXT_NODE:\r
16             rc = rc + node.data\r
17     return rc.strip()\r
18 \r
19 msstrip = re.compile(r'^(.*)\.\d+')\r
20 def readTime(ts):\r
21     noms = msstrip.match(ts)\r
22     if noms:\r
23         ts = noms.group(1)\r
24 \r
25     return datetime(*time.strptime(ts, '%Y-%m-%dT%H:%M:%S')[0:6])\r
26 \r
27 def readEl(el):\r
28     return dict([(n.tagName.lower(), getText(n)) for n in el.childNodes if n.nodeType == el.ELEMENT_NODE])\r
29 \r
30 def readTable(dump, name):\r
31     return [readEl(e) for e in minidom.parseString(dump.read("%s.xml" % name)).getElementsByTagName('row')]\r
32 \r
33 google_accounts_lookup = re.compile(r'^https?://www.google.com/accounts/')\r
34 yahoo_accounts_lookup = re.compile(r'^https?://me.yahoo.com/a/')\r
35 \r
36 openid_lookups = [\r
37     re.compile(r'^https?://www.google.com/profiles/(?P<uname>\w+(\.\w+)*)/?$'),\r
38     re.compile(r'^https?://me.yahoo.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
39     re.compile(r'^https?://openid.aol.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
40     re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).myopenid.com/?$'),\r
41     re.compile(r'^https?://flickr.com/(\w+/)*(?P<uname>\w+(\.\w+)*)/?$'),\r
42     re.compile(r'^https?://technorati.com/people/technorati/(?P<uname>\w+(\.\w+)*)/?$'),\r
43     re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).wordpress.com/?$'),\r
44     re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).blogspot.com/?$'),\r
45     re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).livejournal.com/?$'),\r
46     re.compile(r'^https?://claimid.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
47     re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).pip.verisignlabs.com/?$'),\r
48     re.compile(r'^https?://getopenid.com/(?P<uname>\w+(\.\w+)*)/?$'),\r
49     re.compile(r'^https?://[\w\.]+/(\w+/)*(?P<uname>\w+(\.\w+)*)/?$'),\r
50     re.compile(r'^https?://(?P<uname>[\w\.]+)/?$'),\r
51 ]\r
52 \r
53 def final_username_attempt(sxu):\r
54     openid = sxu.get('openid', None)\r
55 \r
56     if openid:\r
57         if google_accounts_lookup.search(openid):\r
58             return UnknownGoogleUser(sxu.get('id'))\r
59         if yahoo_accounts_lookup.search(openid):\r
60             return UnknownYahooUser(sxu.get('id'))\r
61 \r
62         for lookup in openid_lookups:\r
63             if lookup.search(openid):\r
64                 return lookup.search(openid).group('uname')\r
65 \r
66     return UnknownUser(sxu.get('id'))\r
67 \r
68 class UnknownUser(object):\r
69     def __init__(self, id):\r
70         self._id = id\r
71 \r
72     def __str__(self):\r
73         return _("user-%(id)s") % {'id': self._id}\r
74 \r
75     def __unicode__(self):\r
76         return self.__str__()\r
77 \r
78     def encode(self, *args):\r
79         return self.__str__()\r
80 \r
81 class UnknownGoogleUser(UnknownUser):\r
82     def __str__(self):\r
83         return _("user-%(id)s (google)") % {'id': self._id}\r
84 \r
85 class UnknownYahooUser(UnknownUser):\r
86     def __str__(self):\r
87         return _("user-%(id)s (yahoo)") % {'id': self._id}\r
88 \r
89 \r
90 class IdMapper(dict):\r
91     def __getitem__(self, key):\r
92         key = int(key)\r
93         return super(IdMapper, self).get(key, 1)\r
94 \r
95     def __setitem__(self, key, value):\r
96         super(IdMapper, self).__setitem__(int(key), int(value))\r
97 \r
98 openidre = re.compile('^https?\:\/\/')\r
99 def userimport(dump, options):\r
100     users = readTable(dump, "Users")\r
101 \r
102     user_by_name = {}\r
103     uidmapper = IdMapper()\r
104     merged_users = []\r
105 \r
106     owneruid = options.get('owneruid', None)\r
107     #check for empty values\r
108     if not owneruid:\r
109         owneruid = None\r
110 \r
111     for sxu in users:\r
112         create = True\r
113 \r
114         if sxu.get('id') == '-1':\r
115             continue\r
116 \r
117         if int(sxu.get('id')) == int(owneruid):\r
118             osqau = orm.User.objects.get(id=1)\r
119             uidmapper[owneruid] = 1\r
120             uidmapper[-1] = 1\r
121             create = False\r
122         else:\r
123             username = sxu.get('displayname', sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu))))\r
124 \r
125             if not isinstance(username, UnknownUser) and username in user_by_name:\r
126                 #if options.get('mergesimilar', False) and sxu.get('email', 'INVALID') == user_by_name[username].email:\r
127                 #    osqau = user_by_name[username]\r
128                 #    create = False\r
129                 #    uidmapper[sxu.get('id')] = osqau.id\r
130                 #else:\r
131                 inc = 1\r
132                 while ("%s %d" % (username, inc)) in user_by_name:\r
133                     inc += 1\r
134 \r
135                 username = "%s %d" % (username, inc)\r
136 \r
137         sxbadges = sxu.get('badgesummary', None)\r
138         badges = {'1':'0','2':'0','3':'0'}\r
139 \r
140         if sxbadges:\r
141             badges.update(dict([b.split('=') for b in sxbadges.split()]))\r
142 \r
143         if create:\r
144             osqau = orm.User(\r
145                 id           = sxu.get('id'),\r
146                 username     = unicode(username),\r
147                 password     = '!',\r
148                 email        = sxu.get('email', ''),\r
149                 is_superuser = sxu.get('usertypeid') == '5',\r
150                 is_staff     = sxu.get('usertypeid') == '4',\r
151                 is_active    = True,\r
152                 date_joined  = readTime(sxu.get('creationdate')),\r
153                 last_seen    = readTime(sxu.get('lastaccessdate')),\r
154                 about         = sxu.get('aboutme', ''),\r
155                 date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None,\r
156                 email_isvalid = int(sxu.get('usertypeid')) > 2,\r
157                 website       = sxu.get('websiteurl', ''),\r
158                 reputation    = int(sxu.get('reputation')),\r
159                 gold          = int(badges['1']),\r
160                 silver        = int(badges['2']),\r
161                 bronze        = int(badges['3']),\r
162                 real_name     = sxu.get('realname', ''),\r
163             )\r
164 \r
165             osqau.save()\r
166 \r
167             user_joins = orm.Action(\r
168                 action_type = "userjoins",\r
169                 action_date = osqau.date_joined,\r
170                 user = osqau\r
171             )\r
172             user_joins.save()\r
173 \r
174             rep = orm.ActionRepute(\r
175                 value = 1,\r
176                 user = osqau,\r
177                 date = osqau.date_joined,\r
178                 action = user_joins\r
179             )\r
180             rep.save()            \r
181 \r
182             try:\r
183                 orm.SubscriptionSettings.objects.get(user=osqau)\r
184             except:\r
185                 s = orm.SubscriptionSettings(user=osqau)\r
186                 s.save()\r
187 \r
188             uidmapper[osqau.id] = osqau.id\r
189         else:\r
190             new_about = sxu.get('aboutme', None)\r
191             if new_about and osqau.about != new_about:\r
192                 if osqau.about:\r
193                     osqau.about = "%s\n|\n%s" % (osqau.about, new_about)\r
194                 else:\r
195                     osqau.about = new_about\r
196 \r
197             osqau.username = sxu.get('displayname', sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu))))\r
198             osqau.email = sxu.get('email', '')\r
199             osqau.reputation += int(sxu.get('reputation'))\r
200             osqau.gold += int(badges['1'])\r
201             osqau.silver += int(badges['2'])\r
202             osqau.bronze += int(badges['3'])\r
203 \r
204             merged_users.append(osqau.id)\r
205             osqau.save()\r
206 \r
207         user_by_name[osqau.username] = osqau\r
208 \r
209         openid = sxu.get('openid', None)\r
210         if openid and openidre.match(openid):\r
211             assoc = orm.AuthKeyUserAssociation(user=osqau, key=openid, provider="openidurl")\r
212             assoc.save()\r
213 \r
214     if uidmapper[-1] == -1:\r
215         uidmapper[-1] = 1\r
216 \r
217     return (uidmapper, merged_users)\r
218 \r
219 def tagsimport(dump, uidmap):\r
220     tags = readTable(dump, "Tags")\r
221 \r
222     tagmap = {}\r
223 \r
224     for sxtag in tags:\r
225         otag = orm.Tag(\r
226             id = int(sxtag['id']),\r
227             name = sxtag['name'],\r
228             used_count = int(sxtag['count']),\r
229             created_by_id = uidmap[sxtag.get('userid', 1)],\r
230         )\r
231         otag.save()\r
232 \r
233         tagmap[otag.name] = otag\r
234 \r
235     return tagmap\r
236 \r
237 def postimport(dump, uidmap, tagmap):\r
238     history = {}\r
239     accepted = {}\r
240     all = {}\r
241 \r
242     for h in readTable(dump, "PostHistory"):\r
243         if not history.get(h.get('postid'), None):\r
244             history[h.get('postid')] = []\r
245 \r
246         history[h.get('postid')].append(h)\r
247 \r
248     posts = readTable(dump, "Posts")\r
249 \r
250     for sxpost in posts:\r
251         nodetype = (sxpost.get('posttypeid') == '1') and "nodetype" or "answer"\r
252 \r
253         post = orm.Node(\r
254             node_type = nodetype,\r
255             id = sxpost['id'],\r
256             added_at = readTime(sxpost['creationdate']),\r
257             body = sxpost['body'],\r
258             score = sxpost.get('score', 0),\r
259             author_id = sxpost.get('deletiondate', None) and 1 or uidmap[sxpost['owneruserid']]\r
260         )\r
261 \r
262         post.save()\r
263 \r
264         create_action = orm.Action(\r
265             action_type = (nodetype == "nodetype") and "ask" or "answer",\r
266             user_id = post.author_id,\r
267             node = post,\r
268             action_date = post.added_at\r
269         )\r
270 \r
271         create_action.save()\r
272 \r
273         #if sxpost.get('deletiondate', None):\r
274         #    delete_action = orm.Action(\r
275         #        action_type = "delete",\r
276         #        user_id = 1,\r
277         #        node = post,\r
278         #        action_date = readTime(sxpost['deletiondate'])\r
279         #    )\r
280 \r
281         #    delete_action.save()\r
282         #    post.deleted = delete_action\r
283 \r
284         if sxpost.get('lasteditoruserid', None):\r
285             revise_action = orm.Action(\r
286                 action_type = "revise",\r
287                 user_id = uidmap[sxpost.get('lasteditoruserid')],\r
288                 node = post,\r
289                 action_date = readTime(sxpost['lasteditdate']),\r
290             )\r
291 \r
292             revise_action.save()\r
293             post.last_edited = revise_action\r
294 \r
295         if sxpost.get('communityowneddate', None):\r
296             post.wiki = True\r
297 \r
298             wikify_action = orm.Action(\r
299                 action_type = "wikify",\r
300                 user_id = 1,\r
301                 node = post,\r
302                 action_date = readTime(sxpost['communityowneddate'])\r
303             )\r
304 \r
305             wikify_action.save()\r
306 \r
307 \r
308         if sxpost.get('lastactivityuserid', None):\r
309             post.last_activity_by_id = uidmap[sxpost['lastactivityuserid']]\r
310             post.last_activity_at = readTime(sxpost['lastactivitydate'])\r
311 \r
312             \r
313         if sxpost.get('posttypeid') == '1': #question\r
314             post.node_type = "question"\r
315             post.title = sxpost['title']\r
316 \r
317             tagnames = sxpost['tags'].replace(u'ö', '-').replace(u'é', '').replace(u'à', '')\r
318             post.tagnames = tagnames\r
319 \r
320             post.extra_count = sxpost.get('viewcount', 0)\r
321 \r
322             #if sxpost.get('closeddate', None):\r
323             #    post.marked = True\r
324             #\r
325             #    close_action = orm.Action(\r
326             #        action_type = "close",\r
327             #        user_id = 1,\r
328             #        node = post,\r
329             #        action_date = datetime.now() - timedelta(days=7)\r
330             #    )\r
331             #\r
332             #    close_action.save()\r
333             #    post.extra_action = close_action\r
334 \r
335             #if sxpost.get('acceptedanswerid', None):\r
336             #    accepted[int(sxpost.get('acceptedanswerid'))] = post\r
337 \r
338             #post.save()\r
339 \r
340         else:\r
341             post.parent_id = sxpost['parentid']\r
342 \r
343             #if int(post.id) in accepted:\r
344                 #post.marked = True\r
345 \r
346                 #accept_action = orm.Action(\r
347                 #    action_type = "acceptanswer",\r
348                 #    user_id = accepted[int(post.id)].author_id,\r
349                 #    node = post,\r
350                 #    action_date = datetime.now() - timedelta(days=7)\r
351                 #)\r
352 \r
353                 #accept_action.save()\r
354 \r
355 \r
356                 #post.accepted_at = datetime.now()\r
357                 #post.accepted_by_id = accepted[int(post.id)].author_id\r
358 \r
359                 #accepted[int(post.id)].extra_ref = post\r
360                 #accepted[int(post.id)].save()\r
361 \r
362         post.save()\r
363 \r
364         all[int(post.id)] = post\r
365 \r
366     return all\r
367 \r
368 def comment_import(dump, uidmap, posts):\r
369     comments = readTable(dump, "PostComments")\r
370     currid = max(posts.keys())\r
371     mapping = {}\r
372 \r
373     for sxc in comments:\r
374         currid += 1\r
375         oc = orm.Node(\r
376             id = currid,\r
377             node_type = "comment",\r
378             added_at = readTime(sxc['creationdate']),\r
379             author_id = uidmap[sxc.get('userid', 1)],\r
380             body = sxc['text'],\r
381             parent_id = sxc.get('postid'),\r
382         )\r
383 \r
384         if sxc.get('deletiondate', None):\r
385             delete_action = orm.Action(\r
386                 action_type = "delete",\r
387                 user_id = uidmap[sxc['deletionuserid']],\r
388                 action_date = readTime(sxc['deletiondate'])\r
389             )\r
390 \r
391             oc.author_id = uidmap[sxc['deletionuserid']]\r
392             oc.save()\r
393 \r
394             delete_action.node = oc\r
395             delete_action.save()\r
396 \r
397             oc.deleted = delete_action\r
398         else:\r
399             oc.author_id = uidmap[sxc.get('userid', 1)]\r
400             oc.save()\r
401 \r
402         create_action = orm.Action(\r
403             action_type = "comment",\r
404             user_id = oc.author_id,\r
405             node = oc,\r
406             action_date = oc.added_at\r
407         )\r
408 \r
409         create_action.save()\r
410         oc.save()\r
411 \r
412         posts[oc.id] = oc\r
413         mapping[int(sxc['id'])] = int(oc.id)\r
414 \r
415     return posts, mapping\r
416 \r
417 \r
418 def add_tags_to_posts(posts, tagmap):\r
419     for post in posts.values():\r
420         if post.node_type == "question":\r
421             tags = [tag for tag in [tagmap.get(name.strip()) for name in post.tagnames.split(u' ') if name] if tag]\r
422             post.tagnames = " ".join([t.name for t in tags]).strip()\r
423             post.tags = tags\r
424 \r
425         create_and_activate_revision(post)\r
426 \r
427 \r
428 def create_and_activate_revision(post):\r
429     rev = orm.NodeRevision(\r
430         author_id = post.author_id,\r
431         body = post.body,\r
432         node_id = post.id,\r
433         revised_at = post.added_at,\r
434         revision = 1,\r
435         summary = 'Initial revision',\r
436         tagnames = post.tagnames,\r
437         title = post.title,\r
438     )\r
439 \r
440     rev.save()\r
441     post.active_revision_id = rev.id\r
442     post.save()\r
443 \r
444 def post_vote_import(dump, uidmap, posts):\r
445     votes = readTable(dump, "Posts2Votes")\r
446     close_reasons = dict([(r['id'], r['name']) for r in readTable(dump, "CloseReasons")])\r
447 \r
448     user2vote = []\r
449 \r
450     for sxv in votes:\r
451         action = orm.Action(\r
452             user_id=uidmap[sxv['userid']],\r
453             action_date = readTime(sxv['creationdate']),\r
454         )\r
455 \r
456         node = posts.get(int(sxv['postid']), None)\r
457         if not node: continue\r
458         action.node = node\r
459 \r
460         if sxv['votetypeid'] == '1':\r
461             answer = node\r
462             question = posts.get(int(answer.parent_id), None)\r
463 \r
464             action.action_type = "acceptanswer"\r
465             action.save()\r
466 \r
467             answer.marked = True\r
468             answer.extra_action = action\r
469 \r
470             question.extra_ref_id = answer.id\r
471 \r
472             answer.save()\r
473             question.save()\r
474 \r
475         elif sxv['votetypeid'] in ('2', '3'):\r
476             if not (action.node.id, action.user_id) in user2vote:\r
477                 user2vote.append((action.node.id, action.user_id))\r
478 \r
479                 action.action_type = (sxv['votetypeid'] == '2') and "voteup" or "votedown"\r
480                 action.save()\r
481 \r
482                 ov = orm.Vote(\r
483                     node_id = action.node.id,\r
484                     user_id = action.user_id,\r
485                     voted_at = action.action_date,\r
486                     value = sxv['votetypeid'] == '2' and 1 or -1,\r
487                     action = action\r
488                 )\r
489                 ov.save()\r
490             else:\r
491                 action.action_type = "unknown"\r
492                 action.save()\r
493 \r
494         elif sxv['votetypeid'] in ('4', '12', '13'):\r
495             action.action_type = "flag"\r
496             action.save()\r
497 \r
498             of = orm.Flag(\r
499                 node = action.node,\r
500                 user_id = action.user_id,\r
501                 flagged_at = action.action_date,\r
502                 reason = '',\r
503                 action = action\r
504             )\r
505 \r
506             of.save()\r
507 \r
508         elif sxv['votetypeid'] == '5':\r
509             action.action_type = "favorite"\r
510             action.save()\r
511 \r
512         elif sxv['votetypeid'] == '6':\r
513             action.action_type = "close"\r
514             action.extra = dbsafe_encode(close_reasons[sxv['comment']])\r
515             action.save()\r
516 \r
517             node.marked = True\r
518             node.extra_action = action\r
519             node.save()\r
520 \r
521         elif sxv['votetypeid'] == '7':\r
522             action.action_type = "unknown"\r
523             action.save()\r
524             \r
525             node.marked = False\r
526             node.extra_action = None\r
527             node.save()\r
528 \r
529         elif sxv['votetypeid'] == '10':\r
530             action.action_type = "delete"\r
531             action.save()\r
532 \r
533             node.deleted = action\r
534             node.save()\r
535 \r
536         elif sxv['votetypeid'] == '11':\r
537             action.action_type = "unknown"\r
538             action.save()\r
539 \r
540             node.deleted = None\r
541             node.save()\r
542 \r
543         else:\r
544             action.action_type = "unknown"\r
545             action.save()\r
546 \r
547 \r
548         if sxv.get('targetrepchange', None):\r
549             rep = orm.ActionRepute(\r
550                 action = action,\r
551                 date = action.action_date,\r
552                 user_id = uidmap[sxv['targetuserid']],\r
553                 value = int(sxv['targetrepchange'])\r
554             )\r
555 \r
556             rep.save()\r
557 \r
558         if sxv.get('voterrepchange', None):\r
559             rep = orm.ActionRepute(\r
560                 action = action,\r
561                 date = action.action_date,\r
562                 user_id = uidmap[sxv['userid']],\r
563                 value = int(sxv['voterrepchange'])\r
564             )\r
565 \r
566             rep.save()\r
567 \r
568 \r
569 def comment_vote_import(dump, uidmap, comments, posts):\r
570     votes = readTable(dump, "Comments2Votes")\r
571     user2vote = []\r
572 \r
573     for sxv in votes:\r
574         if sxv['votetypeid'] == "2":\r
575             comment_id = comments[int(sxv['postcommentid'])]\r
576             user_id = uidmap[sxv['userid']]\r
577 \r
578             if not (comment_id, user_id) in user2vote:\r
579                 user2vote.append((comment_id, user_id))\r
580 \r
581                 action = orm.Action(\r
582                     action_type = "voteupcomment",\r
583                     user_id = user_id,\r
584                     action_date = readTime(sxv['creationdate']),\r
585                     node_id = comment_id\r
586                 )\r
587                 action.save()\r
588 \r
589                 ov = orm.Vote(\r
590                     node_id = comment_id,\r
591                     user_id = user_id,\r
592                     voted_at = action.action_date,\r
593                     value = 1,\r
594                     action = action\r
595                 )\r
596 \r
597                 ov.save()\r
598 \r
599                 posts[int(action.node_id)].score += 1\r
600                 posts[int(action.node_id)].save()\r
601 \r
602 \r
603 \r
604 def badges_import(dump, uidmap, post_list):\r
605     node_ctype = orm['contenttypes.contenttype'].objects.get(name='node')\r
606     obadges = dict([(b.cls, b) for b in orm.Badge.objects.all()])\r
607     sxbadges = dict([(int(b['id']), b) for b in readTable(dump, "Badges")])\r
608     user_badge_count = {}\r
609 \r
610     sx_to_osqa = {}\r
611 \r
612     for id, sxb in sxbadges.items():\r
613         cls = "".join(sxb['name'].replace('&', 'And').split(' '))\r
614 \r
615         if cls in obadges:\r
616             sx_to_osqa[id] = obadges[cls]\r
617         else:\r
618             osqab = orm.Badge(\r
619                 cls = cls,\r
620                 awarded_count = 0,\r
621                 type = sxb['class']                \r
622             )\r
623             osqab.save()\r
624             sx_to_osqa[id] = osqab\r
625 \r
626     sxawards = readTable(dump, "Users2Badges")\r
627     osqaawards = []\r
628 \r
629     for sxa in sxawards:\r
630         badge = sx_to_osqa[int(sxa['badgeid'])]\r
631 \r
632         user_id = uidmap[sxa['userid']]\r
633         if not user_badge_count.get(user_id, None):\r
634             user_badge_count[user_id] = 0\r
635 \r
636         action = orm.Action(\r
637             action_type = "award",\r
638             user_id = user_id,\r
639             action_date = readTime(sxa['date'])\r
640         )\r
641 \r
642         action.save()\r
643 \r
644         osqaa = orm.Award(\r
645             user_id = uidmap[sxa['userid']],\r
646             badge = badge,\r
647             node = post_list[user_badge_count[user_id]],\r
648             awarded_at = action.action_date,\r
649             action = action\r
650         )\r
651 \r
652         osqaa.save()\r
653         badge.awarded_count += 1\r
654         user_badge_count[user_id] += 1\r
655 \r
656     for badge in obadges.values():\r
657         badge.save()\r
658 \r
659 \r
660 def reset_sequences():\r
661     from south.db import db\r
662     if db.backend_name == "postgres":\r
663         db.start_transaction()\r
664         db.execute_many(PG_SEQUENCE_RESETS)\r
665         db.commit_transaction()\r
666 \r
667 def sximport(dump, options):\r
668     uidmap, merged_users = userimport(dump, options)\r
669     tagmap = tagsimport(dump, uidmap)\r
670     posts = postimport(dump, uidmap, tagmap)\r
671     posts, comments = comment_import(dump, uidmap, posts)\r
672     add_tags_to_posts(posts, tagmap)\r
673     post_vote_import(dump, uidmap, posts)\r
674     comment_vote_import(dump, uidmap, comments, posts)\r
675     badges_import(dump, uidmap, posts.values())\r
676 \r
677     from south.db import db\r
678     db.commit_transaction()\r
679 \r
680     reset_sequences()\r
681 \r
682     \r
683     \r
684 PG_SEQUENCE_RESETS = """\r
685 SELECT setval('"auth_user_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user";\r
686 SELECT setval('"auth_user_groups_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_groups";\r
687 SELECT setval('"auth_user_user_permissions_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_user_permissions";\r
688 SELECT setval('"forum_keyvalue_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_keyvalue";\r
689 SELECT setval('"forum_action_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_action";\r
690 SELECT setval('"forum_actionrepute_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_actionrepute";\r
691 SELECT setval('"forum_subscriptionsettings_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_subscriptionsettings";\r
692 SELECT setval('"forum_validationhash_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_validationhash";\r
693 SELECT setval('"forum_authkeyuserassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_authkeyuserassociation";\r
694 SELECT setval('"forum_tag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_tag";\r
695 SELECT setval('"forum_markedtag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_markedtag";\r
696 SELECT setval('"forum_node_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node";\r
697 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
698 SELECT setval('"forum_noderevision_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_noderevision";\r
699 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
700 SELECT setval('"forum_questionsubscription_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_questionsubscription";\r
701 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
702 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
703 SELECT setval('"forum_vote_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_vote";\r
704 SELECT setval('"forum_flag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_flag";\r
705 SELECT setval('"forum_badge_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_badge";\r
706 SELECT setval('"forum_award_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_award";\r
707 SELECT setval('"forum_openidnonce_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidnonce";\r
708 SELECT setval('"forum_openidassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidassociation";\r
709 """\r
710 \r
711 \r
712     \r
713