Parent: [3d1483] (diff)

Download this file

attachments.py    175 lines (144 with data), 6.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import json
from ming.orm.ormsession import ThreadLocalORMSession
from forgewiki.command.wiki2markdown.base import BaseImportUnit
from allura.command import base as allura_base
from pylons import c
from allura import model as M
from forgewiki import model as WM
from forgediscussion import model as DM
from forgewiki import converters
class AttachmentsImportUnit(BaseImportUnit):
def _export_pages(self, p):
if p.neighborhood is None:
return
wiki_app = p.app_instance('wiki')
discussion_app = p.app_instance('discussion')
pid = "%s" % p._id
out_dir = os.path.join(self.options.output_dir, pid)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
file_path = os.path.join(out_dir, "attachments.json")
json_attachments = {'pages': {}, 'discuss': {}, 'forums': {}}
if wiki_app is not None:
pages = WM.Page.query.find(dict(app_config_id=wiki_app.config._id)).all()
else:
pages = []
for page in pages:
page_id = "%s" % page._id
attachments_list = page.attachments.all()
if len(attachments_list) == 0:
continue
page_text = page.text
for att in attachments_list:
if att.content_type[:5] == "image":
page_text = '%s\n![%s](%s "%s")' % (page_text,
att.filename,
att.url(),
att.filename
)
else:
page_text = '%s\n[%s](%s)' % (page_text,
att.filename,
att.url()
)
json_attachments['pages'][page_id] = {'text': page_text}
if discussion_app is not None:
discussions = M.Discussion.query.find(app_config_id=discussion_app.config._id).all()
forums = DM.Forum.query.find(app_config_id=discussion_app.config._id).all()
else:
discussions = []
forums = []
for discuss in discussions:
for post in discuss.posts:
post_id = "%s" % post._id
attachments_list = post.attachments.all()
if len(attachments_list) == 0:
continue
post_text = post.text
for att in attachments_list:
if att.content_type[:5] == "image":
post_text = '%s\n![%s](%s "%s")' % (post_text,
att.filename,
att.url(),
att.filename
)
else:
post_text = '%s\n[%s](%s)' % (post_text,
att.filename,
att.url()
)
json_attachments['discuss'][post_id] = {'text': post_text}
for f in forums:
for post in f.posts:
post_id = "%s" % post._id
attachments_list = post.attachments.all()
if len(attachments_list) == 0:
continue
post_text = post.text
for att in attachments_list:
if att.content_type[:5] == "image":
post_text = '%s\n![%s](%s "%s")' % (post_text,
att.filename,
att.url(),
att.filename
)
else:
post_text = '%s\n[%s](%s)' % (post_text,
att.filename,
att.url()
)
json_attachments['forums'][post_id] = {'text': post_text}
if len(json_attachments['pages']) > 0 or len(json_attachments['discuss']) > 0 \
or len(json_attachments['forums']) > 0:
with open(file_path, 'w') as attachments_file:
json.dump(json_attachments, attachments_file)
def _load_pages(self, p):
if p.neighborhood is None:
return
wiki_app = p.app_instance('wiki')
discussion_app = p.app_instance('discussion')
pid = "%s" % p._id
file_path = os.path.join(self.options.output_dir, pid, "attachments.json")
if not os.path.isfile(file_path):
return
c.project = None
json_attachments = {'pages': {}, 'discuss': {}, 'forums': {}}
with open(file_path, 'r') as attachments_file:
json_attachments = json.load(attachments_file)
if wiki_app is not None:
pages = WM.Page.query.find(dict(app_config_id=wiki_app.config._id)).all()
else:
pages = []
for page in pages:
page_id = "%s" % page._id
if page_id in json_attachments['pages']:
page.text = json_attachments['pages'][page_id]['text']
if discussion_app is not None:
discussions = M.Discussion.query.find(app_config_id=discussion_app.config._id).all()
forums = DM.Forum.query.find(app_config_id=discussion_app.config._id).all()
else:
discussions = []
forums = []
for discuss in discussions:
for post in discuss.posts:
post_id = "%s" % post._id
if post_id in json_attachments['discuss']:
post.text = json_attachments['discuss'][post_id]['text']
for f in forums:
for post in f.posts:
post_id = "%s" % post._id
if post_id in json_attachments['forums']:
post.text = json_attachments['forums'][post_id]['text']
ThreadLocalORMSession.flush_all()
ThreadLocalORMSession.close_all()
def extract(self):
projects = M.Project.query.find().all()
for p in projects:
self._export_pages(p)
allura_base.log.info("Export attachments complete")
def load(self):
projects = M.Project.query.find().all()
for p in projects:
self._load_pages(p)
allura_base.log.info("Load attachments complete")