Esempio n. 1
0
def barco_enterform(request, repos, formname):
    if repos not in settings.DRANK_REPOSITORIES:
        raise Http404
    if formname not in settings.BARCO_FORMS:
        raise Http404
    formspec = settings.BARCO_FORMS[formname]
    repopath = settings.DRANK_REPOS_PATH % (repos,)
    template_path = os.path.join(repopath, formspec.template_path)
    template = open_rikf_ar(template_path)
    weight_fields = set()
    if formspec.weights_path is not None:
        weights_path = os.path.join(repopath, formspec.weights_path)
        weights = open_rikf_ar(weights_path)
        for row in weights:
            weight_fields.add(row[0].strip())

    prefill = {}
    if request.method == 'POST':
        form = (formspec.django_form)(request.POST)
        prefill = json.loads(request.POST['jsondata'])
        # The keys are unicode and that gives trouble when passing it to
        # the template and |safe'ing it. This line maps all keys and values to
        # non-unicode.
        prefill = dict(map(lambda x: (str(x[0]), str(x[1])), prefill.items()))
        if form.is_valid():
            # Dump the entered data to a file ...
            fd = form.cleaned_data
            csv = StringIO();
            write = lambda x: csv.write(x.encode("utf-8"))
            write("# Ingevoerd door "+ str(request.user.name) +"\n")
            formspec.entered_data_to_file(fd, write, template, prefill)
            
            # commit it to the repository ...
            fn = os.path.join(formspec.dir_in_repo,'%s.csv'%(fd['formname']))
            with open(os.path.join(repopath, fn), 'w') as fh:
                fh.write(csv.getvalue())
            subprocess.call(['/usr/bin/git', 'add', fn], cwd=repopath)
            msg = ("Barform %s ingevoerd via kninfra\n\n"
                    "Signed-off-by: kninfra <*****@*****.**>" %
                    fd['formname'])
            # XXX Is it safe to use canonical_full_email?
            author = "%s <%s>" % (str(request.user.humanName),
                    request.user.canonical_email)
            subprocess.call(['/usr/bin/git', 'commit', '--author', author,
                '-m', msg, fn], cwd=repopath)
            subprocess.call(['/usr/bin/git', 'pull', '--rebase'], cwd=repopath)
            subprocess.call(['/usr/bin/git', 'push'], cwd=repopath)

            # and get back to the user:
            messages.info(request, "Opgeslagen!")
            return HttpResponseRedirect(reverse('barco-enterform', 
                args=(repos,formname)))
    form = formspec.django_form()
    return render_to_response(formspec.django_template, 
            {'fields': template, 'form': form, 'prefill': prefill,
                'weight_fields': list(weight_fields)},
        context_instance=RequestContext(request))
Esempio n. 2
0
def barco_barform(request, repos):
    if repos not in settings.DRANK_REPOSITORIES:
        raise Http404
    repopath = '/home/infra/barco/%s/' % repos
    fields = open_rikf_ar(repopath + '/barforms/form-template-active.csv')
    prefill = {}
    if request.method == 'POST':
        form = BarFormMeta(request.POST)
        prefill = json.loads(request.POST['jsondata'])
        # The keys are unicode and that gives trouble when passing it to
        # the template and |safe'ing it. This line maps all keys to
        # non-unicode.
        prefill = dict(map(lambda x: (str(x[0]), x[1]), prefill.items()))
        if form.is_valid():
            fd = form.cleaned_data
            csv = StringIO();
            csv.write("# Ingevoerd door "+ str(request.user.name) +"\n")
            csv.write("Bar;;;;\n\n")
            csv.write(fd['pricebase'])
            csv.write(";")
            csv.write(str(fd['date']))
            csv.write(";")
            csv.write(fd['tapper'])
            csv.write(";")
            csv.write(fd['dienst'])
            csv.write(";")
            csv.write(str(fd['beginkas']))
            csv.write(";")
            csv.write(str(fd['eindkas']))
            if fd['comments'] != '':
                csv.write("\n\n# XXX ")
                csv.write(fd['comments'].replace("\n", "\n# "))
            csv.write("\n\n")
            emptyColumn = False
            column = 0
            while not emptyColumn:
                emptyColumn = True
                for row in fields:
                    if column >= len(row):
                        continue
                    emptyColumn = False
                    if row[column] == '':
                        continue
                    if row[column][0] == '!':
                        csv.write("# ");
                        csv.write(row[column][1:])
                        csv.write("\n");
                    else:
                        csv.write(row[column])
                        csv.write(";")
                        if row[column] in prefill:
                            csv.write(str(prefill[row[column]]))
                            csv.write("\n")
                        else:
                            csv.write("0\n")
                column += 1
            fn = 'barforms/%s.csv' % (fd['formname'])
            with open(repopath + fn, 'w') as fh:
                fh.write(csv.getvalue())
            subprocess.call(['/usr/bin/git', 'add', fn], cwd=repopath)
            msg = ("Barform %s ingevoerd via kninfra\n\n"
                    "Signed-off-by: kninfra <*****@*****.**>" %
                    fd['formname'])
            author = "%s <%s>" % (str(request.user.humanName),
                    request.user.canonical_email)
            subprocess.call(['/usr/bin/git', 'commit', '--author', author,
                '-m', msg, fn], cwd=repopath)
            subprocess.call(['/usr/bin/git', 'pull', '--rebase'], cwd=repopath)
            subprocess.call(['/usr/bin/git', 'push'], cwd=repopath)
            request.user.push_message("Opgeslagen!")
            return HttpResponseRedirect(reverse('barco-barform', args=(repos,)))
    else:
        form = BarFormMeta()
    return render_to_response('barco/barform.html', {'fields': fields,
        'form': form, 'prefill': prefill},
        context_instance=RequestContext(request))
