def get(self, request, *args, **kwargs): """Filtering the results based on price, site choice etc.""" result_error = "" # storing the search results in 'results' and applying filters to it' result = cache.get('product_result') if request.is_ajax(): form2 = DashboardFilterForm(request.GET) if form2.is_valid(): filter_data = form2.cleaned_data if result: # sorting price of the result in ascending order if filter_data['price_sort'] == 'LH': result = result.order_by('price') # sorting price of the result in descending order elif filter_data['price_sort'] == 'HL': result = result.order_by('-price') # filtering the results for both amazon and flipkart, # i.e when user check both flipkart and amzon if filter_data['amazon'] and filter_data['flipkart']: pass # filtering the results which are only from amazon elif filter_data['amazon']: result = result.filter( Q(site_reference__icontains='amazon')) # filtering the results which are only from flipkart elif filter_data['flipkart']: result = result.filter( Q(site_reference__icontains='flipkart')) # when no results are found, display the error message, # set the result error flag to true else: # make result to none. result = None result_error = True else: logger = logging.getLogger(constants.LOGGER) logger.error("Wrong data") # create the context ctx = {'result': result, 'dashboard_result_error': result_error} html = render_to_string( 'home/result.html', ctx, context_instance=RequestContext(request)) # encoding it to json json_data = {'result': html} # sending the json response return HttpResponse( json.dumps(json_data), content_type='application/json') # Contexts to send in html. ctx = { 'title': 'Dashboard page', 'dashboard': 'active', 'form2': form2} return render(request, self.template_name, ctx)
def dashboard_filter(request): """View for dashboard filter.""" # Creating form object. form = DashboardSearchForm() form2 = DashboardFilterForm() # If method is ajax if request.is_ajax(): # Getting the searched results from cache. result = cache.get('result_product') # Creating form object. form2 = DashboardFilterForm(request.GET) feedback = None if form2.is_valid(): filter_data = form2.cleaned_data if result: # Filtering with respect to price. if filter_data['price_sort'] == 'LH': result = result.order_by('price') elif filter_data['price_sort'] == 'HL': result = result.order_by('-price') # Filtering with respect to site preferrence. if filter_data['amazon'] and filter_data['flipkart']: pass elif filter_data['amazon']: result = result.filter( Q(site_reference__icontains='amazon')) elif filter_data['flipkart']: result = result.filter( Q(site_reference__icontains='flipkart')) else: # Make result to none. result = None # Send feedback. feedback = "Search in Scrap page " # Context to send in html. ctx = {'result': result, 'dashboard_feedback': feedback} # Passing the context to html and rendering to string. # Store it in result variable. result_html = render_to_string( 'dashboard/result.html', ctx, context_instance=RequestContext(request)) # dictionary to pass in json.dumps. json_data = {'result': result_html} # Send HttpResponse using json.dumps. return HttpResponse( json.dumps(json_data), content_type='application/json') # Contexts to send in html. ctx = {'title': 'Dashboard page', 'dashboard': 'active', 'form': form, 'form2': form2} return render(request, "dashboard/dashboard.html", ctx)
def get(self, request, *args, **kwargs): """Get method for DashboardView class.""" products = [] result_error = "" if request.is_ajax(): # search form form = DashboardSearchForm(request.GET) # filter form form2 = DashboardFilterForm(request.GET) if form.is_valid(): data = form.cleaned_data search_item = data['search'] # getting the products whose name or price or site reference or # product type or description matches with the search item products = Product.objects.filter( Q(name__icontains=search_item) | Q(price__icontains=search_item) | Q(site_reference__icontains=search_item) | Q(product_type__icontains=search_item) | Q(description__icontains=search_item)) # when resuts are found initial 'resutl' variable if products: result = products # otherwise initialize error message else: result = None result_error = True # storing the searched result in the cache, # in order to use it in the DashboardFilterView cache.set('product_result', result, None) # create a dictionary to hold the result and the error feedback ctx = { 'result': result, 'dashboard_result_error': result_error } html = render_to_string( 'home/result.html', ctx, context_instance=RequestContext(request)) # encoding it to json json_data = json.dumps({'result': html}) # sending the json response return HttpResponse(json_data, content_type='application/json') else: form = DashboardSearchForm() form2 = DashboardFilterForm() # create a dictionary for the context ctx = { 'title': 'Dashboard', 'dashboard': 'active', 'form': form, 'result': products, 'result_error': result_error, 'form2': form2 } return render(request, self.template_name, ctx)
def get(self, request, *args, **kwargs): """Filtering the results based on price, site choice etc.""" result_error = "" # storing the search results in 'results' and applying filters to it' result = cache.get('product_result') if request.is_ajax(): form2 = DashboardFilterForm(request.GET) if form2.is_valid(): filter_data = form2.cleaned_data if result: # sorting price of the result in ascending order if filter_data['price_sort'] == 'LH': result = result.order_by('price') # sorting price of the result in descending order elif filter_data['price_sort'] == 'HL': result = result.order_by('-price') # filtering the results for both amazon and flipkart, # i.e when user check both flipkart and amzon if filter_data['amazon'] and filter_data['flipkart']: pass # filtering the results which are only from amazon elif filter_data['amazon']: result = result.filter( Q(site_reference__icontains='amazon')) # filtering the results which are only from flipkart elif filter_data['flipkart']: result = result.filter( Q(site_reference__icontains='flipkart')) # when no results are found, display the error message, # set the result error flag to true else: # make result to none. result = None result_error = True else: logger = logging.getLogger(constants.LOGGER) logger.error("Wrong data") # create the context ctx = {'result': result, 'dashboard_result_error': result_error} html = render_to_string('home/result.html', ctx, context_instance=RequestContext(request)) # encoding it to json json_data = {'result': html} # sending the json response return HttpResponse(json.dumps(json_data), content_type='application/json') # Contexts to send in html. ctx = { 'title': 'Dashboard page', 'dashboard': 'active', 'form2': form2 } return render(request, self.template_name, ctx)