Exemplo n.º 1
0
    def test_empty_query(self):
        """If the search query has no terms, do not use it to filter videos."""
        v1 = VideoFactory.create(title='A', approved=True)
        v2 = VideoFactory.create(title='B', approved=True)

        eq_(set(search_videos(query='')), set([v1, v2]))
        eq_(set(search_videos(query='      ')), set([v1, v2]))
Exemplo n.º 2
0
    def test_fields(self):
        """
        If the fields parameter is specified, only perform a search on the
        fields specified in that list.
        """
        v1 = VideoFactory.create(title="foo", description="bar", approved=True)
        v2 = VideoFactory.create(title="bar", description="foo", approved=True)

        eq_(list(search_videos("foo", fields=["title"])), [v1])
        eq_(list(search_videos("bar", fields=["title"])), [v2])
        eq_(list(search_videos("bar", fields=["description"])), [v1])
        eq_(list(search_videos("bar", fields=["title", "description"])), [v2, v1])
Exemplo n.º 3
0
    def test_fields(self):
        """
        If the fields parameter is specified, only perform a search on the
        fields specified in that list.
        """
        v1 = VideoFactory.create(title='foo', description='bar', approved=True)
        v2 = VideoFactory.create(title='bar', description='foo', approved=True)

        eq_(list(search_videos('foo', fields=['title'])), [v1])
        eq_(list(search_videos('bar', fields=['title'])), [v2])
        eq_(list(search_videos('bar', fields=['description'])), [v1])
        eq_(list(search_videos('bar', fields=['title', 'description'])),
            [v2, v1])
Exemplo n.º 4
0
    def test_fields(self):
        """
        If the fields parameter is specified, only perform a search on the
        fields specified in that list.
        """
        v1 = VideoFactory.create(title='foo', description='bar', approved=True)
        v2 = VideoFactory.create(title='bar', description='foo', approved=True)

        eq_(set(search_videos('foo', fields=['title'])), set([v1]))
        eq_(set(search_videos('bar', fields=['title'])), set([v2]))
        eq_(set(search_videos('bar', fields=['description'])), set([v1]))
        videos = search_videos(
            'bar', fields=['title', 'description'], sort='title')
        eq_(set(videos), set([v2, v1]))
Exemplo n.º 5
0
def video_list(request):
    """Show a list of recent videos, optionally filtered by region or search."""
    page = request.GET.get('page', 1)
    region = request.GET.get('region', None)
    ctx = {}

    if waffle.flag_is_active(request, 'r3'):
        form = VideoSearchForm(request.GET)
        try:
            videos = form.perform_search()
        except ValidationError:
            videos = search_videos()

        ctx['form'] = form
    else:
        videos = Video.objects.filter(approved=True).order_by('-created')
        if region:
            countries = regions.get_countries(region)
            if countries:
                videos = videos.filter(
                    user__userprofile__country__in=countries)

    paginator = Paginator(videos, 12)
    try:
        videos = paginator.page(page)
    except PageNotAnInteger:
        videos = paginator.page(1)  # Default to first page
    except EmptyPage:
        videos = paginator.page(paginator.num_pages)  # Empty page goes to last

    ctx['videos'] = videos
    return render(request, 'videos/2013/list.html', ctx)
Exemplo n.º 6
0
    def test_title_sort(self):
        """If sort is 'title', sort videos alphabetically by their title."""
        video_1 = VideoFactory.create(title='A', approved=True)
        video_2 = VideoFactory.create(title='C', approved=True)
        video_3 = VideoFactory.create(title='B', approved=True)

        eq_(list(search_videos(sort='title')), [video_1, video_3, video_2])
Exemplo n.º 7
0
    def test_random_sort(self):
        """By default, sort the videos by their random_ordering index."""
        video_1 = VideoFactory.create(random_ordering=3, approved=True)
        video_2 = VideoFactory.create(random_ordering=1, approved=True)
        video_3 = VideoFactory.create(random_ordering=2, approved=True)

        eq_(list(search_videos()), [video_2, video_3, video_1])
Exemplo n.º 8
0
    def test_title_sort(self):
        """By default, sort videos alphabetically by their title."""
        video_1 = VideoFactory.create(title='A', approved=True)
        video_2 = VideoFactory.create(title='C', approved=True)
        video_3 = VideoFactory.create(title='B', approved=True)

        eq_(list(search_videos()), [video_1, video_3, video_2])
Exemplo n.º 9
0
    def test_title_sort(self):
        """By default, sort videos alphabetically by their title."""
        video_1 = VideoFactory.create(title="A", approved=True)
        video_2 = VideoFactory.create(title="C", approved=True)
        video_3 = VideoFactory.create(title="B", approved=True)

        eq_(list(search_videos()), [video_1, video_3, video_2])
