def read_template_only(request, isdemo = False): global PageIndex, InitalFormVals """ reads a simple template (not a collection), checks for errors, and then either renders the form or lists the errors. if isdemo, reads the demo template """ if 'codername' in request.POST: civet_form.CoderName = request.POST['codername'] if isdemo: st = open(civet_settings.STATIC_FILE_PATH + civet_settings.DEMO_TEMPLATE,'r') else: if 'template_name' in request.FILES: # print('RTO2:',request.FILES['template_name']) st = request.FILES['template_name'] else: return HttpResponse("No file was selected: please go back to the previous page and select a file") civet_form.read_template(st) if civet_form.FormContent[0].startswith('&Errors:'): return render(request,'djciv_data/template_error.html', {'form_content': civet_form.FormContent[0][8:]}) else: civet_form.create_TempData_header() PageIndex = 0 InitalFormVals = deepcopy(civet_form.FormFields) return render(request,'djciv_data/basic_form.html', get_form_context())
def read_workspace(request, isdemo = False, manage = False): """ Loads a workspace zipped file; replaces anything already in Collection, Text If isdemo, reads the demo collections If docoding, go to select_collection.html, otherwise manage_collections.html""" # 15.07.28 pas: Those massive blocks of code creating records should probably be methods in models.py global CollectionList, SaveFiles, WorkspaceName, CKEditor_Styles, ActiveCollection, CoderText, TermStyles, InitalFormVals # print('RW0',isdemo, manage) if 'codername' in request.POST: civet_form.CoderName = request.POST['codername'] if isdemo: zipfilename = civet_settings.STATIC_FILE_PATH + civet_settings.DEMO_WORKSPACE WorkspaceName = 'Demonstration file' else: if 'filename' in request.FILES: zipfilename = request.FILES['filename'] WorkspaceName = str(zipfilename) else: return HttpResponse("No file was selected: please go back to the previous page and select a file") zf = zipfile.ZipFile(zipfilename, 'r') # print('RW0-1:',zf.namelist()) ActiveCollection = '' CoderText = '' TermStyles = '' Collection.objects.all().delete() Text.objects.all().delete() Case.objects.all().delete() # <15.07.09: Warn first on this? civet_form.FormContent = [''] # should this be initialized here? error_string = '' ka = 0 # DEBUG for file in zf.namelist(): # get the codes. files first, since civet_form.read_template() will need this info if file.startswith('__'): continue try: filename = file[file.index('/')+1:] # zipped file names may or may not have internal / except ValueError: filename = file # print('RW1:',file,filename) if filename.find('codes.') == 0: SaveFiles[filename] = zf.read(file) fin = zf.open(file,'r') civet_form.read_codes_file(fin, file) fin.close() CollectionList = [] # list of available collections for file in zf.namelist(): # print('RW2:',file) if file.startswith('__'): continue try: filename = file[file.index('/')+1:] except ValueError: filename = file if file.endswith('.yml'): ka += 1 if ka > 0: # <= 4: # DEBUG # print('RW2-1:',filename) fin = zf.open(file,'r') try: collinfo, textlist, caselist = civet_utilities.read_YAML_file(fin, file) except Exception as e: # print('Error:' + str(e) + ' in ' + file) error_string += '<p>' + str(e) + ' in the file "' + filename + '"' # <15.09.16>: This needs to be more informative as a zillion things could cause it continue collentry = Collection.objects.create_coll( collid = collinfo['collid'], collfilename = collinfo['collfilename'], colldate = collinfo['colldate'], colledit = collinfo['colledit'], collcmt = collinfo['collcmt'], collcat = collinfo['collcat'] ) collentry.save() CollectionList.append(collinfo['collid']) for dc in textlist: """if dc['textid'] == '2014-06-20_LBY_NR000': # -- DEBUG print('DC1: textoriginal:', dc['textoriginal'])""" textentry = Text.objects.create_text( textparent = dc['textparent'], textid = dc['textid'], textdelete = dc['textdelete'], textdate = dc['textdate'], textpublisher = dc['textpublisher'], textpubid = dc['textpubid'], textbiblio = dc['textbiblio'], textlicense = dc['textlicense'], textgeogloc = dc['textgeogloc'], textlede = dc['textlede'], textcmt = dc['textcmt'], textoriginal = dc['textoriginal'], textmkup = dc['textmkup'], textmkupdate = dc['textmkupdate'], textmkupcoder = dc['textmkupcoder'] ) textentry.save() if len(caselist) > 0: for dc in caselist: caseentry = Case.objects.create_case( caseparent = dc['caseparent'], caseid = dc['caseid'], casedate = dc['casedate'], casecoder = dc['casecoder'], casecmt = dc['casecmt'], casevalues = dc['casevalues'] ) caseentry.save() fin.close() elif filename.find('form.') == 0: SaveFiles[filename] = zf.read(file) fin = zf.open(file,'r') # print('RCD-2:',file) civet_form.read_template(fin) fin.close() # print('RW savelists:\n',civet_form.SaveList,'\n',civet_form.SaveTypes) CKEditor_Styles = civet_utilities.get_styles() else: if len(filename)> 0: SaveFiles[filename] = zf.read(file) if not manage and len(civet_form.FormContent[0]) == 0: error_string += "<p>No 'form.*' file was found in the workspace: This is required for coding." if civet_form.FormContent[0].startswith('&Errors:'): if len(error_string) > 0: error_string = '<h3>Form errors:</h3>' + civet_form.FormContent[0][8:] + '<h3>Collection errrors:</h3>' + error_string else: error_string = '<h3>Form errors:</h3>' + civet_form.FormContent[0][8:] if len(error_string) > 0: context = {} context['workspace'] = WorkspaceName context['errors'] = error_string if manage: context['docoding'] = False else: context['docoding'] = True return render(request,'djciv_data/collection_error.html', context) if manage: return render(request,'djciv_data/manage_workspace.html',{'workspace' : WorkspaceName}) else: InitalFormVals = deepcopy(civet_form.FormFields) # print('==',InitalFormVals) return render(request,'djciv_data/select_collection.html',{'files' : CollectionList, 'workspace' : WorkspaceName})