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 d02920b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 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()

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

0 comments on commit d02920b

Please sign in to comment.