def upload(): fileForm = SimpleFileForm() if fileForm.validate_on_submit(): session["temp_files"] = [] uploaded_files = request.files.getlist("selected_reports") zfname = uniq_file_prefix("pft.zip") zfloc = os.path.join(current_app.config["PFT_DIR"], zfname) pftdir = current_app.config["PFT_DIR"] with zipfile.ZipFile(zfloc, mode='w') as zarchv: for f in uploaded_files: fname = secure_filename(f.filename) session["temp_files"].append(fname) repname = os.path.join(pftdir, fname) f.save(repname) repfact = ReportFactory() repconfig = repfact.get_report_config_by_fname(repname) rep = repfact.make_PFTReport(fname, repconfig.name, repconfig.confdata, wdir=pftdir) rep.clean(db) clean_fname = "clean_"+rep.name+".xlsx" session["temp_files"].append(clean_fname) zarchv.write(os.path.join(pftdir, clean_fname), clean_fname) session["zfname"] = zfname return redirect(url_for("pft.download")) else: flash_errors(fileForm) return render_template("pft/upload.html", form=fileForm)
def home(): form = LoginForm(request.form) # Handle logging in if request.method == 'POST': if form.validate_on_submit(): login_user(form.user) flash("You are logged in.", 'success') redirect_url = request.args.get("next") or url_for("user.members") return redirect(redirect_url) else: flash_errors(form) return render_template("public/home.html", form=form)
def register(): form = RegisterForm(request.form, csrf_enabled=False) if form.validate_on_submit(): new_user = User.create(username=form.username.data, email=form.email.data, password=form.password.data, active=True) flash("Thank you for registering. You can now log in.", 'success') return redirect(url_for('public.home')) else: flash_errors(form) return render_template('public/register.html', form=form)
def suropt_select(): """View for preliminary survey cleaning options This view function presents the user with a list of options for the cleaning of a survey section. These options are the fields of the :class:`~surveycommander.forms.UploadForm` class. .. note:: There are three valid paths for initializing survey cleaning: 1. Upload a survey file, select report configuration and a category. a. The survey file is uploaded and used for category selected. 2. Select a survey file on server, select report configuration and a category. a. The survey file on server is used for category selected. b. If a file is also uploaded it will be uploaded to server, but not used. 3. Select a save session and a report configuration. a. Save session is loaded along with its attendant survey file. b. If a file is also uploaded it will be uploaded to server, but not used. c. If a survey file on server is selected, it will be ignored. d. If a category is selected, then i. If the saved session has data for that category, it will load it. ii. Otherwise the saved session will clean that section in its attendant survey file and load it for manual corrections. """ # TODO: Put this Mongodb code into the models folder. Finds should be included. mlist_cols = current_app.mongocl.masterlists.collection_names() config_profiles = current_app.mongocl.synthesis.survey_configs saved_sessions = current_app.mongocl.synthesis.surcom_sessions # TODO: Error if blank! configs = [(f['name'], f['name']) for f in config_profiles.find({}, {"name": 1}).sort([("name", 1)])] saved_sessions = [ (f['name'], f['name']) for f in saved_sessions.find({}, {"name": 1}).sort([("name", 1)]) ] # TODO: Error if blank! choices = [(cn, cn) for cn in mlist_cols if cn != "system.indexes"] # To allow for choices where category doesn't matter; just pick one... upload_form = UploadForm(category=choices[0][0]) assert(current_app.mongocl.masterlists.collection_names() != []) upload_form.category.choices = choices upload_form.repconfig.choices = configs upload_form.save_session.choices = saved_sessions upload_form.save_session.choices.insert(0, ("", "")) upload_form.save_session.default = "" upload_form.survey_file.choices = [ (cn.Filename, cn.Filename) for cn in SurveyFiles.query.all() ] upload_form.survey_file.choices.insert(0, ("", "")) upload_form.survey_file.default = "" # User supplied a new save session, add to allowable entries if # file name starts with a letter, is under 64 characters and only consists # of letters, numbers, dashes and underscores new_session = upload_form.save_session.data if new_session and new_session not in upload_form.save_session.choices: if re.match("^[A-Za-z][A-Za-z0-9-_]{0,64}", new_session): upload_form.save_session.choices.append((new_session, new_session)) else: msg = "Save session name must start with a letter, be no more than" msg += " 64 characters and only contain letters, numbers, dashes," msg += " and underscores." upload_form.errors["save_session"] = [msg] if upload_form.validate_on_submit(): path = None # Upload survey file, if provided with one if request.files.get("uploaded_survey", None): f = request.files["uploaded_survey"] fname = secure_filename(f.filename) path = os.path.join(current_app.config["SUR_DIR"], fname) with open(path, 'wb') as uploadloc: uploadloc.write(f.read()) # TODO: Include rollback! sf = SurveyFiles(Filename=fname) db.session.add(sf) db.session.commit() if upload_form.survey_file.data: # TODO: Add exception handling for missing file sf = SurveyFiles(Filename=upload_form.survey_file.data) path = sf.get_file_path() session["survey_category"] = request.form.get("category", None) session["survey_path"] = path # Save report configuration and save session name for init_clean session["rep_config"] = upload_form.repconfig.data session["save_session"] = upload_form.save_session.data return redirect(url_for("surveycommander.init_clean")) else: flash_errors(upload_form) return render_template("surveycommander/suropt_select.html", form=upload_form)