Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(courseInstructor): Functionality to add course instructor using excel sheet #1682

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
53 changes: 46 additions & 7 deletions FusionIIIT/applications/programme_curriculum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from .models import Programme, Discipline, Curriculum, Semester, Course, Batch, CourseSlot,NewProposalFile,Proposal_Tracking,CourseInstructor
from .forms import ProgrammeForm, DisciplineForm, CurriculumForm, SemesterForm, CourseForm, BatchForm, CourseSlotForm, ReplicateCurriculumForm,NewCourseProposalFile,CourseProposalTrackingFile, CourseInstructorForm
from .filters import CourseFilter, BatchFilter, CurriculumFilter,CourseInstructorFilter
from django.db import IntegrityError
from django.db import IntegrityError, transaction
from django.utils import timezone
import xlrd

from notification.views import prog_and_curr_notif
# from applications.academic_information.models import Student
Expand Down Expand Up @@ -1654,12 +1655,50 @@ def file_unarchive(request,FileId):
def add_course_instructor(request):
if request.session['currentDesignationSelected'] == "acadadmin":
if request.method == 'POST':
form = CourseInstructorForm(request.POST)
if form.is_valid():
form.save() # Save the form data to the database
return redirect('/programme_curriculum/admin_instructor/') # Redirect to a success page after saving
else:
form = CourseInstructorForm()
if 'form_submit' in request.POST:
form = CourseInstructorForm(request.POST)
if form.is_valid():
form.save() # Save the form data to the database
return redirect('/programme_curriculum/admin_instructor/') # Redirect to a success page after saving
else:
messages.error(request, f"An error occured while adding instructor.")
elif 'excel_submit' in request.POST:
manual_instructor_xsl = request.FILES['manual_instructor_xsl']
try:
excel = xlrd.open_workbook(file_contents=manual_instructor_xsl.read())
sheet = excel.sheet_by_index(0)

# Start reading rows (assuming first row contains headers)
with transaction.atomic():
all_success = True
for i in range(1, sheet.nrows): # Skip the header row
try:
course_code = str(sheet.cell(i, 0).value).strip()
course_version = float(sheet.cell(i, 1).value)
instructor_id = str(sheet.cell(i, 2).value).strip()
year = int(sheet.cell(i, 3).value)
semester_no = int(sheet.cell(i, 4).value)
course = Course.objects.filter(
Q(code__iexact=course_code),
version=course_version
).first()
instructor = Faculty.objects.get(id=instructor_id)
course_instructor = CourseInstructor(
course_id=course,
instructor_id=instructor,
year=year,
semester_no=semester_no
)
course_instructor.save()
except Exception as e:
all_success = False
messages.error(request, f"Error processing Excel file in row {i}: {e}")
if all_success:
messages.success(request, "Instructors added successfully from Excel!")
return redirect('/programme_curriculum/admin_instructor/')
except Exception as e:
messages.error(request, f"Error processing Excel file: {e}")
form = CourseInstructorForm()

return render(request, 'programme_curriculum/acad_admin/add_course_instructor.html', {'form': form})
return HttpResponseRedirect('/programme_curriculum/')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
{% extends 'programme_curriculum/acad_admin/common.html' %}
{% load static %}
{% block content %}
<div class="ui segment" >
<div class="header"><h1>Course Instructor Form</h1></div>
<div class="ui divider"></div>
<!-- Check if there are form errors and trigger an alert -->
{% if form.errors %}
<script type="text/javascript">
var errorMessages = "";
<!-- Tab Menu -->
<div class="ui top attached tabular menu">
<a class="item active" data-tab="manual">Manual Form</a>
<a class="item" data-tab="excel">Upload via Excel</a>
</div>

{% if form.non_field_errors %}
errorMessages += "A Course Instructor with this Course, Instructor, and year combination already exists. Please choose different values.\n";
{% endif %}
<!-- Manual Form Tab -->
<div class="ui bottom attached tab segment active" data-tab="manual">
<!-- Check if there are form errors and trigger an alert -->
<form class="ui large form" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input class="ui primary button" name="form_submit" type="Submit" value="Submit">
</form>
</div>

{% for field in form %}
{% if field.errors %}
errorMessages += "A Course Instructor with this Course, Instructor, and year combination already exists. Please choose different values.\n";
{% endif %}
{% endfor %}

if (errorMessages !== "") {
alert(errorMessages);
}
</script>
{% endif %}
<form class="ui large form" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input class="ui primary button" type="Submit" name="Submit">
</form>
<!-- Excel Upload Form Tab -->
<div class="ui bottom attached tab segment" data-tab="excel">
<form class="ui large form" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<h3>Upload Course Instructors via Excel</h3>
<input type="file" name="manual_instructor_xsl" accept=".xls" required>
<input type="Submit" name="excel_submit" class="ui primary button" value="Upload Excel">
</form>
<div class="ui message">
<p>You can download a sample Excel file for reference:</p>
<a href="{% static 'samples/sample_course_instructor.xls' %}" class="ui secondary button">
Download Sample Excel
</a>
</div>
</div>
</div>

{% endblock %}
<script>
// Activate tabs using Semantic UI's tab module
$('.menu .item').tab();
</script>
{% endblock %}
Loading