Example #1
0
def curated_groups_autocomplete(request):
    if 'q' not in request.GET:
        return http.HttpResponseBadRequest('q')
    q = request.GET.get('q', '').strip()
    if not q:
        return {'groups': []}

    all_ = mozillians.get_all_groups(name=q)
    ids = [x['id'] for x in all_]
    all_.extend([
        x for x in mozillians.get_all_groups(name_search=q)
        if x['id'] not in ids
    ])

    def describe_group(group):
        if group['member_count'] == 1:
            return '%s (1 member)' % (group['name'],)
        else:
            return (
                '%s (%s members)' % (group['name'], group['member_count'])
            )

    groups = [
        (x['name'], describe_group(x))
        for x in all_
    ]
    # naively sort by how good the match is
    groups.sort(key=lambda x: x[0].lower().find(q.lower()))
    return {'groups': groups}
Example #2
0
    def test_get_all_groups_failure(self, rget):
        def mocked_get(url, **options):
            return Response('Failed', status_code=500)

        rget.side_effect = mocked_get

        try:
            mozillians.get_all_groups()
            raise AssertionError("shouldn't happen")
        except mozillians.BadStatusCodeError as msg:
            ok_(settings.MOZILLIANS_API_KEY not in str(msg))
            ok_('xxxscrubbedxxx' in str(msg))
Example #3
0
    def test_get_all_groups_failure(self, rget):

        def mocked_get(url, **options):
            return Response('Failed', status_code=500)

        rget.side_effect = mocked_get

        try:
            mozillians.get_all_groups()
            raise AssertionError("shouldn't happen")
        except mozillians.BadStatusCodeError as msg:
            ok_(settings.MOZILLIANS_API_KEY not in str(msg))
            ok_('xxxscrubbedxxx' in str(msg))
Example #4
0
def curated_groups_autocomplete(request):
    if 'q' not in request.GET:
        return http.HttpResponseBadRequest('q')
    q = request.GET.get('q', '').strip()
    if not q:
        return {'groups': []}

    all_ = mozillians.get_all_groups(name=q)
    ids = [x['id'] for x in all_]
    all_.extend([
        x for x in mozillians.get_all_groups(name_search=q)
        if x['id'] not in ids
    ])

    def describe_group(group):
        if group['member_count'] == 1:
            return '%s (1 member)' % (group['name'], )
        else:
            return ('%s (%s members)' % (group['name'], group['member_count']))

    groups = [(x['name'], describe_group(x)) for x in all_]
    # naively sort by how good the match is
    groups.sort(key=lambda x: x[0].lower().find(q.lower()))
    return {'groups': groups}
Example #5
0
    def test_get_all_groups(self, rget, rlogging):
        calls = []

        def mocked_get(url, **options):
            calls.append(url)
            if 'offset=0' in url:
                return Response(GROUPS1)
            if 'offset=500' in url:
                return Response(GROUPS2)
            raise NotImplementedError(url)
        rget.side_effect = mocked_get

        all = mozillians.get_all_groups()
        eq_(len(all), 750)
        eq_(all[0]['name'], 'GROUP NUMBER 1')
        eq_(all[500]['name'], 'GROUP NUMBER 2')
        eq_(len(calls), 2)
Example #6
0
    def test_get_all_groups(self, rget):
        calls = []

        def mocked_get(url, **options):
            calls.append(url)
            if '/v2/groups/' in url and 'page=2' in url:
                return Response(GROUPS2)
            if '/v2/groups/' in url:
                return Response(GROUPS1)
            raise NotImplementedError(url)
        rget.side_effect = mocked_get

        all = mozillians.get_all_groups()
        eq_(len(all), 3)
        eq_(all[0]['name'], 'GROUP NUMBER 1')
        eq_(all[1]['name'], 'GROUP NUMBER 2')
        eq_(all[2]['name'], 'GROUP NUMBER 3')
        eq_(len(calls), 2)
Example #7
0
    def test_get_all_groups(self, rget):
        calls = []

        def mocked_get(url, **options):
            calls.append(url)
            if '/v2/groups/' in url and 'page=2' in url:
                return Response(GROUPS2)
            if '/v2/groups/' in url:
                return Response(GROUPS1)
            raise NotImplementedError(url)
        rget.side_effect = mocked_get

        all = mozillians.get_all_groups()
        eq_(len(all), 3)
        eq_(all[0]['name'], 'GROUP NUMBER 1')
        eq_(all[1]['name'], 'GROUP NUMBER 2')
        eq_(all[2]['name'], 'GROUP NUMBER 3')
        eq_(len(calls), 2)
Example #8
0
    def test_get_all_groups(self, rget, rlogging):
        calls = []

        def mocked_get(url, **options):
            calls.append(url)
            if 'offset=0' in url:
                return Response(GROUPS1)
            if 'offset=500' in url:
                return Response(GROUPS2)
            raise NotImplementedError(url)

        rget.side_effect = mocked_get

        all = mozillians.get_all_groups()
        eq_(len(all), 750)
        eq_(all[0]['name'], 'GROUP NUMBER 1')
        eq_(all[500]['name'], 'GROUP NUMBER 2')
        eq_(len(calls), 2)