def write_YAML_file(thecoll, filehandle):
    """ writes the Collection thecoll to filehandle in YAML format"""
#    print('WYF-1:', collid)
#    thecoll = Collection.objects.get(collid__exact=collid)
    colldict = thecoll.__dict__
    for flst in collfields:
        if flst == 'colldate' or flst == 'colledit':
            filehandle.write(flst+ ': ' + colldict[flst].strftime("%Y-%m-%d %H:%M:%S") + '\n')
        else:
            filehandle.write(flst+ ': ' + colldict[flst] + '\n')
#    print('WYF-2:', colldict)
    filehandle.write('\ntexts\n')
    
    curtexts = Text.objects.filter(textparent__exact=thecoll.collid)  # write the texts
    for ct in curtexts:
        textdict = ct.__dict__
        filehandle.write('\n  - textid: ' + textdict['textid'] + '\n')
        print('WYF-3:', textdict)
        for flst in textfields[1:-2]:
            if flst in textdict:
                if flst == 'textdate':
                    if textdict[flst]:  # allow possibility of no markup date
                        filehandle.write('    ' + flst + ': ' + textdict[flst].strftime("%Y-%m-%d %H:%M:%S") + '\n')
                else:
                    filehandle.write('    ' + flst + ': ' + textdict[flst] + '\n')
        filehandle.write('    textoriginal: |\n')
        for ls in textdict['textoriginal'].split('\n'):
            filehandle.write('        ' + ls + '\n')
        filehandle.write('    textmkup: |\n')
        for ls in textdict['textmkup'].split('\n'):
            filehandle.write('        ' + ls + '\n')
        for flst in textfields[-2:]:
            if flst in textdict:
                if flst == 'textmkupdate':
                    if textdict[flst]:  # allow possibility of no markup date
                        filehandle.write('    ' + flst+ ': ' + textdict[flst].strftime("%Y-%m-%d %H:%M:%S") + '\n')
                else:
                    filehandle.write('    ' + flst+ ': ' + textdict[flst] + '\n')
    
    curcases = Case.objects.filter(caseparent__exact=thecoll.collid)  # write the texts
    if len(curcases) > 0:
        filehandle.write('\ncases\n')   
        for ct in curcases:
            casedict = ct.__dict__
            print('WYF-4:', casedict)
            filehandle.write('\n  - caseid: ' + casedict['caseid'] + '\n')
            for flst in casefields:
                if flst in casedict:
                    if flst == 'casedate':
                        if casedict[flst]:  # not needed when everything is operational
                            filehandle.write('    ' + flst + ': ' + casedict[flst].strftime("%Y-%m-%d %H:%M:%S") + '\n')
                    else:
                        filehandle.write('    ' + flst + ': ' + casedict[flst] + '\n')
        
            filehandle.write('    casevalues: \n')
            caseval = ct.get_values()
            CIV_template.set_SaveList()  # DEBUG
            print('WYF-5:', CIV_template.SaveList)
            for st in CIV_template.SaveList:
                filehandle.write('        ' + st + ': ' + caseval[st] + '\n')
Example #2
0
def reset_data(request):
    """ 15.07.09: This needs to be broken into different sets of options for collections, basic mode """
    if CIV_template.BasicMode:
        CIV_template.create_TempData_header()
        return render(request,'djciv_data/basic_form.html',  {'form_content' : ActiveTemplate})
    else:
        Case.objects.all().delete()
        return render(request,'djciv_data/select_collection.html',{'files' : CollectionList})
Example #3
0
def setup_download(request):
    print('SD-1: ',CIV_template.BasicMode,CIV_template.TempData[:128])
    context = {}
    context['filename'] = CIV_template.DefaultFileName
    if CIV_template.BasicMode:
        CIV_template.save_to_TempData(request)
        context['download_action'] = 'download_basicmode'
    else:
        CIV_template.save_case(request, ActiveCollection)
        context['download_action'] = 'download_data'
    print(context)
    return render(request,'djciv_data/setup_download.html',context)
Example #4
0
def read_template():
    """ main routine for setting up a template: reads a file, checks for errors, and then either renders the form or 
        lists the errors """
    global thetemplate, basic_mode
    CIV_template.init_template()
    if 'codername' in request.form:
        #        print('RTcn: codername',request.form['codername'] )
        CIV_template.codername = request.form['codername']
    """print('RT keys',request.form.keys() )
    print('RT file keys',request.files.keys() )
    print('RT2*:',request.files['template_name'].filename) """
    if len(request.files['template_name'].filename) > 0:
        #        print('RT2:',request.files['template_name'].filename)
        st = request.files.get('template_name')
    else:
        #        print('RT: Use demo')
        st = open('static/CIVET.demo.template.txt', 'r')
    thecontent = ''
    commln = CIV_template.get_commlines(st)
    while len(commln) > 0:
        thecontent += CIV_template.do_command(commln)
        commln = CIV_template.get_commlines(st)


