Skip to content

Commit 9d538ed

Browse files
Mihir-solanki-13tushhr
authored andcommitted
Refactor yearbook search feature (#100)
* Search field changes * Proper error and messages * Incremental changes Co-authored-by: Tushhr
1 parent b5a5dba commit 9d538ed

File tree

3 files changed

+72
-40
lines changed

3 files changed

+72
-40
lines changed

applications/members/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
urlpatterns = [
1313
re_path(r'^(?P<year>[0-9]{4})/', include(extrapatterns)),
1414
# old link
15-
path('sacbody/', views.sacbody, name="sacbody"),
15+
# path('sacbody/', views.sacbody, name="sacbody"),
1616
# new link
1717
path('alumnibody/', views.alumnibody, name="alumnibody"),
1818
path('search/', views.search, name='search'),

applications/members/views.py

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1-
import json
1+
from importlib.metadata import requires
2+
import json
23

34
from django.shortcuts import render, redirect
4-
from django.db.models import Count, Q
5+
from django.db.models import Count
56
from django.http import JsonResponse
67
from django.contrib.auth.models import User
78
from django.contrib.auth.decorators import login_required
89
from applications.alumniprofile.models import Profile
10+
from django.contrib import messages
911

1012

1113
# Create your views here.
1214

1315
def index(request):
1416
counts = Profile.objects.filter(verify=True).values('batch').order_by('-batch').annotate(count=Count('batch'))
15-
# print(len(counts))
1617
total = 0
18+
1719
for batch, count in counts.values_list('batch', 'count'):
1820
total += count
19-
return render(request, "members/index.html", {'data': counts.values_list('batch', 'count'), 'total': total})
21+
22+
data = counts.values_list('batch', 'count')
23+
24+
context = {
25+
'data': data,
26+
'total': total,
27+
}
28+
29+
return render(request, "members/index.html",context)
2030

2131

2232
def batch(request, year):
33+
2334
programmes = Profile.objects.values_list('programme', flat=True).distinct()
2435
data = {}
2536
for row in programmes:
@@ -29,65 +40,77 @@ def batch(request, year):
2940
for item in result:
3041
data[row][item['branch']] = item['count']
3142

32-
# print(data) #prints {'B.Des': {'CSE': 1}, 'B.Tech': {'CSE': 1, 'ME': 1}}
3343
return render(request, "members/year.html", {'data': data, 'year': year})
3444

3545

3646
def branch(request, programme, year, branch):
3747
# todo: change mail_sent to verify
3848
alumni = Profile.objects.filter(programme=programme, batch=year, branch=branch, verify=True)
39-
# print(alumni)
4049
return render(request, "members/branch.html", {'data': alumni, 'batch': year, 'branch': branch})
4150

42-
def sacbody(request):
43-
return redirect('members:alumnibody')
51+
# def sacbody(request):
52+
# return redirect('members:alumnibody')
4453

4554
def alumnibody(request):
4655
return render(request, "members/alumnibody.html")
4756

4857

4958
@login_required
5059
def search(request):
51-
key = request.GET['search']
52-
profiles = Profile.objects.filter(name__icontains=key) | Profile.objects.filter(
53-
roll_no__icontains=key) | Profile.objects.filter(reg_no__icontains=key)
54-
if len(request.GET) > 1:
55-
if request.GET['batch'] != '':
56-
batch = request.GET['batch']
57-
print(batch)
60+
profiles = Profile.objects.all()
61+
if len(request.POST) > 1:
62+
if request.POST['search'] != '':
63+
key = request.POST['search']
64+
profiles = profiles.filter(name__icontains=key) | Profile.objects.filter(
65+
roll_no__icontains=key) | Profile.objects.filter(reg_no__icontains=key)
66+
67+
if request.POST['batch'] != '':
68+
batch = request.POST['batch']
5869
profiles = profiles.filter(batch=batch)
59-
print(profiles)
60-
if request.GET['city'] != '':
61-
city = request.GET['city']
70+
71+
if request.POST['city'] != '':
72+
city = request.POST['city']
6273
profiles = profiles.filter(city__icontains=city)
63-
if request.GET['programme'] != 'Programme':
64-
programme = request.GET['programme']
74+
75+
if request.POST['programme'] != '':
76+
programme = request.POST['programme']
6577
profiles = profiles.filter(programme__icontains=programme)
66-
if request.GET['branch'] != '':
67-
branch = request.GET['branch']
78+
79+
if request.POST['branch'] != '':
80+
branch = request.POST['branch']
6881
profiles = profiles.filter(branch__icontains=branch)
69-
if request.GET['org'] != '':
70-
org = request.GET['org']
82+
83+
if request.POST['org'] != '':
84+
org = request.POST['org']
7185
profiles1 = profiles.filter(current_organisation__icontains=org)
7286
profiles2 = profiles.filter(current_university__icontains=org)
7387
profiles = profiles1 | profiles2
88+
7489
profiles = profiles.order_by('name')
90+
7591
context = {'profiles': profiles,
76-
'keyy': key,
92+
'keyy': 1,
7793
'zero': len(profiles),
78-
'request': request.GET
94+
'request': request.POST
7995
}
80-
return render(request, "members/index.html", context)
96+
97+
if len(profiles):
98+
messages.success(request,"Total "+str(len(profiles))+" Alumni Found")
99+
else:
100+
messages.error(request, "No Result Found ")
101+
102+
81103

104+
return render(request, "members/index.html", context)
82105

83106
def autoSearch(request):
84107
if request.is_ajax():
108+
# print(request.POST['term'], request.GET['te'])
85109
key = request.GET['term']
86110
search_qs = Profile.objects.filter(name__icontains=key) | Profile.objects.filter(
87111
roll_no__icontains=key) | Profile.objects.filter(reg_no__icontains=key)
88112
data = []
89113
for r in search_qs:
90-
print(r.name)
91114
data.append(r.name)
92115
else:
93116
data = 'fail'
@@ -104,5 +127,5 @@ def mapSearch(request):
104127
'keyy': key,
105128
'zero': len(profiles),
106129
'map': True
107-
}
130+
}
108131
return render(request, "members/index.html", context)

templates/members/index.html

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ <h1 class="text-uppercase mt-4">
7777
{% endif %}
7878

7979
<div class="search-container m-3 p-3" style="margin:6px;">
80-
<form method="GET" action="{% url 'members:search' %}">
80+
<form method="POST" action="{% url 'members:search' %}">
81+
{% csrf_token %}
8182
<div class='row justify-content-center m-3'>
8283
{% if request.search %}
8384
<input type="text" id="search" class="form-control col-5 mx-2"
8485
placeholder="Name, Roll No or Reg No" name="search" value={{request.search}} required>
8586
{% else %}
8687
<input type="text" id="search" class="form-control col-5 mx-2"
87-
placeholder="Name, Roll No or Reg No" name="search" required>
88+
placeholder="Name, Roll No or Reg No" name="search" >
8889
{% endif %}
8990
<button type="submit" class="btn btn-dark col-1 rounded mx-2"><i
9091
class="fa fa-search mx-auto"></i></button>
@@ -165,15 +166,23 @@ <h2 class="font-open-sans"><span class="badge btn-block badge-primary"></span></
165166
{% if not keyy %}
166167
<h2 class="font-open-sans"><span class="badge btn-block badge-primary text-wrap mt-2"> Alumni by
167168
Graduation Year</span></h2>
168-
{% elif map %}
169-
<h2 class="font-open-sans"><span class="badge btn-block badge-primary text-wrap mt-2"> Alumni in {{keyy}}</span></h2>
170-
171-
{% if not zero %}
172-
<div class="alert alert-danger" role="alert">
173-
No Results Found!
174-
</div>
175-
{% endif %}
176169
{% endif %}
170+
<!-- {% if zero %}
171+
<h2 class="font-open-sans"><span class="badge btn-block badge-primary text-wrap mt-2"> Total {{zero}} Alumni Found</span></h2>
172+
{% endif %} -->
173+
{% for message in messages %}
174+
<br>
175+
176+
<div class="alert alert-{% if message.tags == 'success' %}primary {% else %}danger{% endif %} alert-dismissible fade show mb-4" role="alert">
177+
<strong>Message : </strong> {{ message }}
178+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
179+
<span aria-hidden="true">&times;</span>
180+
</button>
181+
</div>
182+
{% endfor %}
183+
184+
185+
177186
</div>
178187
</div>
179188

0 commit comments

Comments
 (0)