summaryrefslogtreecommitdiffstats
path: root/forum/management/commands
diff options
context:
space:
mode:
authorhrcerqueira <hrcerqueira@gmail.com>2010-03-07 23:41:26 +0000
committerhrcerqueira <hrcerqueira@gmail.com>2010-03-07 23:41:26 +0000
commitccfbbd7f183538e96df76d82b2f4081fcc119b1f (patch)
tree9eae31a6f8d4d3c5397d6d6ff457b6ec51dd32b5 /forum/management/commands
parent0d1d1ef3a3416b5bcd4eb867356484b28f0c7f05 (diff)
downloadaskbot-ccfbbd7f183538e96df76d82b2f4081fcc119b1f.tar.gz
askbot-ccfbbd7f183538e96df76d82b2f4081fcc119b1f.tar.bz2
askbot-ccfbbd7f183538e96df76d82b2f4081fcc119b1f.zip
Lots of small fixes, including the missing comments problem.
Diffstat (limited to 'forum/management/commands')
-rwxr-xr-xforum/management/commands/sximport.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/forum/management/commands/sximport.py b/forum/management/commands/sximport.py
new file mode 100755
index 00000000..5a0bb96a
--- /dev/null
+++ b/forum/management/commands/sximport.py
@@ -0,0 +1,109 @@
+from django.core.management.base import LabelCommand
+from zipfile import ZipFile
+from xml.dom import minidom as dom
+import datetime
+
+from forum.models import User
+
+class Command(LabelCommand):
+ def handle_label(self, label, **options):
+ zip = ZipFile(label)
+
+ map = {}
+
+ map['users'] = self.import_users(zip.open("Users.xml"))
+ map['questions'], map['answers'] = self.import_posts(zip.open("Posts.xml"))
+
+
+ def row_to_dic(self, row):
+ return dict([
+ (child.localName.lower(),
+ " ".join([t.nodeValue for t in child.childNodes if t.nodeType == t.TEXT_NODE]))
+ for child in row.childNodes
+ if child.nodeType == child.ELEMENT_NODE
+ ])
+
+ def from_sx_time(self, timestring):
+ if timestring is None:
+ return timestring
+
+ try:
+ return datetime.datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S')
+ except:
+ return datetime.datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S.%f')
+
+
+ def import_users(self, users):
+ pkey_map = {}
+ doc = dom.parse(users)
+
+ rows = doc.getElementsByTagName('row')
+ unknown_count = 0
+
+ added_names = []
+
+ for row in rows:
+ values = self.row_to_dic(row)
+
+ username = values.get('displayname',
+ values.get('realname',
+ values.get('email', None)))
+
+ if username is None:
+ unknown_count += 1
+ username = 'Unknown User %d' % unknown_count
+
+ if username in added_names:
+ cnt = 1
+ new_username = "%s %d" % (username, cnt)
+ while new_username in added_names:
+ cnt += 1
+ new_username = "%s %d" % (username, cnt)
+
+ username = new_username
+
+ added_names.append(username)
+
+ user = User(username=username, email=values.get('email', ''))
+
+ user.reputation = values['reputation']
+ user.last_seen = self.from_sx_time(values['lastaccessdate'])
+
+ user.real_name = values.get('realname', '')
+ user.about = values.get('aboutme', '')
+ user.website = values.get('websiteurl', '')
+ user.date_of_birth = self.from_sx_time(values.get('birthday', None))
+ user.location = values.get('location', '')
+
+ user.is_active = True
+ user.email_isvalid = True
+
+
+ if int(values['usertypeid']) == 5:
+ user.is_superuser = True
+
+ if int(values['usertypeid']) == 5:
+ user.is_staff = True
+
+ user.save()
+
+ pkey_map[values['id']] = user
+
+ return users
+
+ def import_posts(self, posts, map):
+ pkey_map = {}
+ doc = dom.parse(posts)
+
+ rows = doc.getElementsByTagName('row')
+
+ for row in rows:
+ map = {
+ 'title': row['']
+ }
+
+ pass
+ pass
+
+
+