Skip to content

Commit 19adc76

Browse files
committed
Feat: update search to city and property based dropdown
Implement city and property category dropdown for search instead of normal string search. City dropdown was added earlier, property category dropdown is added in this commit
1 parent 3b02d9a commit 19adc76

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

realtoric/core/forms.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,28 @@
44

55

66
class SearchForm(forms.Form):
7+
CATEGORY_CHOICES = (
8+
("", "Choose property category"),
9+
("Apartment", "Apartments"),
10+
("Commercial", "Commercials"),
11+
("House", "Houses"),
12+
("Land", "Lands"),
13+
("Villa", "Villas"),
14+
)
15+
716
city = forms.ModelChoiceField(
817
queryset=City.objects.all(),
918
widget=forms.Select(attrs={"class": "form-select-sm choose-city-dropdown"}),
1019
to_field_name="name",
11-
empty_label="Cities",
20+
empty_label="Choose City",
21+
label="",
22+
)
23+
24+
category = forms.ChoiceField(
25+
choices=CATEGORY_CHOICES,
26+
widget=forms.Select(attrs={"class": "form-select-sm choose-city-dropdown"}),
1227
label="",
28+
required=True,
1329
)
1430

1531

realtoric/core/views.py

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from itertools import chain
2-
31
from django.contrib import messages
42
from django.contrib.auth.mixins import LoginRequiredMixin
53
from django.contrib.messages.views import SuccessMessageMixin
@@ -207,29 +205,31 @@ def get_queryset(self):
207205
Return search results.
208206
"""
209207

210-
query = self.request.GET.get("search", None)
211208
city = self.request.GET.get("city", None)
209+
category = self.request.GET.get("category", None)
212210

213-
if query and city:
214-
apartment = Apartment.objects.filter(
215-
Q(city__icontains=city) & Q(ad_title__icontains=query)
216-
)
217-
commercial = Commercial.objects.filter(
218-
Q(city__icontains=city) & Q(ad_title__icontains=query)
219-
)
220-
house = House.objects.filter(
221-
Q(city__icontains=city) & Q(ad_title__icontains=query)
222-
)
223-
land = Land.objects.filter(
224-
Q(city__icontains=city) & Q(ad_title__icontains=query)
225-
)
226-
villa = Villa.objects.filter(
227-
Q(city__icontains=city) & Q(ad_title__icontains=query)
228-
)
211+
if city and category:
212+
if category == "Apartment":
213+
apartment = Apartment.objects.filter(Q(city__icontains=city))
214+
return apartment
229215

230-
return list(chain(apartment, commercial, house, land, villa))
216+
elif category == "Commercial":
217+
commercial = Commercial.objects.filter(Q(city__icontains=city))
218+
return commercial
219+
220+
elif category == "House":
221+
house = House.objects.filter(Q(city__icontains=city))
222+
return house
223+
224+
elif category == "Land":
225+
land = Land.objects.filter(Q(city__icontains=city))
226+
return land
227+
228+
elif category == "Villa":
229+
villa = Villa.objects.filter(Q(city__icontains=city))
230+
return villa
231231
else:
232-
messages.error(self.request, _("No results found."))
232+
messages.error(self.request, _("Something went wrong."))
233233

234234

235235
property_search_results_view = PropertySearchResultsView.as_view()
@@ -468,7 +468,6 @@ def form_valid(self, form):
468468
)
469469
return super().form_valid(form)
470470

471-
472471
def get_success_url(self):
473472
return reverse("core:commercial_detail", kwargs={"slug": self.object.slug})
474473

@@ -500,7 +499,6 @@ def form_valid(self, form):
500499
form.instance.house_images.create(house=house_data, image=image)
501500
return super().form_valid(form)
502501

503-
504502
def get_success_url(self):
505503
return reverse("core:house_detail", kwargs={"slug": self.object.slug})
506504

realtoric/templates/pages/home.html

+1-8
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,11 @@ <h1 class="heading" data-aos="fade-up">
3030
<form
3131
action="{% url 'core:search_results' %}"
3232
method="GET"
33-
class="narrow-w form-search d-flex align-items-stretch mb-3"
33+
class="narrow-w form-search d-flex align-items-stretch justify-content-center mb-3"
3434
data-aos="fade-up"
3535
data-aos-delay="200"
3636
>
3737
{{ form|crispy }}
38-
<input
39-
required
40-
type="text"
41-
name="search"
42-
class="form-control px-4"
43-
placeholder="Choose city and search..."
44-
/>
4538
<button type="submit" class="btn btn-primary">Search</button>
4639
</form>
4740
</div>

realtoric/templates/pages/search_results.html

+3-11
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@
1212
<form
1313
action="{% url 'core:search_results' %}"
1414
method="GET"
15-
class="narrow-w form-search d-flex align-items-stretch"
15+
class="narrow-w form-search d-flex align-items-stretch justify-content-center"
1616
data-aos="fade-up"
1717
data-aos-delay="200"
1818
>
1919
{{ form|crispy }}
20-
<input
21-
required
22-
type="text"
23-
name="search"
24-
class="form-control px-4"
25-
value="{{ request.GET.search }}"
26-
placeholder="Choose city and search..."
27-
/>
2820
<button type="submit" class="btn btn-primary">Search</button>
2921
</form>
3022
</div>
@@ -36,7 +28,7 @@
3628
<div class="row align-items-center">
3729
<div class="col-lg-6">
3830
<h2 class="font-weight-bold text-primary heading">
39-
Search results for "{{ request.GET.search }}":
31+
Search results for "{{ request.GET.category }} in {{ request.GET.city }}":
4032
</h2>
4133
</div>
4234
</div>
@@ -189,7 +181,7 @@ <h6 class="card-text">{{ property.description|capfirst|truncatewords:6 }}</h6>
189181
<div class="col">
190182
<div class="alert alert-danger d-flex align-items-center" role="alert">
191183
<h4>
192-
No results found for "{{ request.GET.search }}"!
184+
No results found!
193185
</h4>
194186
</div>
195187
</div>

0 commit comments

Comments
 (0)