Spaces:
Sleeping
Sleeping
| from . import admin | |
| from config import get_subject_list, update_questionnaire | |
| from flask import Flask, render_template,current_app, request, jsonify, redirect, url_for, session | |
| import os,json | |
| import shutil | |
| def edit_form(): | |
| if not session.get('logged_in'): | |
| return redirect(url_for('admin.login')) | |
| subject_list= get_subject_list() | |
| upload_subjects = current_app.config['UPLOADING_SUBJECTS'] | |
| uploading = [True if s in upload_subjects else False for s in subject_list] | |
| return render_template('file_editor.html', | |
| maintenance=current_app.config['MAINTENANCE_MODE'], | |
| subject_list=subject_list,uploading=uploading, | |
| subject_list_len=len(subject_list)) | |
| ### api | |
| def get_data(subject): | |
| try: | |
| # 读取convert_data.json | |
| with open(os.path.join("dataset",subject,'convert_data.json'), 'r', encoding='utf-8') as file: | |
| convert_data = json.load(file) | |
| # 读取rule.json | |
| with open(os.path.join("dataset",subject, 'rule.json'), 'r', encoding='utf-8') as file: | |
| rule_data = json.load(file) | |
| except: | |
| convert_data = [] | |
| rule_data = [] | |
| return jsonify({'convert_data': convert_data, 'rule_data': rule_data}) | |
| def save_data(subject): | |
| data = request.json | |
| with open(os.path.join("dataset",subject, 'convert_data.json'), 'w', encoding='utf-8') as file: | |
| json.dump(data['convert_data'], file, ensure_ascii=False, indent=4) | |
| with open(os.path.join("dataset",subject, 'rule.json'), 'w', encoding='utf-8') as file: | |
| json.dump(data['rule_data'], file, ensure_ascii=False, indent=4) | |
| return jsonify({'status': 'success'}) | |
| def delete_questionaire(subject): | |
| # Assume you have a function to handle the deletion logic | |
| success = delete_questionnaire_from_database(subject) | |
| if(current_app.config['SUBJECT'] == subject): | |
| # clear the current questionnaire | |
| current_app.config['SUBJECT'] = None | |
| update_questionnaire(current_app, None, None) | |
| if success: | |
| return jsonify(status='success'), 200 | |
| else: | |
| return jsonify(status='failure', message='Unable to delete the form'), 400 | |
| def delete_questionnaire_from_database(subject): | |
| error=False | |
| try: | |
| shutil.rmtree(os.path.join("dataset",subject)) | |
| except: | |
| error=True | |
| try: | |
| current_app.config['SUBJECT_LIST'].remove(subject) | |
| except: | |
| pass | |
| try: | |
| current_app.config['UPLOADING_SUBJECTS'].remove(subject) | |
| except: | |
| pass | |
| return not error |