def save_list_archives(self): self.status.progress_descr = _(u'Saving mailing lists') try: listfol = self.context['lists'] except KeyError: logger.error("No lists subfolder on %s" % self.context.getId()) return real_site = getSite() for mlistid, mlist in listfol.objectItems(): # XXX filter more? logger.info("exporting %s" % mlistid) setSite(mlist) # Needed to get the export adapter. mlistid = badchars.sub('_', mlistid) # Cargo-culted from listen/browser/import_export.py em = getAdapter(mlist, IMailingListMessageExport, name='mbox') # Nooo don't do it all in memory, we have seen some huge archives. #file_data = em.export_messages() or '' #self.zipfile.writestr('%s/lists/%s.mbox' % (self.context_dirname, mlistid), # file_data) tmpfd, tmpname = em.export_messages_to_tempfile() self.zipfile.write(tmpname, '%s/lists/%s/archive.mbox' % (self.context_dirname, mlistid)) os.unlink(tmpname) del(tmpfd) # Now the list subscribers. logger.info("exporting subscribers.csv for %s" % mlistid) es = getAdapter(mlist, IMailingListSubscriberExport, name='csv') file_data = es.export_subscribers( include_allowed_senders=True) or '' csv_path = '%s/lists/%s/subscribers.csv' % (self.context_dirname, mlistid) self.zipfile.writestr(csv_path, file_data) # Now the metadata and preferences. logger.info("exporting settings.ini for %s" % mlistid) list_info = { 'id': mlist.getId(), 'type': mlist_type_to_workflow(mlist), 'mailto': mlist.mailto, 'managers': mlist.managers, 'archive_setting': mlist_archive_policy(mlist), 'title': mlist.Title(), 'description': mlist.Description(), 'creation_date': mlist.CreationDate(), 'creator': mlist.Creator(), 'sync_with_project': ISyncWithProjectMembership.providedBy(mlist), 'context': self.context.getId(), 'private_archives': mlist.private_archives, } conf_path = '%s/lists/%s/settings.ini' % (self.context_dirname, mlistid) self.zipfile.writestr(conf_path, mlist_conf(list_info)) setSite(real_site) logger.info("finished %s" % mlistid)
def save_list_archives(self): self.status.progress_descr = _(u'Saving mailing lists') try: listfol = self.context['lists'] except KeyError: logger.error("No lists subfolder on %s" % self.context.getId()) return real_site = getSite() for mlistid, mlist in listfol.objectItems(): # XXX filter more? logger.info("exporting %s" % mlistid) setSite(mlist) # Needed to get the export adapter. mlistid = badchars.sub('_', mlistid) # Cargo-culted from listen/browser/import_export.py em = getAdapter(mlist, IMailingListMessageExport, name='mbox') # Nooo don't do it all in memory, we have seen some huge archives. #file_data = em.export_messages() or '' #self.zipfile.writestr('%s/lists/%s.mbox' % (self.context_dirname, mlistid), # file_data) tmpfd, tmpname = em.export_messages_to_tempfile() self.zipfile.write(tmpname, '%s/lists/%s/archive.mbox' % (self.context_dirname, mlistid)) os.unlink(tmpname) del(tmpfd) # Now the list subscribers. logger.info("exporting subscribers.csv for %s" % mlistid) es = getAdapter(mlist, IMailingListSubscriberExport, name='csv') file_data = es.export_subscribers( include_allowed_senders=True) or '' csv_path = '%s/lists/%s/subscribers.csv' % (self.context_dirname, mlistid) self.zipfile.writestr(csv_path, file_data) # And the pending subscriptions, unsubscriptions, and posts. from zope.annotation import IAnnotations import simplejson as json annot = IAnnotations(mlist).get('listen', {}) this_annot = annot.get("pending_a_s_mod_email", {}) this_annot_json = {} for mem_email in this_annot: this_annot_json[mem_email] = { "pin": this_annot[mem_email].get("pin"), "time": this_annot[mem_email].get("time"), "subscriber": this_annot[mem_email].get("subscriber"), "user_name": this_annot[mem_email].get("user_name"), } self.zipfile.writestr( '%s/lists/%s/pending_a_s_mod_email.json' % (self.context_dirname, mlistid), json.dumps(this_annot_json)) this_annot = annot.get("a_s_pending_sub_email", {}) this_annot_json = {} for mem_email in this_annot: this_annot_json[mem_email] = { "pin": this_annot[mem_email].get("pin"), "time": this_annot[mem_email].get("time"), "subscriber": this_annot[mem_email].get("subscriber"), "user_name": this_annot[mem_email].get("user_name"), } self.zipfile.writestr( '%s/lists/%s/a_s_pending_sub_email.json' % (self.context_dirname, mlistid), json.dumps(this_annot_json)) this_annot = annot.get("pending_sub_email", {}) this_annot_json = {} for mem_email in this_annot: this_annot_json[mem_email] = { "pin": this_annot[mem_email].get("pin"), "time": this_annot[mem_email].get("time"), "subscriber": this_annot[mem_email].get("subscriber"), "user_name": this_annot[mem_email].get("user_name"), } self.zipfile.writestr( '%s/lists/%s/pending_sub_email.json' % (self.context_dirname, mlistid), json.dumps(this_annot_json)) this_annot = annot.get("pending_sub_mod_email", {}) this_annot_json = {} for mem_email in this_annot: user_name = this_annot[mem_email].get("user_name") if isinstance(user_name, str): try: user_name = user_name.decode("utf8") except UnicodeDecodeError: user_name = user_name.decode("ISO-8859-1") if user_name: user_name = user_name.encode("utf8") this_annot_json[mem_email] = { "pin": this_annot[mem_email].get("pin"), "time": this_annot[mem_email].get("time"), "subscriber": this_annot[mem_email].get("subscriber"), "user_name": user_name, } self.zipfile.writestr( '%s/lists/%s/pending_sub_mod_email.json' % (self.context_dirname, mlistid), json.dumps(this_annot_json)) this_annot = annot.get("pending_unsub_email", {}) this_annot_json = {} for mem_email in this_annot: user_name = this_annot[mem_email].get("user_name") if isinstance(user_name, str): try: user_name = user_name.decode("utf8") except UnicodeDecodeError: user_name = user_name.decode("ISO-8859-1") if user_name: user_name = user_name.encode("utf8") this_annot_json[mem_email] = { "pin": this_annot[mem_email].get("pin"), "time": this_annot[mem_email].get("time"), "subscriber": this_annot[mem_email].get("subscriber"), "user_name": user_name, } self.zipfile.writestr( '%s/lists/%s/pending_unsub_email.json' % (self.context_dirname, mlistid), json.dumps(this_annot_json)) this_annot = annot.get("pending_mod_post", {}) this_annot_json = {} for mem_email in this_annot: user_name = this_annot[mem_email].get("user_name") if isinstance(user_name, str): try: user_name = user_name.decode("utf8") except UnicodeDecodeError: user_name = user_name.decode("ISO-8859-1") if user_name: user_name = user_name.encode("utf8") this_annot_json[mem_email] = { "pin": this_annot[mem_email].get("pin"), "time": this_annot[mem_email].get("time"), "subscriber": this_annot[mem_email].get("subscriber"), "user_name": user_name, "post": {}, } posts = this_annot[mem_email].get("post") for post_id in posts: body = posts[post_id]['body'] header = posts[post_id]['header'] try: body = body.decode("utf8").encode("utf8") except UnicodeDecodeError: try: body = body.decode("ISO-8859-1").encode("utf8") except UnicodeDecodeError: body = body.decode("ascii").encode("utf8") for header_name in header: if header_name in header: val = header[header_name] try: val = val.decode("utf8").encode("utf8") except UnicodeDecodeError: val = val.decode("ISO-8859-1").encode("utf8") header[header_name] = val this_annot_json[mem_email]['post'][post_id] = { 'header': header, 'body': body, 'postid': posts[post_id]['postid'], } self.zipfile.writestr( '%s/lists/%s/pending_mod_post.json' % (self.context_dirname, mlistid), json.dumps(this_annot_json)) this_annot = annot.get("pending_pmod_post", {}) this_annot_json = {} for mem_email in this_annot: user_name = this_annot[mem_email].get("user_name") if isinstance(user_name, str): try: user_name = user_name.decode("utf8") except UnicodeDecodeError: user_name = user_name.decode("ISO-8859-1") if user_name: user_name = user_name.encode("utf8") this_annot_json[mem_email] = { "pin": this_annot[mem_email].get("pin"), "time": this_annot[mem_email].get("time"), "subscriber": this_annot[mem_email].get("subscriber"), "user_name": user_name, "post": {}, } posts = this_annot[mem_email].get("post") for post_id in posts: body = posts[post_id]['body'] header = posts[post_id]['header'] try: body = body.decode("utf8").encode("utf8") except UnicodeDecodeError: try: body = body.decode("ISO-8859-1").encode("utf8") except UnicodeDecodeError: body = body.decode("ascii").encode("utf8") for header_name in header: if header_name in header: val = header[header_name] try: val = val.decode("utf8").encode("utf8") except UnicodeDecodeError: val = val.decode("ISO-8859-1").encode("utf8") header[header_name] = val this_annot_json[mem_email]['post'][post_id] = { 'header': header, 'body': body, 'postid': posts[post_id]['postid'], } self.zipfile.writestr( '%s/lists/%s/pending_pmod_post.json' % (self.context_dirname, mlistid), json.dumps(this_annot_json)) # Now the metadata and preferences. logger.info("exporting settings.ini for %s" % mlistid) list_info = { 'id': mlist.getId(), 'type': mlist_type_to_workflow(mlist), 'mailto': mlist.mailto, 'managers': mlist.managers, 'archive_setting': mlist_archive_policy(mlist), 'title': mlist.Title(), 'description': mlist.Description(), 'creation_date': str(mlist.created()), 'modification_date': str(mlist.modified()), 'creator': mlist.Creator(), 'sync_with_project': ISyncWithProjectMembership.providedBy(mlist), 'context': self.context.getId(), 'private_archives': mlist.private_archives, } conf_path = '%s/lists/%s/settings.ini' % (self.context_dirname, mlistid) self.zipfile.writestr(conf_path, mlist_conf(list_info)) setSite(real_site) logger.info("finished %s" % mlistid)