Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add search in dashboard

Artur Smęte4f707d78188
diff --git a/saleor/dashboard/search/__init__.py b/saleor/dashboard/search/__init__.py
new file mode 100644
index 0000000000..8b13789179
--- /dev/null
+++ b/saleor/dashboard/search/__init__.py
@@ -0,0 +1 @@
+
diff --git a/saleor/dashboard/search/forms.py b/saleor/dashboard/search/forms.py
new file mode 100644
index 0000000000..21551dc000
--- /dev/null
+++ b/saleor/dashboard/search/forms.py
@@ -0,0 +1,11 @@
+from ...search.backends import get_search_backend
+from ...search.forms import SearchForm
+
+
+class DashboardSearchForm(SearchForm):
+
+ def search(self, queryset_map=None):
+ backend = get_search_backend('dashboard')
+ query = self.cleaned_data['q']
+ results = backend.search(query, queryset_map=queryset_map)
+ return results
diff --git a/saleor/dashboard/search/urls.py b/saleor/dashboard/search/urls.py
new file mode 100644
index 0000000000..939a9ef8d6
--- /dev/null
+++ b/saleor/dashboard/search/urls.py
@@ -0,0 +1,7 @@
+from django.conf.urls import url
+
+from . import views
+
+urlpatterns = [
+ url(r'^$', views.search, name='search')
+]
diff --git a/saleor/dashboard/search/views.py b/saleor/dashboard/search/views.py
new file mode 100644
index 0000000000..d28eacbcfd
--- /dev/null
+++ b/saleor/dashboard/search/views.py
@@ -0,0 +1,31 @@
+from django.contrib.admin.views.decorators import staff_member_required
+from django.conf import settings
+from django.shortcuts import render
+
+from ...order.models import Order
+from ...product.models import Product
+from ...search.views import paginate_results
+from ...userprofile.models import User
+from .forms import DashboardSearchForm
+
+
+@staff_member_required
+def search(request):
+ form = DashboardSearchForm(data=request.GET or None)
+ query = ''
+ queryset_map = {
+ Product: Product.objects.prefetch_related('images'),
+ Order: Order.objects.prefetch_related('user'),
+ User: User.objects.all()}
+ if form.is_valid():
+ results = form.search(queryset_map=queryset_map)
+ page = paginate_results(results, request.GET, settings.PAGINATE_BY)
+ query = form.cleaned_data['q']
+ else:
+ page = []
+ ctx = {
+ 'form': form,
+ 'query': query,
+ 'results': page,
+ 'query_string': '?q=%s' % query}
+ return render(request, 'dashboard/search/results.html', ctx)
diff --git a/saleor/dashboard/urls.py b/saleor/dashboard/urls.py
index f94f78cf69..80f8c54a68 100644
--- a/saleor/dashboard/urls.py
+++ b/saleor/dashboard/urls.py
@@ -7,6 +7,7 @@ from .order.urls import urlpatterns as order_urls
from .payments.urls import urlpatterns as payments_urls
from .product.urls import urlpatterns as product_urls
from .discount.urls import urlpatterns as discount_urls
+from .search.urls import urlpatterns as search_urls
from .shipping.urls import urlpatterns as shipping_urls
@@ -18,5 +19,6 @@ urlpatterns = [
url(r'^payments/', include(payments_urls)),
url(r'^customers/', include(customer_urls)),
url(r'^discounts/', include(discount_urls)),
+ url(r'^search/', include(search_urls)),
url(r'^shipping/', include(shipping_urls)),
]
diff --git a/templates/dashboard/search/results.html b/templates/dashboard/search/results.html
new file mode 100644
index 0000000000..f438372adb
--- /dev/null
+++ b/templates/dashboard/search/results.html
@@ -0,0 +1,96 @@
+{% extends "dashboard/base.html" %}
+{% load i18n %}
+{% load prices_i18n %}
+{% load materializecss %}
+{% load staticfiles %}
+{% load product_first_image from product_images %}
+
+{% block title %}{% trans "Search results" %} - {{ block.super }}{% endblock %}
+
+{% block breadcrumbs %}
+ <ul class="breadcrumbs">
+ <li>{% trans "Search" %}</li>
+ </ul>
+{% endblock %}
+
+{% block menu_catalogue_class %} active{% endblock %}
+
+{% block content %}
+ <div class="row">
+ <div class="col s12">
+ <form class="hide-on-med-and-up row" method="get" action="{% url "dashboard:search" %}">
+ <input id="icon_prefix" type="text" class="validate col s10" placeholder="Search" name="q" value="{% if query %}{{ query }}{% endif %}">
+ <button class="btn-flat search-button col s2" type="submit">
+ <i class="material-icons left right">search</i>
+ </button>
+ </form>
+ <div class="row">
+ <div class="col s12">
+ <form class="row" method="get" action="{% url "dashboard:search" %}">
+ <input type="hidden" class="validate" name="q" value="{% if query %}{{ query }}{% endif %}">
+ </form>
+ </div>
+ </div>
+ {% if results %}
+ <ul class="collection list">
+ {% for result in results %}
+ <li class="collection-item avatar list-item">
+ {% if result.content_type == 'product_product' %}
+ {% with product=result %}
+ <img src="{% product_first_image product size="100x100" method="crop" %}" class="circle teal" alt="{{ product.name }}">
+ <a class="title" href="{% url 'dashboard:product-update' pk=product.pk %}">
+ <span class="list-item-name">{{ product.name }}</span>
+ </a>
+ <p class="list-item-price">
+ <small>{% gross product.price %}</small>
+ </p>
+ {% endwith %}
+ {% elif result.content_type == 'order_order' %}
+ <img src="{% static 'images/product-image-placeholder-small.png' %}" alt="" class="circle teal">
+ <a class="title" href="{% url 'dashboard:order-details' order_pk=result.pk %}">
+ <span class="list-item-name">Order #{{ result.pk }}</span>
+ </a>
+ <p class="list-item-price">
+ <small>Customer: {{ result.user_email }}</small>
+ <small>
+ Placed: {{ result.created }}
+ </small>
+
+ </p>
+ {% elif result.content_type == 'userprofile_user' %}
+ <img src="{% static 'images/product-image-placeholder-small.png' %}" alt="" class="circle teal">
+ <a class="title" href="{% url 'dashboard:order-details' order_pk=result.pk %}">
+ <span class="list-item-name">User {{ result.get_full_name }}</span>
+ </a>
+ <p class="list-item-price">
+ <small>Customer</small>
+ </p>
+ {% endif %}
+ </li>
+ {% endfor %}
+ </ul>
+ {% include "dashboard/includes/_pagination.html" with page_obj=results %}
+ {% elif query %}
+ <p>{% trans "No results for query " %}"{{ query }}".</p>
+ {% endif %}
+ </div>
+ </div>
+ <div id="modal-product-class" class="modal">
+ <div class="modal-content">
+ <form method="post" action=".">
+ {% csrf_token %}
+ <div class="row">
+ {{ form|materializecss }}
+ </div>
+
+ <div class="row">
+ <div class="col s12">
+ <button class="btn" type="submit">
+ <i class="material-icons left">create</i> {% trans "Create new" %}
+ </button>
+ </div>
+ </div>
+ </form>
+ </div>
+ </div>
+{% endblock %}