Exemplo n.º 1
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()
Exemplo n.º 2
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}}
Exemplo n.º 3
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}}