Skip to content

Commit

Permalink
Merge pull request #31 from eldarion/add-hookset
Browse files Browse the repository at this point in the history
Add hookset support
  • Loading branch information
jacobwegner authored Jan 5, 2018
2 parents 690e087 + 00e904c commit e7b1284
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
28 changes: 28 additions & 0 deletions formly/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import importlib

from django.conf import settings # noqa
from django.core.exceptions import ImproperlyConfigured

from appconf import AppConf


def load_path_attr(path):
i = path.rfind(".")
module, attr = path[:i], path[i + 1:]
try:
mod = importlib.import_module(module)
except ImportError as e:
raise ImproperlyConfigured("Error importing {0}: '{1}'".format(module, e))
try:
attr = getattr(mod, attr)
except AttributeError:
raise ImproperlyConfigured("Module '{0}' does not define a '{1}'".format(module, attr))
return attr


class FormlyAppConf(AppConf):

HOOKSET = "formly.hooks.FormlyDefaultHookset"

def configure_hookset(self, value):
return load_path_attr(value)()
7 changes: 6 additions & 1 deletion formly/forms/design.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import forms

from formly.models import Survey, Page, Field, FieldChoice, OrdinalScale
from ..hooks import hookset
from ..models import Survey, Page, Field, FieldChoice, OrdinalScale


class SurveyCreateForm(forms.ModelForm):
Expand Down Expand Up @@ -59,6 +60,10 @@ class Meta:

class FieldForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(FieldForm, self).__init__(*args, **kwargs)
self.fields["field_type"].choices = hookset.field_type_choices

class Meta:
model = Field
fields = [
Expand Down
20 changes: 20 additions & 0 deletions formly/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class FormlyDefaultHookset(object):

@property
def field_type_choices(self):
"""
Customize available field type choices when designing a survey
default: Field.FIELD_TYPE_CHOICES
"""
from formly.models import Field
return Field.FIELD_TYPE_CHOICES


class HookProxy(object):

def __getattr__(self, attr):
from formly.conf import settings
return getattr(settings.FORMLY_HOOKSET, attr)


hookset = HookProxy()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def read(*parts):
tests_require=[],
install_requires=[
"jsonfield>=1.0.3",
"django-appconf>=1.0.2",
],
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down

0 comments on commit e7b1284

Please sign in to comment.