from flask import jsonify, request from form import * from proc import process_form, Fronimo, ERRFILE, add_prefix_and_suffix, del_prefix_and_suffix, PERSONDIR, LOCALBASE import re #GLOBALS if not Fronimo.open_error(ERRFILE): print("Cannot open error file.") sys.exit(2) # Same error file for all instances # Open name- and type-related files Fronimo.open_files() INITIAL_DATA_KEY = 'initial_data' def find_values(stIn): stIn = add_prefix_and_suffix(stIn) diForm = {} #Load data from stFile into Fronimo class fron = Fronimo(stIn) #use short form for form input stIn = del_prefix_and_suffix(stIn) # use data fron Fronimo class to construct defaults diForm['infile'] = stIn # we haven't determined the output file yet diForm['outputfile'] = '' diForm['title'] = fron.title diForm['subtitle'] = fron.subtitle diForm['composer'] = fron.composer diForm['composer0'] = fron.composer0 diForm['ensemble'] = fron.ensemble diForm['part'] = fron.part diForm['key'] = fron.key diForm['difficulty'] = fron.difficulty diForm['type'] = fron.type diForm['source'] = fron.source diForm['document'] = fron.document diForm['volume'] = fron.volume diForm['date'] = fron.date diForm['page'] = fron.page diForm['section'] = fron.section diForm['editor'] = fron.editor diForm['encoder'] = fron.encoder diForm['intabulator'] = fron.intabulator diForm['arranger'] = fron.arranger diForm['contributor'] = fron.contributor diForm['remarks'] = fron.remarks return diForm @app.route('/', methods=['GET', 'POST']) def get_file_name(): message = '' form = FileForm() if form.btn_directory.data: return redirect('/directory', code=302) elif form.btn_cancel.data: directory = 'file:///D:/website' print('Directory = ', directory) return redirect(directory, code=302) elif form.validate_on_submit(): upload_field = getattr(form, 'uploadfile', None) upload_data = upload_field.data if upload_field else None if upload_data and upload_data.filename: stInput = re.sub(r'^.*[\\/]', '', upload_data.filename) if not stInput.endswith('.ft3'): stInput = stInput + '.ft3' stInput = add_prefix_and_suffix(stInput) upload_data.save(stInput) else: stInput = form.inputfile.data if stInput == '': stInput = 'template.ft3' else: if not stInput.endswith('.ft3'): stInput = stInput + '.ft3' session['input_file'] = stInput Fronimo.open_error(ERRFILE) # Open name- and type-related files Fronimo.open_files() session[INITIAL_DATA_KEY] = find_values(stInput) return redirect('/form', code = 302) return render_template('input.html', form=form, message=message) # all Flask routes below import os @app.route('/directory', methods=['GET']) def browse_directory(): stFile = request.args.get('file', '').strip() if stFile != '': stFile = os.path.basename(stFile) session['input_file'] = stFile session[INITIAL_DATA_KEY] = find_values(stFile) return redirect('/form', code=302) liFiles = [] try: for entry in os.listdir(PERSONDIR): if entry.lower().endswith('.ft3'): liFiles.append(entry) except OSError: liFiles = [] liFiles.sort(key=str.lower) return render_template('directory.html', files=liFiles) @app.route('/form', methods=['GET', 'POST']) def input_form(): diInitial = session.get(INITIAL_DATA_KEY) if not diInitial: return redirect('/', code=302) input_form = add_defaults_to_form(diInitial) message = diInitial['infile'] if input_form.btn_cancel.data: return redirect('file://', code=302) if input_form.validate_on_submit(): diSubmitted = get_values_from_form(input_form) session['data'] = diSubmitted if process_form(diSubmitted): return redirect('/success', code=302) else: return redirect('/failure', code=302) return render_template( 'form.html', form=input_form, message=message, instrument_choices=get_instrument_suggestions(), type_choices=get_type_suggestions(), name_choices=get_name_suggestions(), source_choices=get_source_suggestions() ) @app.route('/validate-field', methods=['POST']) def validate_field(): diInitial = session.get(INITIAL_DATA_KEY) if not diInitial: return jsonify({'ok': False, 'error': 'Session expired. Reload the form.'}), 400 field_name = request.form.get('field', '').strip() if field_name == '': return jsonify({'ok': False, 'error': 'No field specified.'}), 400 input_form = add_defaults_to_form(diInitial) field = input_form._fields.get(field_name) if field is None: return jsonify({'ok': False, 'field': field_name, 'error': 'Unknown field.'}), 400 field.errors = [] is_valid = field.validate(input_form) return jsonify({ 'ok': is_valid, 'field': field_name, 'error': field.errors[0] if field.errors else '' }) @app.route('/source-documents', methods=['POST']) def source_documents(): stSource = request.form.get('source', '').strip() stAllowNew = request.form.get('allow_new_source', '').strip().lower() if stSource == '' or stAllowNew == 'yes': return jsonify({'documents': []}) return jsonify({'documents': get_documents_for_source(stSource)}) @app.route('/success', methods=['GET']) def done(): diData = session['data'] message = del_prefix_and_suffix(diData['outfile']) return render_template('success.html', message=message) @app.route('/failure', methods=['GET']) def fail(): diData = session['data'] print("session['data'] = ", session['data']) message = del_prefix_and_suffix(diData['outfile']) return render_template('failure.html', message=message) if __name__ == '__main__': app.run(debug=True)