Esempio n. 3
0
def barco_enterform(request, repos, formname):
    if repos not in settings.DRANK_REPOSITORIES:
        raise Http404
    if formname not in settings.BARCO_FORMS:
        raise Http404
    formspec = settings.BARCO_FORMS[formname]
    repopath = settings.DRANK_REPOS_PATH % (repos, )
    template_path = os.path.join(repopath, formspec.template_path)
    template = open_rikf_ar(template_path)
    weight_fields = set()
    if formspec.weights_path is not None:
        weights_path = os.path.join(repopath, formspec.weights_path)
        weights = open_rikf_ar(weights_path)
        for row in weights:
            weight_fields.add(row[0].strip())

    prefill = {}
    if request.method == 'POST':
        form = (formspec.django_form)(request.POST)
        prefill = json.loads(request.POST['jsondata'])
        # The keys are unicode and that gives trouble when passing it to
        # the template and |safe'ing it. This line maps all keys and values to
        # non-unicode.
        prefill = dict(map(lambda x: (str(x[0]), str(x[1])), prefill.items()))
        if form.is_valid():
            # Dump the entered data to a file ...
            fd = form.cleaned_data
            csv = StringIO()
            write = lambda x: csv.write(x.encode("utf-8"))
            write("# Ingevoerd door " + str(request.user.name) + "\n")
            formspec.entered_data_to_file(fd, write, template, prefill)

            # commit it to the repository ...
            fn = os.path.join(formspec.dir_in_repo,
                              '%s.csv' % (fd['formname']))
            with open(os.path.join(repopath, fn), 'w') as fh:
                fh.write(csv.getvalue())
            subprocess.call(['/usr/bin/git', 'add', fn], cwd=repopath)
            msg = ("Barform %s ingevoerd via kninfra\n\n"
                   "Signed-off-by: kninfra <*****@*****.**>" %
                   fd['formname'])
            # XXX Is it safe to use canonical_full_email?
            author = "%s <%s>" % (str(
                request.user.humanName), request.user.canonical_email)
            subprocess.call(
                ['/usr/bin/git', 'commit', '--author', author, '-m', msg, fn],
                cwd=repopath)
            subprocess.call(['/usr/bin/git', 'pull', '--rebase'], cwd=repopath)
            subprocess.call(['/usr/bin/git', 'push'], cwd=repopath)

            # and get back to the user:
            request.user.push_message("Opgeslagen!")
            return HttpResponseRedirect(
                reverse('barco-enterform', args=(repos, formname)))
    form = formspec.django_form()
    return render_to_response(formspec.django_template, {
        'fields': template,
        'form': form,
        'prefill': prefill,
        'weight_fields': list(weight_fields)
    },
                              context_instance=RequestContext(request))