def cleaned(): survey = load_survey(session["survey_path"], session["save_session"], current_app.mongocl) save_form = SurveySaveForm(survey_name=survey.name) # wsgi_loc is the alias in /etc/apache2/sites-enabled/synthesis.wsgi wsgi_loc = os.path.split(current_app.config["SUR_DIR"])[-1] sfname = survey.name + "." + survey.file_type loc = os.path.join(wsgi_loc, sfname) if save_form.validate_on_submit(): if "save" in request.form.keys(): saveSurveyName = save_form.saveas.data if not save_form.saveas.data: saveSurveyName = survey.name else: survey.name = saveSurveyName surv = SurveyFiles.query.filter_by(Filename=saveSurveyName).first() if surv: surv.Timemark = dt.datetime.utcnow() surv.save() else: surv = SurveyFiles(Filename=saveSurveyName) # TODO: Include rollback! db.session.add(surv) db.session.commit() flash("Survey saved on server as {}.".format(saveSurveyName)) else: return redirect(url_for("surveycommander.suropt_select")) return render_template("surveycommander/cleaned.html", path=loc, repname=sfname, form=save_form)
def header_error_page(category): wrongheaders = session['wrongheaders'] survey = load_survey(session["survey_path"], session['save_session']) survey.set_cleaning_params(session["survey_category"], session["survey_path"]) mlist = MasterList(category) mlist.load_db(current_app.mongocl) correction_form = HeaderCorrectionForm() correction_form.headers.choices = [(c,c) for c in survey.columns] if correction_form.validate_on_submit(): headers = correction_form.headers.data survey.unkmap, survey.problems = survey.gen_unkmap(mlist, headers) survey.start = 0 probs = survey.problems survey.end = PER_PAGE if len(probs) >= PER_PAGE else len(probs) survey.save_session(current_app.mongocl) total_probs = len(survey.problems) return redirect( url_for( "surveycommander.cleaning", start=survey.start, end=survey.end, total_probs=total_probs, final=False if len(survey.problems) >= PER_PAGE else True ) ) else: return render_template("surveycommander/header_error_page.html", wrongheaders=wrongheaders, category=category, form=correction_form)
def cleaning(start, end, final=False): form = EnclosingForm(request.form) survey = load_survey(session["survey_path"], session["save_session"], current_app.mongocl) total_probs = len(survey.problems) if request.method == "POST": problems = [] for i, problemdata in enumerate(form.problems.data): # If the user did not select an option, # pick the first button (AKA default) option if problemdata["opts"] == u'None': if len(survey.problems[start+i].solutions) > 0: problemdata["opts"] = survey.problems[start+i].solutions[0] else: problemdata["opts"] = "" problems.append(problemdata) if form.next.data: new_end = None if len(survey.problems) >= end+PER_PAGE: new_end = end+PER_PAGE else: new_end = len(survey.problems) survey.solved = write_to(survey.solved, problems, start, end) survey.start = end survey.end = new_end # TODO: Make DRY with dup from save survey.save_session(current_app.mongocl) flash("Current state saved as {0}".format(survey.name)) return redirect(url_for("surveycommander.cleaning", start=end, total_probs=total_probs, end=new_end)) elif form.finish.data: survey.solved = write_to(survey.solved, problems, start, end) masterlist = load_masterlist(survey, db="masterlist_provisional") cols = masterlist.map_section_name_to_col_headers( current_app.mongocl, session["rep_config"], session["survey_category"] ) dbadds = survey.incorporate_user_input() survey.run_unkmap(cols) masterlist.add_to_list(dbadds, dbname="masterlist_provisional", cname=survey.category) survey.output_clean_section(current_app.config["SUR_DIR"]) return redirect(url_for("surveycommander.cleaned")) elif form.save.data: #TODO: Make DRY with code dup from next survey.solved = write_to(survey.solved, problems, start, end) survey.save_session(current_app.mongocl) flash("Current state saved as {0}".format(survey.name)) return redirect(url_for("surveycommander.cleaning", total_probs=total_probs, start=start, end=end)) else: new_start = start-PER_PAGE if start-PER_PAGE >= 0 else 0 survey.start = new_start survey.end = start return redirect(url_for("surveycommander.cleaning", start=new_start, end=start, total_probs=total_probs)) else: # Load next page of forms with problem entries and potential solutions for pif in xrange(start, end): prob_entry = {"title": survey.problems[pif].entry} if len(survey.solved) > pif: prob_entry.update(survey.solved[pif]) form.problems.append_entry(prob_entry) choices = [(pr, pr) for pr in survey.problems[pif].solutions] choices += [(NULL, NULL)] form.problems[-1].opts.choices = choices final = False if end < len(survey.problems) else True if final: form.next.label.text = "Finish" return render_template("surveycommander/cleaning.html", forms=form, total_probs=total_probs, start=start, end=end, final=final)
def init_clean(): """ """ saved_session = current_app.mongocl.synthesis.surcom_sessions.find_one( {"name": session["save_session"]} ) name = None if saved_session: if not session["survey_path"]: session["survey_path"] = saved_session['surpath'] elif session["survey_path"] != saved_session['surpath']: msg = "Uploaded file does not match survey saved in session '{}'" flash(msg.format(session["save_session"])) return redirect(url_for("surveycommander.suropt_select")) else: if not session["survey_path"]: msg = "Session '{}' not found, and no path to survey file given!" flash(msg.format(session["save_session"])) return redirect(url_for("surveycommander.suropt_select")) else: name = session["save_session"] survey = load_survey(session["survey_path"], name) swap_to_past_section = False onunseen_section = True if saved_session: swap_category = False oncurrent_section = session['survey_category'] == saved_session['category'] if not oncurrent_section: past_sections = json.loads(saved_session['other_sections']).keys() swap_to_past_section = session['survey_category'] in past_sections if not swap_to_past_section: onunseen_section = True else: onunseen_section = False survey.load_session(current_app.mongocl, name=session["save_session"]) swap_category = survey.category != session["survey_category"] if swap_category: survey.stash_section() survey.set_cleaning_params(session["survey_category"]) else: survey.set_cleaning_params(session["survey_category"], session["survey_path"]) mlist = MasterList(survey.category) mlist.load_db(current_app.mongocl) cols = mlist.map_section_name_to_col_headers( current_app.mongocl, session["rep_config"], survey.category ) if saved_session and swap_to_past_section: # TODO: next line is unDRY as it is in next elif survey.load_section(session["survey_category"]) survey.save_session(current_app.mongocl) elif not saved_session or (saved_session and onunseen_section): try: survey.unkmap, survey.problems = survey.gen_unkmap(mlist, cols) except KeyError as ke: bad_cols = [col for col in cols if not col in survey.columns] session['wrongheaders'] = bad_cols return redirect( url_for( "surveycommander.header_error_page", category=survey.category ) ) survey.start = 0 probs = survey.problems survey.end = PER_PAGE if len(probs) >= PER_PAGE else len(probs) survey.save_session(current_app.mongocl) total_probs = len(survey.problems) return redirect( url_for( "surveycommander.cleaning", start=survey.start, end=survey.end, total_probs=total_probs, final=False if len(survey.problems) >= PER_PAGE else True ) )