def create_forum(app, new_forum): if 'parent' in new_forum and new_forum['parent']: parent_id = ObjectId(str(new_forum['parent'])) shortname = (DM.Forum.query.get(_id=parent_id).shortname + '/' + new_forum['shortname']) else: parent_id = None shortname = new_forum['shortname'] description = new_forum.get('description', '') f = DM.Forum( app_config_id=app.config._id, parent_id=parent_id, name=h.really_unicode(new_forum['name']), shortname=h.really_unicode(shortname), description=h.really_unicode(description), members_only=new_forum.get('members_only', False), anon_posts=new_forum.get('anon_posts', False), monitoring_email=new_forum.get('monitoring_email', None), ) if f.members_only and f.anon_posts: flash('You cannot have anonymous posts in a members only forum.', 'warning') f.anon_posts = False if f.members_only: role_developer = ProjectRole.by_name('Developer')._id f.acl = [ACE.allow(role_developer, ALL_PERMISSIONS), DENY_ALL] elif f.anon_posts: role_anon = ProjectRole.anonymous()._id f.acl = [ACE.allow(role_anon, 'post')] else: f.acl = [] return f
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.iteritems(): log.info('... %s has %d threads with %d total posts', name, len(forum.threads), sum(len(t) for t in forum.threads.itervalues())) for name, forum in json.forums.iteritems(): 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.iteritems(): 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 ThreadLocalORMSession.flush_all() t.update_stats() ThreadLocalORMSession.flush_all() f.update_stats() return warnings
def import_discussion(project, pid, frs_mapping, sf_project_shortname, nbhd): from forgediscussion import model as DM discuss_app = project.app_instance('discussion') if not discuss_app: discuss_app = project.install_app('Discussion', 'discussion') h.set_context(project.shortname, 'discussion', neighborhood=nbhd) assert c.app # set permissions and config options role_admin = M.ProjectRole.by_name('Admin')._id role_developer = M.ProjectRole.by_name('Developer')._id role_auth = M.ProjectRole.by_name('*authenticated')._id role_anon = M.ProjectRole.by_name('*anonymous')._id discuss_app.config.acl = [ M.ACE.allow(role_anon, 'read'), M.ACE.allow(role_auth, 'post'), M.ACE.allow(role_auth, 'unmoderated_post'), M.ACE.allow(role_developer, 'moderate'), M.ACE.allow(role_admin, 'configure'), M.ACE.allow(role_admin, 'admin')] ThreadLocalORMSession.flush_all() DM.Forum.query.remove( dict(app_config_id=discuss_app.config._id, shortname='general')) forums = os.listdir(os.path.join(options.output_dir, pid, 'forum')) for forum in forums: ending = forum[-5:] forum_name = forum[:-5] if '.json' == ending and forum_name in forums: forum_data = loadjson(pid, 'forum', forum) fo = DM.Forum.query.get( shortname=forum_name, app_config_id=discuss_app.config._id) if not fo: fo = DM.Forum(app_config_id=discuss_app.config._id, shortname=forum_name) fo.name = forum_data.title fo.description = forum_data.description fo_num_topics = 0 fo_num_posts = 0 topics = os.listdir(os.path.join(options.output_dir, pid, 'forum', forum_name)) for topic in topics: ending = topic[-5:] topic_name = topic[:-5] if '.json' == ending and topic_name in topics: fo_num_topics += 1 topic_data = loadjson(pid, 'forum', forum_name, topic) thread_query = dict( subject=topic_data.title, discussion_id=fo._id, app_config_id=discuss_app.config._id) if not options.skip_thread_import_id_when_reloading: # temporary/transitional. Just needed the first time # running with this new code against an existing import # that didn't have import_ids thread_query['import_id'] = topic_data.id to = DM.ForumThread.query.get(**thread_query) if not to: to = DM.ForumThread.new( subject=topic_data.title, discussion_id=fo._id, import_id=topic_data.id, app_config_id=discuss_app.config._id) to.import_id = topic_data.id to_num_replies = 0 oldest_post = None newest_post = None posts = sorted( os.listdir(os.path.join(options.output_dir, pid, 'forum', forum_name, topic_name))) for post in posts: ending = post[-5:] post_name = post[:-5] if '.json' == ending: to_num_replies += 1 post_data = loadjson(pid, 'forum', forum_name, topic_name, post) p = DM.ForumPost.query.get( _id='%s%s@import' % ( post_name, str(discuss_app.config._id)), thread_id=to._id, discussion_id=fo._id, app_config_id=discuss_app.config._id) if not p: p = DM.ForumPost( _id='%s%s@import' % ( post_name, str( discuss_app.config._id)), thread_id=to._id, discussion_id=fo._id, app_config_id=discuss_app.config._id) create_date = datetime.strptime( post_data.createdDate, '%Y-%m-%d %H:%M:%S') p.timestamp = create_date p.author_id = str( get_user(post_data.createdByUserName)._id) p.text = convert_post_content( frs_mapping, sf_project_shortname, post_data.content, nbhd) p.status = 'ok' if post_data.replyToId: p.parent_id = '%s%s@import' % ( post_data.replyToId, str(discuss_app.config._id)) slug, full_slug = p.make_slugs( parent=p.parent, timestamp=create_date) p.slug = slug p.full_slug = full_slug if oldest_post is None or oldest_post.timestamp > create_date: oldest_post = p if newest_post is None or newest_post.timestamp < create_date: newest_post = p ThreadLocalORMSession.flush_all() to.num_replies = to_num_replies to.first_post_id = oldest_post._id to.last_post_date = newest_post.timestamp to.mod_date = newest_post.timestamp fo_num_posts += to_num_replies fo.num_topics = fo_num_topics fo.num_posts = fo_num_posts ThreadLocalORMSession.flush_all()