Skip to content

Commit

Permalink
Added search app.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdutton committed Jan 25, 2012
1 parent 2ddf4ee commit d51f871
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 0 deletions.
Empty file added voyager/search/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions voyager/search/forms.py
@@ -0,0 +1,4 @@
from django import forms

class SearchForm(forms.Form):
query = forms.CharField()
1 change: 1 addition & 0 deletions voyager/search/templates/search/base.html
@@ -0,0 +1 @@
{% extends "base.html" %}
32 changes: 32 additions & 0 deletions voyager/search/templates/search/index.html
@@ -0,0 +1,32 @@
{% extends "search/base.html" %}

{% block title %}Search{% if query %}: {{ query }}{% endif %}{% endblock %}

{% block content %}
<h1>Search{% if query %}: {{ query }}{% endif %}</h1>
<div class="text-content">
<form method="get" action=".">
<p>
{{ form.query.label_tag }}
{{ form.query }}
<input type="submit" value="Search"/>
</p>
</form>

{% if query and not results %}
<p>Your search returned no results</p>
{% endif %}
{% if query and results %}
<ol style="list-style:none; padding-left:0;">{% for result in results %}{% if result.thing and result.label %}
<li style="margin:5px"><a href="{% url doc-generic %}?uri={{ result.thing }}" style="vertical-align:middle;">
<div style="width:100px; display:inline-block; text-align:center; vertical-align:middle">
{% if result.depiction %}
<img src="{% url resized-image %}?url={{ result.depiction }}&amp;width=100&amp;height=100" alt=""/>
{% endif %}
</div>
{{ result.label }}
</a></li>
{% endif %}{% endfor %}</ol>
{% endif %}
</div>
{% endblock %}
7 changes: 7 additions & 0 deletions voyager/search/urls.py
@@ -0,0 +1,7 @@
from django.conf.urls.defaults import patterns, url

from . import views

urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
)
48 changes: 48 additions & 0 deletions voyager/search/views.py
@@ -0,0 +1,48 @@
import rdflib

from django_conneg.views import HTMLView, JSONView
from humfrey.utils.views import EndpointView

from . import forms

class IndexView(HTMLView, JSONView, EndpointView):
_json_indent = 2

query = """\
SELECT ?thing
(SAMPLE(?literal_) AS ?literal)
(SAMPLE(?score_) AS ?score)
(SAMPLE(?label_) AS ?label)
(SAMPLE(?depiction_) AS ?depiction) WHERE {
{
SELECT DISTINCT ?thing ?literal_ ?score_ WHERE {
(?literal_ ?score_) pf:textMatch %s .
?thing ?x ?literal_
} LIMIT 200
} .
OPTIONAL { ?thing rdfs:label ?label_ } .
OPTIONAL { ?thing (crm:P138i_has_representation|^crm:P138_represents)+ ?depiction_ } .
OPTIONAL { ?thing a crm:E38_Image . ?thing crm:empty{0} ?depiction_ } .
} GROUP BY ?thing"""

def simplify(self, value):
if isinstance(value, rdflib.Literal):
return unicode(value)
elif isinstance(value, rdflib.URIRef):
return unicode(value)
elif isinstance(value, rdflib.BNode):
return None
else:
return super(IndexView, self).simplify(value)

def get(self, request):
form = forms.SearchForm(request.GET or None)
context = {'form': form,
'results': None,
'query': request.GET.get('query')}

if form.is_valid():
query = self.query % rdflib.Literal(form.cleaned_data['query']).n3()
context['results'] = self.endpoint.query(query)

return self.render(request, context, 'search/index')
1 change: 1 addition & 0 deletions voyager/settings.py
Expand Up @@ -15,6 +15,7 @@
INSTALLED_APPS += (
'voyager.core',
'voyager.places',
'voyager.search',
'django_hosts',
)

Expand Down
2 changes: 2 additions & 0 deletions voyager/urls/data.py
Expand Up @@ -18,6 +18,8 @@
url(r'^doc.+$', desc_views.DocView.as_view(), name='doc'),
url(r'^doc/$', desc_views.DocView.as_view(), name='doc-generic'),
url(r'^desc/$', desc_views.DescView.as_view(), name='desc'),

url(r'^search/$', include('voyager.search.urls', 'search')),

url(r'^objects/$', core_views.ObjectCategoryView.as_view(), name='claros-objects'),
url(r'^objects/(?P<ptype>[a-z-]+)/$', core_views.ObjectView.as_view(), name='claros-objects-detail'),
Expand Down

0 comments on commit d51f871

Please sign in to comment.