From 376c1df2d809d5081904af610d5cc76c88ba316a Mon Sep 17 00:00:00 2001 From: Satish Surath Date: Tue, 19 Sep 2023 08:53:53 -0400 Subject: [PATCH] Completed the saving, viewing and modifying Course Metadata Signed-off-by: Satish Surath --- app/file_operations.py | 15 +++++- app/routes.py | 61 +++++++++++++++++++++-- app/routes_helper.py | 2 +- app/templates/course_contents.html | 77 ++++++++++++++++++++++-------- 4 files changed, 130 insertions(+), 25 deletions(-) diff --git a/app/file_operations.py b/app/file_operations.py index 96d6293..a45511e 100644 --- a/app/file_operations.py +++ b/app/file_operations.py @@ -121,7 +121,7 @@ def delete_file(file_name): return False # Create a New Folder under this app.config["FOLDER_UPLOAD"] -def create__course_folder_with_metadata(folder_name, metadata): +def create_course_folder_with_metadata(folder_name, metadata): try: user_folder = session['folder'] os.makedirs(os.path.join(app.config["FOLDER_UPLOAD"], user_folder, folder_name)) @@ -134,6 +134,19 @@ def create__course_folder_with_metadata(folder_name, metadata): except: return False + +def save_course_metadata(folder_name,metadata): + try: + user_folder = session['folder'] + json_file_path = f"{app.config['FOLDER_UPLOAD']}/{user_folder}/{folder_name}/course_meta.json" + with open(json_file_path, 'w') as json_file: + json.dump(metadata, json_file) + return True + except: + return False + + + #define the allowed files! def allowed_file(filename): ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','doc','docx','ppt','pptx'} diff --git a/app/routes.py b/app/routes.py index 3fd68b5..a26a4c9 100755 --- a/app/routes.py +++ b/app/routes.py @@ -39,7 +39,8 @@ list_folders, rename_folder, delete_folder, - create__course_folder_with_metadata, + create_course_folder_with_metadata, + save_course_metadata, allowed_file, delete_file, get_first_txt_file, @@ -197,14 +198,14 @@ def course_management(): @login_required def create_course(): name = request.form['name'].replace(' ', '-') - meta_data = { + metadata = { 'classname': request.form.get('classname', ''), 'professor': request.form.get('professor', ''), 'assistants': request.form.get('assistants', ''), 'classdescription': request.form.get('classdescription', ''), 'assistant_name': request.form.get('assistant_name', '') } - create__course_folder_with_metadata(name, meta_data) + create_course_folder_with_metadata(name, metadata) return redirect(url_for('course_management')) @app.route('/rename-item', methods=['POST']) @@ -294,6 +295,9 @@ def toggle_activation(course_name, file_name): return jsonify(success=False, error=str(e)) + + + @app.route('/course-contents/', methods=['GET', 'POST']) @login_required def course_contents(course_name): @@ -304,6 +308,22 @@ def course_contents(course_name): # Part 2: Load Course Content: user_folder = session['folder'] folder_path = os.path.join(app.config["FOLDER_UPLOAD"], user_folder, course_name) + # Check if metadata file exists, if not create a blank one + meta_file_path = os.path.join(folder_path, "course_meta.json") + if not os.path.exists(meta_file_path): + metadata = { + 'classname': '', + 'professor': '', + 'assistants': '', + 'classdescription': '', + 'assistant_name': '' + } + with open(meta_file_path, 'w') as json_file: + json.dump(metadata, json_file) + else: + with open(meta_file_path, 'r') as json_file: + metadata = json.load(json_file) + contents = get_content_files(folder_path) file_info = detect_final_data_files(folder_path) # for 'textchunks.npy' and 'textchunks-originaltext.csv' if they exist activations = check_and_update_activations_file(folder_path) @@ -327,10 +347,43 @@ def course_contents(course_name): syllabus=syllabus, name=session.get('name'), form=form, - file_info=file_info + file_info=file_info, + metadata=metadata, # add metadata to the template ) +@app.route('/update-course-metadata', methods=['POST']) +@login_required +def update_course_metadata(): + # Extract the form data from the request + folder_name = request.form.get('course_name', '') + metadata = { + 'classname': request.form.get('name', ''), + 'professor': request.form.get('professor', ''), + 'assistants': request.form.get('assistants', ''), + 'assistant_name': request.form.get('assistant_name', ''), + 'classdescription': request.form.get('classdescription', '') + } + + # Your logic to save/update the metadata goes here + # This could involve saving the data to a database, + # or as in your previous example, writing the data to a JSON file. + # I'll give a simple example using a fictitious database function: + + try: + # Assume save_course_metadata is a function that saves the course metadata to your data storage + save_course_metadata(folder_name, metadata) + flash('Metadata updated successfully!', 'success') + except Exception as e: + # Handle any exceptions that might arise + app.logger.error(f"Error updating metadata: {e}", exc_info=True) + flash(f'Error updating metadata; Contact Support', 'danger') + # Redirect to a relevant page after saving + # For instance, redirecting back to the course management or course content page + return redirect(request.referrer) + + + @app.route('/preview-chunks/', methods=['GET']) @login_required def preview_chunks(course_name): diff --git a/app/routes_helper.py b/app/routes_helper.py index b2d1bb7..04ad087 100644 --- a/app/routes_helper.py +++ b/app/routes_helper.py @@ -16,7 +16,7 @@ list_folders, rename_folder, delete_folder, - create_folder, + create_course_folder_with_metadata, allowed_file, delete_file, get_first_txt_file, diff --git a/app/templates/course_contents.html b/app/templates/course_contents.html index 2d0c316..1a4ef35 100644 --- a/app/templates/course_contents.html +++ b/app/templates/course_contents.html @@ -16,31 +16,39 @@

Course Content: {{ course_name }}


{% if syllabus %}
- View Syllabus - + View Syllabus / Course Information +

Syllabus:

Course Syllabus: [Preview of 250 Characters]:

{{ syllabus[:200].replace('
', '')|safe }} -

-

Full Course Syllabus Preview


+

Full Course Syllabus Preview

+
+

Course Information:

+
    +
  • Class Name: {{ metadata.classname if metadata.classname else "" }}
  • +
  • Professor: {{ metadata.professor if metadata.professor else "" }}
  • +
  • Assistants: {{ metadata.assistants if metadata.assistants else "" }}
  • +
  • Virtual Assistant's Name: {{ metadata.assistant_name if metadata.assistant_name else "" }}
  • +
  • Class Description: {{ metadata.classdescription if metadata.classdescription else "" }}
  • +
- - +
- Replace Syllabus + Modify Syllabus / Course Information
+

Modify Syllabus:

{{ form.hidden_tag() }}

{{ form.csrf_token }} - {{ form.pdf.label }} + Replace Syllabus {{ form.pdf.label }} {{ form.pdf(class_='form-control',placeholder='Upload PDF...') }}


- {{ form.submit(id='submit_button') }} + {{ form.submit(id='submit_button', value='Upload and Replace Syllabus') }} {% for error in form.pdf.errors %} [{{ error }}] {% endfor %} @@ -48,13 +56,47 @@

Course Content: {{ course_name }}



-
- - +
+
+
+

Modify Course Information:

+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+
- +
{% else %} @@ -76,13 +118,10 @@

Course Content: {{ course_name }}


{{ form.submit(id='submit_button') }}


-
- - +
- - +
{% endif %} @@ -93,7 +132,7 @@

Course Content: {{ course_name }}


- +
Modify Course Content