first pass at faceted search

pull/1/head
eric 2014-11-18 13:33:17 -05:00
parent c713f790b9
commit 591f06a267
5 changed files with 219 additions and 3 deletions

View File

@ -108,4 +108,11 @@ class ccinfo():
@property
def is_pd(self):
return self.license == 'PD-US'
def licence_value(facet):
print facet
if facet in FACET_LIST:
print LICENSE_LIST_ALL[FACET_LIST.index(facet)]
return LICENSE_LIST_ALL[FACET_LIST.index(facet)]
else:
return ''

73
core/facets.py Normal file
View File

@ -0,0 +1,73 @@
from django.db.models import get_model
from regluit.core import cc
class BaseFacet(object):
facet_name = 'all'
outer_facet = None
model = get_model('core', 'Work')
def __init__(self, outer_facet):
if outer_facet:
self.outer_facet = outer_facet
def _get_query_set(self):
if self.outer_facet:
return self.outer_facet.get_query_set()
else:
return self.model.objects.filter(editions__ebooks__isnull=False)
def get_query_set(self):
return self._get_query_set()
def get_facet_path(self):
if self.outer_facet:
return self.outer_facet.get_facet_path() + self.facet_name + '/'
else:
return self.facet_name + '/'
class FacetGroup(object):
pass
class NamedFacet(BaseFacet):
def __init__(self, outer_facet):
super(NamedFacet, self).__init__( outer_facet )
self.set_name()
class FormatFacetGroup(FacetGroup):
facets = ['pdf', 'epub', 'mobi']
def has_facet(self, facet_name):
return facet_name in self.facets
def get_facet_class(self, facet_name):
class FormatFacet(NamedFacet):
def set_name(self):
self.facet_name=facet_name
def get_query_set(self):
return self._get_query_set().filter(editions__ebooks__format=self.facet_name)
return FormatFacet
class LicenseFacetGroup(FacetGroup):
licenses = cc.LICENSE_LIST_ALL
facets = cc.FACET_LIST
def has_facet(self, facet_name):
return facet_name in self.facets
def get_facet_class(self, facet_name):
class LicenseFacet(NamedFacet):
def set_name(self):
self.facet_name=facet_name
def get_query_set(self):
return self._get_query_set().filter(editions__ebooks__rights=cc.licence_value(self.facet_name))
return LicenseFacet
facet_groups = [ FormatFacetGroup() , LicenseFacetGroup() ]
def get_facet(facet_name):
for facet_group in facet_groups:
if facet_group.has_facet(facet_name):
return facet_group.get_facet_class(facet_name)
return BaseFacet

View File

