Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
Fix bug 906853: Remove popularity option when voting-end flag is active.
Browse files Browse the repository at this point in the history
Also fixes bug 907978 since the flag will be off in R5.
  • Loading branch information
Michael Kelly committed Aug 23, 2013
1 parent dea4124 commit aa63976
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
16 changes: 12 additions & 4 deletions flicks/videos/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import forms
from django.core.exceptions import ValidationError

import waffle
from tower import ugettext_lazy as _lazy

from flicks.base.regions import region_names
Expand All @@ -25,14 +26,12 @@ class VideoSearchForm(forms.Form):
"""Form for the search feature on the video listing page."""
FIELD_CHOICES = [(value, '') for value in AUTOCOMPLETE_FIELDS.keys()]
REGION_CHOICES = [('', _lazy('All regions'))] + region_names.items()
SORT_CHOICES = (
SORT_CHOICES = [
# L10n: Label for the order in which to sort a list of videos.
('', _lazy('Random')),
# L10n: Label for the order in which to sort a list of videos.
('title', _lazy('by Title')),
# L10n: Label for the order in which to sort a list of videos.
('popular', _lazy('by Popularity')),
)
]

query = forms.CharField(
required=False,
Expand All @@ -55,6 +54,15 @@ class VideoSearchForm(forms.Form):

region_names = dict(REGION_CHOICES)

def __init__(self, request, *args, **kwargs):
super(VideoSearchForm, self).__init__(*args, **kwargs)

# Only make popular option available if voting hasn't ended.
if not waffle.flag_is_active(request, 'voting-end'):

# L10n: Label for the order in which to sort a list of videos.
self.fields['sort'].choices += [('popular', _lazy('by Popularity'))]

def clean(self):
super(VideoSearchForm, self).clean()

Expand Down
31 changes: 25 additions & 6 deletions flicks/videos/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.core.exceptions import ValidationError
from django.test.client import RequestFactory

from mock import patch
from nose.tools import assert_raises, eq_, ok_
from waffle import Flag

from flicks.base.regions import NORTH_AMERICA
from flicks.base.tests import TestCase
Expand All @@ -10,9 +12,26 @@


class VideoSearchFormTests(TestCase):
def setUp(self):
super(VideoSearchFormTests, self).setUp()
self.factory = RequestFactory()
self.request = self.factory.get('/')

def test_popular_sort_include(self):
"""If the voting-end waffle flag is not set, include the popular option for sorting."""
Flag.objects.create(name='voting-end', everyone=False)
form = VideoSearchForm(self.request)
ok_('popular' in [c[0] for c in form.fields['sort'].choices])

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])

@patch('flicks.videos.forms.search_videos')
def test_valid_search(self, search_videos):
form = VideoSearchForm({
form = VideoSearchForm(self.request, {
'query': 'asdf',
'field': 'title',
'region': NORTH_AMERICA,
Expand All @@ -30,7 +49,7 @@ def test_valid_search(self, search_videos):
@patch('flicks.videos.forms.search_videos')
def test_empty_field_passes_none(self, search_videos):
"""If the field isn't specified, pass None to the fields parameter."""
form = VideoSearchForm({
form = VideoSearchForm(self.request, {
'query': 'asdf',
'region': NORTH_AMERICA,
'sort': 'popular'
Expand All @@ -42,7 +61,7 @@ def test_empty_field_passes_none(self, search_videos):

def test_invalid_form(self):
"""If the form fails validation, throw a ValidationError."""
form = VideoSearchForm({
form = VideoSearchForm(self.request, {
'region': -5,
'sort': 'invalid'
})
Expand All @@ -55,7 +74,7 @@ def test_clean_no_query(self):
If no search query is specified, do not alter the sort value or
choices.
"""
form = VideoSearchForm({'region': NORTH_AMERICA, 'sort': 'title'})
form = VideoSearchForm(self.request, {'region': NORTH_AMERICA, 'sort': 'title'})
form.full_clean()

eq_(form.cleaned_data['sort'], 'title')
Expand All @@ -68,15 +87,15 @@ def test_clean_query(self):
choices and, if the sort is currently set to random, switch to title
sort.
"""
form = VideoSearchForm({'query': 'blah', '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({'query': 'blah', 'sort': 'popular'})
form = VideoSearchForm(self.request, {'query': 'blah', 'sort': 'popular'})
form.full_clean()

eq_(form.cleaned_data['sort'], 'popular')
Expand Down
4 changes: 2 additions & 2 deletions flicks/videos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def gallery(request):
"""Show the gallery of all submitted videos."""
ctx = {}
form = VideoSearchForm(request.GET)
form = VideoSearchForm(request, request.GET)

try:
videos = form.perform_search()
Expand All @@ -55,7 +55,7 @@ def video_list(request, videos, ctx=None):
ctx = ctx or {}

if 'form' not in ctx:
ctx['form'] = VideoSearchForm(request.GET)
ctx['form'] = VideoSearchForm(request, request.GET)

paginator = Paginator(videos, settings.VIDEOS_PER_PAGE)
try:
Expand Down

0 comments on commit aa63976

Please sign in to comment.