Example #1
0
    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)
Example #2
0
def dashboard_search(request):
    """View for Dashboard search page.

    It validates form and filter the product table based on search item
    and store the list in the result and send it in the html context.
    """
    # Initial  context values.
    result = None
    feedback = None

    # Creating form object.
    form = DashboardSearchForm()
    form2 = DashboardFilterForm()

    # If method is ajax
    if request.is_ajax():
        # Creating form object.
        form = DashboardSearchForm(request.GET)

        if form.is_valid():
            # Reading search_item value from cleaned_data.
            search_item = form.cleaned_data.get('search_item')

            # Filtering fields in Product table based on search item.
            # Storing the list in variable.
            product = Product.objects.filter(
                Q(name__icontains=search_item) |
                Q(price__icontains=search_item) |
                Q(product_type__icontains=search_item) |
                Q(site_reference__icontains=search_item) |
                Q(description__icontains=search_item))

            if product:
                # Storing the product list in result.
                result = product

            # If product does not exist.
            else:
                # Make result to none.
                result = None

                # Send feedback.
                feedback = "Search in Scrap page "

        # Storing the result in the cache.
        cache.set('result_product', result, None)

        # 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)

    # If method POST.
    if request.method == 'POST':
        # Creating form object.
        form = DashboardSearchForm()

        # Contexts to send in html.
        ctx = {'title': 'Dashboard page', 'dashboard': 'active', 'form': form}
        return render(request, "dashboard/dashboard.html", ctx)
Example #3
0
    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)