Merge pull request #646 from Gluejar/id-facets

Id facets
pull/1/head
Raymond Yee 2016-11-14 11:21:40 -08:00 committed by GitHub
commit 405af3b2e7
9 changed files with 84 additions and 19 deletions

View File

@ -86,8 +86,10 @@ class BaseFacet(object):
class FacetGroup(object):
# a FacetGroup should implement title, facets, has_facet(self, facet_name) and get_facet_class(self, facet_name)
def has_facet(self, facet_name):
return facet_name in self.facets
def get_facets(self):
for facet_name in self.facets:
yield self.get_facet_class(facet_name)(None)
@ -104,7 +106,7 @@ class FormatFacetGroup(FacetGroup):
super(FacetGroup,self).__init__()
self.title = 'Format'
self.facets = ['pdf', 'epub', 'mobi']
self.label = '{} is ...'.format(self.title)
def get_facet_class(self, facet_name):
class FormatFacet(NamedFacet):
@ -126,6 +128,37 @@ class FormatFacetGroup(FacetGroup):
def description(self):
return "These eBooks available in %s format." % self.facet_name
return FormatFacet
idtitles = {'doab': 'indexed in DOAB', 'gtbg':'available in Project Gutenberg'}
idlabels = {'doab': 'DOAB', 'gtbg':'Project Gutenberg'}
class IdFacetGroup(FacetGroup):
def __init__(self):
super(FacetGroup,self).__init__()
self.title = 'Collection'
self.facets = idtitles.keys()
self.label = 'Included in ...'
def get_facet_class(self, facet_name):
class IdFacet(NamedFacet):
def set_name(self):
self.facet_name=facet_name
def id_filter(query_set):
return query_set.filter(identifiers__type=facet_name)
model_filters = {}
def get_query_set(self):
return self._get_query_set().filter(identifiers__type=self.facet_name)
def template(self):
return 'facets/id.html'
@property
def label(self):
return idlabels[self.facet_name]
@property
def title(self):
return idtitles[self.facet_name]
@property
def description(self):
return "These eBooks are {}.".format(idtitles[self.facet_name])
return IdFacet
class LicenseFacetGroup(FacetGroup):
@ -134,6 +167,7 @@ class LicenseFacetGroup(FacetGroup):
self.title = 'License'
self.licenses = cc.LICENSE_LIST_ALL
self.facets = cc.FACET_LIST
self.label = '{} is ...'.format(self.title)
def get_facet_class(self, facet_name):
@ -175,6 +209,7 @@ class KeywordFacetGroup(FacetGroup):
self.title = 'Keyword'
# make facets in TOPKW available for display
self.facets = [('kw.%s' % kw) for kw in TOPKW]
self.label = '{} is ...'.format(self.title)
def has_facet(self, facet_name):
@ -209,7 +244,8 @@ class PublisherFacetGroup(FacetGroup):
self.title = 'Publisher'
# don't display facets
self.facets = []
self.label = 'Published by ...'
def has_facet(self, facet_name):
# recognize any facet_name that starts with "pub." as a valid facet name
@ -249,7 +285,7 @@ class PublisherFacetGroup(FacetGroup):
return PublisherFacet
# order of groups in facet_groups determines order of display on /free/
facet_groups = [KeywordFacetGroup(), FormatFacetGroup(), LicenseFacetGroup(), PublisherFacetGroup()]
facet_groups = [KeywordFacetGroup(), FormatFacetGroup(), LicenseFacetGroup(), PublisherFacetGroup(), IdFacetGroup()]
def get_facet(facet_name):
for facet_group in facet_groups:

View File