Exemplo n.º 10
0
    def test_popular_sort(self):
        """If sort is 'popular', sort by the number of votes."""
        video_1 = VideoFactory.create(title="A", approved=True, vote_count=4)
        video_2 = VideoFactory.create(title="C", approved=True)
        video_3 = VideoFactory.create(title="B", approved=True, vote_count=7)

        eq_(list(search_videos(sort="popular")), [video_3, video_1, video_2])
Exemplo n.º 11
0
    def test_popular_sort(self):
        """If sort is 'popular', sort by the number of votes."""
        video_1 = VideoFactory.create(title='A', approved=True, vote_count=4)
        video_2 = VideoFactory.create(title='C', approved=True)
        video_3 = VideoFactory.create(title='B', approved=True, vote_count=7)

        eq_(list(search_videos(sort='popular')), [video_3, video_1, video_2])
Exemplo n.º 12
0
 def test_no_parameters(self):
     """
     If no parameters are passed, return a queryset of all approved videos.
     """
     VideoFactory.create(approved=False)
     v1 = VideoFactory.create(approved=True)
     v2 = VideoFactory.create(approved=True)
     eq_(list(search_videos()), [v1, v2])
Exemplo n.º 13
0
 def test_no_parameters(self):
     """
     If no parameters are passed, return a queryset of all approved videos.
     """
     VideoFactory.create(approved=False)
     v1 = VideoFactory.create(approved=True)
     v2 = VideoFactory.create(approved=True)
     eq_(list(search_videos()), [v1, v2])
Exemplo n.º 14
0
    def test_query(self):
        """
        If a search query is given, the results should be limited to videos
        that contain any of the terms in their title, description, or user's
        full name.
        """
        VideoFactory.create(title='no match', approved=True)
        VideoFactory.create(description='no match', approved=True)
        user = UserProfileFactory.create(full_name='no match').user
        VideoFactory.create(user=user, approved=True)

        v1 = VideoFactory.create(title='A does match mytitle', approved=True)
        v2 = VideoFactory.create(title='B', description='does match mydesc',
                                 approved=True)
        user = UserProfileFactory.create(full_name='does match name').user
        v3 = VideoFactory.create(title='C', user=user, approved=True)

        eq_(set(search_videos(query='does')), set([v1, v2, v3]))

        # ANY term in the query can be used for matching.
        eq_(set(search_videos(query='what does bylaw')), set([v1, v2, v3]))

        # Search is case-insensitive.
        eq_(set(search_videos(query='DoEs')), set([v1, v2, v3]))

        # Terms may match only part of a word in the video.
        eq_(set(search_videos(query='floor do')), set([v1, v2, v3]))

        # Terms only have to match one of the three possible fields.
        eq_(set(search_videos(query='mytitle')), set([v1]))
        eq_(set(search_videos(query='mydesc')), set([v2]))
        eq_(set(search_videos(query='name')), set([v3]))
Exemplo n.º 15
0
    def test_query(self):
        """
        If a search query is given, the results should be limited to videos
        that contain any of the terms in their title, description, or user's
        full name.
        """
        VideoFactory.create(title='no match', approved=True)
        VideoFactory.create(description='no match', approved=True)
        user = UserProfileFactory.create(full_name='no match').user
        VideoFactory.create(user=user, approved=True)

        v1 = VideoFactory.create(title='A does match mytitle', approved=True)
        v2 = VideoFactory.create(title='B',
                                 description='does match mydesc',
                                 approved=True)
        user = UserProfileFactory.create(full_name='does match name').user
        v3 = VideoFactory.create(title='C', user=user, approved=True)

        eq_(list(search_videos(query='does')), [v1, v2, v3])

        # ANY term in the query can be used for matching.
        eq_(list(search_videos(query='what does bylaw')), [v1, v2, v3])

        # Search is case-insensitive.
        eq_(list(search_videos(query='DoEs')), [v1, v2, v3])

        # Terms may match only part of a word in the video.
        eq_(list(search_videos(query='floor do')), [v1, v2, v3])

        # Terms only have to match one of the three possible fields.
        eq_(list(search_videos(query='mytitle')), [v1])
        eq_(list(search_videos(query='mydesc')), [v2])
        eq_(list(search_videos(query='name')), [v3])
