Skip to content

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

Add docstrings

Jakub Draganek5ca05751f77d
diff --git a/saleor/search/backends/picker.py b/saleor/search/backends/picker.py
index bcf99bbe3b..c431c3309a 100644
--- a/saleor/search/backends/picker.py
+++ b/saleor/search/backends/picker.py
@@ -6,16 +6,27 @@ from . import (elasticsearch, elasticsearch_dashboard, postgresql,
def elastic_configured():
+ '''Evaluate elasticsearch configuration status'''
return settings.ES_URL and not settings.PREFER_DB_SEARCH
def pick_backend():
+ '''Get storefront search function of configured backend
+
+ :rtype: callable with one argument of type str
+
+ '''
if elastic_configured():
return elasticsearch.search
return postgresql.search
def pick_dashboard_backend():
+ '''Get dashboard search function of configured backend
+
+ :rtype: callable with one argument of type str
+
+ '''
if elastic_configured():
return elasticsearch_dashboard.search
return postgresql_dashboard.search
diff --git a/saleor/search/backends/postgresql.py b/saleor/search/backends/postgresql.py
index 16974d5a9e..cd21076210 100644
--- a/saleor/search/backends/postgresql.py
+++ b/saleor/search/backends/postgresql.py
@@ -4,6 +4,16 @@ from django.contrib.postgres.search import TrigramSimilarity
def search(phrase):
+ '''Storefront full text product search
+
+ Fuzzy storefront search that is resistant to small typing errors made
+ by user. Name is matched using trigram similarity, description uses
+ standard postgres full text search.
+
+ Args:
+ phrase (str): searched phrase
+
+ '''
name_sim = TrigramSimilarity('name', phrase)
published = Q(is_published=True)
ft_in_description = Q(description__search=phrase)
diff --git a/saleor/search/backends/postgresql_dashboard.py b/saleor/search/backends/postgresql_dashboard.py
index c6ccbe412e..1859dd1982 100644
--- a/saleor/search/backends/postgresql_dashboard.py
+++ b/saleor/search/backends/postgresql_dashboard.py
@@ -6,11 +6,18 @@ from ...userprofile.models import User
def search_products(phrase):
+ '''Dashboard full text product search'''
sv = SearchVector('name', 'description')
return Product.objects.annotate(search=sv).filter(search=phrase)
def search_orders(phrase):
+ '''Dashboard full text order search
+
+ When phrase is convertable to int, no full text search is performed,
+ just order with according id is looked up.
+
+ '''
try:
order_id = int(phrase.strip())
return Order.objects.filter(id=order_id)
@@ -24,6 +31,7 @@ def search_orders(phrase):
def search_users(phrase):
+ '''Dashboard full text user search'''
sv = SearchVector('email',
'default_billing_address__first_name',
'default_billing_address__last_name')
@@ -31,6 +39,14 @@ def search_users(phrase):
def search(phrase):
+ '''Dashboard full text postgres products, orders and users search
+
+ Composes independent search querysets into dictionary result.
+
+ Args:
+ phrase (str): searched phrase
+
+ '''
return {'products': search_products(phrase),
'orders': search_orders(phrase),
'users': search_users(phrase)}
diff --git a/tests/test_postgresql_search.py b/tests/test_postgresql_search.py
index 9ee215b6cf..ae10adef0a 100644
--- a/tests/test_postgresql_search.py
+++ b/tests/test_postgresql_search.py
@@ -36,6 +36,7 @@ def named_products(default_category, product_class):
def search_storefront(client, phrase):
+ '''Execute storefront search on client mathing phrase'''
resp = client.get(reverse('search:search'), {'q': phrase})
return [prod for prod, _ in resp.context['results'].object_list]
@@ -67,6 +68,7 @@ def test_storefront_filter_published_products(client, named_products):
def search_dashboard(client, phrase):
+ '''Execute dashboard search on client mathing phrase'''
response = client.get(reverse('dashboard:search'), {'q': phrase})
assert response.context['query'] in phrase
context = response.context