示例#1
0
def edit_widget(request, **kwargs):
    try:
        if request.POST:
            "get form populated with widget options"
            widget = Widget.objects.get(id=kwargs.get("id"))
            options_form = WidgetOptionsForm(widget.widget_class, \
                                request.POST)
            if options_form.is_valid():
                if options_form.save(widget=widget):
                    data = {'valid': True, 'form': 'saved'}
            elif options_form.errors:
                data = ajaxerror(options_form)
        else:
            "This is a request to get a form for widget"
            ctx = RequestContext(request)
            "get widget form populated with widget options" 
            widget = Widget.objects.get(id=kwargs.get("id")) 
            if widget.hasOptions:
                initial = dict(("option_%s" % option.name, option.value) \
                             for option in widget.options.all())
                options_form = WidgetOptionsForm(widget.widget_class, \
                                data=initial)

                o = get_template("widget/options.html")
                ctx.update({'options_form': options_form})

                options = o.render(ctx)
                data = {'valid': False, 'type': 'ef', 'data': options}
            else:
                data = {'valid': True, 'type': 'nf'}

        return HttpResponse(json_serializer.encode(data), \
                            mimetype='application/json')
    except Exception:
        raise
示例#2
0
    def test_widget_options_form_saving(self):
        """
        This will modify a test widget with the assigned options
        """
        form = WidgetOptionsForm("TestWidget",
            data={"option_First": "Option 1", "option_Second": "Option 2", "option_Third": "Option 3"}
        )
        self.assertEqual(form.is_valid(), True)

        form.save(self.test_widgets[0])

        self.assertListEqual([o.value for o in self.test_widgets[0].options.all()], ['Option 1', 'Option 2', 'Option 3'])
示例#3
0
def widget_list(request):
    """
    Renders widget options based on supplied widget
    class or displays a select screen
    """
    data = {}
    #widget class exists so render widget options if any
    ctx = RequestContext(request)
    if request.user.is_superuser:
        widget_form = WidgetForm(request.POST, restrict_list=None)
    else:
        widget_form = WidgetForm(request.POST)
    widget_class = request.POST["widget_class"]
    widget_class_obj = get_widget(widget_class)

    #Widget has options, lets generate the options form
    options_form = WidgetOptionsForm(widget_class)
    if widget_form.is_valid():
        extra_js = []
        o = get_template("widget/options.html")
        ctx.update({'options_form': options_form,
                    'widget_class': widget_class_obj})
        model_form = get_model_form_for_widget(widget_class_obj)
        if model_form:
            ctx.update({'model_form': model_form})
            extra_js += model_form.extra_js

        options = o.render(ctx)
        extra_js += options_form.extra_js
        data = {'valid': False, 'type': 'fi', 'data': options, 'extra_js': extra_js}
    else:
        data = ajaxerror(widget_form)
    return HttpResponse(json_serializer.encode(data), mimetype='application/json')
示例#4
0
    def test_widget_options_form_saving(self):
        """
        This will modify a test widget with the assigned options
        """
        form = WidgetOptionsForm("TestWidget",
                                 data={
                                     "option_First": "Option 1",
                                     "option_Second": "Option 2",
                                     "option_Third": "Option 3"
                                 })
        self.assertEqual(form.is_valid(), True)

        form.save(self.test_widgets[0])

        self.assertListEqual(
            [o.value for o in self.test_widgets[0].options.all()],
            ['Option 1', 'Option 2', 'Option 3'])
示例#5
0
def widget_options(request, type):
    try:
        options_form = WidgetOptionsForm(type)
        ctx = RequestContext(request)
        o = get_template("widget/options.html")
        ctx.update({'options_form': options_form,
                    'widget_class': options_form.widget_class})

        options = o.render(ctx)
        data = {'valid': True, 'type': 'fi', 'opts': options}
    except WidgetHasNoOptions:
        data = {"valid": False, "error": "None"}

    return data
示例#6
0
def edit_widget(request, **kwargs):
    try:

        widget = Widget.objects.get(id=kwargs.get("id"))
        widget_class_obj = get_widget(widget.widget_class)
        containsModel = hasModel(widget_class_obj)

        if request.POST:
            "get form populated with widget options"
            options_form = WidgetOptionsForm(widget.widget_class, request.POST)
            if options_form.is_valid():
                if options_form.save(widget=widget):
                    data = {'valid': True, 'form': 'saved'}
            elif options_form.errors:
                data = ajaxerror(options_form)
            if containsModel:
                obj = get_widget_model_queryset(widget, widget_class_obj)
                model_form = get_model_form_for_widget(widget_class_obj,
                                                       {"POST": request.POST, "FILES": request.FILES}, instance=obj,
                                                       widget=widget)
                try:
                    if model_form.is_valid():
                        saved_obj = model_form.save()
                        data.update({"obj": saved_obj.id})
                    elif model_form.errors:
                        model_data = ajaxerror(model_form)
                        errors = dict(data.get("errors", {}), **model_data["errors"])
                        data = {'valid': False, "errors": errors}
                except Exception:
                    raise
        else:
            "This is a request to get a form for widget"
            ctx = RequestContext(request)
            "get widget form populated with widget options"

            initial = {'status': widget.status}
            if widget.hasOptions:
                initial.update(dict(("option_%s" % option.name, option.value), for option in widget.options.all()))
            options_form = WidgetOptionsForm(widget.widget_class,  data=initial)
            extra_js = []
            o = get_template("widget/options.html")
            ctx.update({'options_form': options_form})
            if containsModel:
                obj = get_widget_model_queryset(widget, widget_class_obj)
                model_form = get_model_form_for_widget(widget_class_obj, instance=obj, widget=widget)
                if model_form:
                    ctx.update({'model_form': model_form})
                    extra_js += model_form.extra_js

            options = o.render(ctx)

            extra_js += options_form.extra_js
            data = {'valid': False, 'type': 'ef', 'data': options, 'extra_js': extra_js}

        return HttpResponse(json_serializer.encode(data), mimetype='application/json')
    except Exception:
        raise Http404()
