]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/sximporter/importer.py
Make the admin title link point to the admin base, and added a link to et back to...
[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                 location      = sxu.get('location', ''),\r
164             )\r
165 \r
166             osqau.save()\r
167 \r
168             user_joins = orm.Action(\r
169                 action_type = "userjoins",\r
170                 action_date = osqau.date_joined,\r
171                 user = osqau\r
172             )\r
173             user_joins.save()\r
174 \r
175             rep = orm.ActionRepute(\r
176                 value = 1,\r
177                 user = osqau,\r
178                 date = osqau.date_joined,\r
179                 action = user_joins\r
180             )\r
181             rep.save()            \r
182 \r
183             try:\r
184                 orm.SubscriptionSettings.objects.get(user=osqau)\r
185             except:\r
186                 s = orm.SubscriptionSettings(user=osqau)\r
187                 s.save()\r
188 \r
189             uidmapper[osqau.id] = osqau.id\r
190         else:\r
191             new_about = sxu.get('aboutme', None)\r
192             if new_about and osqau.about != new_about:\r
193                 if osqau.about:\r
194                     osqau.about = "%s\n|\n%s" % (osqau.about, new_about)\r
195                 else:\r
196                     osqau.about = new_about\r
197 \r
198             osqau.username = sxu.get('displayname', sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu))))\r
199             osqau.email = sxu.get('email', '')\r
200             osqau.reputation += int(sxu.get('reputation'))\r
201             osqau.gold += int(badges['1'])\r
202             osqau.silver += int(badges['2'])\r
203             osqau.bronze += int(badges['3'])\r
204 \r
205             osqau.date_joined = readTime(sxu.get('creationdate'))\r
206             osqau.website = sxu.get('websiteurl', '')\r
207             osqau.date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None\r
208             osqau.location = sxu.get('location', '')\r
209             osqau.real_name = sxu.get('realname', '')\r
210 \r
211             merged_users.append(osqau.id)\r
212             osqau.save()\r
213 \r
214         user_by_name[osqau.username] = osqau\r
215 \r
216         openid = sxu.get('openid', None)\r
217         if openid and openidre.match(openid):\r
218             assoc = orm.AuthKeyUserAssociation(user=osqau, key=openid, provider="openidurl")\r
219             assoc.save()\r
220 \r
221     if uidmapper[-1] == -1:\r
222         uidmapper[-1] = 1\r
223 \r
224     return (uidmapper, merged_users)\r
225 \r
226 def tagsimport(dump, uidmap):\r
227     tags = readTable(dump, "Tags")\r
228 \r
229     tagmap = {}\r
230 \r
231     for sxtag in tags:\r
232         otag = orm.Tag(\r
233             id = int(sxtag['id']),\r
234             name = sxtag['name'],\r
235             used_count = int(sxtag['count']),\r
236             created_by_id = uidmap[sxtag.get('userid', 1)],\r
237         )\r
238         otag.save()\r
239 \r
240         tagmap[otag.name] = otag\r
241 \r
242     return tagmap\r
243 \r
244 def postimport(dump, uidmap, tagmap):\r
245     history = {}\r
246     accepted = {}\r
247     all = {}\r
248 \r
249     for h in readTable(dump, "PostHistory"):\r
250         if not history.get(h.get('postid'), None):\r
251             history[h.get('postid')] = []\r
252 \r
253         history[h.get('postid')].append(h)\r
254 \r
255     posts = readTable(dump, "Posts")\r
256 \r
257     for sxpost in posts:\r
258         nodetype = (sxpost.get('posttypeid') == '1') and "nodetype" or "answer"\r
259 \r
260         post = orm.Node(\r
261             node_type = nodetype,\r
262             id = sxpost['id'],\r
263             added_at = readTime(sxpost['creationdate']),\r
264             body = sxpost['body'],\r
265             score = sxpost.get('score', 0),\r
266             author_id = sxpost.get('deletiondate', None) and 1 or uidmap[sxpost['owneruserid']]\r
267         )\r
268 \r
269         post.save()\r
270 \r
271         create_action = orm.Action(\r
272             action_type = (nodetype == "nodetype") and "ask" or "answer",\r
273             user_id = post.author_id,\r
274             node = post,\r
275             action_date = post.added_at\r
276         )\r
277 \r
278         create_action.save()\r
279 \r
280         #if sxpost.get('deletiondate', None):\r
281         #    delete_action = orm.Action(\r
282         #        action_type = "delete",\r
283         #        user_id = 1,\r
284         #        node = post,\r
285         #        action_date = readTime(sxpost['deletiondate'])\r
286         #    )\r
287 \r
288         #    delete_action.save()\r
289         #    post.deleted = delete_action\r
290 \r
291         if sxpost.get('lasteditoruserid', None):\r
292             revise_action = orm.Action(\r
293                 action_type = "revise",\r
294                 user_id = uidmap[sxpost.get('lasteditoruserid')],\r
295                 node = post,\r
296                 action_date = readTime(sxpost['lasteditdate']),\r
297             )\r
298 \r
299             revise_action.save()\r
300             post.last_edited = revise_action\r
301 \r
302         if sxpost.get('communityowneddate', None):\r
303             post.wiki = True\r
304 \r
305             wikify_action = orm.Action(\r
306                 action_type = "wikify",\r
307                 user_id = 1,\r
308                 node = post,\r
309                 action_date = readTime(sxpost['communityowneddate'])\r
310             )\r
311 \r
312             wikify_action.save()\r
313 \r
314 \r
315         if sxpost.get('lastactivityuserid', None):\r
316             post.last_activity_by_id = uidmap[sxpost['lastactivityuserid']]\r
317             post.last_activity_at = readTime(sxpost['lastactivitydate'])\r
318 \r
319             \r
320         if sxpost.get('posttypeid') == '1': #question\r
321             post.node_type = "question"\r
322             post.title = sxpost['title']\r
323 \r
324             tagnames = sxpost['tags'].replace(u'ö', '-').replace(u'é', '').replace(u'à', '')\r
325             post.tagnames = tagnames\r
326 \r
327             post.extra_count = sxpost.get('viewcount', 0)\r
328 \r
329             #if sxpost.get('closeddate', None):\r
330             #    post.marked = True\r
331             #\r
332             #    close_action = orm.Action(\r
333             #        action_type = "close",\r
334             #        user_id = 1,\r
335             #        node = post,\r
336             #        action_date = datetime.now() - timedelta(days=7)\r
337             #    )\r
338             #\r
339             #    close_action.save()\r
340             #    post.extra_action = close_action\r
341 \r
342             #if sxpost.get('acceptedanswerid', None):\r
343             #    accepted[int(sxpost.get('acceptedanswerid'))] = post\r
344 \r
345             #post.save()\r
346 \r
347         else:\r
348             post.parent_id = sxpost['parentid']\r
349 \r
350             #if int(post.id) in accepted:\r
351                 #post.marked = True\r
352 \r
353                 #accept_action = orm.Action(\r
354                 #    action_type = "acceptanswer",\r
355                 #    user_id = accepted[int(post.id)].author_id,\r
356                 #    node = post,\r
357                 #    action_date = datetime.now() - timedelta(days=7)\r
358                 #)\r
359 \r
360                 #accept_action.save()\r
361 \r
362 \r
363                 #post.accepted_at = datetime.now()\r
364                 #post.accepted_by_id = accepted[int(post.id)].author_id\r
365 \r
366                 #accepted[int(post.id)].extra_ref = post\r
367                 #accepted[int(post.id)].save()\r
368 \r
369         post.save()\r
370 \r
371         all[int(post.id)] = post\r
372 \r
373     return all\r
374 \r
375 def comment_import(dump, uidmap, posts):\r
376     comments = readTable(dump, "PostComments")\r
377     currid = max(posts.keys())\r
378     mapping = {}\r
379 \r
380     for sxc in comments:\r
381         currid += 1\r
382         oc = orm.Node(\r
383             id = currid,\r
384             node_type = "comment",\r
385             added_at = readTime(sxc['creationdate']),\r
386             author_id = uidmap[sxc.get('userid', 1)],\r
387             body = sxc['text'],\r
388             parent_id = sxc.get('postid'),\r
389         )\r
390 \r
391         if sxc.get('deletiondate', None):\r
392             delete_action = orm.Action(\r
393                 action_type = "delete",\r
394                 user_id = uidmap[sxc['deletionuserid']],\r
395                 action_date = readTime(sxc['deletiondate'])\r
396             )\r
397 \r
398             oc.author_id = uidmap[sxc['deletionuserid']]\r
399             oc.save()\r
400 \r
401             delete_action.node = oc\r
402             delete_action.save()\r
403 \r
404             oc.deleted = delete_action\r
405         else:\r
406             oc.author_id = uidmap[sxc.get('userid', 1)]\r
407             oc.save()\r
408 \r
409         create_action = orm.Action(\r
410             action_type = "comment",\r
411             user_id = oc.author_id,\r
412             node = oc,\r
413             action_date = oc.added_at\r
414         )\r
415 \r
416         create_action.save()\r
417         oc.save()\r
418 \r
419         posts[oc.id] = oc\r
420         mapping[int(sxc['id'])] = int(oc.id)\r
421 \r
422     return posts, mapping\r
423 \r
424 \r
425 def add_tags_to_posts(posts, tagmap):\r
426     for post in posts.values():\r
427         if post.node_type == "question":\r
428             tags = [tag for tag in [tagmap.get(name.strip()) for name in post.tagnames.split(u' ') if name] if tag]\r
429             post.tagnames = " ".join([t.name for t in tags]).strip()\r
430             post.tags = tags\r
431 \r
432         create_and_activate_revision(post)\r
433 \r
434 \r
435 def create_and_activate_revision(post):\r
436     rev = orm.NodeRevision(\r
437         author_id = post.author_id,\r
438         body = post.body,\r
439         node_id = post.id,\r
440         revised_at = post.added_at,\r
441         revision = 1,\r
442         summary = 'Initial revision',\r
443         tagnames = post.tagnames,\r
444         title = post.title,\r
445     )\r
446 \r
447     rev.save()\r
448     post.active_revision_id = rev.id\r
449     post.save()\r
450 \r
451 def post_vote_import(dump, uidmap, posts):\r
452     votes = readTable(dump, "Posts2Votes")\r
453     close_reasons = dict([(r['id'], r['name']) for r in readTable(dump, "CloseReasons")])\r
454 \r
455     user2vote = []\r
456 \r
457     for sxv in votes:\r
458         action = orm.Action(\r
459             user_id=uidmap[sxv['userid']],\r
460             action_date = readTime(sxv['creationdate']),\r
461         )\r
462 \r
463         node = posts.get(int(sxv['postid']), None)\r
464         if not node: continue\r
465         action.node = node\r
466 \r
467         if sxv['votetypeid'] == '1':\r
468             answer = node\r
469             question = posts.get(int(answer.parent_id), None)\r
470 \r
471             action.action_type = "acceptanswer"\r
472             action.save()\r
473 \r
474             answer.marked = True\r
475             answer.extra_action = action\r
476 \r
477             question.extra_ref_id = answer.id\r
478 \r
479             answer.save()\r
480             question.save()\r
481 \r
482         elif sxv['votetypeid'] in ('2', '3'):\r
483             if not (action.node.id, action.user_id) in user2vote:\r
484                 user2vote.append((action.node.id, action.user_id))\r
485 \r
486                 action.action_type = (sxv['votetypeid'] == '2') and "voteup" or "votedown"\r
487                 action.save()\r
488 \r
489                 ov = orm.Vote(\r
490                     node_id = action.node.id,\r
491                     user_id = action.user_id,\r
492                     voted_at = action.action_date,\r
493                     value = sxv['votetypeid'] == '2' and 1 or -1,\r
494                     action = action\r
495                 )\r
496                 ov.save()\r
497             else:\r
498                 action.action_type = "unknown"\r
499                 action.save()\r
500 \r
501         elif sxv['votetypeid'] in ('4', '12', '13'):\r
502             action.action_type = "flag"\r
503             action.save()\r
504 \r
505             of = orm.Flag(\r
506                 node = action.node,\r
507                 user_id = action.user_id,\r
508                 flagged_at = action.action_date,\r
509                 reason = '',\r
510                 action = action\r
511             )\r
512 \r
513             of.save()\r
514 \r
515         elif sxv['votetypeid'] == '5':\r
516             action.action_type = "favorite"\r
517             action.save()\r
518 \r
519         elif sxv['votetypeid'] == '6':\r
520             action.action_type = "close"\r
521             action.extra = dbsafe_encode(close_reasons[sxv['comment']])\r
522             action.save()\r
523 \r
524             node.marked = True\r
525             node.extra_action = action\r
526             node.save()\r
527 \r
528         elif sxv['votetypeid'] == '7':\r
529             action.action_type = "unknown"\r
530             action.save()\r
531             \r
532             node.marked = False\r
533             node.extra_action = None\r
534             node.save()\r
535 \r
536         elif sxv['votetypeid'] == '10':\r
537             action.action_type = "delete"\r
538             action.save()\r
539 \r
540             node.deleted = action\r
541             node.save()\r
542 \r
543         elif sxv['votetypeid'] == '11':\r
544             action.action_type = "unknown"\r
545             action.save()\r
546 \r
547             node.deleted = None\r
548             node.save()\r
549 \r
550         else:\r
551             action.action_type = "unknown"\r
552             action.save()\r
553 \r
554 \r
555         if sxv.get('targetrepchange', None):\r
556             rep = orm.ActionRepute(\r
557                 action = action,\r
558                 date = action.action_date,\r
559                 user_id = uidmap[sxv['targetuserid']],\r
560                 value = int(sxv['targetrepchange'])\r
561             )\r
562 \r
563             rep.save()\r
564 \r
565         if sxv.get('voterrepchange', None):\r
566             rep = orm.ActionRepute(\r
567                 action = action,\r
568                 date = action.action_date,\r
569                 user_id = uidmap[sxv['userid']],\r
570                 value = int(sxv['voterrepchange'])\r
571             )\r
572 \r
573             rep.save()\r
574 \r
575 \r
576 def comment_vote_import(dump, uidmap, comments, posts):\r
577     votes = readTable(dump, "Comments2Votes")\r
578     user2vote = []\r
579 \r
580     for sxv in votes:\r
581         if sxv['votetypeid'] == "2":\r
582             comment_id = comments[int(sxv['postcommentid'])]\r
583             user_id = uidmap[sxv['userid']]\r
584 \r
585             if not (comment_id, user_id) in user2vote:\r
586                 user2vote.append((comment_id, user_id))\r
587 \r
588                 action = orm.Action(\r
589                     action_type = "voteupcomment",\r
590                     user_id = user_id,\r
591                     action_date = readTime(sxv['creationdate']),\r
592                     node_id = comment_id\r
593                 )\r
594                 action.save()\r
595 \r
596                 ov = orm.Vote(\r
597                     node_id = comment_id,\r
598                     user_id = user_id,\r
599                     voted_at = action.action_date,\r
600                     value = 1,\r
601                     action = action\r
602                 )\r
603 \r
604                 ov.save()\r
605 \r
606                 posts[int(action.node_id)].score += 1\r
607                 posts[int(action.node_id)].save()\r
608 \r
609 \r
610 \r
611 def badges_import(dump, uidmap, post_list):\r
612     node_ctype = orm['contenttypes.contenttype'].objects.get(name='node')\r
613     obadges = dict([(b.cls, b) for b in orm.Badge.objects.all()])\r
614     sxbadges = dict([(int(b['id']), b) for b in readTable(dump, "Badges")])\r
615     user_badge_count = {}\r
616 \r
617     sx_to_osqa = {}\r
618 \r
619     for id, sxb in sxbadges.items():\r
620         cls = "".join(sxb['name'].replace('&', 'And').split(' '))\r
621 \r
622         if cls in obadges:\r
623             sx_to_osqa[id] = obadges[cls]\r
624         else:\r
625             osqab = orm.Badge(\r
626                 cls = cls,\r
627                 awarded_count = 0,\r
628                 type = sxb['class']                \r
629             )\r
630             osqab.save()\r
631             sx_to_osqa[id] = osqab\r
632 \r
633     sxawards = readTable(dump, "Users2Badges")\r
634     osqaawards = []\r
635 \r
636     for sxa in sxawards:\r
637         badge = sx_to_osqa[int(sxa['badgeid'])]\r
638 \r
639         user_id = uidmap[sxa['userid']]\r
640         if not user_badge_count.get(user_id, None):\r
641             user_badge_count[user_id] = 0\r
642 \r
643         action = orm.Action(\r
644             action_type = "award",\r
645             user_id = user_id,\r
646             action_date = readTime(sxa['date'])\r
647         )\r
648 \r
649         action.save()\r
650 \r
651         osqaa = orm.Award(\r
652             user_id = uidmap[sxa['userid']],\r
653             badge = badge,\r
654             node = post_list[user_badge_count[user_id]],\r
655             awarded_at = action.action_date,\r
656             action = action\r
657         )\r
658 \r
659         osqaa.save()\r
660         badge.awarded_count += 1\r
661         user_badge_count[user_id] += 1\r
662 \r
663     for badge in obadges.values():\r
664         badge.save()\r
665 \r
666 \r
667 def reset_sequences():\r
668     from south.db import db\r
669     if db.backend_name == "postgres":\r
670         db.start_transaction()\r
671         db.execute_many(PG_SEQUENCE_RESETS)\r
672         db.commit_transaction()\r
673 \r
674 def sximport(dump, options):\r
675     uidmap, merged_users = userimport(dump, options)\r
676     tagmap = tagsimport(dump, uidmap)\r
677     posts = postimport(dump, uidmap, tagmap)\r
678     posts, comments = comment_import(dump, uidmap, posts)\r
679     add_tags_to_posts(posts, tagmap)\r
680     post_vote_import(dump, uidmap, posts)\r
681     comment_vote_import(dump, uidmap, comments, posts)\r
682     badges_import(dump, uidmap, posts.values())\r
683 \r
684     from south.db import db\r
685     db.commit_transaction()\r
686 \r
687     reset_sequences()\r
688 \r
689     \r
690     \r
691 PG_SEQUENCE_RESETS = """\r
692 SELECT setval('"auth_user_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user";\r
693 SELECT setval('"auth_user_groups_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_groups";\r
694 SELECT setval('"auth_user_user_permissions_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_user_permissions";\r
695 SELECT setval('"forum_keyvalue_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_keyvalue";\r
696 SELECT setval('"forum_action_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_action";\r
697 SELECT setval('"forum_actionrepute_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_actionrepute";\r
698 SELECT setval('"forum_subscriptionsettings_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_subscriptionsettings";\r
699 SELECT setval('"forum_validationhash_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_validationhash";\r
700 SELECT setval('"forum_authkeyuserassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_authkeyuserassociation";\r
701 SELECT setval('"forum_tag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_tag";\r
702 SELECT setval('"forum_markedtag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_markedtag";\r
703 SELECT setval('"forum_node_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node";\r
704 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
705 SELECT setval('"forum_noderevision_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_noderevision";\r
706 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
707 SELECT setval('"forum_questionsubscription_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_questionsubscription";\r
708 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
709 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";\r
710 SELECT setval('"forum_vote_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_vote";\r
711 SELECT setval('"forum_flag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_flag";\r
712 SELECT setval('"forum_badge_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_badge";\r
713 SELECT setval('"forum_award_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_award";\r
714 SELECT setval('"forum_openidnonce_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidnonce";\r
715 SELECT setval('"forum_openidassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidassociation";\r
716 """\r
717 \r
718 \r
719     \r
720