3838import datetime
3939import html2markdown
4040from django .core .paginator import Paginator , EmptyPage , PageNotAnInteger
41+ from difflib import SequenceMatcher
4142
4243json .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
131132def 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
580587def 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