Пример #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()
Пример #2
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')
Пример #3
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
Пример #4
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')
Пример #5
0
def widget_list(request):
    """
    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': [response],
            'permission': False
        }
    else:
        if request.POST:
            "widget class exists so render widget options if any"
            ctx = RequestContext(request)
            widget_form = WidgetForm(request.POST)
            widget_class = request.POST["widget_class"]
            widget_class_obj = get_widget(widget_class)

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

                options = o.render(ctx)
                data = {'valid': False, 'type':'fi', 'data':options}
            else:
                data = ajaxerror(widget_form)
            return HttpResponse(json_serializer.encode(data), mimetype='application/json')

        else:
            return HttpResponseRedirect("/")
Пример #6
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"],}}
Пример #7
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}}
Пример #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}}