def set_context(project_shortname_or_id, mount_point=None, app_config_id=None, neighborhood=None): from allura import model try: p = model.Project.query.get(_id=ObjectId(str(project_shortname_or_id))) except InvalidId: p = None if p is None and type(project_shortname_or_id) != ObjectId: if neighborhood is None: raise TypeError('neighborhood is required; it must not be None') if not isinstance(neighborhood, model.Neighborhood): n = model.Neighborhood.query.get(name=neighborhood) if n is None: try: n = model.Neighborhood.query.get(_id=ObjectId(str(neighborhood))) except InvalidId: pass if n is None: raise exc.NoSuchNeighborhoodError("Couldn't find neighborhood %s" % repr(neighborhood)) neighborhood = n query = dict(shortname=project_shortname_or_id, neighborhood_id=neighborhood._id) p = model.Project.query.get(**query) if p is None: raise exc.NoSuchProjectError("Couldn't find project %s nbhd %s" % (project_shortname_or_id, neighborhood)) c.project = p if app_config_id is None: c.app = p.app_instance(mount_point) else: if isinstance(app_config_id, basestring): app_config_id = ObjectId(app_config_id) app_config = model.AppConfig.query.get(_id=app_config_id) c.app = p.app_instance(app_config)
def command(self): self.basic_setup() if len(self.args) >= 2: p_name_or_id = self.args[1] try: project = M.Project.query.get(_id=ObjectId(p_name_or_id)) except InvalidId: projects = M.Project.query.find({'$or': [ {'shortname': p_name_or_id}, {'name': p_name_or_id} ]}) if projects.count() > 1: raise exc.ForgeError('Multiple projects has a shortname %s. ' 'Use project _id instead.' % p_name_or_id) project = projects.first() if not project: raise exc.NoSuchProjectError('The project %s ' 'could not be found' % p_name_or_id) self.fix_for_project(project) else: base.log.info( 'Checking discussion instances for each tracker in all projects') for project in M.Project.query.find(): self.fix_for_project(project)
def set_context(project_shortname_or_id, mount_point=None, app_config_id=None, neighborhood=None): """ Set ``c.project`` and ``c.app`` globals :param project_id: _id or shortname of a project :type project_id: ObjectId|str :param mount_point: mount point to set c.app by :type mount_point: str :param app_config_id: alternative to mount_point parameter :type app_config_id: ObjectId|str :param neighborhood: neighborhood full name, required if project is specified by shortname :type neighborhood: str """ from allura import model try: p = model.Project.query.get(_id=ObjectId(str(project_shortname_or_id))) except InvalidId: p = None if p is None and not isinstance(project_shortname_or_id, ObjectId): if neighborhood is None: raise TypeError('neighborhood is required; it must not be None') if not isinstance(neighborhood, model.Neighborhood): n = model.Neighborhood.query.get(name=neighborhood) if n is None: try: n = model.Neighborhood.query.get( _id=ObjectId(str(neighborhood))) except InvalidId: pass if n is None: raise exc.NoSuchNeighborhoodError( "Couldn't find neighborhood %s" % repr(neighborhood)) neighborhood = n query = dict(shortname=project_shortname_or_id, neighborhood_id=neighborhood._id) p = model.Project.query.get(**query) if p is None: raise exc.NoSuchProjectError("Couldn't find project %s nbhd %s" % (project_shortname_or_id, neighborhood)) c.project = p if app_config_id is None: c.app = p.app_instance(mount_point) else: if isinstance(app_config_id, six.string_types): app_config_id = ObjectId(app_config_id) app_config = model.AppConfig.query.get(_id=app_config_id) c.app = p.app_instance(app_config)