-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search day-care funconallity to dog-owner homepage.
The dog-owner user can search for day-care that relevant for him. If the user didnt search for day-care he will see all Day-Cares in the application. Signed-off-by: tamirmatok <[email protected]>
- Loading branch information
1 parent
bd7bf3a
commit 2dcf8d7
Showing
6 changed files
with
157 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django import forms | ||
from .models import DayCare, Area | ||
from django.db.models import Max, Min | ||
|
||
|
||
class DayCareSearchForm(forms.Form): | ||
min_price = DayCare.objects.all().aggregate(Min('price_per_day')).get('price_per_day__min') | ||
max_price = DayCare.objects.all().aggregate(Max('price_per_day')).get('price_per_day__max') | ||
start_date = forms.DateField(required=True, label=' Start date', | ||
widget=forms.widgets.DateInput(attrs={'class': 'form-control', 'type': 'date'})) | ||
end_date = forms.DateField(required=True, label=' End date', | ||
widget=forms.widgets.DateInput(attrs={'class': 'form-control', 'type': 'date'}), ) | ||
area = forms.ChoiceField(required=False, label='Area', | ||
choices=(Area.choices + [('', 'All'), ]), initial="") | ||
city = forms.CharField(required=False, label='City') | ||
name = forms.CharField(required=False, label='Day care name') | ||
price_per_day = forms.ChoiceField(required=False, label='Max price', | ||
choices=((str(x), x) for x in range(min_price, max_price + 11, 10)), | ||
initial=str(max_price)) | ||
|
||
def clean(self): | ||
cleaned_data = super().clean() | ||
start_date = cleaned_data.get("start_date") | ||
end_date = cleaned_data.get("end_date") | ||
|
||
if start_date and end_date: | ||
if end_date < start_date: | ||
self._errors['end_date'] = self.error_class(["End date should be greater!"]) | ||
|
||
return cleaned_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,58 @@ | ||
{% extends "main/base_template.html" %} | ||
{% load crispy_forms_tags %} | ||
{% load static %} | ||
|
||
{% block stylesheets %} | ||
<link rel="stylesheet" href="{% static 'CSS/dog_owner_homepage.css' %}"> | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<div class="row row-cols-1 g-3 cards"> | ||
<div style="display: flex; flex-direction: row; flex-wrap: wrap"> | ||
{% for daycare in daycares %} | ||
<div class="card"> | ||
<img src="{{ daycare.get_daycare_primary_image_url }}" alt="{{ daycare.name }} image" class="card-img-top"> | ||
<div class="card-body"> | ||
<div style="display: flex; flex-direction: row"> | ||
<h5 class="card-title">{{ daycare.name }}</h5> | ||
|
||
<div class="row"> | ||
<form method='POST' action=''>{% csrf_token %} | ||
<div class="col-4" id="searchBoxArea"> | ||
<span>Search for daycare:</span> | ||
<div class="form-group"> | ||
{{ form.start_date|as_crispy_field }} | ||
</div> | ||
<div class="form-group"> | ||
{{ form.end_date|as_crispy_field }} | ||
</div> | ||
<div class="form-group"> | ||
{{ form.price_per_day|as_crispy_field }} | ||
</div> | ||
<div class="form-group"> | ||
{{ form.area|as_crispy_field }} | ||
</div> | ||
<div class="form-group"> | ||
{{ form.city|as_crispy_field }} | ||
</div> | ||
<div class="form-group"> | ||
{{ form.name|as_crispy_field }} | ||
</div> | ||
<input type="submit" value='Search' style="height:30px;width:70px"> | ||
</div> | ||
</form> | ||
<div class="col-10"> | ||
<span id="searchResult">Found {{ day_care_queryset.count }} results for your dog!</span> | ||
<div class="cards"> | ||
{% for daycare in day_care_queryset %} | ||
<div class="card"> | ||
<img src="{{ daycare.get_daycare_primary_image_url }}" alt="{{ daycare.name }} image" class="card-img-top"> | ||
<div class="card-body"> | ||
<div style="display: flex; flex-direction: row"> | ||
<h5 class="card-title">{{ daycare.name }}</h5> | ||
</div> | ||
<p class="card-text">{{ daycare.area | truncatechars:20 }}</p> | ||
<p class="card-text">{{ daycare.city | truncatechars:20 }}</p> | ||
<p class="card-text">{{ daycare.price_per_day | truncatechars:20 }}</p> | ||
<p class="card-text">{{ daycare.description | truncatechars:35}}</p> | ||
<a href="/daycare/{{ daycare.id }}" class="btn btn-warning">Daycare Profile</a> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
<p class="card-text">{{ daycare.description | truncatechars:250 }}</p> | ||
<a href="/daycare/{{ daycare.id }}" class="btn btn-primary">Daycare Profile</a> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.shortcuts import render | ||
from daycare.models import DayCare | ||
from daycare.forms import DayCareSearchForm | ||
|
||
|
||
@login_required() | ||
def dog_owner_home(request): | ||
title = 'Search for day-cares' | ||
form = DayCareSearchForm(request.POST or None) | ||
day_care_queryset = DayCare.objects.all() | ||
context = { | ||
'daycares': DayCare.objects.all(), | ||
'day_care_queryset': day_care_queryset, | ||
'form': form | ||
} | ||
if request.method == 'POST': | ||
if form.is_valid(): | ||
day_care_queryset = DayCare.objects.filter(area__startswith=form['area'].value(), | ||
city__icontains=form['city'].value(), | ||
name__icontains=form['name'].value(), | ||
price_per_day__lte=form['price_per_day'].value()) | ||
context = { | ||
'title': title, | ||
'day_care_queryset': day_care_queryset, | ||
'form': form | ||
} | ||
|
||
return render(request, 'dogowner/dog_owner_homepage.html', context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters