Skip to content

Commit

Permalink
Add view_duplicates
Browse files Browse the repository at this point in the history
Add a way for admins to see duplicate (hence redundant) students
In prehistoric times, students with the same ID were allowed to be created twice
This feature allows admins to see obsolete Student objects
  • Loading branch information
aryanpingle committed Feb 20, 2023
1 parent 663206d commit 83533ae
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions swd/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@
url(r'^admin/get_cor_add', views.get_corr_address, name='get_corr_address'),
url(r'^admin/delete_students/',views.delete_students, name='delete_students'),

url(r'^admin/view_duplicates/(?P<end_year>\d+)?/?', views.view_duplicates, name='view_duplicates'),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
62 changes: 62 additions & 0 deletions swd/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,68 @@

from calendar import monthrange

@user_passes_test(lambda a: a.is_superuser)
def view_duplicates(request, end_year:str):
"""
Shows duplicate students from year 2000 to the given end_year (default: current year)
URL: admin/view_duplicates/<optional end_year>
"""
students = []

# If the end year isn't given, default to current year
if end_year == None:
end_year = datetime.now().year

years = [2000, int(end_year)+1]
for year in range(*years):
students.extend(Student.objects.filter(bitsId__startswith=str(year)))

# students -> list of Student objects in the given range of years

id_dict = {}
duplicate_list = []
nulls = []
fields = ['admit', 'bDay']
for idx, student in enumerate(students):
# If student hasn't been seen before
# Add to set of ids and continue
if not student.bitsId in id_dict:
id_dict[student.bitsId] = idx
continue

# At this point we know that `student` has been seen before

s1 = student
s2 = students[id_dict[student.bitsId]]

# Identify which of s1 and s2 is the inferior duplicate
inferior = None
master = None
for field in fields:
f1 = getattr(s1, field)
f2 = getattr(s2, field)
if f1==None and f2==None: continue
if f1!=None and f2!=None: continue
if f1:
master = s1
inferior = s2
break
master = s2
inferior = s1
break
if inferior == None:
# Contingency in case somehow both objects have all fields
nulls.append(s1)
else:
duplicate_list.append([master, inferior])

# duplicate_list -> list of [master object, inferior object] lists

return render(request, "view_duplicates.html", {
"students": duplicate_list,
"start_year": 2000,
"end_year": end_year,
})

def noPhD(func):
def check(request, *args, **kwargs):
Expand Down
25 changes: 25 additions & 0 deletions swd/templates/view_duplicates.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends 'indexbase.html' %}

{% block content %}

<h3>Duplicate Students from {{ start_year }} to {{ end_year }} ({{ students|length }})</h3>

<table class="centered bordered striped">
<tr>
<th style="text-align: center">More Details</th>
<th style="text-align: center">Less Details (Should be deleted)</th>
<th style="text-align: center">Name</th>
<th style="text-align: center">ID</th>
</tr>

{% for student_master,student_inferior in students %}
<tr align="center">
<td><a href="../main/student/{{student_master.pk}}/change/">{{ student_master.pk }}</a></td>
<td><a href="../main/student/{{student_inferior.pk}}/change/">{{ student_inferior.pk }}</a></td>
<td>{{ student_master.name }}</td>
<td>{{ student_master.bitsId }}</td>
</tr>
{% endfor %}
</table>

{% endblock %}

0 comments on commit 83533ae

Please sign in to comment.