def get_details(request): """ fetch the details for the given the beerid """ if not request.user.is_authenticated(): return redirect('auth_login') if not request.is_ajax(): return redirect('index') if request.POST.get('beerid') is None: raise Http404('Beer ID not provided') else: beerid = request.POST['beerid'] endpoint = 'beer/{}'.format(beerid) params = {'withBreweries': 'Y'} data = api_request(endpoint, params) if data.get('status') == 'success': response_data = { 'success': True, 'data': data, 'details': render_to_string( 'search/detail_modal_details.html', {'data': data['data'], 'STATIC_URL': settings.STATIC_URL} ), } return HttpResponse(json.dumps(response_data), mimetype="application/json") else: raise Http404('api request failed')
def index(request): """ Main beer search view. Given GET param `q`, make an api request for all beers that match that query string. """ # Default context = {} # Handle the search request if request.REQUEST.get('q'): # Add the query value to the context context.update({'q': request.REQUEST['q']}) endpoint = 'search' params = { 'q': request.REQUEST['q'].lower(), 'type': 'beer', 'withBreweries': 'Y' } if request.REQUEST.get('p'): params.update({'p': request.REQUEST['p']}) data = api_request(endpoint, params) form = CellarItemForm() context.update({ 'count': data.get('totalResults', 0), 'results': data.get('data', []), 'remaining': data.get('remaining', "?"), 'currentPage': data.get('currentPage', 1), 'numberOfPages': data.get('numberOfPages', 1), 'form': form, }) if request.REQUEST.get('p', 1) > 1 and request.is_ajax(): return render(request, 'search/search_item.html', context) else: return render(request, 'search/index.html', context)