Skip to content

Commit

Permalink
Add get_form_class method to content blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Vetter committed Oct 23, 2013
1 parent 92e8119 commit 77c2a1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 3 additions & 0 deletions fancypages/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ class AbstractContentBlock(models.Model):

objects = InheritanceManager()

def get_form_class(self):
return self.form_class

def get_template_names(self):
if self.template_name:
return [self.template_name]
Expand Down
22 changes: 13 additions & 9 deletions fancypages/api/serialisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def get_form_kwargs(self, obj):
def get_form_class(self, obj):
# check if the block has a class-level attribute that
# defines a specific form class to be used
form_class = getattr(obj.__class__, 'form_class', None)
return modelform_factory(obj.__class__, form=form_class)
get_form_class = getattr(obj.__class__, 'get_form_class', None)
if not get_form_class:
return modelform_factory(obj.__class__)
return modelform_factory(obj.__class__, form=get_form_class())


class BlockSerializer(RenderFormFieldMixin, serializers.ModelSerializer):
Expand All @@ -67,13 +69,15 @@ def restore_object(self, attrs, instance=None):

def get_form_class(self, obj):
model = self.object.__class__
form_class = getattr(model, 'form_class')
if not form_class:
form_class = getattr(
forms,
"%sForm" % model.__name__,
forms.BlockForm
)
get_form_class = getattr(model, 'get_form_class')
if get_form_class:
return modelform_factory(model, form=get_form_class())

form_class = getattr(
forms,
"%sForm" % model.__name__,
forms.BlockForm
)
return modelform_factory(model, form=form_class)

class Meta:
Expand Down

0 comments on commit 77c2a1f

Please sign in to comment.