Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ddf4ee
commit d51f871
Showing
8 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from django import forms | ||
|
||
class SearchForm(forms.Form): | ||
query = forms.CharField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends "base.html" %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }}&width=100&height=100" alt=""/> | ||
{% endif %} | ||
</div> | ||
{{ result.label }} | ||
</a></li> | ||
{% endif %}{% endfor %}</ol> | ||
{% endif %} | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
INSTALLED_APPS += ( | ||
'voyager.core', | ||
'voyager.places', | ||
'voyager.search', | ||
'django_hosts', | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters