-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from LabD/feature/segment-template-block
Adds an ifsegment template block
- Loading branch information
Showing
4 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/wagtail_personalisation/templatetags/wagtail_personalisation_tags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django import template | ||
from django.template import TemplateSyntaxError | ||
|
||
from django.utils.safestring import mark_safe | ||
|
||
from wagtail_personalisation.models import Segment | ||
from wagtail_personalisation.utils import parse_tag | ||
|
||
register = template.Library() | ||
|
||
|
||
def do_segment(parser, token): | ||
"""Block that only shows content if user is in chosen segment.""" | ||
# Parse the tag | ||
tag_name, _, kwargs = parse_tag(token, parser) | ||
|
||
# If no segment is provided this block will raise an error | ||
if set(kwargs.keys()) != {'name'}: | ||
usage = '{% {tag_name} name="segmentname" %} ... {% end{tag_name} %}'.format(tag_name=tag_name) | ||
raise TemplateSyntaxError("Usage: %s" % usage) | ||
|
||
nodelist = parser.parse(('endsegment',)) | ||
parser.delete_first_token() | ||
|
||
return SegmentNode(nodelist, name=kwargs['name']) | ||
|
||
|
||
register.tag('segment', do_segment) | ||
|
||
|
||
class SegmentNode(template.Node): | ||
"""Node that only returns contents if user is in the segment. | ||
This node checks if the chosen segment exists and if the | ||
user has been segmented in the chosen segment. | ||
If not it will return nothing | ||
""" | ||
def __init__(self, nodelist, name): | ||
self.nodelist = nodelist | ||
self.name = name | ||
|
||
def render(self, context): | ||
# Check if segment exists | ||
name = self.name.resolve(context) | ||
segment = Segment.objects.filter(name=name).first() | ||
if not segment: | ||
return "" | ||
|
||
# Check if user has segment | ||
user_segment = context['request'].segment_adapter.get_segment(segment_id=segment.pk) | ||
if not user_segment: | ||
return "" | ||
|
||
content = self.nodelist.render(context) | ||
content = mark_safe(content) | ||
|
||
return content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters