Skip to content

Commit

Permalink
Completed the saving, viewing and modifying Course Metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Satish Surath <[email protected]>
  • Loading branch information
satishsurath committed Sep 19, 2023
1 parent 53d41c4 commit 376c1df
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 25 deletions.
15 changes: 14 additions & 1 deletion app/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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'}
Expand Down
61 changes: 57 additions & 4 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -294,6 +295,9 @@ def toggle_activation(course_name, file_name):
return jsonify(success=False, error=str(e))





@app.route('/course-contents/<course_name>', methods=['GET', 'POST'])
@login_required
def course_contents(course_name):
Expand All @@ -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)
Expand All @@ -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/<course_name>', methods=['GET'])
@login_required
def preview_chunks(course_name):
Expand Down
2 changes: 1 addition & 1 deletion app/routes_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
77 changes: 58 additions & 19 deletions app/templates/course_contents.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,87 @@ <h1 class="center-align">Course Content: {{ course_name }}</h1><br>

{% if syllabus %}
<details id="viewSyllabus">
<summary class="course-content-heading">View Syllabus </summary>

<summary class="course-content-heading">View Syllabus / Course Information</summary>

<h3 style="text-align: center;">Syllabus:</h3>
<b>Course Syllabus: [Preview of 250 Characters]:</b>
<br><br>
{{ syllabus[:200].replace('<br>', '')|safe }}
<br><br>
<p><a href="{{ url_for('course_syllabus', course_name=course_name) }}" target="_blank">Full Course Syllabus Preview <svg style="fill: var(--text-color); height:1.5rem; width:1.5rem;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0" x="0px" y="0px" viewBox="0 0 100 125" enable-background="new 0 0 100 100" xml:space="preserve"><path d="M83.333,50v33.333H16.667V16.667H50V10H16.667C13.001,10,10,13,10,16.667v66.667C10,86.999,13.001,90,16.667,90h66.666 C86.999,90,90,86.999,90,83.333V50H83.333z"/><path d="M90,36.667l-0.003-23.335C90,11.493,88.506,10,86.667,10H63.333v6.665h15.283L50,45.285L54.714,50l28.619-28.615v15.281H90z "/></svg></a></p>
<br>
<p><a href="{{ url_for('course_syllabus', course_name=course_name) }}" target="_blank">Full Course Syllabus Preview <svg style="fill: var(--text-color); height:1.5rem; width:1.5rem;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0" x="0px" y="0px" viewBox="0 0 100 125" enable-background="new 0 0 100 100" xml:space="preserve"><path d="M83.333,50v33.333H16.667V16.667H50V10H16.667C13.001,10,10,13,10,16.667v66.667C10,86.999,13.001,90,16.667,90h66.666 C86.999,90,90,86.999,90,83.333V50H83.333z"/><path d="M90,36.667l-0.003-23.335C90,11.493,88.506,10,86.667,10H63.333v6.665h15.283L50,45.285L54.714,50l28.619-28.615v15.281H90z "/></svg></a></p>
<hr>
<h3 style="text-align: center;">Course Information:</h3>
<ul>
<li><strong>Class Name:</strong> {{ metadata.classname if metadata.classname else "<empty>" }}</li>
<li><strong>Professor:</strong> {{ metadata.professor if metadata.professor else "<empty>" }}</li>
<li><strong>Assistants:</strong> {{ metadata.assistants if metadata.assistants else "<empty>" }}</li>
<li><strong>Virtual Assistant's Name:</strong> {{ metadata.assistant_name if metadata.assistant_name else "<empty>" }}</li>
<li><strong>Class Description:</strong> {{ metadata.classdescription if metadata.classdescription else "<empty>" }}</li>
</ul>
</details>


<br>
<details id="ReplaceSyllabus">
<summary class="course-content-heading">Replace Syllabus </summary>
<summary class="course-content-heading">Modify Syllabus / Course Information </summary>


<form method="POST" enctype="multipart/form-data" novalidate>
<h3 style="text-align: center;">Modify Syllabus:</h3>
{{ form.hidden_tag() }}
<p>
{{ form.csrf_token }}
{{ form.pdf.label }}
<span class="alert alert-warning" role="alert"> Replace Syllabus {{ form.pdf.label }} </span>

{{ form.pdf(class_='form-control',placeholder='Upload PDF...') }}
<p> <br> </p>
{{ form.submit(id='submit_button') }}
{{ form.submit(id='submit_button', value='Upload and Replace Syllabus') }}
{% for error in form.pdf.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}

</p>
<br>
<div class="alert alert-warning" role="alert"> Warning: Uploading the Course Syllabus will overwrite the existing Course Syllabus without any prompt.</div>
<div id="processing" class="processing"></div>


<div id="processing" class="processing"></div>
</form>
<hr>
<div style="text-align: center;">
<h3>Modify Course Information:</h3>
<form action="{{ url_for('update_course_metadata') }}" method="post">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<table style="margin: auto; border:0px;">
<tr>
<td style="text-align: left;border: 0px"><label for="name">Course / Class Name:</label></td>
<td style="text-align: left;border: 0px"><input type="text" id="name" name="name" size="35" value="{{ metadata.classname if metadata.classname else '' }}">
<input type="hidden" name="course_name" value="{{ course_name }}" />
</td>
</tr>
<tr>
<td style="text-align: left;border: 0px"><label for="professor">Professor:</label></td>
<td style="text-align: left;border: 0px"><input type="text" id="professor" name="professor" size="35" value="{{ metadata.professor if metadata.professor else '' }}"></td>
</tr>
<tr>
<td style="text-align: left;border: 0px"><label for="assistant_name">Assistant Name:</label></td>
<td style="text-align: left;border: 0px"><input type="text" id="assistant_name" name="assistant_name" size="35" value="{{ metadata.assistant_name if metadata.assistant_name else '' }}"></td>
</tr>
<tr>
<td style="text-align: left;border: 0px"><label for="assistants">Virtual Assistant's Name:</label></td>
<td style="text-align: left;border: 0px"><input type="text" id="assistants" name="assistants" value="{{ metadata.assistant_name if metadata.assistant_name else 'Virtual Teaching Assistant' }}" placeholder="Virtual Teaching Assistant" size="35"></td>
</tr>
<tr>
<td style="text-align: left;border: 0px"><label for="classdescription">Class Description:</label></td>
<td style="text-align: left;border: 0px"><textarea id="classdescription" name="classdescription" rows="3" cols="28" value="{{ metadata.classdescription if metadata.classdescription else '' }}"></textarea></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;border: 0px">
<input type="submit" value="Modify" class="form-control">
</td>
</tr>
</table>
</form>
</div>

</details>

<br>


{% else %}
Expand All @@ -76,13 +118,10 @@ <h1 class="center-align">Course Content: {{ course_name }}</h1><br>
{{ form.submit(id='submit_button') }}
</p>
<br>
<div id="processing" class="processing"></div>


<div id="processing" class="processing"></div>
</form>

</details>

<br>
{% endif %}


Expand All @@ -93,7 +132,7 @@ <h1 class="center-align">Course Content: {{ course_name }}</h1><br>
<input type="hidden" name="course_name" value="{{ course_name }}">
</form>
</details>

<br>

<details id="modifyCourseContent">
<summary class="course-content-heading">Modify Course Content </summary>
Expand Down

0 comments on commit 376c1df

Please sign in to comment.