Skip to content
This repository has been archived by the owner on Jun 6, 2020. It is now read-only.

Commit

Permalink
[#52] Page model should use the standard statemachine
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewebdev committed Apr 20, 2016
1 parent 001edeb commit a0da355
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ostinato/pages/__init__.py
Expand Up @@ -5,7 +5,7 @@
OSTINATO_PAGES_SETTINGS = getattr(settings, 'OSTINATO_PAGES_SETTINGS', {})
PAGES_SETTINGS = {
'CACHE_NAME': 'default',
'DEFAULT_STATE': 5,
'DEFAULT_STATE': 'public',
}
PAGES_SETTINGS.update(OSTINATO_PAGES_SETTINGS)

2 changes: 1 addition & 1 deletion ostinato/pages/managers.py
Expand Up @@ -10,7 +10,7 @@ class PageManager(TreeManager):

def published(self):
return self.get_queryset().filter(
publish_date__lte=timezone.now(), state=5)
publish_date__lte=timezone.now(), state='public')

def get_navbar(self, for_page=None, clear_cache=False):
"""
Expand Down
24 changes: 24 additions & 0 deletions ostinato/pages/migrations/0002_auto_20160420_2100.py
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-04-20 21:00
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterModelOptions(
name='page',
options={'permissions': (('public_view_page', '[Public] Can View Page'), ('public_edit_page', '[Public] Can Edit Page'), ('public_delete_page', '[Public] Can Delete Page'), ('can_make_private_page', 'Can Make_private Page'), ('private_view_page', '[Private] Can View Page'), ('private_edit_page', '[Private] Can Edit Page'), ('private_delete_page', '[Private] Can Delete Page'), ('can_make_public_page', 'Can Make_public Page')), 'verbose_name': 'Page', 'verbose_name_plural': 'Pages'},
),
migrations.AlterField(
model_name='page',
name='state',
field=models.CharField(choices=[(b'public', b'Public'), (b'private', b'Private')], default=5, max_length=20, verbose_name='State'),
),
]
32 changes: 32 additions & 0 deletions ostinato/pages/migrations/0003_auto_20160420_2111.py
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-04-20 21:11
from __future__ import unicode_literals

from django.db import migrations


def update_states(apps, schema_editor):
"""
Update from old integer based states to the now more verbose
states
"""
Page = apps.get_model('ostinato_pages', 'Page')
for page in Page.objects.all():
if page.state == '1':
page.state == 'private'
elif page.state == '5':
page.state == 'public'
page.save()



class Migration(migrations.Migration):

dependencies = [
('ostinato_pages', '0002_auto_20160420_2100'),
]

operations = [
migrations.RunPython(update_states),
]

9 changes: 5 additions & 4 deletions ostinato/pages/models.py
Expand Up @@ -52,9 +52,10 @@ class Page(MPTTModel):
show_in_nav = models.BooleanField(_("Show in nav"), default=True)
show_in_sitemap = models.BooleanField(_("Show in sitemap"), default=True)

state = models.IntegerField(
_("State"), default=PAGES_SETTINGS['DEFAULT_STATE'],
choices=get_workflow().get_choices())
state = models.CharField(_("State"),
max_length=20,
default=PAGES_SETTINGS['DEFAULT_STATE'],
choices=get_workflow().get_choices())

created_date = models.DateTimeField(_("Created date"), null=True, blank=True)
modified_date = models.DateTimeField(_("Modified date"), null=True, blank=True)
Expand Down Expand Up @@ -87,7 +88,7 @@ def save(self, *args, **kwargs):

# since it's created the first time, and we want it
# published by default, we need to set the date now.
if self.state == 5:
if self.state == 'public':
self.publish_date = now

self.modified_date = now
Expand Down
12 changes: 6 additions & 6 deletions ostinato/pages/workflow.py
@@ -1,24 +1,24 @@
from django.conf import settings
from django.utils import timezone

from ostinato.statemachine import State, IntegerStateMachine
from ostinato.statemachine import State, StateMachine


DEFAULT_STATE = getattr(settings, 'OSTINATO_PAGES_DEFAULT_STATE', 5)
DEFAULT_STATE = getattr(settings, 'OSTINATO_PAGES_DEFAULT_STATE', 'public')


class Private(State):
verbose_name = 'Private'
transitions = {'make_public': 5}
transitions = {'make_public': 'public'}


class Public(State):
verbose_name = 'Public'
transitions = {'make_private': 1}
transitions = {'make_private': 'private'}


class PageWorkflow(IntegerStateMachine):
state_map = {1: Private, 5: Public}
class PageWorkflow(StateMachine):
state_map = {'private': Private, 'public': Public}
initial_state = DEFAULT_STATE


Expand Down
2 changes: 1 addition & 1 deletion ostinato/tests/pages/tests/test_managers.py
Expand Up @@ -18,7 +18,7 @@ def test_published(self):
list(Page.objects.published().values_list('id', flat=True)))

def test_get_empty_navbar(self):
Page.objects.published().update(state=1)
Page.objects.published().update(state='private')
empty_nav = Page.objects.get_navbar()
self.assertEqual([], empty_nav)

Expand Down
4 changes: 2 additions & 2 deletions ostinato/tests/pages/tests/test_views.py
Expand Up @@ -54,7 +54,7 @@ def test_function_based_view(self):
def test_unauthorized_user_raises_forbidden(self):
# First we make the page private
p = Page.objects.get(slug='page-1')
p.state = 1
p.state = 'private'
p.save()

response = self.client.get('/page-1/')
Expand All @@ -63,7 +63,7 @@ def test_unauthorized_user_raises_forbidden(self):
def test_superuser_can_access_private_page(self):
# First we make the page private
p = Page.objects.get(slug='page-1')
p.state = 1
p.state = 'private'
p.save()

# Make a second user, which is a superuser
Expand Down

0 comments on commit a0da355

Please sign in to comment.