Exemplo n.º 1
0
def test_url_for_group_encodes_groupid(hashids):
    """It should encode the groupid to get the hashid.

    And then pass the hashid to route_url().

    """
    hashids.encode.return_value = mock.sentinel.hashid
    request = mock.Mock()
    group = mock.Mock()

    logic.url_for_group(request, group)

    assert hashids.encode.call_args[1]['number'] == group.id
    assert request.route_url.call_args[1]['hashid'] == mock.sentinel.hashid
Exemplo n.º 2
0
def test_url_for_group_encodes_groupid(hashids):
    """It should encode the groupid to get the hashid.

    And then pass the hashid to route_url().

    """
    hashids.encode.return_value = mock.sentinel.hashid
    request = mock.Mock()
    group = mock.Mock()

    logic.url_for_group(request, group)

    assert hashids.encode.call_args[1]['number'] == group.id
    assert request.route_url.call_args[1]['hashid'] == mock.sentinel.hashid
Exemplo n.º 3
0
Arquivo: views.py Projeto: ningyifan/h
def read(request):
    """Render the page for a group."""
    if not request.feature('groups'):
        raise exc.HTTPNotFound()

    hashid = request.matchdict["hashid"]
    slug = request.matchdict.get("slug")
    group_id = hashids.decode(request, 'h.groups', hashid)

    group = models.Group.get_by_id(group_id)
    if group is None:
        raise exc.HTTPNotFound()

    if slug is None or slug != group.slug:
        return exc.HTTPMovedPermanently(
            location=logic.url_for_group(request, group))

    if not request.authenticated_userid:
        return _login_to_join(request, group)
    else:
        user = accounts_models.User.get_by_userid(
            request.domain, request.authenticated_userid)
        if group in user.groups:
            return _read_group(request, group)
        else:
            return _join(request, group)
Exemplo n.º 4
0
def read(request):
    """Render the page for a group."""
    if not request.feature('groups'):
        raise exc.HTTPNotFound()

    hashid = request.matchdict["hashid"]
    slug = request.matchdict.get("slug")
    group_id = hashids.decode(request, 'h.groups', hashid)

    group = models.Group.get_by_id(group_id)
    if group is None:
        raise exc.HTTPNotFound()

    if slug is None or slug != group.slug:
        return exc.HTTPMovedPermanently(
            location=logic.url_for_group(request, group))

    if not request.authenticated_userid:
        return _login_to_join(request, group)
    else:
        user = accounts_models.User.get_by_userid(request.domain,
                                                  request.authenticated_userid)
        if group in user.groups:
            return _read_group(request, group)
        else:
            return _join(request, group)
Exemplo n.º 5
0
def test_url_for_group_returns_url(_):
    """It should return the URL from request.route_url()."""
    request = mock.Mock()
    request.route_url.return_value = mock.sentinel.group_url

    url = logic.url_for_group(request, mock.Mock())

    assert url == mock.sentinel.group_url
Exemplo n.º 6
0
def test_url_for_group_returns_url(_):
    """It should return the URL from request.route_url()."""
    request = mock.Mock()
    request.route_url.return_value = mock.sentinel.group_url

    url = logic.url_for_group(request, mock.Mock())

    assert url == mock.sentinel.group_url
Exemplo n.º 7
0
Arquivo: views.py Projeto: ningyifan/h
def _read_group(request, group):
    """Return the rendered "Share this group" page.

    This is the page that's shown when a user who is already a member of a
    group visits the group's URL.

    """
    template_data = {
        'group': group, 'group_url': logic.url_for_group(request, group)}
    return renderers.render_to_response(
        renderer_name='h:groups/templates/read.html.jinja2',
        value=template_data, request=request)
Exemplo n.º 8
0
def _read_group(request, group):
    """Return the rendered "Share this group" page.

    This is the page that's shown when a user who is already a member of a
    group visits the group's URL.

    """
    template_data = {
        'group': group,
        'group_url': logic.url_for_group(request, group)
    }
    return renderers.render_to_response(
        renderer_name='h:groups/templates/read.html.jinja2',
        value=template_data,
        request=request)
Exemplo n.º 9
0
Arquivo: views.py Projeto: ningyifan/h
def create(request):
    """Respond to a submission of the create group form."""
    if not request.feature('groups'):
        raise exc.HTTPNotFound()

    form = deform.Form(schemas.GroupSchema().bind(request=request))
    try:
        appstruct = form.validate(request.POST.items())
    except deform.ValidationFailure:
        return {'form': form, 'data': request.params}

    user = accounts_models.User.get_by_userid(
        request.domain, request.authenticated_userid)
    group = models.Group(name=appstruct["name"], creator=user)
    request.db.add(group)

    # We need to flush the db session here so that group.id will be generated.
    request.db.flush()

    return exc.HTTPSeeOther(logic.url_for_group(request, group))
Exemplo n.º 10
0
def create(request):
    """Respond to a submission of the create group form."""
    if not request.feature('groups'):
        raise exc.HTTPNotFound()

    form = deform.Form(schemas.GroupSchema().bind(request=request))
    try:
        appstruct = form.validate(request.POST.items())
    except deform.ValidationFailure:
        return {'form': form, 'data': request.params}

    user = accounts_models.User.get_by_userid(request.domain,
                                              request.authenticated_userid)
    group = models.Group(name=appstruct["name"], creator=user)
    request.db.add(group)

    # We need to flush the db session here so that group.id will be generated.
    request.db.flush()

    return exc.HTTPSeeOther(logic.url_for_group(request, group))
Exemplo n.º 11
0
Arquivo: views.py Projeto: ningyifan/h
def join(request):
    if not request.feature('groups'):
        raise exc.HTTPNotFound()

    hashid = request.matchdict["hashid"]
    group_id = hashids.decode(request, "h.groups", hashid)
    group = models.Group.get_by_id(group_id)

    if group is None:
        raise exc.HTTPNotFound()

    user = accounts_models.User.get_by_userid(
        request.domain, request.authenticated_userid)

    group.members.append(user)

    request.session.flash(_(
        "You've joined the {name} group.").format(name=group.name),
        'success')

    return exc.HTTPSeeOther(logic.url_for_group(request, group))
Exemplo n.º 12
0
def join(request):
    if not request.feature('groups'):
        raise exc.HTTPNotFound()

    hashid = request.matchdict["hashid"]
    group_id = hashids.decode(request, "h.groups", hashid)
    group = models.Group.get_by_id(group_id)

    if group is None:
        raise exc.HTTPNotFound()

    user = accounts_models.User.get_by_userid(request.domain,
                                              request.authenticated_userid)

    group.members.append(user)

    request.session.flash(
        _("You've joined the {name} group.").format(name=group.name),
        'success')

    return exc.HTTPSeeOther(logic.url_for_group(request, group))