@ -45,9 +45,17 @@ from regluit.core.parameters import (
logger = logging.getLogger(__name__)
good_providers = ('Internet Archive', 'Unglue.it', 'Github', 'OAPEN Library')
def id_for(obj, type):
if not obj.pk:
return ''
try:
return obj.identifiers.filter(type=type)[0].value
except IndexError:
return ''
class Identifier(models.Model):
# olib, ltwk, goog, gdrd, thng, isbn, oclc, olwk, doab, gute, glue, doi
# olib, ltwk, goog, gdrd, thng, isbn, oclc, olwk, doab, gtbg, glue, doi
type = models.CharField(max_length=4, null=False)
value = models.CharField(max_length=250, null=False)
work = models.ForeignKey("Work", related_name="identifiers", null=False)
@ -110,6 +118,17 @@ class Work(models.Model):
def __init__(self, *args, **kwargs):
self._last_campaign = None
super(Work, self).__init__(*args, **kwargs)
def id_for(self, type):
return id_for(self, type)
@property
def gtbg(self):
return id_for(self, 'gtbg')
@property
def doab(self):
return id_for(self, 'doab')
@property
def googlebooks_id(self):
@ -149,10 +168,7 @@ class Work(models.Model):
@property
def librarything_id(self):
try:
return self.identifiers.filter(type='ltwk')[0].value
except IndexError:
return ''
return self.id_for('ltwk')
@property
def librarything_url(self):
@ -160,10 +176,7 @@ class Work(models.Model):
@property
def openlibrary_id(self):
try:
return self.identifiers.filter(type='olwk')[0].value
except IndexError:
return ''
return self.id_for('olwk')
@property
def openlibrary_url(self):
@ -820,12 +833,7 @@ class Edition(models.Model):
return regluit.core.isbn.convert_13_to_10(self.isbn_13)
def id_for(self, type):
if not self.pk:
return ''
try:
return self.identifiers.filter(type=type)[0].value
except IndexError:
return ''
return id_for(self, type)
@property
def isbn_13(self):

View File

@ -0,0 +1,11 @@
<div>
<p style="">
<img src="/static/images/{{ facet.facet_name }}32.png" height="32" alt="{{ facet.facet_name }}" title="{{ facet.facet_name }}" />
{% if facet.facet_name == 'gtbg' %}
These books are included in <a href="https://www.gutenberg.org">Project Gutenberg</a>. They are archived and can be improved at <a href="https://www.gitenberg.org">GITenberg</a>.
{% endif %}
{% if facet.facet_name == 'doab' %}
These books are included in the <a href="http://doabooks.org/">Directory of Open Access Books</a>. This means that they have been peer-reviewed and are of interest to scholars.
{% endif %}
</p>
</div>

View File

@ -9,7 +9,7 @@
{% for group in vertex.get_other_groups %}
<ul class="menu level2">
{% for facet in group.get_facets %}
{% if forloop.first %}<li class="first"><span>{{ group.title }} is ...</span></li>{% endif %}
{% if forloop.first %}<li class="first"><span>{{ group.label }}</span></li>{% endif %}
<li><a href="{% url 'faceted_list' path %}{{facet.facet_name}}/?{% if setkw %}setkw={{setkw}}&amp;{% endif %}order_by={{order_by}}" title="{{ facet.title }}"><span>{{ facet.label }}</span></a></li>
{% endfor %}
</ul>

View File

@ -289,6 +289,16 @@
<a href="{% url 'work' work_rel.to_work.id %}">{{ work_rel.to_work }}</a> is a {{ work_rel.relation }} of this work.
</p>
{% endfor %}
{% if work.doab %}
<p>
This book is included in <a href="{% url 'faceted_list' 'id/doab' %}?order_by=popular">DOAB</a>.
</p>
{% endif %}
{% if work.gtbg %}
<p>
This book is included in <a href="{% url 'faceted_list' 'id/gtbg' %}?order_by=popular">Project Gutenberg</a>.
</p>
{% endif %}
</div>
</div>
</div>

BIN
static/images/doab.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

BIN
static/images/doab32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
static/images/gtbg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

BIN
static/images/gtbg32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB