Skip to content

Commit

Permalink
Refactor yearbook search feature (#100)
Browse files Browse the repository at this point in the history
* Search field changes

* Proper error and messages

* Incremental changes

Co-authored-by: Tushhr
  • Loading branch information
Mihir-solanki-13 authored and tushhr committed Sep 14, 2022
1 parent b5a5dba commit 9d538ed
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 40 deletions.
2 changes: 1 addition & 1 deletion applications/members/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
urlpatterns = [
re_path(r'^(?P<year>[0-9]{4})/', include(extrapatterns)),
# old link
path('sacbody/', views.sacbody, name="sacbody"),
# path('sacbody/', views.sacbody, name="sacbody"),
# new link
path('alumnibody/', views.alumnibody, name="alumnibody"),
path('search/', views.search, name='search'),
Expand Down
81 changes: 52 additions & 29 deletions applications/members/views.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import json
from importlib.metadata import requires
import json

from django.shortcuts import render, redirect
from django.db.models import Count, Q
from django.db.models import Count
from django.http import JsonResponse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from applications.alumniprofile.models import Profile
from django.contrib import messages


# Create your views here.

def index(request):
counts = Profile.objects.filter(verify=True).values('batch').order_by('-batch').annotate(count=Count('batch'))
# print(len(counts))
total = 0

for batch, count in counts.values_list('batch', 'count'):
total += count
return render(request, "members/index.html", {'data': counts.values_list('batch', 'count'), 'total': total})

data = counts.values_list('batch', 'count')

context = {
'data': data,
'total': total,
}

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


def batch(request, year):

programmes = Profile.objects.values_list('programme', flat=True).distinct()
data = {}
for row in programmes:
Expand All @@ -29,65 +40,77 @@ def batch(request, year):
for item in result:
data[row][item['branch']] = item['count']

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


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

def sacbody(request):
return redirect('members:alumnibody')
# def sacbody(request):
# return redirect('members:alumnibody')

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


@login_required
def search(request):
key = request.GET['search']
profiles = Profile.objects.filter(name__icontains=key) | Profile.objects.filter(
roll_no__icontains=key) | Profile.objects.filter(reg_no__icontains=key)
if len(request.GET) > 1:
if request.GET['batch'] != '':
batch = request.GET['batch']
print(batch)
profiles = Profile.objects.all()
if len(request.POST) > 1:
if request.POST['search'] != '':
key = request.POST['search']
profiles = profiles.filter(name__icontains=key) | Profile.objects.filter(
roll_no__icontains=key) | Profile.objects.filter(reg_no__icontains=key)

if request.POST['batch'] != '':
batch = request.POST['batch']
profiles = profiles.filter(batch=batch)
print(profiles)
if request.GET['city'] != '':
city = request.GET['city']

if request.POST['city'] != '':
city = request.POST['city']
profiles = profiles.filter(city__icontains=city)
if request.GET['programme'] != 'Programme':
programme = request.GET['programme']

if request.POST['programme'] != '':
programme = request.POST['programme']
profiles = profiles.filter(programme__icontains=programme)
if request.GET['branch'] != '':
branch = request.GET['branch']

if request.POST['branch'] != '':
branch = request.POST['branch']
profiles = profiles.filter(branch__icontains=branch)
if request.GET['org'] != '':
org = request.GET['org']

if request.POST['org'] != '':
org = request.POST['org']
profiles1 = profiles.filter(current_organisation__icontains=org)
profiles2 = profiles.filter(current_university__icontains=org)
profiles = profiles1 | profiles2

profiles = profiles.order_by('name')

context = {'profiles': profiles,
'keyy': key,
'keyy': 1,
'zero': len(profiles),
'request': request.GET
'request': request.POST
}
return render(request, "members/index.html", context)

if len(profiles):
messages.success(request,"Total "+str(len(profiles))+" Alumni Found")
else:
messages.error(request, "No Result Found ")



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

def autoSearch(request):
if request.is_ajax():
# print(request.POST['term'], request.GET['te'])
key = request.GET['term']
search_qs = Profile.objects.filter(name__icontains=key) | Profile.objects.filter(
roll_no__icontains=key) | Profile.objects.filter(reg_no__icontains=key)
data = []
for r in search_qs:
print(r.name)
data.append(r.name)
else:
data = 'fail'
Expand All @@ -104,5 +127,5 @@ def mapSearch(request):
'keyy': key,
'zero': len(profiles),
'map': True
}
}
return render(request, "members/index.html", context)
29 changes: 19 additions & 10 deletions templates/members/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ <h1 class="text-uppercase mt-4">
{% endif %}

<div class="search-container m-3 p-3" style="margin:6px;">
<form method="GET" action="{% url 'members:search' %}">
<form method="POST" action="{% url 'members:search' %}">
{% csrf_token %}
<div class='row justify-content-center m-3'>
{% if request.search %}
<input type="text" id="search" class="form-control col-5 mx-2"
placeholder="Name, Roll No or Reg No" name="search" value={{request.search}} required>
{% else %}
<input type="text" id="search" class="form-control col-5 mx-2"
placeholder="Name, Roll No or Reg No" name="search" required>
placeholder="Name, Roll No or Reg No" name="search" >
{% endif %}
<button type="submit" class="btn btn-dark col-1 rounded mx-2"><i
class="fa fa-search mx-auto"></i></button>
Expand Down Expand Up @@ -165,15 +166,23 @@ <h2 class="font-open-sans"><span class="badge btn-block badge-primary"></span></
{% if not keyy %}
<h2 class="font-open-sans"><span class="badge btn-block badge-primary text-wrap mt-2"> Alumni by
Graduation Year</span></h2>
{% elif map %}
<h2 class="font-open-sans"><span class="badge btn-block badge-primary text-wrap mt-2"> Alumni in {{keyy}}</span></h2>

{% if not zero %}
<div class="alert alert-danger" role="alert">
No Results Found!
</div>
{% endif %}
{% endif %}
<!-- {% if zero %}
<h2 class="font-open-sans"><span class="badge btn-block badge-primary text-wrap mt-2"> Total {{zero}} Alumni Found</span></h2>
{% endif %} -->
{% for message in messages %}
<br>

<div class="alert alert-{% if message.tags == 'success' %}primary {% else %}danger{% endif %} alert-dismissible fade show mb-4" role="alert">
<strong>Message : </strong> {{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}



</div>
</div>

Expand Down

0 comments on commit 9d538ed

Please sign in to comment.