Пример #1
0
def long_chain_limit(request, upload_form):
    buff = StringIO()
    zfile = zipfile.ZipFile(buff, 'w', zipfile.ZIP_DEFLATED)
    for f in upload_form.cleaned_data["files"]:
        parser = dataparser.DataParser(f)
        homolumo, gap = parser.get_graphs()

        name, _ = os.path.splitext(f.name)
        if len(upload_form.cleaned_data["files"]) > 1:
            zfile.writestr(name + "/output.txt", parser.format_output())
            zfile.writestr(name + "/homolumo.eps", homolumo.getvalue())
            zfile.writestr(name + "/gap.eps", gap.getvalue())
        else:
            zfile.writestr("output.txt", parser.format_output())
            zfile.writestr("homolumo.eps", homolumo.getvalue())
            zfile.writestr("gap.eps", gap.getvalue())

    if len(upload_form.cleaned_data["files"]) > 1:
        name = "output"
    zfile.close()
    buff.flush()

    ret_zip = buff.getvalue()
    buff.close()

    response = HttpResponse(ret_zip, content_type="application/zip")
    response["Content-Disposition"] = "attachment; filename=%s.zip" % name
    return response
Пример #2
0
def long_chain_limit(request, upload_form):
    buff = StringIO()
    zfile = zipfile.ZipFile(buff, 'w', zipfile.ZIP_DEFLATED)
    for f in upload_form.cleaned_data["files"]:
        parser = dataparser.DataParser(f)
        homolumo, gap = parser.get_graphs()

        name, _ = os.path.splitext(f.name)
        if len(upload_form.cleaned_data["files"]) > 1:
            zfile.writestr(name + "/output.txt", parser.format_output())
            zfile.writestr(name + "/homolumo.eps", homolumo.getvalue())
            zfile.writestr(name + "/gap.eps", gap.getvalue())
        else:
            zfile.writestr("output.txt", parser.format_output())
            zfile.writestr("homolumo.eps", homolumo.getvalue())
            zfile.writestr("gap.eps", gap.getvalue())

    if len(upload_form.cleaned_data["files"]) > 1:
        name = "output"
    zfile.close()
    buff.flush()

    ret_zip = buff.getvalue()
    buff.close()

    response = HttpResponse(ret_zip, content_type="application/zip")
    response["Content-Disposition"] = "attachment; filename=%s.zip" % name
    return response
Пример #3
0
def draw(xvals, yvals, vert, horz):
    offset = 0.25
    scale = 10
    mins = numpy.array([min(xvals), min(yvals)])
    maxs = numpy.array([max(xvals), max(yvals)])
    dimensions = maxs - mins + 2 * offset
    mins = mins - offset
    dimensions *= scale

    WIDTH = int(dimensions[1])
    HEIGHT = int(dimensions[0])

    f = StringIO()
    surface = cairo.SVGSurface(f, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)

    ctx.scale(scale, scale)
    ctx.rotate(math.pi / 2)
    # hack to fix the translation from the rotation
    ctx.translate(0, -dimensions[1] / scale)
    ctx.translate(-mins[0], -mins[1])
    ctx.set_line_width(0.1)

    for i, (x, y) in enumerate(zip(xvals, yvals)):
        if i in vert:
            color = (0, 255, 0)
        elif i in horz:
            color = (255, 0, 0)
        else:
            color = (0, 0, 255)

        ctx.set_source_rgb(*color)
        ctx.arc(x, y, 0.25, 0, 2 * math.pi)
        ctx.fill()
    surface.write_to_png(f)
    # THIS IS REQUIRED BECAUSE OF ISSUES WITH CAIRO. 
    del surface
    del ctx
    #############
    string = "data:image/png;base64,"
    string += base64.b64encode(f.getvalue())
    return string