#    print('thecontent:',thecontent)
    if len(CIV_template.savelist) == 0:
        thecontent += '~Error~<p>A "save:" command is required in the template<br>\n'
    else:
        misslist = []
        for ele in CIV_template.savelist:
            if ele not in CIV_template.varlist:
                misslist.append(ele)
        if len(misslist) > 0:
            thecontent += '~Error~<p>The following variables are in the "save:" command but were not declared in a data field<br>' + str(
                misslist) + '\n'

    if '~Error~' in thecontent:
        errortext = ''
        indx = thecontent.find('~Error~')
        while indx >= 0:
            indy = thecontent.find('\n', indx)
            errortext += thecontent[indx + 7:indy + 1]
            indx = thecontent.find('~Error~', indy)
        return render_template('template_error.html', form_content=errortext)
    else:
        thetemplate = thecontent
        create_header()
        basic_mode = True
        return render_template('basic_form.html', form_content=thecontent)
Example #5
0
def download_data(request):
    """ writes all cases to a tab-delimited file """
    # need a time option here to just get the new cases
    CIV_template.SaveList = [u'typeincid', u'_date_', u'authreport', u'location', u'descrp']  # DEBUGGING   
    tempdata = '\t'.join(CIV_template.SaveList) + '\n'     
    for acase in Case.objects.all():
        values = acase.get_values()
#        print('DD-Mk-1:',values)
#        print('DD-Mk-2:', CIV_template.SaveList)
        if len(values) >= len(CIV_template.SaveList): # skips blank records, I hope
            for avar in CIV_template.SaveList: 
#                print('STT2:',avar)
                if avar in CIV_template.SpecialVarList: 
                    tempdata += CIV_template.get_special_var(avar) + '\t'
                elif avar in CIV_template.ConstVarDict.keys(): 
                    tempdata += CIV_template.ConstVarDict[avar] + '\t'
                elif avar in values:
                    tempdata += values[avar]+'\t'
                else:
                    tempdata += CIV_template.unchecked[avar] + '\t'
        tempdata = tempdata[:-1] + '\n'
    curfilename = request.POST['filename']
    if curfilename[-4:] != ".txt":
        curfilename += '.txt'
#    print('DD1:',curfilename)
    response = HttpResponse(content_type='text/plain')
    response['Content-Disposition'] = 'attachment; filename="' + curfilename + '"'
    response.write(tempdata)        
    return(response)
Example #6
0
def read_template():
    """ main routine for setting up a template: reads a file, checks for errors, and then either renders the form or 
        lists the errors """
    global thetemplate, basic_mode
    CIV_template.init_template()
    if 'codername' in request.form:
#        print('RTcn: codername',request.form['codername'] )
        CIV_template.codername = request.form['codername']        
    """print('RT keys',request.form.keys() )
    print('RT file keys',request.files.keys() )
    print('RT2*:',request.files['template_name'].filename) """       
    if len(request.files['template_name'].filename) > 0:
#        print('RT2:',request.files['template_name'].filename)        
        st = request.files.get('template_name')
    else:
#        print('RT: Use demo')
        st = open('static/CIVET.demo.template.txt','r')
    thecontent = ''
    commln = CIV_template.get_commlines(st)
    while len(commln) > 0:
        thecontent += CIV_template.do_command(commln)
        commln = CIV_template.get_commlines(st)

#    print('thecontent:',thecontent)
    if len(CIV_template.savelist) == 0:
        thecontent += '~Error~<p>A "save:" command is required in the template<br>\n'
    else:
        misslist = []
        for ele in CIV_template.savelist:
            if ele not in CIV_template.varlist:
                misslist.append(ele)
        if len(misslist) > 0:
            thecontent += '~Error~<p>The following variables are in the "save:" command but were not declared in a data field<br>' + str(misslist) + '\n'

    if '~Error~' in thecontent:
        errortext = ''
        indx = thecontent.find('~Error~')
        while indx >= 0:
            indy = thecontent.find('\n',indx)
            errortext += thecontent[indx+7:indy+1]
            indx = thecontent.find('~Error~',indy)
        return render_template('template_error.html', form_content = errortext)
    else:
        thetemplate = thecontent
        create_header()
        basic_mode = True
        return render_template('basic_form.html', form_content = thecontent)
Example #7
0
def get_coder_template(request):
    """ main routine for setting up a template: reads a file, checks for errors, and then either renders the form or 
        lists the errors """
    global ActiveTemplate

    CIV_template.init_template()
#    print('GCT-1: ',CollectionForm)
    if len(CollectionForm) > 0:
        st = open(CollectionForm,'r')
    else:
#        print('RT: Use demo')
        st = open('djciv_data/static/djciv_data/CIVET.demo.coder.template.txt','r')
    thecontent = ''
    commln = CIV_template.get_commlines(st)
    while len(commln) > 0:
        thecontent += CIV_template.do_command(commln)
        commln = CIV_template.get_commlines(st)

