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() n_id = self.args[1] n_feature = self.args[2] # try to get a bool or int val, otherwise treat it as a string try: n_value = literal_eval(self.args[3]) except ValueError: n_value = self.args[3] if n_feature not in [ "max_projects", "css", "google_analytics", "private_projects" ]: raise exceptions.NoSuchNBFeatureError( "%s is not a valid " "neighborhood feature. The valid features are \"max_projects\", " "\"css\", \"google_analytics\" and \"private_projects\"" % n_feature) n = M.Neighborhood.query.get(name=n_id) if not n: n = M.Neighborhood.query.get(_id=ObjectId(n_id)) if not n: raise exceptions.NoSuchNeighborhoodError( "The neighborhood %s " "could not be found in the database" % n_id) else: if n_feature == "max_projects": if isinstance(n_value, int) or n_value is None: n.features['max_projects'] = n_value else: raise exceptions.InvalidNBFeatureValueError( "max_projects must be " "an int or None.") elif n_feature == "css": if n_value in ['none', 'custom', 'picker']: n.features['css'] = n_value else: raise exceptions.InvalidNBFeatureValueError( "css must be " "'none', 'custom', or 'picker'") elif n_feature == "google_analytics": if isinstance(n_value, bool): n.features['google_analytics'] = n_value else: raise exceptions.InvalidNBFeatureValueError( "google_analytics must be " "a boolean") else: if isinstance(n_value, bool): n.features['private_projects'] = n_value else: raise exceptions.InvalidNBFeatureValueError( "private_projects must be " "a boolean") session(M.Neighborhood).flush()
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)
def command(self): self.basic_setup() shortname = self.args[1] nb = M.Neighborhood.query.get(name=shortname) if not nb: nb = M.Neighborhood.query.get(_id=ObjectId(shortname)) if nb is None: raise exceptions.NoSuchNeighborhoodError( "The neighborhood %s " "could not be found in the database" % shortname) tool_value = self.args[2].lower() if tool_value[:1] == "t": home_tool_active = True else: home_tool_active = False if home_tool_active == nb.has_home_tool: return p = nb.neighborhood_project if home_tool_active: zero_position_exists = False for ac in p.app_configs: if ac.options['ordinal'] == 0: zero_position_exists = True break if zero_position_exists: for ac in p.app_configs: ac.options['ordinal'] = ac.options['ordinal'] + 1 p.install_app('home', 'home', 'Home', ordinal=0) else: app_config = p.app_config('home') zero_position_exists = False if app_config.options['ordinal'] == 0: zero_position_exists = True p.uninstall_app('home') if zero_position_exists: for ac in p.app_configs: ac.options['ordinal'] = ac.options['ordinal'] - 1 session(M.AppConfig).flush() session(M.Neighborhood).flush()