X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/6e4c911e9bfed3ecd83427f5329ed024be9c62c6..524edb6c52ca7f56e9e032a175dc4b7fba0fca6c:/forum_modules/exporter/exporter.py diff --git a/forum_modules/exporter/exporter.py b/forum_modules/exporter/exporter.py index e9983d2..d652211 100644 --- a/forum_modules/exporter/exporter.py +++ b/forum_modules/exporter/exporter.py @@ -1,3 +1,5 @@ +from __future__ import with_statement + import os, tarfile, datetime, logging, re, ConfigParser, shutil, zipfile from django.core.cache import cache @@ -7,10 +9,34 @@ from forum.settings import APP_URL from forum.templatetags.extra_tags import diff_date import xml.etree.ElementTree from xml.etree import ElementTree as ET -from xml.etree.ElementTree import Comment, _encode, ProcessingInstruction, QName, fixtag, _escape_attrib, _escape_cdata +from xml.etree.ElementTree import Comment, _encode, ProcessingInstruction, QName, _escape_attrib, _escape_cdata, _namespace_map from forum import settings from django.conf import settings as djsettings import settings as selfsettings +import string + +try: + from xml.etree.ElementTree import fixtag +except ImportError: + def fixtag(tag, namespaces): + # given a decorated tag (of the form {uri}tag), return prefixed + # tag and namespace declaration, if any + if isinstance(tag, QName): + tag = tag.text + namespace_uri, tag = string.split(tag[1:], "}", 1) + prefix = namespaces.get(namespace_uri) + if prefix is None: + prefix = _namespace_map.get(namespace_uri) + if prefix is None: + prefix = "ns%d" % len(namespaces) + namespaces[namespace_uri] = prefix + if prefix == "xml": + xmlns = None + else: + xmlns = ("xmlns:%s" % prefix, namespace_uri) + else: + xmlns = None + return "%s:%s" % (prefix, tag), xmlns CACHE_KEY = "%s_exporter_state" % APP_URL EXPORT_STEPS = [] @@ -91,8 +117,13 @@ def ET_Element_add_tag(el, tag_name, content = None, **attrs): if content: try: tag.text = unicode(content) - except: - tag.text = u'' + except Exception, e: + #logging.error('error converting unicode characters') + #import traceback + #logging.error(traceback.print_exc()) + + import string + tag.text = unicode("".join([c for c in content if c in string.printable])) for k, v in attrs.items(): tag.set(k, unicode(v)) @@ -210,18 +241,24 @@ def export_upfiles(tf): folder = str(settings.UPFILES_FOLDER) if os.path.exists(folder): - tf.add(folder, arcname='/upfiles') + if isinstance(tf, zipfile.ZipFile): + tf.write(folder, arcname='/upfiles') + else: + tf.add(folder, arcname='/upfiles') def export_skinsfolder(tf): folder = djsettings.TEMPLATE_DIRS[0] if os.path.exists(folder): - tf.add(folder, arcname='/skins') + if isinstance(tf, zipfile.ZipFile): + tf.write(folder, arcname='/skins') + else: + tf.add(folder, arcname='/skins') def export(options, user): - original__write = xml.etree.ElementTree.ElementTree._write + original__write = xml.etree.ElementTree.ElementTree.write xml.etree.ElementTree.ElementTree._write = Etree_pretty__write xml.etree.ElementTree._ElementInterface.add = ET_Element_add_tag @@ -373,29 +410,34 @@ def export_users(u, el, anon_data): key.add('provider', a.provider) key.add('key', a.key) + try: + ss = u.subscription_settings - ss = u.subscription_settings - - notify = el.add('notifications', enabled=ss.enable_notifications and 'true' or 'false') + notify = el.add('notifications', enabled=ss.enable_notifications and 'true' or 'false') - notify.add('notify', **dict([(t, ss.__dict__.get(t, 'n') == 'i' and 'true' or 'false') for t in ['member_joins', 'new_question', 'new_question_watched_tags', 'subscribed_questions']])) + notify.add('notify', **dict([(t, ss.__dict__.get(t, 'n') == 'i' and 'true' or 'false') for t in ['member_joins', 'new_question', 'new_question_watched_tags', 'subscribed_questions']])) - notify.add('autoSubscribe', **dict([(t, ss.__dict__.get(t, False) and 'true' or 'false') for t in [ - 'all_questions', 'all_questions_watched_tags', 'questions_asked', 'questions_answered', 'questions_commented', 'questions_viewed']])) + notify.add('autoSubscribe', **dict([(t, ss.__dict__.get(t, False) and 'true' or 'false') for t in [ + 'all_questions', 'all_questions_watched_tags', 'questions_asked', 'questions_answered', 'questions_commented', 'questions_viewed']])) - notify.add('notifyOnSubscribed', **dict([(t, ss.__dict__.get("notify_%s" % t, False) and 'true' or 'false') for t in [ - 'answers', 'reply_to_comments', 'comments_own_post', 'comments', 'accepted']])) + notify.add('notifyOnSubscribed', **dict([(t, ss.__dict__.get("notify_%s" % t, False) and 'true' or 'false') for t in [ + 'answers', 'reply_to_comments', 'comments_own_post', 'comments', 'accepted']])) - notify.add('digest', ss.send_digest and 'on' or 'off') + notify.add('digest', ss.send_digest and 'on' or 'off') + except SubscriptionSettings.DoesNotExist: + pass watched = el.add('watchedTags') rejected = el.add('rejectedTags') for m in u.tag_selections.all(): - if m.reason == 'good': - watched.add('tag', m.tag.name) - else: - rejected.add('tag', m.tag.name) + try: + if m.reason == 'good': + watched.add('tag', m.tag.name) + else: + rejected.add('tag', m.tag.name) + except Tag.DoesNotExist: + pass @@ -408,7 +450,7 @@ def export_nodes(n, el, anon_data): el.add('author', n.author.id) 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 "") + el.add('absparent', n.abs_parent and n.abs_parent.id or "") act = el.add('lastactivity') act.add('by', n.last_activity_by and n.last_activity_by.id or "") @@ -444,6 +486,7 @@ def export_nodes(n, el, anon_data): rev.add('tags', ", ".join(r.tagname_list())) el.add('marked', n.marked and 'true' or 'false') + el.add('wiki', n.nis.wiki and 'true' or 'false') el.add('extraRef', n.extra_ref and n.extra_ref.id or "") make_extra(el.add('extraData'), n.extra) el.add('extraCount', n.extra_count and n.extra_count or "") @@ -507,20 +550,3 @@ def export_awards(a, el, anon_data): def export_settings(s, el, anon_data): el.add('key', s.key) make_extra(el.add('value'), s.value) - - - - - - - - - - - - - - - - -