def register_neighborhood_project(self, neighborhood, users, allow_register=False): from allura import model as M shortname = '--init--' name = 'Home Project for %s' % neighborhood.name p = M.Project(neighborhood_id=neighborhood._id, shortname=shortname, name=name, short_description='', description=( 'You can edit this description in the admin page'), homepage_title = '# ' + name, last_updated = datetime.utcnow(), is_nbhd_project=True, is_root=True) try: p.configure_project( users=users, is_user_project=False, apps=[ ('Wiki', 'wiki', 'Wiki'), ('admin', 'admin', 'Admin')]) except: ThreadLocalORMSession.close_all() log.exception('Error registering project %s' % p) raise if allow_register: role_auth = M.ProjectRole.authenticated(p) security.simple_grant(p.acl, role_auth._id, 'register') state(p).soil() return p
def commit(self): """Save off a snapshot of the artifact and increment the version #""" self.version += 1 try: ip_address = request.headers.get("X_FORWARDED_FOR", request.remote_addr) ip_address = ip_address.split(",")[0].strip() except: ip_address = "0.0.0.0" data = dict( artifact_id=self._id, artifact_class="%s.%s" % (self.__class__.__module__, self.__class__.__name__), version=self.version, author=dict( id=c.user._id, username=c.user.username, display_name=c.user.get_pref("display_name"), logged_ip=ip_address, ), timestamp=datetime.utcnow(), data=state(self).clone(), ) ss = self.__mongometa__.history_class(**data) session(ss).insert_now(ss, state(ss)) log.info("Snapshot version %s of %s", self.version, self.__class__) return ss
def commit(self, update_stats=True): '''Save off a snapshot of the artifact and increment the version #''' self.version += 1 try: ip_address = request.headers.get('X_FORWARDED_FOR', request.remote_addr) ip_address = ip_address.split(',')[0].strip() except: ip_address = '0.0.0.0' data = dict( artifact_id=self._id, artifact_class='%s.%s' % ( self.__class__.__module__, self.__class__.__name__), version=self.version, author=dict( id=c.user._id, username=c.user.username, display_name=c.user.get_pref('display_name'), logged_ip=ip_address), timestamp=datetime.utcnow(), data=state(self).clone()) ss = self.__mongometa__.history_class(**data) session(ss).insert_now(ss, state(ss)) log.info('Snapshot version %s of %s', self.version, self.__class__) if update_stats: if self.version > 1: g.statsUpdater.modifiedArtifact( self.type_s, self.mod_date, self.project, c.user) else : g.statsUpdater.newArtifact( self.type_s, self.mod_date, self.project, c.user) return ss
def register_neighborhood_project(self, neighborhood, users, allow_register=False): from allura import model as M shortname = '--init--' name = 'Home Project for %s' % neighborhood.name database_uri = M.Project.default_database_uri(shortname) p = M.Project(neighborhood_id=neighborhood._id, shortname=shortname, name=name, short_description='', description=('You can edit this description in the admin page'), homepage_title = '# ' + name, database_uri=database_uri, last_updated = datetime.utcnow(), is_nbhd_project=True, is_root=True) try: p.configure_project( users=users, is_user_project=False, apps=[ ('Wiki', 'wiki', 'Wiki'), ('admin', 'admin', 'Admin')]) except: ThreadLocalORMSession.close_all() log.exception('Error registering project %s' % p) raise if allow_register: role_auth = M.ProjectRole.authenticated(p) security.simple_grant(p.acl, role_auth._id, 'register') state(p).soil() return p
def create_thumbnail(self, attachment, att_cls): if attachment.is_image(): base.log.info("Processing image attachment '%s'", attachment.filename) doc = state(attachment).document.deinstrumented_clone() del doc['_id'] del doc['file_id'] doc['type'] = 'thumbnail' count = att_cls.query.find(doc).count() if count == 1: base.log.info("Thumbnail already exists for '%s' - skipping", attachment.filename) return elif count > 1: base.log.warning( "There are %d thumbnails for '%s' - consider clearing them with --force", count, attachment.filename) return image = PIL.Image.open(attachment.rfile()) del doc['content_type'] del doc['filename'] att_cls.save_thumbnail(attachment.filename, image, attachment.content_type, att_cls.thumbnail_size, doc, square=True) base.log.info("Created thumbnail for '%s'", attachment.filename) self.created_thumbs += 1
def create_thumbnail(self, attachment, att_cls): if attachment.is_image(): base.log.info("Processing image attachment '%s'", attachment.filename) doc = state(attachment).document.deinstrumented_clone() del doc['_id'] del doc['file_id'] doc['type'] = 'thumbnail' count = att_cls.query.find(doc).count() if count == 1: base.log.info( "Thumbnail already exists for '%s' - skipping", attachment.filename) return elif count > 1: base.log.warning( "There are %d thumbnails for '%s' - consider clearing them with --force", count, attachment.filename) return image = PIL.Image.open(attachment.rfile()) del doc['content_type'] del doc['filename'] att_cls.save_thumbnail(attachment.filename, image, attachment.content_type, att_cls.thumbnail_size, doc, square=True) base.log.info("Created thumbnail for '%s'", attachment.filename) self.created_thumbs += 1
def make_slug(self): base = BlogPost.make_base_slug(self.title, self.timestamp) self.slug = base while True: try: session(self).insert_now(self, state(self)) return self.slug except DuplicateKeyError: self.slug = base + '-%.3d' % randint(0, 999)
def upsert(cls, **kw): obj = cls.query.get(**kw) if obj is not None: return obj try: obj = cls(**kw) session(obj).insert_now(obj, state(obj)) except pymongo.errors.DuplicateKeyError: session(obj).expunge(obj) obj = cls.query.get(**kw) return obj
def reply(self): new_id = h.gen_message_id() slug, full_slug = self.make_slugs(self) new_args = dict(state(self).document, _id=new_id, slug=slug, full_slug=full_slug, parent_id=self._id, timestamp=datetime.utcnow(), author_id=c.user._id) return self.__class__(**new_args)
def commit(self, update_stats=True): '''Save off a snapshot of the artifact and increment the version #''' try: ip_address = utils.ip_address(request) except: ip_address = '0.0.0.0' data = dict( artifact_id=self._id, artifact_class='%s.%s' % ( self.__class__.__module__, self.__class__.__name__), author=dict( id=c.user._id, username=c.user.username, display_name=c.user.get_pref('display_name'), logged_ip=ip_address), data=state(self).clone()) while True: self.version += 1 data['version'] = self.version data['timestamp'] = datetime.utcnow() ss = self.__mongometa__.history_class(**data) try: session(ss).insert_now(ss, state(ss)) except pymongo.errors.DuplicateKeyError: log.warning('Trying to create duplicate version %s of %s', self.version, self.__class__) session(ss).expunge(ss) continue else: break log.debug('Snapshot version %s of %s', self.version, self.__class__) if update_stats: if self.version > 1: g.statsUpdater.modifiedArtifact( self.type_s, self.mod_date, self.project, c.user) else: g.statsUpdater.newArtifact( self.type_s, self.mod_date, self.project, c.user) return ss
def reply(self): new_id = h.gen_message_id() slug, full_slug = self.make_slugs(self) new_args = dict( state(self).document, _id=new_id, slug=slug, full_slug=full_slug, parent_id=self._id, timestamp=datetime.utcnow(), author_id=c.user._id) return self.__class__(**new_args)
def test_create(self): doc = self.Basic() assert state(doc).status == 'new' self.session.flush() assert state(doc).status == 'clean' doc.a = 5 assert state(doc).status == 'dirty' self.session.flush() assert state(doc).status == 'clean' c = doc.c c.e = 5 assert state(doc).status == 'dirty', state(doc).status assert repr(state(doc)).startswith('<ObjectState')
def make_slug(self): slugsafe = ''.join( ch.lower() for ch in self.title.replace(' ', '-') if ch.isalnum() or ch == '-') base = '%s/%s' % ( self.timestamp.strftime('%Y/%m'), slugsafe) self.slug = base while True: try: session(self).insert_now(self, state(self)) return self.slug except DuplicateKeyError: self.slug = base + '-%.3d' % randint(0,999) return self.slug
def set_tool_data(self, tool, **kw): d = self.tool_data.setdefault(tool, {}) d.update(kw) state(self).soil()
def bootstrap(command, conf, vars): """Place any commands to setup allura here""" # are we being called by the test suite? test_run = conf.get('__file__', '').endswith('test.ini') if not test_run: # when this is run via the `setup-app` cmd, some macro rendering needs this set up REGISTRY.register(ew.widget_context, ew.core.WidgetContext('http', ew.ResourceManager())) create_test_data = asbool(os.getenv('ALLURA_TEST_DATA', True)) # if this is a test_run, skip user project creation to save time make_user_projects = not test_run def make_user(*args, **kw): kw.update(make_project=make_user_projects) return create_user(*args, **kw) # Temporarily disable auth extensions to prevent unintended side-effects tg.config['auth.method'] = tg.config['registration.method'] = 'local' assert tg.config['auth.method'] == 'local' conf['auth.method'] = conf['registration.method'] = 'local' # Clean up all old stuff ThreadLocalORMSession.close_all() c.user = c.project = c.app = None wipe_database() try: g.solr.delete(q='*:*') except: # pragma no cover log.error('SOLR server is %s', g.solr_server) log.error('Error clearing solr index') # set up mongo indexes index = EnsureIndexCommand('ensure_index') index.run(['']) if create_test_data and asbool(conf.get('cache_test_data')): if restore_test_data(): h.set_context('test', neighborhood='Projects') return log.info('Initializing search') log.info('Registering root user & default neighborhoods') M.User( _id=None, username='******', display_name='Anonymous') # never make a user project for the root user if create_test_data: root = create_user('Root', make_project=False) else: from getpass import getpass root_name = raw_input('Enter username for root user (default "root"): ').strip() if not root_name: root_name = 'root' ok = False while not ok: root_password1 = getpass('Enter password: '******'Password must not be empty') continue root_password2 = getpass('Confirm password: '******'t match") continue root = create_user(root_name, password=root_password1, make_project=False) ok = True n_projects = M.Neighborhood(name='Projects', url_prefix='/p/', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) n_users = M.Neighborhood(name='Users', url_prefix='/u/', shortname_prefix='u/', anchored_tools='profile:Profile,userstats:Statistics', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) assert tg.config['auth.method'] == 'local' project_reg = plugin.ProjectRegistrationProvider.get() p_projects = project_reg.register_neighborhood_project( n_projects, [root], allow_register=True) p_users = project_reg.register_neighborhood_project(n_users, [root]) def set_nbhd_wiki_content(nbhd_proj, content): wiki = nbhd_proj.app_instance('wiki') page = WM.Page.query.get( app_config_id=wiki.config._id, title=wiki.root_page_name) page.text = content set_nbhd_wiki_content(p_projects, dedent(''' Welcome to the "Projects" neighborhood. It is the default neighborhood in Allura. You can edit this wiki page as you see fit. Here's a few ways to get started: [Register a new project](/p/add_project) [Neighborhood administration](/p/admin) [[projects show_total=yes]] ''')) set_nbhd_wiki_content(p_users, dedent(''' This is the "Users" neighborhood. All users automatically get a user-project created for them, using their username. [Neighborhood administration](/u/admin) [[projects show_total=yes]] ''')) if create_test_data: n_adobe = M.Neighborhood( name='Adobe', url_prefix='/adobe/', project_list_url='/adobe/', features=dict(private_projects=True, max_projects=None, css='custom', google_analytics=True)) p_adobe = project_reg.register_neighborhood_project(n_adobe, [root]) set_nbhd_wiki_content(p_adobe, dedent(''' This is the "Adobe" neighborhood. It is just an example of having projects in a different neighborhood. [Neighborhood administration](/adobe/admin) [[projects show_total=yes]] ''')) # add the adobe icon file_name = 'adobe_icon.png' file_path = os.path.join( allura.__path__[0], 'public', 'nf', 'images', file_name) M.NeighborhoodFile.from_path(file_path, neighborhood_id=n_adobe._id) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() if create_test_data: # Add some test users for unum in range(10): make_user('Test User %d' % unum) log.info('Creating basic project categories') M.ProjectCategory(name='clustering', label='Clustering') cat2 = M.ProjectCategory(name='communications', label='Communications') M.ProjectCategory( name='synchronization', label='Synchronization', parent_id=cat2._id) M.ProjectCategory( name='streaming', label='Streaming', parent_id=cat2._id) M.ProjectCategory(name='fax', label='Fax', parent_id=cat2._id) M.ProjectCategory(name='bbs', label='BBS', parent_id=cat2._id) cat3 = M.ProjectCategory(name='database', label='Database') M.ProjectCategory( name='front_ends', label='Front-Ends', parent_id=cat3._id) M.ProjectCategory( name='engines_servers', label='Engines/Servers', parent_id=cat3._id) if create_test_data: log.info('Registering "regular users" (non-root) and default projects') # since this runs a lot for tests, separate test and default users and # do the minimal needed if asbool(conf.get('load_test_data')): u_admin = make_user('Test Admin') u_admin.preferences = dict(email_address='*****@*****.**') u_admin.email_addresses = ['*****@*****.**'] u_admin.set_password('foo') u_admin.claim_address('*****@*****.**') ThreadLocalORMSession.flush_all() admin_email = M.EmailAddress.get(email='*****@*****.**') admin_email.confirmed = True else: u_admin = make_user('Admin 1', username='******') # Admin1 is almost root, with admin access for Users and Projects # neighborhoods p_projects.add_user(u_admin, ['Admin']) p_users.add_user(u_admin, ['Admin']) n_projects.register_project('allura', u_admin, 'Allura') make_user('Test User') n_adobe.register_project('adobe-1', u_admin, 'Adobe project 1') p_adobe.add_user(u_admin, ['Admin']) p0 = n_projects.register_project('test', u_admin, 'Test Project') n_projects.register_project('test2', u_admin, 'Test 2') p0._extra_tool_status = ['alpha', 'beta'] sess = session(M.Neighborhood) # all the sessions are the same _list = (n_projects, n_users, p_projects, p_users) if create_test_data: _list += (n_adobe, p_adobe) for x in _list: # Ming doesn't detect substructural changes in newly created objects # (vs loaded from DB) state(x).status = 'dirty' # TODO: Hope that Ming can be improved to at least avoid stuff below sess.flush(x) ThreadLocalORMSession.flush_all() if asbool(conf.get('load_test_data')): if asbool(conf.get('cache_test_data')): cache_test_data() else: # pragma no cover # regular first-time setup create_trove_categories = CreateTroveCategoriesCommand('create_trove_categories') create_trove_categories.run(['']) if create_test_data: p0.add_user(u_admin, ['Admin']) log.info('Registering initial apps') with h.push_config(c, user=u_admin): p0.install_apps([{'ep_name': ep_name} for ep_name, app in g.entry_points['tool'].iteritems() if app._installable(tool_name=ep_name, nbhd=n_projects, project_tools=[]) ]) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() if create_test_data: # reload our p0 project so that p0.app_configs is accurate with all the # newly installed apps p0 = M.Project.query.get(_id=p0._id) sub = p0.new_subproject('sub1', project_name='A Subproject') with h.push_config(c, user=u_admin): sub.install_app('wiki') ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all()
'''Merge all the OldProjectRole collections in various project databases into a central ProjectRole collection. ''' import logging from pylons import c from ming.orm import session, state from allura import model as M log = logging.getLogger(__name__) seen_databases = set() projects = M.Project.query.find().all() for p in projects: if p.database_uri in seen_databases: continue seen_databases.add(p.database_uri) log.info('Moving project roles in database %s to main DB', p.database_uri) c.project = p for opr in M.OldProjectRole.query.find(): pr = M.ProjectRole(**state(opr).document) session(opr).clear() session(pr).flush() session(pr).clear()
# 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. '''Merge all the OldProjectRole collections in into a ProjectRole collection. ''' import logging from ming.orm import session, state from allura import model as M log = logging.getLogger(__name__) log.info('Moving project roles in database %s to main DB', M.Project.database_uri()) for opr in M.OldProjectRole.query.find(): pr = M.ProjectRole(**state(opr).document) session(opr).clear() session(pr).flush() session(pr).clear()
def bootstrap(command, conf, vars): """Place any commands to setup allura here""" # are we being called by the test suite? test_run = conf.get("__file__", "").endswith("test.ini") if not test_run: # when this is run via the `setup-app` cmd, some macro rendering needs this set up REGISTRY.register(ew.widget_context, ew.core.WidgetContext("http", ew.ResourceManager())) create_test_data = asbool(os.getenv("ALLURA_TEST_DATA", True)) # if this is a test_run, skip user project creation to save time make_user_projects = not test_run def make_user(*args, **kw): kw.update(make_project=make_user_projects) return create_user(*args, **kw) # Temporarily disable auth extensions to prevent unintended side-effects tg.config["auth.method"] = tg.config["registration.method"] = "local" assert tg.config["auth.method"] == "local" conf["auth.method"] = conf["registration.method"] = "local" # Clean up all old stuff ThreadLocalORMSession.close_all() c.user = c.project = c.app = None wipe_database() try: g.solr.delete(q="*:*") except: # pragma no cover log.error("SOLR server is %s", g.solr_server) log.error("Error clearing solr index") # set up mongo indexes index = EnsureIndexCommand("ensure_index") index.run([""]) if create_test_data and asbool(conf.get("cache_test_data")): if restore_test_data(): h.set_context("test", neighborhood="Projects") return log.info("Initializing search") log.info("Registering root user & default neighborhoods") M.User(_id=None, username="******", display_name="Anonymous") # never make a user project for the root user if create_test_data: root = create_user("Root", make_project=False) else: from getpass import getpass root_name = raw_input('Enter username for root user (default "root"): ').strip() if not root_name: root_name = "root" ok = False while not ok: root_password1 = getpass("Enter password: "******"Password must not be empty") continue root_password2 = getpass("Confirm password: "******"Passwords don't match") continue root = create_user(root_name, password=root_password1, make_project=False) ok = True n_projects = M.Neighborhood( name="Projects", url_prefix="/p/", features=dict(private_projects=True, max_projects=None, css="none", google_analytics=False), ) n_users = M.Neighborhood( name="Users", url_prefix="/u/", shortname_prefix="u/", anchored_tools="profile:Profile,userstats:Statistics", features=dict(private_projects=True, max_projects=None, css="none", google_analytics=False), ) assert tg.config["auth.method"] == "local" project_reg = plugin.ProjectRegistrationProvider.get() p_projects = project_reg.register_neighborhood_project(n_projects, [root], allow_register=True) p_users = project_reg.register_neighborhood_project(n_users, [root]) def set_nbhd_wiki_content(nbhd_proj, content): wiki = nbhd_proj.app_instance("wiki") page = WM.Page.query.get(app_config_id=wiki.config._id, title=wiki.root_page_name) page.text = content set_nbhd_wiki_content( p_projects, dedent( """ Welcome to the "Projects" neighborhood. It is the default neighborhood in Allura. You can edit this wiki page as you see fit. Here's a few ways to get started: [Register a new project](/p/add_project) [Neighborhood administration](/p/admin) [[projects show_total=yes]] """ ), ) set_nbhd_wiki_content( p_users, dedent( """ This is the "Users" neighborhood. All users automatically get a user-project created for them, using their username. [Neighborhood administration](/u/admin) [[projects show_total=yes]] """ ), ) if create_test_data: n_adobe = M.Neighborhood( name="Adobe", url_prefix="/adobe/", project_list_url="/adobe/", features=dict(private_projects=True, max_projects=None, css="custom", google_analytics=True), ) p_adobe = project_reg.register_neighborhood_project(n_adobe, [root]) set_nbhd_wiki_content( p_adobe, dedent( """ This is the "Adobe" neighborhood. It is just an example of having projects in a different neighborhood. [Neighborhood administration](/adobe/admin) [[projects show_total=yes]] """ ), ) # add the adobe icon file_name = "adobe_icon.png" file_path = os.path.join(allura.__path__[0], "public", "nf", "images", file_name) M.NeighborhoodFile.from_path(file_path, neighborhood_id=n_adobe._id) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() if create_test_data: # Add some test users for unum in range(10): make_user("Test User %d" % unum) log.info("Creating basic project categories") M.ProjectCategory(name="clustering", label="Clustering") cat2 = M.ProjectCategory(name="communications", label="Communications") M.ProjectCategory(name="synchronization", label="Synchronization", parent_id=cat2._id) M.ProjectCategory(name="streaming", label="Streaming", parent_id=cat2._id) M.ProjectCategory(name="fax", label="Fax", parent_id=cat2._id) M.ProjectCategory(name="bbs", label="BBS", parent_id=cat2._id) cat3 = M.ProjectCategory(name="database", label="Database") M.ProjectCategory(name="front_ends", label="Front-Ends", parent_id=cat3._id) M.ProjectCategory(name="engines_servers", label="Engines/Servers", parent_id=cat3._id) if create_test_data: log.info('Registering "regular users" (non-root) and default projects') # since this runs a lot for tests, separate test and default users and # do the minimal needed if asbool(conf.get("load_test_data")): u_admin = make_user("Test Admin") u_admin.preferences = dict(email_address="*****@*****.**") u_admin.email_addresses = ["*****@*****.**"] u_admin.set_password("foo") u_admin.claim_address("*****@*****.**") ThreadLocalORMSession.flush_all() admin_email = M.EmailAddress.get(email="*****@*****.**") admin_email.confirmed = True else: u_admin = make_user("Admin 1", username="******") # Admin1 is almost root, with admin access for Users and Projects # neighborhoods p_projects.add_user(u_admin, ["Admin"]) p_users.add_user(u_admin, ["Admin"]) n_projects.register_project("allura", u_admin, "Allura") make_user("Test User") n_adobe.register_project("adobe-1", u_admin, "Adobe project 1") p_adobe.add_user(u_admin, ["Admin"]) p0 = n_projects.register_project("test", u_admin, "Test Project") n_projects.register_project("test2", u_admin, "Test 2") p0._extra_tool_status = ["alpha", "beta"] sess = session(M.Neighborhood) # all the sessions are the same _list = (n_projects, n_users, p_projects, p_users) if create_test_data: _list += (n_adobe, p_adobe) for x in _list: # Ming doesn't detect substructural changes in newly created objects # (vs loaded from DB) state(x).status = "dirty" # TODO: Hope that Ming can be improved to at least avoid stuff below sess.flush(x) ThreadLocalORMSession.flush_all() if asbool(conf.get("load_test_data")): if asbool(conf.get("cache_test_data")): cache_test_data() else: # pragma no cover # regular first-time setup create_trove_categories = CreateTroveCategoriesCommand("create_trove_categories") create_trove_categories.run([""]) if create_test_data: p0.add_user(u_admin, ["Admin"]) log.info("Registering initial apps") with h.push_config(c, user=u_admin): p0.install_apps( [ {"ep_name": ep_name} for ep_name, app in g.entry_points["tool"].iteritems() if app._installable(tool_name=ep_name, nbhd=n_projects, project_tools=[]) ] ) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() if create_test_data: # reload our p0 project so that p0.app_configs is accurate with all the # newly installed apps p0 = M.Project.query.get(_id=p0._id) sub = p0.new_subproject("sub1", project_name="A Subproject") with h.push_config(c, user=u_admin): sub.install_app("wiki") ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all()
def bootstrap(command, conf, vars): """Place any commands to setup allura here""" # are we being called by the test suite? test_run = conf.get('__file__', '').endswith('test.ini') # if this is a test_run, skip user project creation to save time make_user_projects = not test_run def make_user(*args, **kw): kw.update(make_project=make_user_projects) return create_user(*args, **kw) # Our bootstrap doesn't play nicely with SFX project and user APIs tg.config['auth.method'] = tg.config['registration.method'] = 'local' assert tg.config['auth.method'] == 'local' conf['auth.method'] = conf['registration.method'] = 'local' # Clean up all old stuff ThreadLocalORMSession.close_all() c.queued_messages = defaultdict(list) c.user = c.project = c.app = None database = conf.get('db_prefix', '') + 'project:test' wipe_database() try: g.solr.delete(q='*:*') except: # pragma no cover log.error('SOLR server is %s', g.solr_server) log.error('Error clearing solr index') if asbool(conf.get('cache_test_data')): if restore_test_data(): from allura.lib import helpers as h h.set_context('test', neighborhood='Projects') return log.info('Initializing search') log.info('Registering root user & default neighborhoods') anonymous = M.User(_id=None, username='******', display_name='Anonymous') # never make a user project for the root user root = create_user('Root', make_project=False) n_projects = M.Neighborhood(name='Projects', url_prefix='/p/', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) n_users = M.Neighborhood( name='Users', url_prefix='/u/', shortname_prefix='u/', anchored_tools='profile:Profile,userstats:Statistics', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) n_adobe = M.Neighborhood(name='Adobe', url_prefix='/adobe/', project_list_url='/adobe/', features=dict(private_projects=True, max_projects=None, css='custom', google_analytics=True)) assert tg.config['auth.method'] == 'local' project_reg = plugin.ProjectRegistrationProvider.get() p_projects = project_reg.register_neighborhood_project(n_projects, [root], allow_register=True) p_users = project_reg.register_neighborhood_project(n_users, [root]) p_adobe = project_reg.register_neighborhood_project(n_adobe, [root]) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() # add the adobe icon file_name = 'adobe_icon.png' file_path = os.path.join(allura.__path__[0], 'public', 'nf', 'images', file_name) M.NeighborhoodFile.from_path(file_path, neighborhood_id=n_adobe._id) # Add some test users for unum in range(10): make_user('Test User %d' % unum) log.info('Creating basic project categories') cat1 = M.ProjectCategory(name='clustering', label='Clustering') cat2 = M.ProjectCategory(name='communications', label='Communications') cat2_1 = M.ProjectCategory(name='synchronization', label='Synchronization', parent_id=cat2._id) cat2_2 = M.ProjectCategory(name='streaming', label='Streaming', parent_id=cat2._id) cat2_3 = M.ProjectCategory(name='fax', label='Fax', parent_id=cat2._id) cat2_4 = M.ProjectCategory(name='bbs', label='BBS', parent_id=cat2._id) cat3 = M.ProjectCategory(name='database', label='Database') cat3_1 = M.ProjectCategory(name='front_ends', label='Front-Ends', parent_id=cat3._id) cat3_2 = M.ProjectCategory(name='engines_servers', label='Engines/Servers', parent_id=cat3._id) log.info('Registering "regular users" (non-root) and default projects') # since this runs a lot for tests, separate test and default users and # do the minimal needed if asbool(conf.get('load_test_data')): u_admin = make_user('Test Admin') u_admin.preferences = dict(email_address='*****@*****.**') u_admin.email_addresses = ['*****@*****.**'] u_admin.set_password('foo') u_admin.claim_address('*****@*****.**') else: u_admin = make_user('Admin 1', username='******') # Admin1 is almost root, with admin access for Users and Projects neighborhoods p_projects.add_user(u_admin, ['Admin']) p_users.add_user(u_admin, ['Admin']) p_allura = n_projects.register_project('allura', u_admin) u1 = make_user('Test User') p_adobe1 = n_adobe.register_project('adobe-1', u_admin) p_adobe.add_user(u_admin, ['Admin']) p0 = n_projects.register_project('test', u_admin) p1 = n_projects.register_project('test2', u_admin) p0._extra_tool_status = ['alpha', 'beta'] sess = session(M.Neighborhood) # all the sessions are the same for x in (n_adobe, n_projects, n_users, p_projects, p_users, p_adobe): # Ming doesn't detect substructural changes in newly created objects (vs loaded from DB) state(x).status = 'dirty' # TODO: Hope that Ming can be improved to at least avoid stuff below sess.flush(x) c.project = p0 c.user = u_admin p1 = p0.new_subproject('sub1') ThreadLocalORMSession.flush_all() if asbool(conf.get('load_test_data')): if asbool(conf.get('cache_test_data')): cache_test_data() else: # pragma no cover # regular first-time setup p0.add_user(u_admin, ['Admin']) log.info('Registering initial apps') for ep_name, app in g.entry_points['tool'].iteritems(): if not app.installable: continue p0.install_app(ep_name) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all()
def bootstrap(command, conf, vars): """Place any commands to setup allura here""" # are we being called by the test suite? test_run = conf.get('__file__', '').endswith('test.ini') # if this is a test_run, skip user project creation to save time make_user_projects = not test_run def make_user(*args, **kw): kw.update(make_project=make_user_projects) return create_user(*args, **kw) # Temporarily disable auth extensions to prevent unintended side-effects tg.config['auth.method'] = tg.config['registration.method'] = 'local' assert tg.config['auth.method'] == 'local' conf['auth.method'] = conf['registration.method'] = 'local' # Clean up all old stuff ThreadLocalORMSession.close_all() c.queued_messages = defaultdict(list) c.user = c.project = c.app = None database = conf.get('db_prefix', '') + 'project:test' wipe_database() try: g.solr.delete(q='*:*') except: # pragma no cover log.error('SOLR server is %s', g.solr_server) log.error('Error clearing solr index') if asbool(conf.get('cache_test_data')): if restore_test_data(): h.set_context('test', neighborhood='Projects') return log.info('Initializing search') log.info('Registering root user & default neighborhoods') anonymous = M.User( _id=None, username='******', display_name='Anonymous') # never make a user project for the root user root = create_user('Root', make_project=False) n_projects = M.Neighborhood(name='Projects', url_prefix='/p/', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) n_users = M.Neighborhood(name='Users', url_prefix='/u/', shortname_prefix='u/', anchored_tools='profile:Profile,userstats:Statistics', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) n_adobe = M.Neighborhood( name='Adobe', url_prefix='/adobe/', project_list_url='/adobe/', features=dict(private_projects=True, max_projects=None, css='custom', google_analytics=True)) assert tg.config['auth.method'] == 'local' project_reg = plugin.ProjectRegistrationProvider.get() p_projects = project_reg.register_neighborhood_project( n_projects, [root], allow_register=True) p_users = project_reg.register_neighborhood_project(n_users, [root]) p_adobe = project_reg.register_neighborhood_project(n_adobe, [root]) def set_nbhd_wiki_content(nbhd_proj, content): wiki = nbhd_proj.app_instance('wiki') page = WM.Page.query.get( app_config_id=wiki.config._id, title=wiki.root_page_name) page.text = content set_nbhd_wiki_content(p_projects, dedent(''' Welcome to the "Projects" neighborhood. It is the default neighborhood in Allura. You can edit this wiki page as you see fit. Here's a few ways to get started: [Register a new project](/p/add_project) [Neighborhood administration](/p/admin) [[projects show_total=yes]] ''')) set_nbhd_wiki_content(p_users, dedent(''' This is the "Users" neighborhood. All users automatically get a user-project created for them, using their username. [Neighborhood administration](/u/admin) [[projects show_total=yes]] ''')) set_nbhd_wiki_content(p_adobe, dedent(''' This is the "Adobe" neighborhood. It is just an example of having projects in a different neighborhood. [Neighborhood administration](/adobe/admin) [[projects show_total=yes]] ''')) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() # add the adobe icon file_name = 'adobe_icon.png' file_path = os.path.join( allura.__path__[0], 'public', 'nf', 'images', file_name) M.NeighborhoodFile.from_path(file_path, neighborhood_id=n_adobe._id) # Add some test users for unum in range(10): make_user('Test User %d' % unum) log.info('Creating basic project categories') cat1 = M.ProjectCategory(name='clustering', label='Clustering') cat2 = M.ProjectCategory(name='communications', label='Communications') cat2_1 = M.ProjectCategory( name='synchronization', label='Synchronization', parent_id=cat2._id) cat2_2 = M.ProjectCategory( name='streaming', label='Streaming', parent_id=cat2._id) cat2_3 = M.ProjectCategory(name='fax', label='Fax', parent_id=cat2._id) cat2_4 = M.ProjectCategory(name='bbs', label='BBS', parent_id=cat2._id) cat3 = M.ProjectCategory(name='database', label='Database') cat3_1 = M.ProjectCategory( name='front_ends', label='Front-Ends', parent_id=cat3._id) cat3_2 = M.ProjectCategory( name='engines_servers', label='Engines/Servers', parent_id=cat3._id) log.info('Registering "regular users" (non-root) and default projects') # since this runs a lot for tests, separate test and default users and # do the minimal needed if asbool(conf.get('load_test_data')): u_admin = make_user('Test Admin') u_admin.preferences = dict(email_address='*****@*****.**') u_admin.email_addresses = ['*****@*****.**'] u_admin.set_password('foo') u_admin.claim_address('*****@*****.**') else: u_admin = make_user('Admin 1', username='******') # Admin1 is almost root, with admin access for Users and Projects # neighborhoods p_projects.add_user(u_admin, ['Admin']) p_users.add_user(u_admin, ['Admin']) p_allura = n_projects.register_project('allura', u_admin, 'Allura') u1 = make_user('Test User') p_adobe1 = n_adobe.register_project('adobe-1', u_admin, 'Adobe project 1') p_adobe.add_user(u_admin, ['Admin']) p0 = n_projects.register_project('test', u_admin, 'Test Project') p1 = n_projects.register_project('test2', u_admin, 'Test 2') p0._extra_tool_status = ['alpha', 'beta'] sess = session(M.Neighborhood) # all the sessions are the same for x in (n_adobe, n_projects, n_users, p_projects, p_users, p_adobe): # Ming doesn't detect substructural changes in newly created objects # (vs loaded from DB) state(x).status = 'dirty' # TODO: Hope that Ming can be improved to at least avoid stuff below sess.flush(x) ThreadLocalORMSession.flush_all() if asbool(conf.get('load_test_data')): if asbool(conf.get('cache_test_data')): cache_test_data() else: # pragma no cover # regular first-time setup p0.add_user(u_admin, ['Admin']) log.info('Registering initial apps') with h.push_config(c, user=u_admin): for ep_name, app in g.entry_points['tool'].iteritems(): if not app.installable: continue p0.install_app(ep_name) # reload our p0 project so that p0.app_configs is accurate with all the # newly installed apps ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() p0 = M.Project.query.get(_id=p0._id) sub = p0.new_subproject('sub1', project_name='A Subproject') with h.push_config(c, user=u_admin): sub.install_app('wiki') ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all()