66import re
77from .models import Subject , Category
88from apps .users .models import TutorProfile
9- from apps .users . managers import TutorProfileManager
9+ from apps .uploads . models import UploadRecord
1010from .serializers import TutorSearchResultSerializer
1111
1212# Create your views here.
@@ -29,29 +29,83 @@ def get(self, request, format=None):
2929 except Exception as e :
3030 return Response ({"message" : f"Error parsing location: { str (e )} " }, status = status .HTTP_400_BAD_REQUEST )
3131
32- # Filter tutors by location
33- filtered_tutors = TutorProfile .objects .filter_tutors_by_location (what , city , state )
34- # Perform search with 'what' term
35- search_results = TutorProfile .objects .search (filtered_tutors , what )
32+ # Apply all filters before searching with django-watson
33+
34+ # Filter tutors by location (Required)
35+ filtered_tutors = TutorProfile .objects .filter_tutors_by_location (city , state )
36+
37+ # Filter tutors by pay (Optional)
38+ min_price = request .query_params .get ('min-price' , "" ).strip ()
39+ max_price = request .query_params .get ('max-price' , "" ).strip ()
40+
41+ try :
42+ # Convert from string to float
43+ min_price = float (min_price ) if min_price else None
44+ max_price = float (max_price ) if max_price else None
45+
46+ # Check if max price is not less than min price
47+ if min_price is not None and max_price is not None and max_price < min_price :
48+ return Response ({"message" : "Max price cannot be less than min price." }, status = status .HTTP_400_BAD_REQUEST )
49+
50+ # Filter based of price range
51+ filtered_tutors = TutorProfile .objects .filter_by_price_range (filtered_tutors , min_price , max_price )
52+
53+ except ValueError :
54+ return Response ({"message" : "Price filters must be valid numbers." }, status = status .HTTP_400_BAD_REQUEST )
55+
56+
57+ # Filter tutors by rating (Optional)
58+ rating = request .query_params .get ('rating' , "" ).strip ()
59+ if rating :
60+ try :
61+ # Convert from string to int
62+ rating = int (rating )
63+ # Filter based of rating (i.e. 4 stars and up, 3 stars and up, etc)
64+ filtered_tutors = TutorProfile .objects .filter_tutors_by_rating (filtered_tutors , rating )
65+
66+ except ValueError :
67+ return Response ({"message" : "Invalid value for rating. It must be a number." }, status = status .HTTP_400_BAD_REQUEST )
68+
69+ is_subjects_filtered = False
70+ query = Q ()
71+
72+ # Get subjects from request
73+ subjects = request .query_params .get ('subjects' , "" ).strip ()
74+ # Filter tutors by subject (Optional)
75+ if subjects :
76+ subject_list = subjects .split ("," ) # Assuming subjects are comma-separated
77+ # Execute the query for subjects
78+ query = Subject .objects .filter (Q (title__in = subject_list ))
79+ filtered_tutors = TutorProfile .objects .filter_tutors_by_subject (filtered_tutors , query , is_subjects_filtered )
80+ is_subjects_filtered = True
3681
3782 # Split the 'what' search term into individual words, handling non-word characters and spaces
38- # This helps match partial words that django-watson might overlook
83+ # This helps match partial words regarding subject and is used to pull up other
84+ # tutor results whose subjects fits this search term
3985 search_terms = re .split (r'[\W\s]+' , what )
40- query = Q ()
86+ lookup_subjects_query = Q ()
87+ # This helps match partial names regarding tutors
88+ lookup_tutors_query = Q ()
4189
4290 # Loop through each search term and create a query that checks if the term is
4391 # in the category title or the subject title
4492 for term in search_terms :
45- query |= Q (category__title__icontains = term ) | Q (title__icontains = term )
93+ lookup_subjects_query |= Q (category__title__icontains = term ) | Q (title__icontains = term )
94+ lookup_tutors_query |= Q (profile__user__first_name__icontains = term ) | Q (profile__user__last_name__icontains = term )
95+
96+ # Perform search with 'what' term
97+ search_results = TutorProfile .objects .search (filtered_tutors , what )
98+ # Combine with partial_tutor_matches
99+ partial_tutor_matches = TutorProfile .objects .filter (lookup_tutors_query )
100+ search_results = (search_results | partial_tutor_matches )
46101
47102 try :
48- # Attempt to filter subjects based on the search query
49- subject_query = Subject .objects .filter (query )
50- # If matching subjects are found, filter tutors by these subjects
51- if subject_query .exists ():
52- filtered_tutors = TutorProfile .objects .filter_tutors_by_subject (filtered_tutors , subject_query )
53- # Combine the filtered tutors with the existing search results
54- search_results = (filtered_tutors | search_results ).distinct ()
103+ # Execute the query for subjects
104+ subject_query = Subject .objects .filter (lookup_subjects_query )
105+ filtered_tutors = TutorProfile .objects .filter_tutors_by_subject (filtered_tutors , subject_query , is_subjects_filtered )
106+ # Combine the filtered tutors with the existing search results
107+ search_results = (search_results | filtered_tutors ).distinct ()
108+
55109 except Exception as e :
56110 return Response ({"message" : f"Error searching subjects: { str (e )} " }, status = status .HTTP_500_INTERNAL_SERVER_ERROR )
57111
0 commit comments