Exemplo n.º 1
0
def run_script(request, **argv):
    
    if 'script' in request.POST:
        code = request.POST['script']
        request.session['script_code'] = code
        x = decode_script("code", code)
        if x:    
            form_class=form_from_str(x[0])
            return render(request, 'schsimplescripts/script_form.html',  {'form': form_class()})
    else:
        if request.method == 'POST':
            if 'script_code' in request.session:
                code = request.session['script_code']
                x = decode_script("code", code)
                if x:                
                    form_class=form_from_str(x[0])
                    form=form_class(request.POST)
    
                    if form.is_valid():
                        argv = form.cleaned_data
                        exec(x[1])
                        v = locals().get('scripts_code', None)
                        if v:
                            parms = v(request, form.cleaned_data)
                            parms['form'] = form
                            script = SCRIPT_TEMPLATE % x[2]
                            template = Template(script)
                            context = RequestContext(request, parms)
                            ret_str = template.render(context)
                            return HttpResponse(ret_str)
                            
    return HttpResponse("Error")
Exemplo n.º 2
0
    def get_form(self, view, request, form_class, adding=False):
        data = self.get_json_data()
        if hasattr(self, "get_form_source"):
            txt = self.get_form_source()
            if txt:
                if data:
                    form_class2 = form_from_str(txt,
                                                init_data=data,
                                                base_form_class=form_class,
                                                prefix="json_")
                else:
                    form_class2 = form_from_str(txt,
                                                init_data={},
                                                base_form_class=form_class,
                                                prefix="json_")
                return view.get_form(form_class2)
            else:
                return view.get_form(form_class)
        elif data:

            class form_class2(form_class):
                def __init__(self, *args, **kwargs):
                    nonlocal data
                    super().__init__(*args, **kwargs)
                    for key, value in data.items():
                        self.fields["json_%s" % key] = forms.CharField(
                            label=key, initial=value)

            return view.get_form(form_class2)
        return view.get_form(form_class)
Exemplo n.º 3
0
def run(request, pk):
    
    script = models.Scripts.objects.get(pk=pk)
    form = None
    if script:
        form_class=form_from_str(script._form)    
        if form_class:
            if request.method == 'POST':
                form=form_class(request.POST)
                if form.is_valid():
                    argv = form.cleaned_data
                    exec(script._view)
                    v = locals().get('scripts_'+ script.name, None)
                    if v:
                        parms = v(request, form.cleaned_data)
                        parms['form'] = form
                        template_script = SCRIPT_TEMPLATE % script._template
                        template = Template(template_script)
                        context = RequestContext(request, parms)
                        ret_str = template.render(context)
                        return HttpResponse(ret_str)                
            else:
                form = form_class()
        return render_to_response('schsimplescripts/script_form.html',  {'form': form}, request=request)
    raise Http404("Script does not exist")
Exemplo n.º 4
0
def edit__group(request, group_id):

    group = models.CommonGroup.objects.get(pk=group_id)
    group_def = models.CommonGroupDef.objects.get(name=group.group_def_name)

    if group_def.declaration:
        form_class = form_from_str(group_def.declaration)
    else:
        return HttpResponse("ERROR")

    if request.POST or request.FILES:
        if request.method == 'POST':
            form = form_class(request.POST, request.FILES)
            if form.is_valid():
                if group_def.save_fun:
                    exec(group_def.save_fun)
                    data = locals()['save'](form, group)
                else:
                    data = form.cleaned_data
                if 'title' in data:
                    group.title = data['title']
                    del data['title']
                group._data = data
                group._data['json_update'] = True
                group.save()
                url = make_path('ok')
                return HttpResponseRedirect(url)

    if not request.POST:
        data = group.get_json_data()

        if group_def.load_fun:
            exec(group_def.load_fun)
            data_form = locals()['load'](data)
        else:
            data_form = data
        data_form['title'] = group.title
        form = form_class(initial=data_form)

    t = Template(ihtml_to_html(None, group_def.template))
    c = RequestContext(request, {
        'form': form,
        'group': group,
        'group_def': group_def
    })

    return HttpResponse(t.render(c))
Exemplo n.º 5
0
def edit__rep(request, rep_id):

    rep = models.Report.objects.get(pk=rep_id)
    rep_def = models.ReportDef.objects.get(name=rep.report_def_name)

    if rep_def.declaration:
        form_class = form_from_str(rep_def.declaration)
    else:
        return "ERROR"

    if request.POST or request.FILES:
        if request.method == 'POST':
            form = form_class(request.POST, request.FILES)
            if form.is_valid():
                if rep_def.save_fun:
                    exec(rep_def.save_fun)
                    data = locals()['save'](form, rep)
                else:
                    data = form.cleaned_data
                rep._data = data
                rep._data['json_update'] = True
                rep.save()
                url = make_path('ok')
                return HttpResponseRedirect(url)

    if not request.POST:
        data = rep.get_json_data()

        if rep_def.load_fun:
            exec(rep_def.load_fun)
            data_form = locals()['load'](data)
        else:
            data_form = data
        form = form_class(initial=data_form)

    t = Template(ihtml_to_html(None, rep_def.template))
    c = RequestContext(request, {'form': form, 'rep': rep, 'rep_def': rep_def})

    return HttpResponse(t.render(c))
Exemplo n.º 6
0
def edit_page_object(request):

    name = request.GET.get('name', None)
    if name:
        name = name.replace('\r', '').strip()
        name0 = name.split('_')[0]
        object_list = models.PageObjectsConf.objects.filter(name=name0)
        if len(object_list) > 0:
            obj = object_list[0]
            pk = request.GET.get('page_id', 0)
            page = models.Page.objects.get(pk=pk)

            if obj.edit_form:
                form_class = form_from_str(obj.edit_form)
            else:
                form_class = None
                form = None

            context = {'conf': obj, 'page': page, 'request': request}

            if request.POST or request.FILES:
                if request.method == 'POST':
                    if form_class:
                        form = form_class(request.POST, request.FILES)
                        if form.is_valid():
                            if obj.save_fun:
                                try:
                                    context['old_data'] = page.get_json_data(
                                    )[name]
                                except:
                                    pass
                                exec(obj.save_fun)
                                data = locals()['save'](form, context)
                            else:
                                data = form.cleaned_data
                            page._data = {name: data}
                            page._data['json_update'] = True
                            page.operator = request.user.username
                            page.update_time = datetime.datetime.now()
                            page.save()
                            url = make_path('ok')
                            return HttpResponseRedirect(url)
                    else:
                        url = make_path('ok')
                        return HttpResponseRedirect(url)

            if not request.POST:
                if form_class:
                    data = page.get_json_data()

                    if obj.load_fun:
                        exec(obj.load_fun)
                        data_form = locals()['load'](data, context)
                    else:
                        if name in data:
                            data_form = data[name]
                        else:
                            data_form = {}
                    form = form_class(initial=data_form)

            template_name1 = (obj.app + "/" +
                              obj.name).lower() + "_wikiobj_edit.html"
            template_name2 = "schwiki/wikiobj_edit.html"

            t = select_template([
                template_name1,
                template_name2,
            ])
            c = {'form': form, 'object': obj, 'page': page}

            return HttpResponse(t.render(c, request))

    url = make_path('ok')
    return HttpResponseRedirect(url)