Ejemplo n.º 1
0
def nbhd_lookup_first_path(nbhd, name, current_user, remainder, api=False):
    """
    Resolve first part of a neighborhood url.  May raise 404, redirect, or do other side effects.

    Shared between NeighborhoodController and NeighborhoodRestController

    :param nbhd: neighborhood
    :param name: project or tool name (next part of url)
    :param current_user: a User
    :param remainder: remainder of url
    :param bool api: whether this is handling a /rest/ request or not

    :return: project (to be set as c.project)
    :return: remainder (possibly modified)
    """

    prefix = nbhd.shortname_prefix
    pname = unquote(name)
    provider = plugin.ProjectRegistrationProvider.get()
    try:
        provider.shortname_validator.to_python(pname, check_allowed=False, neighborhood=nbhd)
    except Invalid:
        project = None
    else:
        project = M.Project.query.get(shortname=prefix + pname, neighborhood_id=nbhd._id)
    if project is None and prefix == "u/":
        # create user-project if it is missing
        user = M.User.query.get(username=pname, disabled=False, pending=False)
        if user:
            project = user.private_project()
            if project.shortname != prefix + pname:
                # might be different URL than the URL requested
                # e.g. if username isn't valid project name and user_project_shortname() converts the name
                new_url = project.url()
                if api:
                    new_url = "/rest" + new_url
                new_url += "/".join(remainder)
                if request.query_string:
                    new_url += "?" + request.query_string
                redirect(new_url)
    if project is None:
        # look for neighborhood tools matching the URL
        project = nbhd.neighborhood_project
        return project, (pname,) + remainder  # include pname in new remainder, it is actually the nbhd tool path
    if project and prefix == "u/":
        # make sure user-projects are associated with an enabled user
        user = project.user_project_of
        if not user or user.disabled or user.pending:
            raise exc.HTTPNotFound
    if project.database_configured is False:
        if remainder == ("user_icon",):
            redirect(g.forge_static("images/user.png"))
        elif current_user.username == pname:
            log.info("Configuring %s database for access to %r", pname, remainder)
            project.configure_project(is_user_project=True)
        else:
            raise exc.HTTPNotFound, pname
    if project is None or (project.deleted and not has_access(project, "update")()):
        raise exc.HTTPNotFound, pname
    return project, remainder
Ejemplo n.º 2
0
def nbhd_lookup_first_path(nbhd, name, current_user, remainder, api=False):
    """
    Resolve first part of a neighborhood url.  May raise 404, redirect, or do other side effects.

    Shared between NeighborhoodController and NeighborhoodRestController

    :param nbhd: neighborhood
    :param name: project or tool name (next part of url)
    :param current_user: a User
    :param remainder: remainder of url
    :param bool api: whether this is handling a /rest/ request or not

    :return: project (to be set as c.project)
    :return: remainder (possibly modified)
    """

    prefix = nbhd.shortname_prefix
    pname = unquote(name)
    provider = plugin.ProjectRegistrationProvider.get()
    try:
        provider.shortname_validator.to_python(pname, check_allowed=False, neighborhood=nbhd)
    except Invalid:
        project = None
    else:
        project = M.Project.query.get(shortname=prefix + pname, neighborhood_id=nbhd._id)
    if project is None and prefix == 'u/':
        # create user-project if it is missing
        user = M.User.query.get(username=pname, disabled=False, pending=False)
        if user:
            project = user.private_project()
            if project.shortname != prefix + pname:
                # might be different URL than the URL requested
                # e.g. if username isn't valid project name and user_project_shortname() converts the name
                new_url = project.url()
                if api:
                    new_url = '/rest' + new_url
                new_url += '/'.join(remainder)
                if request.query_string:
                    new_url += '?' + request.query_string
                redirect(new_url)
    if project is None:
        # look for neighborhood tools matching the URL
        project = nbhd.neighborhood_project
        return project, (pname,) + remainder  # include pname in new remainder, it is actually the nbhd tool path
    if project and prefix == 'u/':
        # make sure user-projects are associated with an enabled user
        user = project.user_project_of
        if not user or user.disabled or user.pending:
            raise exc.HTTPNotFound
    if project.database_configured is False:
        if remainder == ('user_icon',):
            redirect(g.forge_static('images/user.png'))
        elif current_user.username == pname:
            log.info('Configuring %s database for access to %r', pname, remainder)
            project.configure_project(is_user_project=True)
        else:
            raise exc.HTTPNotFound, pname
    if project is None or (project.deleted and not has_access(project, 'update')()):
        raise exc.HTTPNotFound, pname
    return project, remainder
 def _lookup(self, pname, *remainder):
     pname = unquote(pname)
     if not h.re_project_name.match(pname):
         raise exc.HTTPNotFound, pname
     project = M.Project.query.get(shortname=self.prefix + pname,
                                   neighborhood_id=self.neighborhood._id)
     if project is None:
         project = self.neighborhood.neighborhood_project
         c.project = project
         return ProjectController()._lookup(pname, *remainder)
     if project.database_configured == False:
         if remainder == ('user_icon', ):
             redirect(g.forge_static('images/user.png'))
         elif c.user.username == pname:
             log.info('Configuring %s database for access to %r', pname,
                      remainder)
             project.configure_project(is_user_project=True)
         else:
             raise exc.HTTPNotFound, pname
     c.project = project
     if project is None or (project.deleted
                            and not has_access(c.project, 'update')()):
         raise exc.HTTPNotFound, pname
     if project.neighborhood.name != self.neighborhood_name:
         redirect(project.url())
     return ProjectController(), remainder
Ejemplo n.º 4
0
 def user_icon(self, **kw):
     try:
         return self.icon(**kw)
     except exc.HTTPNotFound:
         if config.get('default_avatar_image'):
             default_image_url = config['default_avatar_image']
         else:
             default_image_url = g.forge_static('images/user.png')
         redirect(default_image_url)