@ -0,0 +1,110 @@
{% extends "base.html" %}
{% load endless %}
{% load lang_utils %}
{% block title %} Creative Commons {{license}} Books {% if pub_lang %} in {{pub_lang|ez_lang_name}}{% endif %}{% endblock %}
{% block extra_css %}
<link type="text/css" rel="stylesheet" href="/static/css/supporter_layout.css" />
<link type="text/css" rel="stylesheet" href="/static/css/book_list.css" />
<link type="text/css" rel="stylesheet" href="/static/css/book_panel2.css" />
<link type="text/css" rel="stylesheet" href="/static/css/lists.css" />
{% endblock %}
{% block extra_head %}
<script type="text/javascript" src="/static/js/wishlist.js"></script>
<script type="text/javascript" src="{{ jquery_ui_home }}"></script>
<script type="text/javascript" src="/static/js/greenpanel.js"></script>
<script type="text/javascript" src="/static/js/toggle.js"></script>
<script type="text/javascript" src="/static/js/hijax_unglued.js"></script>
<script type="text/javascript" src="/static/js/tabs.js"></script>
{% endblock %}
{% block topsection %}
<div id="locationhash">#1</div>
<div id="js-topsection">
<div class="js-main">
<div class="js-topnews">
<div class="js-topnews1">
<div class="js-topnews2">
<div class="js-topnews3">
<div class="user-block">
<div id="user-block1">
<div id="block-intro-text"><br /><span class="special-user-name">Ready to Read</span></div>
</div>
<div class="user-block24"><p style="font-size: larger;"><span class="user-short-info">These {% if pub_lang %}{{ pub_lang|ez_lang_name }} language {% endif %}{% if cc.is_cc %}<a href="http://creativecommons.org/">Creative Commons</a>{% endif %} {% if license %}<a href="{{cc.url}}">{{ license }}</a>{% endif %} {%if cc.is_pd %}ebooks are ready to read - they belong to all of us!{% else %}licensed ebooks are ready to read - the people who created them want you to read and share them.{% endif %}</span></p>
{% if license %}<p style="font-size: smaller;"><a href="{{cc.url}}"><img src="{{cc.badge}}" alt="badge for {{license}}" style="float:left;padding:0.5em" /></a> {{cc.description}}
</p>{% endif %}
</div>
<div class="user-block3">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block content %}
<div id="main-container">
<div class="js-main">
<div id="js-leftcol">
{% include "explore.html" %}
</div>
<div id="js-maincol-fr">
<div class="js-maincol-inner">
<div id="content-block">
<div class="content-block-heading unglued" id="tabs">
<ul class="book-list-view">
<li>View As:</li>
<li class="view-list">
<a href="#" id="toggle-list">
<img src="/static/images/booklist/view-list.png" alt="view list" title="view list" height="21" width="24" />
</a>
</li>
<li class="view-list">
<a href="#" id="toggle-panel">
<img src="/static/images/booklist/view-icon.png" alt="view icon" title="view icon" height="22" width="22" />
</a>
</li>
</ul>
</div>
<div id="content-block-content">
<div id="books-go-here">
{% ifequal work_list.count 0 %}
There aren't any {{ pub_lang|ez_lang_name }} {% if cc.is_cc %}Creative Commons{% endif %} {{ license }} works in this list. Why don't you add some? Use the "More" tab on a work, there are links there for adding ebooks.
{% else %}
{% lazy_paginate 20 work_list using "work_list" %}
{% for work in work_list %}
<div class="{% cycle 'row1' 'row2' %}">
{% with work.googlebooks_id as googlebooks_id %}
{% include "book_panel.html" %}
{% endwith %}
</div>
{% endfor %}
<div class="pagination content-block-heading tabs-{{ activetab }}">
{% get_pages %}
{% for page in pages %}
<a href="{{ page.path }}#1" class="endless_page_link">{{ page.number }}</a>
{% endfor %}
</div>
{% endifequal %}
</div>
</div>
</div>
{% if request.user.libpref %}
<div id="libtools">
<p>for libraries...</p>
<form method="GET" action="{% url faceted_list_marc path %}">
{% include 'marc_form.html' %}
<input type="submit" name="submit" value="download MARC" id="submit">
</form>
</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -38,6 +38,7 @@ from regluit.frontend.views import (
send_to_kindle,
LibModeView,
DownloadView,
FacetedView,
)
urlpatterns = patterns(
@ -73,6 +74,9 @@ urlpatterns = patterns(
url(r"^campaigns/(?P<facet>\w*)/marc/$", CampaignListView.as_view(send_marc=True), name='campaign_list_marc'),
url(r"^lists/(?P<facet>\w*)$", WorkListView.as_view(), name='work_list'),
url(r"^lists/(?P<facet>\w*)/marc/$", WorkListView.as_view(send_marc=True), name='work_list_marc'),
url(r"^free/(?P<path>[^\s]*)/marc/$", FacetedView.as_view(send_marc=True), name='faceted_list_marc'),
url(r"^free/(?P<path>[^\s]*)/$", FacetedView.as_view(), name='faceted_list'),
url(r"^free/$", FacetedView.as_view(), name='free'),
url(r"^pid/all/(?P<pubname>\d+)$", ByPubView.as_view(), name='bypubname_list'),
url(r"^pid/(?P<facet>\w*)/(?P<pubname>\d+)$", ByPubView.as_view(), name='bypubname_list'),
url(r"^pid/(?P<facet>\w*)/(?P<pubname>\d+)/marc/$", ByPubView.as_view(send_marc=True), name='bypubname_list_marc'),
@ -157,6 +161,7 @@ urlpatterns = patterns(
url(r"^send_to_kindle/(?P<work_id>\d+)/(?P<javascript>\d)/$", "send_to_kindle", name="send_to_kindle"),
url(r"^marc/$", direct_to_template, {'template': 'marc.html'}, name="marc"),
url(r"^accounts/edit/marc_config/$", login_required(LibModeView.as_view()), name="marc_config"),
)
if settings.DEBUG:

View File

@ -79,6 +79,7 @@ from regluit.core.search import gluejar_search
from regluit.core.signals import supporter_message
from regluit.core.tasks import send_mail_task, emit_notifications, watermark_acq
from regluit.core.parameters import *
from regluit.core.facets import get_facet
from regluit.frontend.forms import (
UserData,
@ -802,7 +803,7 @@ class FilterableListView(ListView):
return qs_marc_records(self.request, qs=self.object_list)
else:
return super(FilterableListView,self).render_to_response(context, **response_kwargs)
recommended_user = User.objects.filter( username=settings.UNGLUEIT_RECOMMENDED_USERNAME)
class WorkListView(FilterableListView):
@ -811,7 +812,7 @@ class WorkListView(FilterableListView):
max_works=100000
def get_queryset_all(self):
facet = self.kwargs['facet']
facet = self.kwargs.get('facet', None)
if (facet == 'popular'):
return models.Work.objects.exclude(num_wishes=0).order_by('-num_wishes', 'id')
elif (facet == 'recommended'):
@ -847,6 +848,26 @@ class WorkListView(FilterableListView):
return context
class FacetedView(FilterableListView):
template_name = "faceted_list.html"
def get_queryset_all(self):
facet_path = self.kwargs.get('path', '')
facets = facet_path.strip('/').split('/')
self.vertex = None
for facet in facets:
self.vertex = get_facet(facet)(self.vertex)
return self.vertex.get_query_set().distinct()
def get_context_data(self, **kwargs):
context = super(FacetedView, self).get_context_data(**kwargs)
facet = self.kwargs.get('facet','all')
qs=self.get_queryset()
context['activetab'] = "#1"
context['tab_override'] = 'tabs-1'
context['path'] = self.vertex.get_facet_path()
return context
class ByPubView(WorkListView):
template_name = "bypub_list.html"
context_object_name = "work_list"