0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

SearchPick => SearchPromotion

This commit is contained in:
Karl Hobley 2015-07-07 16:01:19 +01:00
parent 669c7ac34c
commit aea1ec742a
5 changed files with 42 additions and 38 deletions

View File

@ -4,18 +4,18 @@ from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailadmin.widgets import AdminPageChooser
from wagtail.wagtailsearch.models import Query
from wagtail.contrib.wagtailsearchpicks.models import SearchPick
from wagtail.contrib.wagtailsearchpicks.models import SearchPromotion
class SearchPickForm(forms.ModelForm):
class SearchPromotionForm(forms.ModelForm):
sort_order = forms.IntegerField(required=False)
def __init__(self, *args, **kwargs):
super(SearchPickForm, self).__init__(*args, **kwargs)
super(SearchPromotionForm, self).__init__(*args, **kwargs)
self.fields['page'].widget = AdminPageChooser()
class Meta:
model = SearchPick
model = SearchPromotion
fields = ('query', 'page', 'description')
widgets = {
@ -23,15 +23,15 @@ class SearchPickForm(forms.ModelForm):
}
SearchPicksFormSetBase = inlineformset_factory(Query, SearchPick, form=SearchPickForm, can_order=True, can_delete=True, extra=0)
SearchPromotionsFormSetBase = inlineformset_factory(Query, SearchPromotion, form=SearchPromotionForm, can_order=True, can_delete=True, extra=0)
class SearchPicksFormSet(SearchPicksFormSetBase):
class SearchPromotionsFormSet(SearchPromotionsFormSetBase):
minimum_forms = 1
minimum_forms_message = _("Please specify at least one recommendation for this search term.")
def add_fields(self, form, *args, **kwargs):
super(SearchPicksFormSet, self).add_fields(form, *args, **kwargs)
super(SearchPromotionsFormSet, self).add_fields(form, *args, **kwargs)
# Hide delete and order fields
form.fields['DELETE'].widget = forms.HiddenInput()

View File

@ -38,6 +38,10 @@ class Migration(migrations.Migration):
),
migrations.RenameModel(
old_name='EditorsPick',
new_name='SearchPick'
)
new_name='SearchPromotion'
),
migrations.AlterModelOptions(
name='searchpromotion',
options={'ordering': ('sort_order',), 'verbose_name': 'Search promotion'},
),
]

View File

@ -4,15 +4,15 @@ from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailsearch.models import Query
class SearchPick(models.Model):
class SearchPromotion(models.Model):
query = models.ForeignKey(Query, db_index=True, related_name='editors_picks')
page = models.ForeignKey('wagtailcore.Page', verbose_name=_('Page'))
sort_order = models.IntegerField(null=True, blank=True, editable=False)
description = models.TextField(verbose_name=_('Description'), blank=True)
def __repr__(self):
return 'SearchPick(query="' + self.query.query_string + '", page="' + self.page.title + '")'
return 'SearchPromotion(query="' + self.query.query_string + '", page="' + self.page.title + '")'
class Meta:
ordering = ('sort_order', )
verbose_name = _("Editor's Pick")
verbose_name = _("Search promotion")

View File

@ -3,14 +3,14 @@ from django.core.urlresolvers import reverse
from wagtail.tests.utils import WagtailTestUtils
from wagtail.wagtailsearch.models import Query
from wagtail.contrib.wagtailsearchpicks.models import SearchPick
from wagtail.contrib.wagtailsearchpicks.models import SearchPromotion
from wagtail.contrib.wagtailsearchpicks.templatetags.wagtailsearchpicks_tags import get_search_picks
class TestSearchPicks(TestCase):
class TestSearchPromotions(TestCase):
def test_search_pick_create(self):
# Create a search pick to the root page
SearchPick.objects.create(
SearchPromotion.objects.create(
query=Query.get("root page"),
page_id=1,
sort_order=0,
@ -24,19 +24,19 @@ class TestSearchPicks(TestCase):
def test_search_pick_ordering(self):
# Add 3 search picks in a different order to their sort_order values
# They should be ordered by their sort order values and not their insertion order
SearchPick.objects.create(
SearchPromotion.objects.create(
query=Query.get("root page"),
page_id=1,
sort_order=0,
description="First search pick",
)
SearchPick.objects.create(
SearchPromotion.objects.create(
query=Query.get("root page"),
page_id=1,
sort_order=2,
description="Last search pick",
)
SearchPick.objects.create(
SearchPromotion.objects.create(
query=Query.get("root page"),
page_id=1,
sort_order=1,
@ -49,10 +49,10 @@ class TestSearchPicks(TestCase):
self.assertEqual(Query.get("root page").editors_picks.last().description, "Last search pick")
class TestGetSearchPicksTemplateTag(TestCase):
class TestGetSearchPromotionsTemplateTag(TestCase):
def test_get_search_picks_template_tag(self):
# Create a search pick to the root page
pick = SearchPick.objects.create(
pick = SearchPromotion.objects.create(
query=Query.get("root page"),
page_id=1,
sort_order=0,
@ -60,7 +60,7 @@ class TestGetSearchPicksTemplateTag(TestCase):
)
# Create another search pick against a different query
SearchPick.objects.create(
SearchPromotion.objects.create(
query=Query.get("root page again"),
page_id=1,
sort_order=0,
@ -72,7 +72,7 @@ class TestGetSearchPicksTemplateTag(TestCase):
self.assertEqual(search_picks, [pick])
class TestSearchPicksIndexView(TestCase, WagtailTestUtils):
class TestSearchPromotionsIndexView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
@ -88,7 +88,7 @@ class TestSearchPicksIndexView(TestCase, WagtailTestUtils):
def make_search_picks(self):
for i in range(50):
SearchPick.objects.create(
SearchPromotion.objects.create(
query=Query.get("query " + str(i)),
page_id=1,
sort_order=0,
@ -132,7 +132,7 @@ class TestSearchPicksIndexView(TestCase, WagtailTestUtils):
self.assertEqual(response.context['queries'].number, response.context['queries'].paginator.num_pages)
class TestSearchPicksAddView(TestCase, WagtailTestUtils):
class TestSearchPromotionsAddView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
@ -176,7 +176,7 @@ class TestSearchPicksAddView(TestCase, WagtailTestUtils):
self.assertFormsetError(response, 'searchpicks_formset', None, None, "Please specify at least one recommendation for this search term.")
class TestSearchPicksEditView(TestCase, WagtailTestUtils):
class TestSearchPromotionsEditView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
@ -214,7 +214,7 @@ class TestSearchPicksEditView(TestCase, WagtailTestUtils):
self.assertRedirects(response, reverse('wagtailsearchpicks:index'))
# Check that the search pick description was edited
self.assertEqual(SearchPick.objects.get(id=self.search_pick.id).description, "Description has changed")
self.assertEqual(SearchPromotion.objects.get(id=self.search_pick.id).description, "Description has changed")
def test_post_reorder(self):
# Check order before reordering
@ -244,8 +244,8 @@ class TestSearchPicksEditView(TestCase, WagtailTestUtils):
self.assertRedirects(response, reverse('wagtailsearchpicks:index'))
# Check that the ordering has been saved correctly
self.assertEqual(SearchPick.objects.get(id=self.search_pick.id).sort_order, 1)
self.assertEqual(SearchPick.objects.get(id=self.search_pick_2.id).sort_order, 0)
self.assertEqual(SearchPromotion.objects.get(id=self.search_pick.id).sort_order, 1)
self.assertEqual(SearchPromotion.objects.get(id=self.search_pick_2.id).sort_order, 0)
# Check that the recommendations were reordered
self.assertEqual(Query.get("Hello").editors_picks.all()[0], self.search_pick_2)
@ -275,10 +275,10 @@ class TestSearchPicksEditView(TestCase, WagtailTestUtils):
self.assertRedirects(response, reverse('wagtailsearchpicks:index'))
# Check that the recommendation was deleted
self.assertFalse(SearchPick.objects.filter(id=self.search_pick_2.id).exists())
self.assertFalse(SearchPromotion.objects.filter(id=self.search_pick_2.id).exists())
# The other recommendation should still exist
self.assertTrue(SearchPick.objects.filter(id=self.search_pick.id).exists())
self.assertTrue(SearchPromotion.objects.filter(id=self.search_pick.id).exists())
def test_post_without_recommendations(self):
# Submit
@ -305,7 +305,7 @@ class TestSearchPicksEditView(TestCase, WagtailTestUtils):
self.assertFormsetError(response, 'searchpicks_formset', None, None, "Please specify at least one recommendation for this search term.")
class TestSearchPicksDeleteView(TestCase, WagtailTestUtils):
class TestSearchPromotionsDeleteView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
@ -330,7 +330,7 @@ class TestSearchPicksDeleteView(TestCase, WagtailTestUtils):
self.assertRedirects(response, reverse('wagtailsearchpicks:index'))
# Check that both recommendations were deleted
self.assertFalse(SearchPick.objects.filter(id=self.search_pick_2.id).exists())
self.assertFalse(SearchPromotion.objects.filter(id=self.search_pick_2.id).exists())
# The other recommendation should still exist
self.assertFalse(SearchPick.objects.filter(id=self.search_pick.id).exists())
self.assertFalse(SearchPromotion.objects.filter(id=self.search_pick.id).exists())

View File

@ -79,7 +79,7 @@ def add(request):
query = Query.get(query_form['query_string'].value())
# Save search picks
searchpicks_formset = forms.SearchPicksFormSet(request.POST, instance=query)
searchpicks_formset = forms.SearchPromotionsFormSet(request.POST, instance=query)
if save_searchpicks(query, query, searchpicks_formset):
messages.success(request, _("Editor's picks for '{0}' created.").format(query), buttons=[
messages.button(reverse('wagtailsearchpicks:edit', args=(query.id,)), _('Edit'))
@ -91,10 +91,10 @@ def add(request):
else:
messages.error(request, _("Recommendations have not been created due to errors")) # specific errors will be displayed within form fields
else:
searchpicks_formset = forms.SearchPicksFormSet()
searchpicks_formset = forms.SearchPromotionsFormSet()
else:
query_form = search_forms.QueryForm()
searchpicks_formset = forms.SearchPicksFormSet()
searchpicks_formset = forms.SearchPromotionsFormSet()
return render(request, 'wagtailsearchpicks/add.html', {
'query_form': query_form,
@ -109,7 +109,7 @@ def edit(request, query_id):
# Get query
query_form = search_forms.QueryForm(request.POST)
# and the recommendations
searchpicks_formset = forms.SearchPicksFormSet(request.POST, instance=query)
searchpicks_formset = forms.SearchPromotionsFormSet(request.POST, instance=query)
if query_form.is_valid():
new_query = Query.get(query_form['query_string'].value())
@ -128,7 +128,7 @@ def edit(request, query_id):
else:
query_form = search_forms.QueryForm(initial=dict(query_string=query.query_string))
searchpicks_formset = forms.SearchPicksFormSet(instance=query)
searchpicks_formset = forms.SearchPromotionsFormSet(instance=query)
return render(request, 'wagtailsearchpicks/edit.html', {
'query_form': query_form,