Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silage - db driven search backend #425

Merged
merged 2 commits into from
Oct 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions geonode/layers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,19 @@ def keyword_csv(self):

class Meta:
abstract = True


def add_bbox_query(q, bbox):
'''modify the queryset q to limit to the provided bbox

bbox - 4 tuple of floats representing x0,x1,y0,y1
returns the modified query
'''
q = q.filter(bbox_x0__gte=bbox[0])
q = q.filter(bbox_x1__lte=bbox[1])
q = q.filter(bbox_y0__gte=bbox[2])
return q.filter(bbox_y1__lte=bbox[3])


class Layer(ResourceBase):
"""
Expand Down
4 changes: 2 additions & 2 deletions geonode/layers/templates/layers/layer_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ <h3>{% trans "Selected Data" %}</h3>

{% block extra_script %}
<script type="text/html" id="searchResultsTemplate">
<%#rows%>
<%#results%>
<tr>
<td><a href="<%detail%>"><%title%></a></td>
<td><a href="<%owner_detail%>"><%owner%></a></td>
<td><abbr class="timeago" title="<%last_modified%>"><%last_modified%></abbr></td>
<td><input class="asset-selector pull-right" data-name="<%name%>" data-type="layer" type="checkbox"></td>
</tr>
<%/rows%>
<%/results%>
</script>
<script type="text/html" id="searchNoResultsTemplate">
<tr>
Expand Down
2 changes: 1 addition & 1 deletion geonode/layers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def test_search(self):
def test_search_api(self):
'''/data/search/api -> Test accessing the data search api JSON'''
c = Client()
response = c.get('/data/search/api')
response = c.get('/search/api/data')
self.failUnlessEqual(response.status_code, 200)

def test_describe_data(self):
Expand Down
1 change: 0 additions & 1 deletion geonode/layers/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
url(r'^tag/(?P<slug>[-\w]+?)/$', 'layer_tag', name='layer_browse_tag'),
url(r'^acls/?$', 'layer_acls', name='layer_acls'),
url(r'^search/?$', 'layer_search_page', name='layer_search_page'),
url(r'^search/api/?$', 'layer_search', name='layer_search_api'),
url(r'^upload$', 'layer_upload', name='layer_upload'),
url(r'^download$', 'layer_batch_download', name='layer_batch_download'),
url(r'^(?P<layername>[^/]*)$', 'layer_detail', name="layer_detail"),
Expand Down
10 changes: 9 additions & 1 deletion geonode/maps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from django.core.urlresolvers import reverse

from geonode.layers.models import Layer, TopicCategory
from geonode.maps.signals import map_changed_signal
from geonode.security.models import PermissionLevelMixin
from geonode.security.models import AUTHENTICATED_USERS, ANONYMOUS_USERS
from geonode.utils import GXPMapBase
Expand Down Expand Up @@ -109,7 +110,9 @@ def layers(self):

@property
def local_layers(self):
return True
layer_names = MapLayer.objects.filter(map__id=self.id).values('name')
return Layer.objects.filter(typename__in=layer_names) | \
Layer.objects.filter(name__in=layer_names)

def json(self, layer_filter):
map_layers = MapLayer.objects.filter(map=self.id)
Expand Down Expand Up @@ -169,6 +172,7 @@ def source_for(layer):
return conf["sources"][layer["source"]]

layers = [l for l in conf["map"]["layers"]]
layer_names = set([l.typename for l in self.local_layers])

for layer in self.layer_set.all():
layer.delete()
Expand All @@ -180,6 +184,10 @@ def source_for(layer):
layer_from_viewer_config(
MapLayer, layer, source_for(layer), ordering
))

if layer_names != set([l.typename for l in self.local_layers]):
map_changed_signal.send_robust(sender=self,what_changed='layers')

self.save()

def keyword_list(self):
Expand Down
3 changes: 3 additions & 0 deletions geonode/maps/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.dispatch import Signal

map_changed_signal = Signal(providing_args=['what_changed'])
4 changes: 2 additions & 2 deletions geonode/maps/templates/maps/map_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ <h2 class="page-title">{% trans "Search Maps" %}</h2>

{% block extra_script %}
<script type="text/html" id="searchResultsTemplate">
<%#rows%>
<%#results%>
<tr>
<td><a href="<%detail%>"><%title%></a></td>
<td><a href="<%owner_detail%>"><%owner%></a></td>
<td><abbr class="timeago" title="<%last_modified%>"><%last_modified%></abbr></td>
</tr>
<%/rows%>
<%/results%>
</script>
<script type="text/html" id="searchNoResultsTemplate">
<tr>
Expand Down
6 changes: 3 additions & 3 deletions geonode/maps/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,16 @@ def test_maps_search(self):
map_id_2 = int(response['Location'].split('/')[-1])
c.logout()

url = '/maps/search/api/?'
url = '/search/api/maps'

# Test GET method
response = c.get(url, {'q': '', 'start': 1, 'limit': '', 'sort': '', 'dir': ''}, content_type="text/json")
response = c.get(url, {'q': '', 'start': 1}, content_type="text/json")
self.assertEquals(response.status_code,200)
response_dict = json.loads(response.content)
self.assertEquals(response_dict['success'], True)

# Test POST method
response = c.post(url, {'q': '', 'start': 1, 'limit': '', 'sort': '', 'dir': ''}, content_type="text/json")
response = c.post(url, {'q': '', 'start': 1}, content_type="text/json")
self.assertEquals(response.status_code,200)
response_dict = json.loads(response.content)
self.assertEquals(response_dict['success'], True)
Expand Down
1 change: 0 additions & 1 deletion geonode/maps/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
url(r'^category/(?P<slug>[-\w]+?)/$', 'maps_category', name='maps_browse_category'),
url(r'^tag/(?P<slug>[-\w]+?)/$', 'maps_tag', name='maps_browse_tag'),
url(r'^search/?$', 'maps_search_page', name='maps_search'),
url(r'^search/api/?$', 'maps_search', name='maps_search_api'),
url(r'^new$', 'new_map', name="new_map"),
url(r'^new/data$', 'new_map_json', name='new_map_json'),
url(r'^(?P<mapid>\d+)$', 'map_detail', name='map_detail'),
Expand Down
Empty file added geonode/search/__init__.py
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions geonode/search/backends/silage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


66 changes: 66 additions & 0 deletions geonode/search/backends/silage/extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#########################################################################
#
# Copyright (C) 2012 OpenPlans
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################

from geonode.people.models import Contact
from geonode.layers.models import Layer
from geonode.maps.models import Map
from geonode.search.backends.silage.util import resolve_extension

from django.conf import settings
import re

date_fmt = lambda dt: dt.isoformat()
USER_DISPLAY = 'User'
MAP_DISPLAY = 'Map'
LAYER_DISPLAY = 'Layer'

# settings API
search_config = getattr(settings,'SIMPLE_SEARCH_SETTINGS', {})

exclude_patterns = search_config.get('layer_exclusions',[])

exclude_regex = [ re.compile(e) for e in exclude_patterns ]

process_results = resolve_extension('process_search_results')
if process_results is None:
process_results = lambda r: r

owner_query = resolve_extension('owner_query')
if not owner_query:
owner_query = lambda q: Contact.objects.filter()

owner_query_fields = resolve_extension('owner_query_fields') or []

layer_query = resolve_extension('layer_query')
if not layer_query:
layer_query = lambda q: Layer.objects.filter()

map_query = resolve_extension('map_query')
if not map_query:
map_query = lambda q: Map.objects.filter()

display_names = resolve_extension('display_names')
if display_names:
USER_DISPLAY = display_names.get('user')
MAP_DISPLAY = display_names.get('map')
LAYER_DISPLAY = display_names.get('layer')

owner_rank_rules = resolve_extension('owner_rank_rules')
if not owner_rank_rules:
owner_rank_rules = lambda: []
Loading