示例#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()
def render_widgets_for_slot(slot, widget_context):
    """
    Renders all widgets assigned to a WidgetSlot
    """

    page = widget_context.get("page", None)
    user = widget_context["request"].user
    rendered_widgets = []
    "Render regular paged widgets and page less widgets (universal widgets)"
    widgets = Widget.objects.published_for_page_or_pageless(page, slot, user)
    for widget in widgets:
        widget_class = get_widget(widget.widget_class)
        widget_context.update({'widget': widget})
        if widget_class is not None:
            widget_options = None
            widget_class = widget_class()
            if hasattr(widget_class, "options"):
                try:
                    widget_options = dict(((o["name"], o["value"])\
                         for o in widget.options.values("name", "value")))
                    widget_context.update({"opts": widget_options})
                except Exception, e:
                    raise e
            queryset = get_widget_model_queryset(widget, widget_class)
            rendered_widget = render_template(widget_class\
                    ._render(widget_context, slot, queryset, widget_options),\
                              widget_class.template, raw=widget_class.raw)


            rendered_widgets.append({'widget': widget, \
                                    'meta': widget_class.Meta, \
                                    'content': rendered_widget})
def render_widget(widget, widget_context):
    """
    Render a single widgtet
    """
    widget_class = get_widget(widget.widget_class)
    widget_context.update({'widget': widget})

    if widget_class is not None:
        widget_options = None
        widget_class = widget_class()
        if hasattr(widget_class, "options"):
            try:
                widget_options = dict(((o["name"], o["value"])\
                    for o in widget.options.values("name", "value")))
                widget_context.update({"opts": widget_options})
            except Exception, e:
                raise e
        queryset = get_widget_model_queryset(widget, widget_class)
        rendered_widget = render_template(widget_class\
        ._render(widget_context, widget.slot, queryset, widget_options),\
            widget_class.template, raw=widget_class.raw)


        return {'widget': widget,\
                                 'meta': widget_class.Meta,\
                                 'content': rendered_widget}
示例#4
0
def render_widgets_for_slot(slot, widget_context):
    """
    Renders all widgets assigned to a WidgetSlot
    """

    page = widget_context.get("page", None)
    user = widget_context["request"].user
    rendered_widgets = []
    "Render regular paged widgets and page less widgets (universal widgets)"
    widgets = Widget.objects.published_for_page_or_pageless(page, slot, user)
    for widget in widgets:
        widget_class = get_widget(widget.widget_class)
        widget_context.update({'widget': widget})
        if widget_class is not None:
            widget_options = None
            widget_class = widget_class()
            if hasattr(widget_class, "options"):
                try:
                    widget_options = dict(((o["name"], o["value"]), for o in widget.options.values("name", "value")))
                    widget_context.update({"opts": widget_options})
                except Exception, e:
                    raise e
            queryset = get_widget_model_queryset(widget, widget_class)
            rendered_widget = render_template(widget_class._render(widget_context, slot, queryset, widget_options),
                                              widget_class.template, raw=widget_class.raw)

            rendered_widgets.append({'widget': widget, 'meta': widget_class.Meta, 'content': rendered_widget})
示例#5
0
def render_widget(widget, widget_context):
    """
    Render a single widgtet
    """
    widget_class = get_widget(widget.widget_class)
    widget_context.update({'widget': widget})

    if widget_class is not None:
        widget_options = None
        widget_class = widget_class()
        if hasattr(widget_class, "options"):
            try:
                widget_options = dict(((o["name"], o["value"]), for o in widget.options.values("name", "value")))
                widget_context.update({"opts": widget_options})
            except Exception, e:
                raise e
        queryset = get_widget_model_queryset(widget, widget_class)
        rendered_widget = render_template(widget_class._render(widget_context, widget.slot, queryset, widget_options),
                                          widget_class.template, raw=widget_class.raw)

        return {'widget': widget, 'meta': widget_class.Meta, 'content': rendered_widget}
    """
    Render a single widgtet
    """
    widget_class = get_widget(widget.widget_class)
    widget_context.update({'widget': widget})

    if widget_class is not None:
        widget_options = None
        widget_class = widget_class()
        if hasattr(widget_class, "options"):
            try:
                widget_options = dict(((o["name"], o["value"]), for o in widget.options.values("name", "value")))
                widget_context.update({"opts": widget_options})
            except Exception, e:
                raise e
        queryset = get_widget_model_queryset(widget, widget_class)
        rendered_widget = render_template(widget_class._render(widget_context, widget.slot, queryset, widget_options),
                                          widget_class.template, raw=widget_class.raw)

        return {'widget': widget, 'meta': widget_class.Meta, 'content': rendered_widget}


def render_widgets_for_slot(slot, widget_context):
    """
    Renders all widgets assigned to a WidgetSlot
    """

    page = widget_context.get("page", None)
    user = widget_context["request"].user
    rendered_widgets = []
    "Render regular paged widgets and page less widgets (universal widgets)"