-
Notifications
You must be signed in to change notification settings - Fork 4
/
dj_search_url.py
85 lines (58 loc) · 1.8 KB
/
dj_search_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
import os
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
# Register database schemes in URLs.
urlparse.uses_netloc.append("elasticsearch")
urlparse.uses_netloc.append("solr")
urlparse.uses_netloc.append("whoosh")
urlparse.uses_netloc.append("simple")
DEFAULT_ENV = "SEARCH_URL"
SCHEMES = {
"elasticsearch": "haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine",
"solr": "haystack.backends.solr_backend.SolrEngine",
"whoosh": "haystack.backends.whoosh_backend.WhooshEngine",
"simple": "haystack.backends.simple_backend.SimpleEngine",
}
USES_URL = ["solr"]
USES_INDEX = ["elasticsearch"]
USES_PATH = ["whoosh"]
def config(env=DEFAULT_ENV, default=None):
"""Returns configured HAYSTACK_CONNECTIONS dictionary from ``env``."""
config = {}
s = os.environ.get(env, default)
if s:
config = parse(s)
return config
def parse(url):
"""Parses a search URL."""
config = {}
url = urlparse.urlparse(url)
# Remove query strings.
path = url.path[1:]
path = path.split('?', 2)[0]
if url.scheme in SCHEMES:
config["ENGINE"] = SCHEMES[url.scheme]
if url.scheme in USES_URL:
config["URL"] = urlparse.urlunparse(("http",) + url[1:])
if url.scheme in USES_INDEX:
if path.endswith("/"):
path = path[:-1]
split = path.rsplit("/", 1)
if len(split) > 1:
path = split[:-1]
index = split[-1]
else:
path = ""
index = split[0]
config.update({
"URL": urlparse.urlunparse(("http",) + url[1:2] + (path,) + url[3:]),
"INDEX_NAME": index,
})
if url.scheme in USES_PATH:
config.update({
"PATH": path,
})
return config