Skip to content

Commit

Permalink
Use constants for enabled/disabling status on Segment
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed May 31, 2017
1 parent 3d920d8 commit 31f8a32
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/wagtail_personalisation/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def refresh(self):
still apply to the requesting visitor.
"""
enabled_segments = Segment.objects.filter(status='enabled')
enabled_segments = Segment.objects.filter(status=Segment.STATUS_ENABLED)
persistent_segments = enabled_segments.filter(persistent=True)
session_segments = self.request.session['segments']
rules = AbstractBaseRule.__subclasses__()
Expand Down
16 changes: 10 additions & 6 deletions src/wagtail_personalisation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
@python_2_unicode_compatible
class Segment(ClusterableModel):
"""The segment model."""
STATUS_ENABLED = 'enabled'
STATUS_DISABLED = 'disabled'

STATUS_CHOICES = (
(STATUS_ENABLED, 'Enabled'),
(STATUS_DISABLED, 'Disabled'),
)

name = models.CharField(max_length=255)
create_date = models.DateTimeField(auto_now_add=True)
edit_date = models.DateTimeField(auto_now=True)
enable_date = models.DateTimeField(null=True, editable=False)
disable_date = models.DateTimeField(null=True, editable=False)
visit_count = models.PositiveIntegerField(default=0, editable=False)
STATUS_CHOICES = (
('enabled', 'Enabled'),
('disabled', 'Disabled'),
)
status = models.CharField(max_length=20, choices=STATUS_CHOICES,
default="enabled")
status = models.CharField(
max_length=20, choices=STATUS_CHOICES, default=STATUS_ENABLED)
persistent = models.BooleanField(
default=False, help_text=_("Should the segment persist between visits?"))
match_any = models.BooleanField(
Expand Down
4 changes: 2 additions & 2 deletions src/wagtail_personalisation/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def check_status_change(sender, instance, *args, **kwargs):
original_status = ""

if original_status != instance.status:
if instance.status == "enabled":
if instance.status == instance.STATUS_ENABLED:
instance.enable_date = timezone.now()
instance.visit_count = 0
return instance
if instance.status == "disabled":
if instance.status == instance.STATUS_DISABLED:
instance.disable_date = timezone.now()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ <h2>{{ segment }}</h2>

{% if user_can_create %}
<ul class="block_actions">
{% if segment.status == "disabled" %}
{% if segment.status == segment.STATUS_DISABLED %}
<li><a href="{% url 'segment:toggle' segment.pk %}" title="{% trans "Enable this segment" %}">enable</a></li>
{% elif segment.status == "enabled" %}
{% elif segment.status == segment.STATUS_ENABLED %}
<li><a href="{% url 'segment:toggle' segment.pk %}" title="{% trans "Disable this segment" %}">disable</a></li>
{% endif %}
<li><a href="edit/{{ segment.pk }}" title="{% trans "Configure this segment" %}">configure this</a></li>
Expand Down Expand Up @@ -110,4 +110,4 @@ <h2>{{ segment }}</h2>
{% endblock %}
</div>
</div>
{% endblock %}
{% endblock %}
8 changes: 4 additions & 4 deletions src/wagtail_personalisation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def toggle(request, segment_id):
if request.user.has_perm('wagtailadmin.access_admin'):
segment = get_object_or_404(Segment, pk=segment_id)

if segment.status == 'enabled':
segment.status = 'disabled'
elif segment.status == 'disabled':
segment.status = 'enabled'
if segment.status == Segment.STATUS_ENABLED:
segment.status = Segment.STATUS_DISABLED
elif segment.status == Segment.STATUS_DISABLED:
segment.status = Segment.STATUS_ENABLED

segment.save()

Expand Down
4 changes: 2 additions & 2 deletions src/wagtail_personalisation/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def serve_variation(page, request, serve_args, serve_kwargs):

for segment in adapter.get_all_segments():
try:
user_segment = Segment.objects.get(pk=segment['id'],
status='enabled')
user_segment = Segment.objects.get(
pk=segment['id'], status=Segment.STATUS_ENABLED)
except Segment.DoesNotExist:
user_segment = None
if user_segment:
Expand Down

0 comments on commit 31f8a32

Please sign in to comment.