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 bceaf35
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 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
11 changes: 11 additions & 0 deletions pagetree/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,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 Down

0 comments on commit bceaf35

Please sign in to comment.