]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/exporter/exporter.py
Added some documentation about how to properly set the DJANGO_SETTINGS_MODULE.
[osqa.git] / forum_modules / exporter / exporter.py
index 5559e44bf910532930b3e5f3c8a7144676f7068d..c5a12be07735eca407c44ad2df8ea325a257ba7b 100644 (file)
@@ -20,6 +20,7 @@ LAST_BACKUP = os.path.join(TMP_FOLDER, 'backup.tar.gz')
 
 DATE_AND_AUTHOR_INF_SECTION = 'DateAndAuthor'
 OPTIONS_INF_SECTION = 'Options'
+META_INF_SECTION = 'Meta'
 
 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
 DATE_FORMAT = "%Y-%m-%d"
@@ -56,10 +57,10 @@ def Etree_pretty__write(self, file, node, encoding, namespaces,
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     raise #_raise_serialization_error(v)
-                file.write(" %s=\"%s\"" % (_encode(k, encoding),
+                file.write(u" %s=\"%s\"" % (_encode(k, encoding),
                                             _escape_attrib(v, encoding)))
             for k, v in xmlns_items:
-                file.write(" %s=\"%s\"" % (_encode(k, encoding),
+                file.write(u" %s=\"%s\"" % (_encode(k, encoding),
                                             _escape_attrib(v, encoding)))
         if node.text or len(node):
             file.write(">")
@@ -78,17 +79,18 @@ def Etree_pretty__write(self, file, node, encoding, namespaces,
     if node.tail:
         file.write(_escape_cdata(node.tail.replace("\n", level * identator + "\n"), encoding))
 
-def _add_tag(el, name, content = None):
-    tag = ET.SubElement(el, name)
-    if content:
-        tag.text = content
-    return tag
+def make_date(date, with_time=True):
+    try:
+        return date.strftime(with_time and DATETIME_FORMAT or DATE_FORMAT)
+    except ValueError, e:
+        return date.replace(year=1900).strftime(with_time and DATETIME_FORMAT or DATE_FORMAT)
+
 
 def ET_Element_add_tag(el, tag_name, content = None, **attrs):
     tag = ET.SubElement(el, tag_name)
 
     if content:
-        tag.text = unicode(content).encode('utf-8')
+        tag.text = unicode(content)
 
     for k, v in attrs.items():
         tag.set(k, unicode(v))
@@ -177,6 +179,11 @@ def create_targz(tmp, files, start_time, options, user, state, set_state):
     inf.set(OPTIONS_INF_SECTION, 'with-upfiles', str(options.get('uplodaded_files', False)))
     inf.set(OPTIONS_INF_SECTION, 'with-skins', str(options.get('import_skins_folder', False)))
 
+    inf.add_section(META_INF_SECTION)
+
+    for id, s in state.items():
+        inf.set(META_INF_SECTION, id, str(s['count']))
+
     with open(os.path.join(tmp, 'backup.inf'), 'wb') as inffile:
         inf.write(inffile)
 
@@ -332,14 +339,14 @@ def export_users(u, el, anon_data):
     el.add('email', u.email, validated=u.email_isvalid and 'true' or 'false')
     el.add('reputation', u.reputation)
     el.add('badges', bronze=u.bronze, silver=u.silver, gold=u.gold)
-    el.add('joindate', u.date_joined.strftime(DATETIME_FORMAT))
+    el.add('joindate', make_date(u.date_joined))
     el.add('active', u.is_active and 'true' or 'false')
 
     el.add('realname', u.real_name)
     el.add('bio', u.about)
     el.add('location', u.location)
     el.add('website', u.website)
-    el.add('birthdate', u.date_of_birth and u.date_of_birth.strftime(DATE_FORMAT) or "")
+    el.add('birthdate', u.date_of_birth and make_date(u.date_of_birth, with_time=False) or "")
 
     roles = el.add('roles')
 
@@ -388,17 +395,19 @@ def export_nodes(n, el, anon_data):
 
     if not anon_data:
         el.add('author', n.author.id)
-    el.add('date', n.added_at.strftime(DATETIME_FORMAT))
+    el.add('date', make_date(n.added_at))
     el.add('parent', n.parent and n.parent.id or "")
     el.add('absparent', n.abs_parent and n.abs_parent or "")
 
     act = el.add('lastactivity')
     act.add('by', n.last_activity_by and n.last_activity_by.id or "")
-    act.add('at', n.last_activity_at and n.last_activity_at.strftime(DATETIME_FORMAT) or "")
+    act.add('at', n.last_activity_at and make_date(n.last_activity_at) or "")
 
     el.add('title', n.title)
     el.add('body', n.body)
 
+    el.add('score', n.score)
+
     tags = el.add('tags')
 
     for t in n.tagname_list():
@@ -407,12 +416,12 @@ def export_nodes(n, el, anon_data):
     revs = el.add('revisions', active=n.active_revision and n.active_revision.revision or n.revisions.order_by('revision')[0].revision)
 
     for r in n.revisions.order_by('revision'):
-        rev = _add_tag(revs, 'revision')
+        rev = revs.add('revision')
         rev.add('number', r.revision)
         rev.add('summary', r.summary)
         if not anon_data:
             rev.add('author', r.author.id)
-        rev.add('date', r.revised_at.strftime(DATETIME_FORMAT))
+        rev.add('date', make_date(r.revised_at))
 
         rev.add('title', r.title)
         rev.add('body', r.body)
@@ -428,7 +437,7 @@ def export_nodes(n, el, anon_data):
 def export_actions(a, el, anon_data):
     el.add('id', a.id)
     el.add('type', a.action_type)
-    el.add('date', a.action_date)
+    el.add('date', make_date(a.action_date))
 
     if not anon_data:
         el.add('user', a.user.id)
@@ -445,7 +454,7 @@ def export_actions(a, el, anon_data):
             canceled.add('user', a.canceled_by.id)
             canceled.add('ip', a.canceled_ip)
 
-        canceled.add('date', a.canceled_at)        
+        canceled.add('date', make_date(a.canceled_at))        
 
     if not anon_data:
         reputes = el.add('reputes')