Пример #4
0
def reset_gjf(request, upload_form):
    job_form = JobForm.get_form(request, "{{ name }}")

    errors = []
    strings = []
    names = []
    for f in upload_form.cleaned_data["files"]:
        try:
            parser = fileparser.Log(f)

            name, _ = os.path.splitext(f.name)
            td = False
            if request.REQUEST.get("td_reset"):
                name += '_TD'
                td = True
            strings.append(parser.format_gjf(td=td))
            names.append(name)
        except Exception as e:
            logger.warn("There was an error when trying to reset a gjf: '%s'" % str(e))
            errors.append((f.name, e))

    if request.REQUEST.get("gjf_submit"):
        if not job_form.is_valid(request.method):
            if request.is_ajax():
                upload_form_html = render_crispy_form(upload_form,
                                                      context=RequestContext(request))
                job_form_html = render_crispy_form(job_form,
                                                   context=RequestContext(request))
                a = {
                    "success": False,
                    "job_form_html": job_form_html,
                    "upload_form_html": upload_form_html,
                }
                return HttpResponse(json.dumps(a),
                                    content_type="application/json")
            c = {
                "job_form": job_form,
                "upload_form": upload_form,
            }
            return render(request, "chem/upload_log.html", c)

        d = dict(job_form.cleaned_data)
        cred = d.pop("credential")
        a = cluster.interface.run_jobs(cred, names, strings, **d)
        a["failed"].extend(errors)
        do_html = request.REQUEST.get("html", False)
        if do_html:
            html = render_to_string("chem/multi_submit.html", a)
            temp = {"success": True, "html": html}
            return HttpResponse(json.dumps(temp),
                                content_type="application/json")
        else:
            return HttpResponse(json.dumps(a),
                                content_type="application/json")

    buff = StringIO()
    zfile = zipfile.ZipFile(buff, 'w', zipfile.ZIP_DEFLATED)
    for name, string in zip(names, strings):
        zfile.writestr("%s.gjf" % name, string)
    if errors:
        temp = ['%s - %s' % (name, error) for (name, error) in errors]
        zfile.writestr("errors.txt", '\n'.join(temp))
    zfile.close()
    buff.flush()
    ret_zip = buff.getvalue()
    buff.close()

    response = HttpResponse(ret_zip, content_type="application/zip")
    response["Content-Disposition"] = "attachment; filename=output.zip"
    return response
Пример #5
0
def reset_gjf(request, upload_form):
    job_form = JobForm.get_form(request, "{{ name }}")

    errors = []
    strings = []
    names = []
    for f in upload_form.cleaned_data["files"]:
        try:
            parser = fileparser.Log(f)

            name, _ = os.path.splitext(f.name)
            td = False
            if request.REQUEST.get("td_reset"):
                name += '_TD'
                td = True
            strings.append(parser.format_gjf(td=td))
            names.append(name)
        except Exception as e:
            logger.warn("There was an error when trying to reset a gjf: '%s'" %
                        str(e))
            errors.append((f.name, e))

    if request.REQUEST.get("gjf_submit"):
        if not job_form.is_valid(request.method):
            if request.is_ajax():
                upload_form_html = render_crispy_form(
                    upload_form, context=RequestContext(request))
                job_form_html = render_crispy_form(
                    job_form, context=RequestContext(request))
                a = {
                    "success": False,
                    "job_form_html": job_form_html,
                    "upload_form_html": upload_form_html,
                }
                return HttpResponse(json.dumps(a),
                                    content_type="application/json")
            c = {
                "job_form": job_form,
                "upload_form": upload_form,
            }
            return render(request, "chem/upload_log.html", c)

        d = dict(job_form.cleaned_data)
        cred = d.pop("credential")
        a = cluster.interface.run_jobs(cred, names, strings, **d)
        a["failed"].extend(errors)
        do_html = request.REQUEST.get("html", False)
        if do_html:
            html = render_to_string("chem/multi_submit.html", a)
            temp = {"success": True, "html": html}
            return HttpResponse(json.dumps(temp),
                                content_type="application/json")
        else:
            return HttpResponse(json.dumps(a), content_type="application/json")

    buff = StringIO()
    zfile = zipfile.ZipFile(buff, 'w', zipfile.ZIP_DEFLATED)
    for name, string in zip(names, strings):
        zfile.writestr("%s.gjf" % name, string)
    if errors:
        temp = ['%s - %s' % (name, error) for (name, error) in errors]
        zfile.writestr("errors.txt", '\n'.join(temp))
    zfile.close()
    buff.flush()
    ret_zip = buff.getvalue()
    buff.close()

    response = HttpResponse(ret_zip, content_type="application/zip")
    response["Content-Disposition"] = "attachment; filename=output.zip"
    return response