Skip to content

Commit

Permalink
Make add_pageblock_from_dict more flexible
Browse files Browse the repository at this point in the history
This allows you to pass a pageblock instance to
`add_pageblock_from_dict`, as well as a dictionary.

For a complex custom pageblock, it can be simplest to initialize it
on its own with a factory, and then pass it into a call to
`add_child_section_from_dict`, similar to here:
http://django-pagetree.readthedocs.org/en/latest/testing.html

referencing issue #92
  • Loading branch information
nikolas committed Jun 19, 2015
1 parent 35ab9e9 commit 8699b65
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
7 changes: 7 additions & 0 deletions pagetree/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Meta:
display_name = None
"""The name for this block that's used in pagetree's menus."""

def as_dict(self):
"""Returns the PageBlock as a dictionary.
:rtype: dictionary
"""
return dict(block_type=self.display_name)

def allow_redo(self):
"""Determines whether this block allows users to resubmit answers.
Expand Down
9 changes: 8 additions & 1 deletion pagetree/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,14 @@ def from_dict(self, d):
self.add_child_section_from_dict(c)

def add_pageblock_from_dict(self, d):
target_type = d.get('block_type', '')
try:
target_type = d.get('block_type', '')
except AttributeError:
# If we can't call .get on the dictionary,
# it must not be a dictionary. Assume it's a pageblock,
# and turn it into a dict here.
d = d.as_dict()
target_type = d.get('block_type', '')

# now we need to figure out which kind of pageblock to create
found_pbclass = None
Expand Down
4 changes: 3 additions & 1 deletion pagetree/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def edit(self, vals, files):
self.save()

def as_dict(self):
return dict(body=self.body)
d = super(TestBlock, self).as_dict()
d['body'] = self.body
return d

def import_from_dict(self, d):
self.body = d.get('body', '')
Expand Down
2 changes: 1 addition & 1 deletion pagetree/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
model = User

username = factory.Sequence(lambda n: "user%03d" % n)
password = factory.PostGenerationMethodCall('set_password', 'test')
Expand Down
39 changes: 36 additions & 3 deletions pagetree/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import django.db

from pagetree.models import Hierarchy, PageBlock, UserPageVisit
from pagetree.test_models import TestBlock
from pagetree.tests.factories import (
RootSectionFactory, TestBlockFactory, UserFactory, UserPageVisitFactory
)
Expand Down Expand Up @@ -424,7 +425,7 @@ def test_update_pageblocks_order(self):
normal_order)

def test_import_from_dict(self):
b = self.section1.pageblock_set.all()[0]
b = self.section1.pageblock_set.first()
d = {'label': 'new label',
'css_extra': 'new css_extra',
'body': 'new body'}
Expand All @@ -435,6 +436,18 @@ def test_import_from_dict(self):
sub = b.block()
self.assertEquals(sub.body, 'new body')

def test_import_custom_block_dict(self):
b = self.section1.pageblock_set.first()
d = {
'block_type': 'Test Block',
'body': 'abc',
}
b.import_from_dict(d)

sub = b.block()
self.assertEquals(sub.display_name, 'Test Block')
self.assertEquals(sub.body, 'abc')


class UserTrackingTest(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -765,6 +778,17 @@ def test_add_pageblock_from_dict(self):
block = self.section.pageblock_set.first()
assert(block is not None)
self.assertEqual(block.block().body, 'test body')
self.assertEqual(
self.section.pageblock_set.first(), block,
'The PageBlock has been added to the Section')

def test_add_pageblock_from_dict_instance(self):
testblock = TestBlockFactory()
self.section.add_pageblock_from_dict(testblock)
self.assertEqual(
self.section.pageblock_set.first().block().display_name,
testblock.display_name,
'The PageBlock has been added to the Section')

def test_empty_section_is_unlocked(self):
self.assertTrue(self.section.unlocked(self.user))
Expand All @@ -779,9 +803,18 @@ def test_section_with_test_block_is_unlocked(self):


class TestBlockTest(TestCase):
def setUp(self):
self.b = TestBlockFactory()

def test_is_valid_from_factory(self):
block = TestBlockFactory()
block.full_clean()
self.b.full_clean()

def test_create_from_dict(self):
testblock = TestBlock.create_from_dict({
'body': 'abc',
})
self.assertEqual(testblock.display_name, 'Test Block')
self.assertEqual(testblock.body, 'abc')


class UserPageVisitTest(TestCase):
Expand Down

0 comments on commit 8699b65

Please sign in to comment.