Exemplo n.º 16
0
    def test_query(self):
        """
        If a search query is given, the results should be limited to videos
        that contain any of the terms in their title, description, or user's
        full name.
        """
        VideoFactory.create(title="no match", approved=True)
        VideoFactory.create(description="no match", approved=True)
        user = UserProfileFactory.create(full_name="no match").user
        VideoFactory.create(user=user, approved=True)

        v1 = VideoFactory.create(title="A does match mytitle", approved=True)
        v2 = VideoFactory.create(title="B", description="does match mydesc", approved=True)
        user = UserProfileFactory.create(full_name="does match name").user
        v3 = VideoFactory.create(title="C", user=user, approved=True)

        eq_(list(search_videos(query="does")), [v1, v2, v3])

        # ANY term in the query can be used for matching.
        eq_(list(search_videos(query="what does bylaw")), [v1, v2, v3])

        # Search is case-insensitive.
        eq_(list(search_videos(query="DoEs")), [v1, v2, v3])

        # Terms may match only part of a word in the video.
        eq_(list(search_videos(query="floor do")), [v1, v2, v3])

        # Terms only have to match one of the three possible fields.
        eq_(list(search_videos(query="mytitle")), [v1])
        eq_(list(search_videos(query="mydesc")), [v2])
        eq_(list(search_videos(query="name")), [v3])
Exemplo n.º 17
0
    def test_invaid_region(self, get_countries):
        """If the given region is invalid, do not filter by it."""
        get_countries.return_value = None
        user_fr = UserProfileFactory.create(country='fr').user
        user_pt = UserProfileFactory.create(country='pt').user

        video_fr = VideoFactory.create(user=user_fr, approved=True)
        video_pt = VideoFactory.create(user=user_pt, approved=True)

        eq_(list(search_videos(region=1)), [video_fr, video_pt])
        get_countries.assert_called_with(1)
Exemplo n.º 18
0
    def test_invaid_region(self, get_countries):
        """If the given region is invalid, do not filter by it."""
        get_countries.return_value = None
        user_fr = UserProfileFactory.create(country="fr").user
        user_pt = UserProfileFactory.create(country="pt").user

        video_fr = VideoFactory.create(user=user_fr, approved=True)
        video_pt = VideoFactory.create(user=user_pt, approved=True)

        eq_(list(search_videos(region=1)), [video_fr, video_pt])
        get_countries.assert_called_with(1)
Exemplo n.º 19
0
def gallery(request):
    """Show the gallery of all submitted videos."""
    ctx = {}
    form = VideoSearchForm(request, request.GET)

    try:
        videos = form.perform_search()
    except ValidationError:
        videos = search_videos()

    ctx['form'] = form
    return video_list(request, videos, ctx)
Exemplo n.º 20
0
def gallery(request):
    """Show the gallery of all submitted videos."""
    ctx = {}
    form = VideoSearchForm(request, request.GET)

    try:
        videos = form.perform_search()
    except ValidationError:
        videos = search_videos()

    ctx['form'] = form
    return video_list(request, videos, ctx)
Exemplo n.º 21
0
    def test_region(self, get_countries):
        """
        If the region filter is given, only return videos from countries in
        that region.
        """
        get_countries.return_value = ['us', 'fr']
        user_fr = UserProfileFactory.create(country='fr').user
        user_pt = UserProfileFactory.create(country='pt').user

        video_fr = VideoFactory.create(user=user_fr, approved=True)
        VideoFactory.create(user=user_pt, approved=True)

        eq_(list(search_videos(region=1)), [video_fr])
        get_countries.assert_called_with(1)
Exemplo n.º 22
0
    def test_region(self, get_countries):
        """
        If the region filter is given, only return videos from countries in
        that region.
        """
        get_countries.return_value = ["us", "fr"]
        user_fr = UserProfileFactory.create(country="fr").user
        user_pt = UserProfileFactory.create(country="pt").user

        video_fr = VideoFactory.create(user=user_fr, approved=True)
        VideoFactory.create(user=user_pt, approved=True)

        eq_(list(search_videos(region=1)), [video_fr])
        get_countries.assert_called_with(1)
Exemplo n.º 23
0
    def perform_search(self):
        """
        Perform a search using the parameters bound to this form.

        :throws ValidationError: If the form doesn't pass validation.
        """
        if not self.is_valid():
            raise ValidationError('Form must be valid to perform search.')

        return search_videos(
            query=self.cleaned_data['query'],
            fields=AUTOCOMPLETE_FIELDS.get(self.cleaned_data['field'], None),
            region=self.cleaned_data['region'],
            sort=self.cleaned_data['sort'],
        )
Exemplo n.º 24
0
    def perform_search(self):
        """
        Perform a search using the parameters bound to this form.

        :throws ValidationError: If the form doesn't pass validation.
        """
        if not self.is_valid():
            raise ValidationError('Form must be valid to perform search.')

        return search_videos(
            query=self.cleaned_data['query'],
            fields=FIELD_FILTERS.get(self.cleaned_data['field'], None),
            region=self.cleaned_data['region'],
            sort=self.cleaned_data['sort'],
        )