Commits

Osiloke Emoekpere  committed d757256

Cleaned widget fields code, added a RESTRICTED_WIDGETS setting to filter out widgets that shouldnt show up in frontend administration

  • Participants
  • Parent commits b19401e
  • Branches enhancements

Comments (0)

Files changed (4)

File widget/defaults.py

 #                  "for the image fields`."),
 #    editable=False,
 #    default="django.forms.FileInput",
-#)
+#)
+
+register_setting(
+    name="RESTRICTED_WIDGETS",
+    label="Restricted Widgets",
+    description=_("Widgets which cannot be added by a client "),
+    editable=False,
+    append=True,
+    default=[
+        "TestWidget"
+    ],
+)

File widget/fields.py

         super(PageWidgetClass, self).__init__(*args, **kwargs)
 
     def formfield(self, form_class=forms.CharField, **kwargs):
-        # This is a fairly standard way to set up some defaults
-#        while letting the caller override them.
         defaults = {'form_class': PageWidgetClassField}
-#        if self.choices:
-#            defaults = {'choices': self.choices}
-#        else:
-#            defaults = {'form_class': PageWidgetClassField}
         defaults.update(kwargs)
         return super(PageWidgetClass, self).formfield(**defaults)
 

File widget/forms.py

 from django import forms
 from django.forms.widgets import HiddenInput
 
-from widget.widget_pool import get_widget_options, WidgetHasNoOptions
+from widget.widget_pool import get_widget_options, WidgetHasNoOptions, get_all_page_widgets
 from widget.models import WidgetOptionEntry, Widget
 
 from mezzanine.pages.models import Page
+from mezzanine.conf import settings
+
+from uuid import uuid4
+
 import option_fields
 
-from mezzanine.conf import settings
-from uuid import uuid4
-
 
 class WidgetForm(forms.ModelForm):
 
         super(WidgetForm, self).__init__(*args, **kwargs)
         self.uuid = str(uuid4())
         self.fields["page"].queryset = Page.objects.get_query_set()
+        self.fields["widget_class"].choices = get_all_page_widgets(settings.RESTRICTED_WIDGETS)
 
 
     class Meta:
     extra_js = []
     hasOptions = False
 
+#    widget_class = forms.HiddenInput(choices=get_all_page_widgets(settings.RESTRICTED_WIDGETS))
+
     def __init__(self, widget_class, *args, **kwargs):
         """
         Dynamically add each of the form fields for the given form model

File widget/widget_pool.py

     except Exception:
         return None
 
-def get_all_page_widgets():
+def get_all_page_widgets(restrict_list=None):
     autodiscover()
-    listes = ((w, page_widgets[w].Meta.name) for w in page_widgets)
-    return listes
+    widgs = ()
+    if restrict_list:
+        restrict = lambda w: w in restrict_list or False
+    else:
+        restrict = lambda w: False
+
+    for w in page_widgets:
+        if not restrict(w):
+            yield (w, page_widgets[w].Meta.name)
 
 def get_widget_array():
     return [w for w in page_widgets]