[60ada4]: / ForgeDiscussion / forgediscussion / import_support.py  Maximize  Restore  History

Download this file

179 lines (148 with data), 5.7 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
175
176
177
178
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from datetime import datetime
from ming import schema as S
from ming.odm import ThreadLocalODMSession, session
from tg import tmpl_context as c
from allura import model as M
from forgediscussion import model as DM
import six
log = logging.getLogger(__name__)
def validate_import(json, username_mapping, default_username=None):
warnings = []
schema = make_schema(username_mapping, default_username, warnings)
json = schema.validate(json)
return warnings, json
def perform_import(json, username_mapping, default_username=None, create_users=False):
if create_users:
default_username = create_user
# Validate the import, create missing users
warnings, json = validate_import(json, username_mapping, default_username)
for w in warnings:
log.warning('Importing to %s/%s: %s',
c.project.shortname,
c.app.config.options.mount_point,
w)
for name, forum in json.forums.items():
log.info('... %s has %d threads with %d total posts',
name, len(forum.threads), sum(len(t) for t in forum.threads.values()))
for name, forum in json.forums.items():
log.info('... creating %s/%s: %s',
c.project.shortname,
c.app.config.options.mount_point,
name)
f = DM.Forum(
app_config_id=c.app.config._id,
name=forum['name'],
shortname=forum['name'],
description=forum['description'])
for tid, posts in forum.threads.items():
rest, head = posts[:-1], posts[-1]
t = DM.ForumThread.new(
_id=tid,
discussion_id=f._id,
subject=head['subject'])
for p in posts:
p = create_post(f._id, t._id, p)
t.first_post_id = p._id
ThreadLocalODMSession.flush_all()
t.update_stats()
ThreadLocalODMSession.flush_all()
f.update_stats()
return warnings
def make_schema(user_name_map, default_username, warnings):
USER = AlluraUser(user_name_map, default_username, warnings)
TIMESTAMP = TimeStamp()
POST = {
'msg_id': str,
'is_followup_to': str,
'is_deleted': str,
'thread_id': str,
'poster_name': str,
'poster_user': USER,
'subject': str,
'date': TIMESTAMP,
'body': str,
}
FORUM = {
'name': str,
'description': str,
'threads': {str: [POST]},
}
result = S.SchemaItem.make({
'class': str,
'trackers': [None],
'forums': {str: FORUM}
})
return result
class AlluraUser(S.FancySchemaItem):
def __init__(self, mapping, default_username, warnings, **kw):
self.mapping = mapping
self.default_username = default_username
self.warnings = warnings
super().__init__(**kw)
def _validate(self, value, **kw):
value = S.String().validate(value)
sf_username = self.mapping.get(value, value)
result = M.User.by_username(sf_username)
if result is None:
self.warnings.append('User %s not found' % value)
if callable(self.default_username):
sf_username = self.default_username(value)
else:
sf_username = self.default_username
self.warnings.append('... setting username to %r' % sf_username)
result = M.User.by_username(sf_username)
self.mapping[value] = sf_username
return result
def _convert_from_python(self, value, state):
return value.username
class TimeStamp(S.FancySchemaItem):
def _validate(self, value, **kwargs):
try:
value = int(value)
except TypeError:
raise S.Invalid('%s is not int()-able' % value, value, None)
value = datetime.utcfromtimestamp(value)
return value
def create_user(json_username):
allura_username = c.project.shortname + '-' + json_username
while True:
try:
M.User.register(
dict(
username=allura_username,
display_name=allura_username),
False)
session(M.User).flush()
break
except Exception:
raise
return allura_username
def create_post(discussion_id, thread_id, json_post):
p = DM.ForumPost(
_id='%s@import' % (json_post.msg_id),
thread_id=thread_id,
discussion_id=discussion_id,
timestamp=json_post.date,
author_id=json_post.poster_user._id,
text=json_post.body,
status='ok')
if json_post.is_followup_to not in ('0', '-1'):
p.parent_id = '%s@import' % (json_post['is_followup_to'])
return p