示例#7
0
def create_widget(request, **kwargs):
    """
    Renders widget options based on supplied widget
    class or displays a select screen
    """
    data = {}
    if not is_editable(Widget(), request):
        response = _("Permission denied")
        data = {
            'error': {"_all_": [response]},
            'permission': False
        }
    else:
        if request.POST:
            widget_class = request.POST["widget_class"]
            slot = request.POST["widgetslot"]
            try:
                page_obj = Page.objects.published(request.user)\
                                .get(id=request.POST["page"])
                options_form = WidgetOptionsForm(widget_class, request.POST)
                if options_form.is_valid():
                    try:
                        "update widget if it exists"
                        widget = Widget.objects.get(id=request.POST["widget"])
                    except Exception:
                        widget = Widget(widgetslot=slot, page=page_obj,
                                        widget_class=widget_class,
                                        user=request.user)
                        widget.save()

                    if options_form.save(widget=widget):
                        data = {'valid': True, 'form': 'saved'}
                elif options_form.errors:
                    data = ajaxerror(options_form)
            except Exception, e:
                data = {"valid": "false", "error": { "_all_": ["Something went wrong, please refresh the page"],}}
示例#8
0
def create_widget(request, **kwargs):
    """
    Renders widget options based on supplied widget
    class or displays a select screen
    """
    data = {}
    widget_class = request.POST["widget_class"]
    widget_class_obj = get_widget(widget_class)
    containsModel = hasModel(widget_class_obj)

    slot = request.POST["widgetslot"]
    try:
        page = Page.objects.get(id=request.POST["page"])

        ### HANDLE OPTIONS FORM ####
        options_form = WidgetOptionsForm(widget_class, request.POST, request.FILES)
        widget = None
        if options_form.is_valid():
            try:
                "update widget if it exists"
                widget = Widget.objects.get(id=request.POST["widget"])
            except Exception:
                widget = Widget(widgetslot=slot,
                                widget_class=widget_class,
                                user=request.user, page=page)
                widget.save()

            if options_form.save(widget=widget):
                data = {'valid': True, 'form': 'saved'}
        elif options_form.errors:
            data = ajaxerror(options_form)

        if widget is None and not options_form.hasOptions and containsModel:
            try:
                "update widget if it exists"
                widget = Widget.objects.get(id=request.POST["widget"])
            except Exception:
                widget = Widget(widgetslot=slot,
                                widget_class=widget_class,
                                user=request.user, page=page)
                widget.save()

        model_widget = None
        if widget: model_widget = widget
        model_form = get_model_form_for_widget(widget_class_obj,
                                               {"POST": request.POST, "FILES": request.FILES},
                                               widget=model_widget
        )
        if model_form:
            try:
                if model_form.is_valid():
                    saved_obj = model_form.save()
                    data.update({"obj": saved_obj.id})
                elif model_form.errors:
                    model_data = ajaxerror(model_form)
                    errors = dict(data.get("errors", {}), **model_data["errors"])
                    data = {'valid': False, "errors": errors}
            except Exception:
                raise
    except Exception, e:
        data = {"valid": False, \
                "errors": {"_all_": ["Something went wrong, please refresh the page"], "exception": e.message}}
示例#9
0
def create_widget(request, **kwargs):
    """
    Renders widget options based on supplied widget
    class or displays a select screen
    """
    data = {}
    widget_class = request.POST["widget_class"]
    widget_class_obj = get_widget(widget_class)
    containsModel = hasModel(widget_class_obj)

    slot = request.POST["widgetslot"]
    try:
        page = Page.objects.get(id=request.POST["page"])

        ### HANDLE OPTIONS FORM ####
        options_form = WidgetOptionsForm(widget_class, request.POST, request.FILES)
        widget = None
        if options_form.is_valid():
            try:
                "update widget if it exists"
                widget = Widget.objects.get(id=request.POST["widget"])
            except Exception:
                widget = Widget(widgetslot=slot,
                                widget_class=widget_class,
                                user=request.user, page=page)
                widget.save()

            if options_form.save(widget=widget):
                data = {'valid': True, 'form': 'saved'}
        elif options_form.errors:
            data = ajaxerror(options_form)

        if widget is None and not options_form.hasOptions and containsModel:
            try:
                "update widget if it exists"
                widget = Widget.objects.get(id=request.POST["widget"])
            except Exception:
                widget = Widget(widgetslot=slot,
                                widget_class=widget_class,
                                user=request.user, page=page)
                widget.save()

        model_widget = None
        if widget:
            model_widget = widget
        model_form = get_model_form_for_widget(widget_class_obj,
                                               {"POST": request.POST, "FILES": request.FILES},
                                               widget=model_widget
                                               )
        if model_form:
            try:
                if model_form.is_valid():
                    saved_obj = model_form.save()
                    data.update({"obj": saved_obj.id})
                elif model_form.errors:
                    model_data = ajaxerror(model_form)
                    errors = dict(data.get("errors", {}), **model_data["errors"])
                    data = {'valid': False, "errors": errors}
            except Exception:
                raise
    except Exception, e:
        data = {"valid": False,
                "errors": {"_all_": ["Something went wrong, please refresh the page"], "exception": e.message}}
示例#10
0
    def test_widget_options_form_creation(self):
        form = WidgetOptionsForm("TestWidget")
        field_label = [f for f in form.fields]
        expected = ['option_First', 'option_Second', 'option_Third']

        self.assertListEqual(field_label, expected)