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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanfoulis committed Aug 23, 2017
1 parent ff6aa92 commit de0385f
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 185 deletions.
16 changes: 2 additions & 14 deletions addon.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
{
"name": "Aldryn Segmentation",
"description": "Provides market segmentation to Aldryn!",
"url": "https://github.com/aldryn/aldryn-segmentation",
"package-name": "aldryn-segmentation",
"installed-apps": [
"aldryn_segmentation"
],
"author": {
"name": "Divio",
"url": "https://www.aldryn.com"
},
"license": {
"name": "BSD"
}
}
"installed-apps": []
}
15 changes: 15 additions & 0 deletions aldryn_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
from aldryn_client import forms


class Form(forms.BaseForm):

def to_settings(self, data, settings):
# from functools import partial
# from aldryn_addons.utils import boolean_ish, djsenv
# env = partial(djsenv, settings=settings)

settings['INSTALLED_APPS'].extend([
'aldryn_segmentation',
])
return settings
39 changes: 39 additions & 0 deletions aldryn_segmentation/migrations/0002_auto_20170823_0446.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('aldryn_segmentation', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='authenticatedsegmentpluginmodel',
name='cmsplugin_ptr',
field=models.OneToOneField(parent_link=True, related_name='aldryn_segmentation_authenticatedsegmentpluginmodel', auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin'),
),
migrations.AlterField(
model_name='cookiesegmentpluginmodel',
name='cmsplugin_ptr',
field=models.OneToOneField(parent_link=True, related_name='aldryn_segmentation_cookiesegmentpluginmodel', auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin'),
),
migrations.AlterField(
model_name='fallbacksegmentpluginmodel',
name='cmsplugin_ptr',
field=models.OneToOneField(parent_link=True, related_name='aldryn_segmentation_fallbacksegmentpluginmodel', auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin'),
),
migrations.AlterField(
model_name='segmentlimitpluginmodel',
name='cmsplugin_ptr',
field=models.OneToOneField(parent_link=True, related_name='aldryn_segmentation_segmentlimitpluginmodel', auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin'),
),
migrations.AlterField(
model_name='switchsegmentpluginmodel',
name='cmsplugin_ptr',
field=models.OneToOneField(parent_link=True, related_name='aldryn_segmentation_switchsegmentpluginmodel', auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin'),
),
]
125 changes: 0 additions & 125 deletions aldryn_segmentation/south_migrations/0001_initial.py

This file was deleted.

Empty file.
151 changes: 105 additions & 46 deletions aldryn_segmentation/templatetags/segmentation_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,125 @@

from __future__ import unicode_literals

from cms.toolbar.utils import get_toolbar_from_request
from django import template

from classytags.core import Options
from classytags.arguments import Argument

from cms.templatetags.cms_tags import RenderPlugin
from cms.templatetags.cms_tags import render_plugin

from ..segment_pool import SegmentOverride


register = template.Library()


class RenderSegmentPlugin(RenderPlugin):
name = 'render_segment_plugin'
options = Options(
Argument('plugin'),
Argument('render_plugin')
)
def plugin_is_renderable(context, plugin_instance):
'''
Determines whether this plugin is to be rendered in this context.
'''
child_plugin = plugin_instance.get_plugin_class_instance()

def is_renderable(self, context, plugin_instance):
'''
Determines whether this plugin is to be rendered in this context.
'''
try:
# This is for the toolbar to be able to override the "active" state
# to test as a content editor.
allow_overrides = child_plugin.allow_overrides
get_override = child_plugin.get_segment_override
except AttributeError:
# This doesn't quack like a SegmentPlugin, so, it always renders.
return True

# TODO: Seems to me this method is not necessary
# Once tests are established then maybe we can remove this.
# This is a segment plugin... or at least quacks like one.
if allow_overrides:
# only evaluate override if we allow overrides
override = get_override(context, plugin_instance)

child_plugin = plugin_instance.get_plugin_class_instance()

try:
allow_overrides = child_plugin.allow_overrides
get_override = child_plugin.get_segment_override
except AttributeError:
# This doesn't quack like a SegmentPlugin, so, it always renders.
if override == SegmentOverride.ForcedActive:
return True

# This is a segment plugin... or at least quacks like one.
if allow_overrides:
# only evaluate override if we allow overrides
override = get_override(context, plugin_instance)

if override == SegmentOverride.ForcedActive:
return True
elif override == SegmentOverride.ForcedInactive:
return False

# OK, what does the plugin's is_context_appropriate() say?
return child_plugin.is_context_appropriate(context, plugin_instance)

def render_tag(self, context, plugin, render_plugin):
response = super(RenderSegmentPlugin, self).render_tag(context, plugin)

if not (render_plugin and self.is_renderable(context, plugin)):
# OK, this is a Segmentation Plugin that is NOT appropriate for
# rendering in the current context. Unfortunately, we need to
# render the plugin, but throw away the results in order to allow
# the structureboard to display properly. Ugh!
elif override == SegmentOverride.ForcedInactive:
return False

# OK, what does the plugin's is_context_appropriate() say?
return child_plugin.is_context_appropriate(context, plugin_instance)


@register.simple_tag(takes_context=True)
def render_segment_plugin(context, plugin, render_plugin):
request = context['request']
toolbar = get_toolbar_from_request(request)
is_renderable = plugin_is_renderable(context, plugin)
if toolbar.uses_legacy_structure_mode:
# We need to handle it the old way. Render ALL plugins on the page so
# that the legacy structure board can display all of them.
rendered = render_plugin(context, plugin)
if render_plugin and is_renderable:
return rendered
else:
# We had to render the plugin and all its sub-plugins above and
# so that the legacy structure board sees the invisible plugins.
# (js via sekizai).
# We return nothing as content though, since the plugin should not
# be visible.
return ''
return response


register.tag(RenderSegmentPlugin)
else:
# The new way. yay!
# The structure including all the hidden plugins is handled
# entirely separate. So we don't have to worry about it here.
# We only need to render plugins that are actually visible.
if is_renderable:
return render_plugin(context, plugin)


# class RenderSegmentPlugin(RenderPlugin):
# name = 'render_segment_plugin'
# options = Options(
# Argument('plugin'),
# Argument('render_plugin')
# )
#
# def is_renderable(self, context, plugin_instance):
# '''
# Determines whether this plugin is to be rendered in this context.
# '''
#
# # TODO: Seems to me this method is not necessary
# # Once tests are established then maybe we can remove this.
#
# child_plugin = plugin_instance.get_plugin_class_instance()
#
# try:
# # This is for the toolbar to be able to override the "active" state
# # to test as a content editor.
# allow_overrides = child_plugin.allow_overrides
# get_override = child_plugin.get_segment_override
# except AttributeError:
# # This doesn't quack like a SegmentPlugin, so, it always renders.
# return True
#
# # This is a segment plugin... or at least quacks like one.
# if allow_overrides:
# # only evaluate override if we allow overrides
# override = get_override(context, plugin_instance)
#
# if override == SegmentOverride.ForcedActive:
# return True
# elif override == SegmentOverride.ForcedInactive:
# return False
#
# # OK, what does the plugin's is_context_appropriate() say?
# return child_plugin.is_context_appropriate(context, plugin_instance)
#
# def render_tag(self, context, plugin, render_plugin):
# response = super(RenderSegmentPlugin, self).render_tag(context, plugin)
#
# if not (render_plugin and self.is_renderable(context, plugin)):
# # OK, this is a Segmentation Plugin that is NOT appropriate for
# # rendering in the current context. Unfortunately, we need to
# # render the plugin, but throw away the results in order to allow
# # the structureboard to display properly. Ugh!
# return ''
# return response


# register.tag(RenderSegmentPlugin)

0 comments on commit de0385f

Please sign in to comment.