Skip to content

Commit 9ab658a

Browse files
committed
Tag Search Added + JS Added For Title
1 parent fb583c5 commit 9ab658a

5 files changed

Lines changed: 104 additions & 7 deletions

File tree

website/forum/forms.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@ def clean_body(self):
7575
class Meta:
7676
model = Comments_Answers
7777
fields = ('body',)
78+
79+
class SearchForm(forms.Form):
80+
key = forms.CharField (max_length=100)

website/forum/templates/home.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@
9393
<div class="col-sm-12 col-md-7 col-lg-12">
9494
<div class="card pl-3 pr-5" style="background-color: inherit;border-style: none;margin-top: 4vw;margin-bottom: 1vw;">
9595
<div class="container">
96-
<h1 class="title" style="color:#F95C39;margin-left:0px;">RECursion</h1>
97-
<h5 class="subtitle" style="color:#528CDD;">Programming Community of NIT Durgapur</h5>
98-
<p class="pr-5">We are programming community of NIT Durgapur,
96+
<h1 class="title" style="margin-left:0px;">RECursi <p class="pr-5">We are programming community of NIT Durgapur,
9997
with focus on Improving Coding culture institute wide by conducting regular lectures from beginner to advance topics of programming, Our goal is to increse
10098
student's participation in inter-colligiate contest like
10199
ACM-ICPC and get better ranks.</p><br>
@@ -149,4 +147,11 @@ <h3>Who we are</h3>
149147
</div>
150148
</div>
151149
</div>
150+
<script>
151+
$(document).ready(function() {
152+
var colors = [ "#F95C39", "#FED330", "#FC5C65", "#26DE81",];
153+
var rand = Math.floor(Math.random() * colors.length);
154+
$('.title').css("color", colors[rand]);
155+
});
156+
</script>
152157
{% endblock %}

website/forum/templates/questions.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ <h2 class="mt-3"id="haveAQ">Have a question?</h2>
2626
<div class="block row">
2727
<div class="col-sm-12 col-lg-6">
2828
<input class="form-control m-2" type="text" placeholder="Search Tag" style="float:left;width:75%;">
29+
<form method="POST">
30+
<table>
31+
{{ form_search }}
32+
</table>
33+
<br>
34+
<button type="submit" class="save btn btn-primary" style="margin-left:150px;">SAVE</button>
35+
</form>
2936
<a href="#" class="ml-2 p-2" style="position:relative;background:#E0EAF5;color:white;top:15px;
3037
">
3138
<i class="fas fa-search"></i>

website/forum/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
path('deletecomment/<int:id>/', delete_comment, name='delete_comment'),
2525
path('deletecomment_answer/<int:id>/', delete_answer_comment, name='delete_answer_comment'),
2626
path('filter/<int:id>/', filter_question, name='filter_question'),
27+
path('search/<str:key>', search_question, name='search_question'),
2728
url(r'^markdownx/', include('markdownx.urls')),
2829
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

website/forum/views.py

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import datetime
3939
import html2markdown
4040
from django.core.paginator import Paginator , EmptyPage, PageNotAnInteger
41+
from difflib import SequenceMatcher
4142

4243
json.JSONEncoder.default = lambda self,obj: (obj.isoformat() if isinstance(obj, datetime.datetime) else None)
4344

@@ -129,6 +130,12 @@ def add_question(request):
129130
return render(request, 'forum/questions-form.html', {'form': form,'form2':form2,})
130131

131132
def list_questions(request):
133+
search = SearchForm(request.POST or None)
134+
if request.method == 'POST':
135+
if search.is_valid():
136+
key_req = search.cleaned_data
137+
key = key_req.get('key')
138+
return HttpResponseRedirect(reverse('forum:search_question', args=(key,)))
132139
questions = Questions.objects.all()
133140
paginator = Paginator(questions, 5)
134141
page = request.GET.get('page')
@@ -168,7 +175,7 @@ def list_questions(request):
168175
tags_recent_record.append(taggings_recent[count].tag)
169176
count+=1
170177
profiles=Profile.objects.all()
171-
args = {'profile':profiles, 'questions':questions_list, 'answers':answers, 'follows':follows, 'tags':tags_recent, 'taggings':taggings_recent, 'tags_recent':tags_recent_record, 'tags_popular':tags_popular_record, 'q_count':q_count}
178+
args = {'form_search':search, 'profile':profiles, 'questions':questions_list, 'answers':answers, 'follows':follows, 'tags':tags_recent, 'taggings':taggings_recent, 'tags_recent':tags_recent_record, 'tags_popular':tags_popular_record, 'q_count':q_count}
172179
if request.is_ajax():
173180
return render(request, 'list.html', args)
174181
return render(request, 'questions.html', args)
@@ -567,7 +574,7 @@ def voting(request, id):
567574
upvote = Upvotes.objects.create(answer=answer, user=user)
568575
upvote.save()
569576
if Upvotes.objects.filter(answer=answer).exists():
570-
count=Upvotes.objects.filter(answer=answer).count()
577+
count=Upvotes.objects.filter(aqnswer=answer).count()
571578
return HttpResponse(json.dumps({
572579
'count':count,
573580
'Success':'upvoted'
@@ -578,6 +585,12 @@ def voting(request, id):
578585

579586

580587
def filter_question(request ,id):
588+
search = SearchForm(request.POST or None)
589+
if request.method == 'POST':
590+
if search.is_valid():
591+
key_req = search.cleaned_data
592+
key = key_req.get('key')
593+
return HttpResponseRedirect(reverse('forum:search_question', args=(key,)))
581594
try:
582595
required_tag=get_object_or_404(Tags, pk=id)
583596
except:
@@ -625,7 +638,7 @@ def filter_question(request ,id):
625638
return HttpResponse('')
626639
questions_list = paginator.page(paginator.num_pages)
627640
profiles = Profile.objects.all()
628-
args = {'profile': profiles, 'questions':questions_list, 'answers':answers, 'follows':follows, 'tags':tags_recent, 'taggings':taggings_recent, 'tags_recent':tags_recent_record, 'tags_popular':tags_popular_record, 'q_count':q_count}
641+
args = {'form_search':search, 'profile': profiles, 'questions':questions_list, 'answers':answers, 'follows':follows, 'tags':tags_recent, 'taggings':taggings_recent, 'tags_recent':tags_recent_record, 'tags_popular':tags_popular_record, 'q_count':q_count}
629642
if request.is_ajax():
630643
return render(request, 'list.html', args)
631644
return render(request, 'questions.html', args)
@@ -766,4 +779,72 @@ def delete_answer_comment(request, id):
766779
answer_comment.delete()
767780
return HttpResponseRedirect(reverse('detail_questions', args=(question.id,)))
768781

769-
782+
def search_question(request, key):
783+
search = SearchForm(request.POST or None)
784+
if request.method == 'POST':
785+
if search.is_valid():
786+
key_req = search.cleaned_data
787+
key = key_req.get('key')
788+
return HttpResponseRedirect(reverse('forum:search_question', args=(key,)))
789+
questions_found=[]
790+
questions_list=Questions.objects.all()
791+
tags_found=[]
792+
tags_recent = Tags.objects.all().order_by('-updated_at')
793+
taggings_recent = Taggings.objects.all().order_by('-updated_at')
794+
for tag in tags_recent:
795+
if SequenceMatcher(None, tag.name.lower(), key.lower()).ratio()>0.4:
796+
tags_found.append(tag)
797+
for tag in tags_found:
798+
for tagging in taggings_recent:
799+
if tagging.tag==tag:
800+
questions_found.append([SequenceMatcher(None, tag.name.lower(), key.lower()).ratio(), tagging.question])
801+
for question in questions_list:
802+
if SequenceMatcher(None, question.title.lower(), key.lower()).ratio()>0.3:
803+
questions_found.append([SequenceMatcher(None, question.title.lower(), key.lower()).ratio(), question])
804+
questions_found.sort(key=lambda x: x[0], reverse=True)
805+
questions=[]
806+
for question in questions_found:
807+
if question[1] not in questions:
808+
questions.append(question[1])
809+
answers = Answers.objects.all()
810+
follows = Follows.objects.all()
811+
tags_popular = []
812+
check = []
813+
if tags_recent.count() > 10:
814+
limit = 10
815+
else:
816+
limit = tags_recent.count()
817+
for tag in tags_recent:
818+
tagging = Taggings.objects.filter(tag=tag)
819+
count = tagging.count()
820+
tags_popular.append([count, tag])
821+
if count > 0:
822+
check.append(tag)
823+
tags_popular.sort(key=lambda x: x[0], reverse=True)
824+
tags_recent_record = []
825+
tags_popular_record = []
826+
for i in range(limit):
827+
tags_popular_record.append(tags_popular[i][1])
828+
count = 0
829+
while len(tags_recent_record) < len(taggings_recent) and len(tags_recent_record) < len(check):
830+
if taggings_recent[count].tag not in tags_recent_record:
831+
tags_recent_record.append(taggings_recent[count].tag)
832+
count += 1
833+
q_count = Questions.objects.all().count() # For displaying total number of questions
834+
paginator = Paginator(questions, 5)
835+
page = request.GET.get('page')
836+
try:
837+
questions_list = paginator.page(page)
838+
except PageNotAnInteger:
839+
questions_list = paginator.page(1)
840+
except EmptyPage:
841+
if request.is_ajax():
842+
return HttpResponse('')
843+
questions_list = paginator.page(paginator.num_pages)
844+
profiles = Profile.objects.all()
845+
args = {'form_search':search, 'profile': profiles, 'questions': questions_list, 'answers': answers, 'follows': follows,
846+
'tags': tags_recent, 'taggings': taggings_recent, 'tags_recent': tags_recent_record,
847+
'tags_popular': tags_popular_record, 'q_count': q_count}
848+
if request.is_ajax():
849+
return render(request, 'list.html', args)
850+
return render(request, 'questions.html', args)

0 commit comments

Comments
 (0)