예제 #1
0
    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')
예제 #2
0
    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')
예제 #3
0
파일: views.py 프로젝트: ajayvahan/scrapper
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)
예제 #4
0
 def get(self, request, *args, **kwargs):
     """Get method for the ScrapView class."""
     form = ScrapSearchForm()
     # creating a dictionary for the context
     ctx = {'form': form, 'title': 'Scrap page', 'scrap': 'active'}
     return render(request, self.template_name, ctx)