Ejemplo n.º 5
0
    def icon_url(cls, size):
        """Return URL for icon of the given ``size``.

        Subclasses can define their own icons by overriding
        :attr:`icons`.

        """
        resource, url = cls.icons.get(size), ""
        if resource:
            resource_path = os.path.join("nf", resource)
            url = g.forge_static(resource) if cls.has_resource(resource_path) else g.theme_href(resource)
        return url
Ejemplo n.º 6
0
    def icon_url(cls, size):
        """Return URL for icon of the given ``size``.

        Subclasses can define their own icons by overriding
        :attr:`icons`.

        """
        resource, url = cls.icons.get(size), ''
        if resource:
            resource_path = os.path.join('nf', resource)
            url = (g.forge_static(resource) if cls.has_resource(resource_path)
                   else g.theme_href(resource))
        return url
Ejemplo n.º 7
0
 def _lookup(self, pname, *remainder):
     pname = unquote(pname)
     provider = plugin.ProjectRegistrationProvider.get()
     try:
         provider.shortname_validator.to_python(
             pname, check_allowed=False, neighborhood=self.neighborhood)
     except Invalid:
         project = None
     else:
         project = M.Project.query.get(
             shortname=self.prefix + pname,
             neighborhood_id=self.neighborhood._id)
     if project is None and self.prefix == 'u/':
         # create user-project if it is missing
         user = M.User.query.get(username=pname,
                                 disabled=False,
                                 pending=False)
         if user:
             project = user.private_project()
             if project.shortname != self.prefix + pname:
                 # might be different URL than the URL requested
                 # e.g. if username isn't valid project name and user_project_shortname() converts the name
                 redirect(project.url())
     if project is None:
         # look for neighborhood tools matching the URL
         project = self.neighborhood.neighborhood_project
         c.project = project
         return ProjectController()._lookup(pname, *remainder)
     if project and self.prefix == 'u/':
         # make sure user-projects are associated with an enabled user
         user = project.user_project_of
         if not user or user.disabled or user.pending:
             raise exc.HTTPNotFound
     if project.database_configured == False:
         if remainder == ('user_icon', ):
             redirect(g.forge_static('images/user.png'))
         elif c.user.username == pname:
             log.info('Configuring %s database for access to %r', pname,
                      remainder)
             project.configure_project(is_user_project=True)
         else:
             raise exc.HTTPNotFound, pname
     c.project = project
     if project is None or (project.deleted
                            and not has_access(c.project, 'update')()):
         raise exc.HTTPNotFound, pname
     return ProjectController(), remainder
Ejemplo n.º 8
0
 def _lookup(self, pname, *remainder):
     pname = unquote(pname)
     provider = plugin.ProjectRegistrationProvider.get()
     try:
         provider.shortname_validator.to_python(
             pname, check_allowed=False, neighborhood=self.neighborhood)
     except Invalid:
         project = None
     else:
         project = M.Project.query.get(
             shortname=self.prefix + pname, neighborhood_id=self.neighborhood._id)
     if project is None and self.prefix == 'u/':
         # create user-project if it is missing
         user = M.User.query.get(username=pname, disabled=False)
         if user:
             project = self.neighborhood.register_project(
                 plugin.AuthenticationProvider.get(
                     request).user_project_shortname(user),
                 user=user, user_project=True)
     if project is None:
         # look for neighborhood tools matching the URL
         project = self.neighborhood.neighborhood_project
         c.project = project
         return ProjectController()._lookup(pname, *remainder)
     if project and self.prefix == 'u/':
         # make sure user-projects are associated with an enabled user
         user = project.user_project_of
         if not user or user.disabled:
             raise exc.HTTPNotFound
     if project.database_configured == False:
         if remainder == ('user_icon',):
             redirect(g.forge_static('images/user.png'))
         elif c.user.username == pname:
             log.info('Configuring %s database for access to %r',
                      pname, remainder)
             project.configure_project(is_user_project=True)
         else:
             raise exc.HTTPNotFound, pname
     c.project = project
     if project is None or (project.deleted and not has_access(c.project, 'update')()):
         raise exc.HTTPNotFound, pname
     if project.neighborhood.name != self.neighborhood_name:
         redirect(project.url())
     return ProjectController(), remainder
 def _lookup(self, pname, *remainder):
     pname = unquote(pname)
     if not h.re_project_name.match(pname):
         raise exc.HTTPNotFound, pname
     project = M.Project.query.get(shortname=self.prefix + pname, neighborhood_id=self.neighborhood._id)
     if project is None:
         project = self.neighborhood.neighborhood_project
         c.project = project
         return ProjectController()._lookup(pname, *remainder)
     if project.database_configured == False:
         if remainder == ('user_icon',):
             redirect(g.forge_static('images/user.png'))
         elif c.user.username == pname:
             log.info('Configuring %s database for access to %r',
                      pname, remainder)
             project.configure_project(is_user_project=True)
         else:
             raise exc.HTTPNotFound, pname
     c.project = project
     if project is None or (project.deleted and not has_access(c.project, 'update')()):
         raise exc.HTTPNotFound, pname
     if project.neighborhood.name != self.neighborhood_name:
         redirect(project.url())
     return ProjectController(), remainder
Ejemplo n.º 10
0
 def user_icon(self, **kw):
     try:
         return self.icon()
     except exc.HTTPNotFound:
         redirect(g.forge_static('images/user.png'))
Ejemplo n.º 11
0
 def user_icon(self, **kw):
     try:
         return self.icon()
     except exc.HTTPNotFound:
         redirect(g.forge_static('images/user.png'))