def post(self, request, *args, **kwargs): """Post method for ScrapView class.""" form = ScrapSearchForm(request.POST) if form.is_valid(): data = form.cleaned_data search_item = data['search'] # create an objec fo Scrap class obj = Scrap() product_list = None feedback = None # scrap the product based on user's choice of the website # 1 is for amazon and 2 is for flipkart if data['site_choice'] == '1': try: product_list = obj.amazon(search_item) # when no product is found, # set the feedback with error message if not product_list: # no items found feedback = True except: # no items found feedback = True else: try: # import pdb # pdb.set_trace() product_list = obj.flipkart(search_item) # when no product is found, # set the feedback with error message if not product_list: # no items found feedback = True except: # no items found feedback = True # creating the context ctx = {'result': product_list, 'scrap_result_error': feedback} html = render_to_string('home/result.html', ctx, context_instance=RequestContext(request)) json_data = json.dumps({'result': html}) return HttpResponse(json_data, content_type='application/json')
def post(self, request, *args, **kwargs): """Post method for ScrapView class.""" form = ScrapSearchForm(request.POST) if form.is_valid(): data = form.cleaned_data search_item = data['search'] # create an objec fo Scrap class obj = Scrap() product_list = None feedback = None # scrap the product based on user's choice of the website # 1 is for amazon and 2 is for flipkart if data['site_choice'] == '1': try: product_list = obj.amazon(search_item) # when no product is found, # set the feedback with error message if not product_list: # no items found feedback = True except: # no items found feedback = True else: try: # import pdb # pdb.set_trace() product_list = obj.flipkart(search_item) # when no product is found, # set the feedback with error message if not product_list: # no items found feedback = True except: # no items found feedback = True # creating the context ctx = {'result': product_list, 'scrap_result_error': feedback} html = render_to_string( 'home/result.html', ctx, context_instance=RequestContext(request)) json_data = json.dumps({'result': html}) return HttpResponse(json_data, content_type='application/json')
def scrap(request): """View for Scrap page. It validates the form and based on site choice it call the respective method from Scrap class and store the returned values in result to send in html context. """ # If the method is POST. if request.method == 'POST': # Creating form object. form = ScrapSearchForm(request.POST) # Initial context value. result = None if form.is_valid(): # Reading values from cleaned_data. search_item = form.cleaned_data.get("search_item") site_choice = form.cleaned_data.get("site_choice") # Instantiating Scrap class. scrap = Scrap() # Setting intial value. feedback = None # If site choice is 'F'. if site_choice == 'F': # Calling flipkart method in Scrap. # Store the return value in result. result = scrap.flipkart(search_item) if not result: feedback = "Search item not found" else: # Calling amazon method in Scrap. # Store the return value in result. result = scrap.amazon(search_item) if not result: feedback = "Search item not found" # Context to send in html. ctx = {'result': result, 'scrap_feedback': feedback} # Passing the context to html and rendering to string. # Store it in result variable. result = render_to_string( 'dashboard/result.html', ctx, context_instance=RequestContext(request)) # dictionary to pass in json.dumps. json_data = {'result': result} # Send HttpResponse using json.dumps. return HttpResponse( json.dumps(json_data), content_type='application/json') # If method is GET. elif request.method == 'GET': # Creating form object. form = ScrapSearchForm() result = None feedback = None # Context to send in html. ctx = {'title': 'Scrap page', 'scrap': 'active', 'form': form, 'result': result, 'feedback': feedback} return render(request, "dashboard/scrap.html", ctx)