class CountSearch JSONResponseMixin View def render_to_response self c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class CountSearch(JSONResponseMixin, View):
def render_to_response(self, context, **response_kwargs):
return self.render_to_json_response(context, **response_kwargs)
def get(self, request, *args, **kwargs):
#http://127.0.0.1:5000/count/?type=1&districts=5,6,7&subways=1,2,3&year=2014&min_price=1000&max_price=4000
form = CountSearchForm(request.GET)
if form.is_valid():
data = form.cleaned_data
estates = EstateItem.objects\
.filter(apartments_type__id=data['flat_type'])\
.filter(district__id__in=data['districts'])\
.filter(metro__id__in=data['subways'])\
.filter(price_start_all=data['price_start_all'])\
.filter(price_end_all=data['price_end_all'])\
.filter(date_release__date_release__year=data['year'])
estate_count = estates.count()
json_response = {
'flats': 1,
'buildings': estate_count
}
context = {'count': json_response}
else:
context = {'errors': form.errors}
return self.render_to_response(context)