Skip to content

Commit

Permalink
Rename variations to variants
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Jun 2, 2017
1 parent 2e74741 commit 281086a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/wagtail_personalisation/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Migration(migrations.Migration):
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
('is_segmented', models.BooleanField(default=False)),
('canonical_page', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='variations', to='wagtail_personalisation.PersonalisablePage')),
('canonical_page', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='variants', to='wagtail_personalisation.PersonalisablePage')),
],
options={
'abstract': False,
Expand Down
12 changes: 6 additions & 6 deletions src/wagtail_personalisation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,19 @@ class PersonalisablePageMetadata(ClusterableModel):
Segment, related_name='page_metadata', null=True, blank=True)

@cached_property
def has_variations(self):
def has_variants(self):
"""Return a boolean indicating whether or not the personalisable page
has variations.
has variants.
:returns: A boolean indicating whether or not the personalisable page
has variations.
has variants.
:rtype: bool
"""
return self.variations.exists()
return self.variants_metadata.exists()

@cached_property
def variations(self):
def variants_metadata(self):
return (
PersonalisablePageMetadata.objects
.filter(canonical_page_id=self.canonical_page_id)
Expand Down Expand Up @@ -179,7 +179,7 @@ def copy_for_segment(self, segment):
segment=segment)
return new_page

def variants_for_segments(self, segments):
def metadata_for_segments(self, segments):
return (
self.__class__.objects
.filter(
Expand Down
6 changes: 3 additions & 3 deletions src/wagtail_personalisation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ def copy_page_view(request, page_id, segment_id):
page = get_object_or_404(Page, pk=page_id).specific

metadata = page.personalisation_metadata
variants = metadata.variants_for_segments([segment])
if variants.exists():
variant = variants.first()
variant_metadata = metadata.metadata_for_segments([segment])
if variant_metadata.exists():
variant = variant_metadata.first()
else:
variant = metadata.copy_for_segment(segment)
edit_url = reverse('wagtailadmin_pages:edit', args=[variant.id])
Expand Down
18 changes: 9 additions & 9 deletions src/wagtail_personalisation/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def segment_user(page, request, serve_args, serve_kwargs):


@hooks.register('before_serve_page')
def serve_variation(page, request, serve_args, serve_kwargs):
def serve_variant(page, request, serve_args, serve_kwargs):
"""Apply a segment to a visitor before serving the page.
:param page: The page being served
Expand All @@ -81,14 +81,14 @@ def serve_variation(page, request, serve_args, serve_kwargs):
metadata = page.personalisation_metadata

# TODO: This is never more then one page? (fix query count)
variations = metadata.variants_for_segments(user_segments)
if variations:
variation = variations.first().variant.specific
return variation.serve(request, *serve_args, **serve_kwargs)
metadata = metadata.metadata_for_segments(user_segments)
if metadata:
variant = metadata.first().variant.specific
return variant.serve(request, *serve_args, **serve_kwargs)


@hooks.register('construct_explorer_page_queryset')
def dont_show_variations(parent_page, pages, request):
def dont_show_variant(parent_page, pages, request):
return [page for page in pages
if (page.personalisation_metadata is None)
or (page.personalisation_metadata.is_canonical)]
Expand Down Expand Up @@ -126,9 +126,9 @@ def page_listing_more_buttons(page, page_perms, is_parent=False):

metadata = page.personalisation_metadata

for variation in metadata.variations:
yield Button('%s variation' % (variation.segment.name),
reverse('wagtailadmin_pages:edit', args=[variation.variant_id]),
for vm in metadata.variants_metadata:
yield Button('%s variation' % (vm.segment.name),
reverse('wagtailadmin_pages:edit', args=[vm.variant_id]),
attrs={"title": _('Edit this variant')},
classes=("icon", "icon-fa-pencil"),
priority=0)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def test_segment_create():


@pytest.mark.django_db
def test_metadata_page_has_variations(segmented_page):
def test_metadata_page_has_variants(segmented_page):
assert not segmented_page.personalisation_metadata.is_canonical
assert not segmented_page.personalisation_metadata.has_variations
assert not segmented_page.personalisation_metadata.has_variants

canonical = segmented_page.personalisation_metadata.canonical_page
assert canonical.personalisation_metadata.is_canonical
assert canonical.personalisation_metadata.has_variations
assert canonical.personalisation_metadata.has_variants
12 changes: 6 additions & 6 deletions tests/unit/test_wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@


@pytest.mark.django_db
def test_serve_variation_no_variant(site, rf):
def test_serve_variant_no_variant(site, rf):
page = site.root_page
request = rf.get('/')
args = tuple()
kwargs = {}

result = wagtail_hooks.serve_variation(page, request, args, kwargs)
result = wagtail_hooks.serve_variant(page, request, args, kwargs)
assert result is None


@pytest.mark.django_db
def test_serve_variation_with_variant_no_segment(site, rf, segmented_page):
def test_serve_variant_with_variant_no_segment(site, rf, segmented_page):
request = rf.get('/')
args = tuple()
kwargs = {}

page = segmented_page.personalisation_metadata.canonical_page
result = wagtail_hooks.serve_variation(page, request, args, kwargs)
result = wagtail_hooks.serve_variant(page, request, args, kwargs)
assert result is None


@pytest.mark.django_db
def test_serve_variation_with_variant_segmented(site, rf, segmented_page):
def test_serve_variant_with_variant_segmented(site, rf, segmented_page):
request = rf.get('/')
args = tuple()
kwargs = {}
Expand All @@ -38,7 +38,7 @@ def test_serve_variation_with_variant_segmented(site, rf, segmented_page):
adapter = adapters.get_segment_adapter(request)
adapter.set_segments([segment])

result = wagtail_hooks.serve_variation(page, request, args, kwargs)
result = wagtail_hooks.serve_variant(page, request, args, kwargs)
assert result.status_code == 200


Expand Down

0 comments on commit 281086a

Please sign in to comment.