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)
def test_invalid_form(self): """If the form fails validation, throw a ValidationError.""" form = VideoSearchForm({ 'region': -5, 'sort': 'invalid' }) with assert_raises(ValidationError): form.perform_search()
def test_valid_search(self, search_videos): form = VideoSearchForm( self.request, {"query": "asdf", "field": "title", "region": NORTH_AMERICA, "sort": "popular"} ) eq_(form.perform_search(), search_videos.return_value) search_videos.assert_called_with( query="asdf", fields=AUTOCOMPLETE_FIELDS["title"], region=NORTH_AMERICA, sort="popular" )
def test_invalid_sort(self): """ An invalid value for sort should not break clean. Regression test for an issue where a user was attempting to break Flicks by submitting a bunch of invalid values for sort. """ form = VideoSearchForm(self.request, {"query": "blah", "sort": "invalid"}) form.full_clean() eq_(form.is_valid(), False)
def test_clean_no_query(self): """ If no search query is specified, do not alter the sort value or choices. """ form = VideoSearchForm(self.request, {"region": NORTH_AMERICA, "sort": "title"}) form.full_clean() eq_(form.cleaned_data["sort"], "title") choice_values = zip(*form.fields["sort"].choices)[0] ok_("" in choice_values)
def test_empty_field_passes_none(self, search_videos): """If the field isn't specified, pass None to the fields parameter.""" form = VideoSearchForm({ 'query': 'asdf', 'region': NORTH_AMERICA, 'sort': 'popular' }) eq_(form.perform_search(), search_videos.return_value) search_videos.assert_called_with(query='asdf', fields=None, region=NORTH_AMERICA, sort='popular')
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)
def test_invalid_sort(self): """ An invalid value for sort should not break clean. Regression test for an issue where a user was attempting to break Flicks by submitting a bunch of invalid values for sort. """ form = VideoSearchForm(self.request, { 'query': 'blah', 'sort': 'invalid' }) form.full_clean() eq_(form.is_valid(), False)
def test_valid_search(self, search_videos): form = VideoSearchForm({ 'query': 'asdf', 'field': 'title', 'region': NORTH_AMERICA, 'sort': 'popular' }) eq_(form.perform_search(), search_videos.return_value) search_videos.assert_called_with(query='asdf', fields=FIELD_FILTERS['title'], region=NORTH_AMERICA, sort='popular')
def test_clean_no_query(self): """ If no search query is specified, do not alter the sort value or choices. """ form = VideoSearchForm(self.request, { 'region': NORTH_AMERICA, 'sort': 'title' }) form.full_clean() eq_(form.cleaned_data['sort'], 'title') choice_values = zip(*form.fields['sort'].choices)[0] ok_('' in choice_values)
def test_valid_search(self, search_videos): form = VideoSearchForm({ 'query': 'asdf', 'field': 'title', 'region': NORTH_AMERICA, 'sort': 'popular' }) eq_(form.perform_search(), search_videos.return_value) search_videos.assert_called_with( query='asdf', fields=FIELD_FILTERS['title'], region=NORTH_AMERICA, sort='popular' )
def video_list(request, videos, ctx=None): """Show a list of videos.""" page = request.GET.get('page', 1) ctx = ctx or {} if 'form' not in ctx: ctx['form'] = VideoSearchForm(request, request.GET) paginator = Paginator(videos, settings.VIDEOS_PER_PAGE) 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)
def test_clean_query(self): """ If a search query is specified, remove the random option from the sort choices and, if the sort is currently set to random, switch to title sort. """ form = VideoSearchForm(self.request, {'query': 'blah', 'sort': ''}) form.full_clean() eq_(form.cleaned_data['sort'], 'title') choice_values = zip(*form.fields['sort'].choices)[0] ok_('' not in choice_values) # Check that sort is preserved if it is not random. form = VideoSearchForm(self.request, { 'query': 'blah', 'sort': 'popular' }) form.full_clean() eq_(form.cleaned_data['sort'], 'popular') choice_values = zip(*form.fields['sort'].choices)[0] ok_('' not in choice_values)
def test_clean_query(self): """ If a search query is specified, remove the random option from the sort choices and, if the sort is currently set to random, switch to title sort. """ form = VideoSearchForm(self.request, {"query": "blah", "sort": ""}) form.full_clean() eq_(form.cleaned_data["sort"], "title") choice_values = zip(*form.fields["sort"].choices)[0] ok_("" not in choice_values) # Check that sort is preserved if it is not random. form = VideoSearchForm(self.request, {"query": "blah", "sort": "popular"}) form.full_clean() eq_(form.cleaned_data["sort"], "popular") choice_values = zip(*form.fields["sort"].choices)[0] ok_("" not in choice_values)
def test_invalid_form(self): """If the form fails validation, throw a ValidationError.""" form = VideoSearchForm({'region': -5, 'sort': 'invalid'}) with assert_raises(ValidationError): form.perform_search()
def test_empty_field_passes_none(self, search_videos): """If the field isn't specified, pass None to the fields parameter.""" form = VideoSearchForm(self.request, {"query": "asdf", "region": NORTH_AMERICA, "sort": "popular"}) eq_(form.perform_search(), search_videos.return_value) search_videos.assert_called_with(query="asdf", fields=None, region=NORTH_AMERICA, sort="popular")
def test_invalid_form(self): """If the form fails validation, throw a ValidationError.""" form = VideoSearchForm(self.request, {"region": -5, "sort": "invalid"}) with assert_raises(ValidationError): form.perform_search()
def test_popular_sort_exclude(self): """If the voting-end waffle flag is set, do not include the popular option for sorting.""" Flag.objects.create(name='voting-end', everyone=True) form = VideoSearchForm(self.request) ok_('popular' not in [c[0] for c in form.fields['sort'].choices])