#    print('thecontent:',thecontent)
    if len(CIV_template.SaveList) == 0:
        thecontent += '~Error~<p>A "save:" command is required in the template<br>\n'
    else:
        misslist = []
        for ele in CIV_template.SaveList:
            if ele not in CIV_template.VarList:
                misslist.append(ele)
        if len(misslist) > 0:
            thecontent += '~Error~<p>The following variables are in the "save:" command but were not declared in a data field<br>' + str(misslist) + '\n'

    if '~Error~' in thecontent:
        errortext = ''
        indx = thecontent.find('~Error~')
        while indx >= 0:
            indy = thecontent.find('\n',indx)
            errortext += thecontent[indx+7:indy+1]
            indx = thecontent.find('~Error~',indy)
        return render(request,'djciv_data/template_error.html', {'form_content' :  errortext})
    else:
        ActiveTemplate = thecontent
        return  thecontent
Example #8
0
def save_to_tempdata():
    global tempdata
#    print('STT1',CIV_template.savelist)
    for avar in CIV_template.savelist: 
#        print('STT2:',avar)
        if avar in CIV_template.specialvarlist: 
            tempdata += CIV_template.specialvar(avar) + '\t'
        elif avar in CIV_template.constvardict.keys(): 
            tempdata += CIV_template.constvardict[avar] + '\t'
        elif avar in request.form:
            tempdata += request.form[avar]+'\t'
        else:
            tempdata += CIV_template.unchecked[avar] + '\t'
    tempdata = tempdata[:-1] + '\n'
    print('STT3:\n',tempdata)
Example #9
0
def save_to_tempdata():
    global tempdata
    #    print('STT1',CIV_template.savelist)
    for avar in CIV_template.savelist:
        #        print('STT2:',avar)
        if avar in CIV_template.specialvarlist:
            tempdata += CIV_template.specialvar(avar) + '\t'
        elif avar in CIV_template.constvardict.keys():
            tempdata += CIV_template.constvardict[avar] + '\t'
        elif avar in request.form:
            tempdata += request.form[avar] + '\t'
        else:
            tempdata += CIV_template.unchecked[avar] + '\t'
    tempdata = tempdata[:-1] + '\n'
    print('STT3:\n', tempdata)
Example #10
0
def read_template(request):
    """ main routine for setting up a template: reads a file, checks for errors, and then either renders the form or 
        lists the errors """
    global ActiveTemplate
    CIV_template.init_template()
    if 'codername' in request.POST:
        CIV_template.codername = request.POST['codername']        
    if 'template_name' in request.FILES:
#        print('RT2:',request.FILES['template_name'])        
        st = request.FILES['template_name']
    else:
#        print('RT: Use demo')
        st = open('djciv_data/static/djciv_data/CIVET.demo.template.txt','r')
    thecontent = ''
    commln = CIV_template.get_commlines(st)
    while len(commln) > 0:
        thecontent += CIV_template.do_command(commln)
        commln = CIV_template.get_commlines(st)

#    print('thecontent:',thecontent)
    if len(CIV_template.SaveList) == 0:
        thecontent += '~Error~<p>A "save:" command is required in the template<br>\n'
    else:
        misslist = []
        for ele in CIV_template.SaveList:
            if ele not in CIV_template.VarList:
                misslist.append(ele)
        if len(misslist) > 0:
            thecontent += '~Error~<p>The following variables are in the "save:" command but were not declared in a data field<br>' + str(misslist) + '\n'

    if '~Error~' in thecontent:
        errortext = ''
        indx = thecontent.find('~Error~')
        while indx >= 0:
            indy = thecontent.find('\n',indx)
            errortext += thecontent[indx+7:indy+1]
            indx = thecontent.find('~Error~',indy)
        return render(request,'djciv_data/template_error.html', {'form_content' :  errortext})
    else:
        ActiveTemplate = thecontent
        CIV_template.create_TempData_header()
        CIV_template.BasicMode = True
        return render(request,'djciv_data/basic_form.html', {'form_content' : thecontent})
Example #11
0
def save_and_new(request):
    CIV_template.save_case(request, ActiveCollection)
    print('SaN-Mk1:',CollectionList)
    return render(request,'djciv_data/select_collection.html',{'files' : CollectionList})
Example #12
0
def save_and_return(request):
    CIV_template.save_case(request, ActiveCollection)
    return render(request,'djciv_data/civet_coder.html',CoderContext)
Example #13
0
def get_new_case(request):
    CIV_template.save_to_TempData(request)
    return render(request,'djciv_data/file_select.html')
Example #14
0
def save_basic(request):
    """ save data then return to template-based form """
    global ActiveTemplate
    CIV_template.save_to_TempData(request)
    return render(request,'djciv_data/basic_form.html', {'form_content' : ActiveTemplate})