Merge branch 'ry'
commit
c05b63a0a0
|
@ -201,13 +201,13 @@ class WorkListView(ListView):
|
|||
def get_queryset(self):
|
||||
facet = self.kwargs['facet']
|
||||
if (facet == 'popular'):
|
||||
return models.Work.objects.filter(wishlists__isnull=False).distinct().annotate(wished=Count('wishlists')).order_by('-wished')
|
||||
return models.Work.objects.filter(wishlists__isnull=False).distinct().annotate(wished=Count('wishlists')).order_by('-wished', 'id')
|
||||
elif (facet == 'recommended'):
|
||||
return models.Work.objects.filter(wishlists__user=recommended_user)
|
||||
elif (facet == 'new'):
|
||||
return models.Work.objects.filter(wishlists__isnull=False).distinct().order_by('-created')
|
||||
return models.Work.objects.filter(wishlists__isnull=False).distinct().order_by('-created', 'id')
|
||||
else:
|
||||
return models.Work.objects.all().order_by('-created')
|
||||
return models.Work.objects.all().order_by('-created', 'id')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(WorkListView, self).get_context_data(**kwargs)
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
from regluit.core import librarything, bookloader
|
||||
from regluit.core import librarything, bookloader, models
|
||||
import itertools
|
||||
import django
|
||||
|
||||
from django.db.models import Q, F
|
||||
from regluit.core import bookloader
|
||||
import warnings
|
||||
import datetime
|
||||
|
||||
def ry_lt_books():
|
||||
"""return parsing of rdhyee's LibraryThing collection"""
|
||||
lt = librarything.LibraryThing('rdhyee')
|
||||
|
@ -25,4 +30,84 @@ def ry_wish_list_equal_loadable_lt_books():
|
|||
editions = editions_for_lt(ry_lt_books())
|
||||
# assume only one user -- and that we have run a LT book loading process for that user
|
||||
ry = django.contrib.auth.models.User.objects.all()[0]
|
||||
return set([ed.work for ed in filter(None, editions)]) == set(ry.wishlist.works.all())
|
||||
return set([ed.work for ed in filter(None, editions)]) == set(ry.wishlist.works.all())
|
||||
|
||||
def clear_works_editions_ebooks():
|
||||
models.Ebook.objects.all().delete()
|
||||
models.Work.objects.all().delete()
|
||||
models.Edition.objects.all().delete()
|
||||
|
||||
def load_gutenberg_edition(title, gutenberg_etext_id, ol_work_id, url, format, license, lang, publication_date):
|
||||
|
||||
# let's start with instantiating the relevant Work and Edition if they don't already exist
|
||||
|
||||
works = models.Work.objects.filter(Q(identifiers__type='olwk') & Q(identifiers__value=ol_work_id))
|
||||
|
||||
try:
|
||||
work = models.Identifier.objects.get(type='olwk',value=ol_work_id).work
|
||||
except models.Identifier.DoesNotExist: # create a new work
|
||||
work = models.Work()
|
||||
work.title = title
|
||||
work.language = lang
|
||||
work.openlibrary_lookup = None
|
||||
work.save()
|
||||
|
||||
work_id = models.Identifier.get_or_add(type='olwk',value=ol_work_id, work=work)
|
||||
|
||||
# Now pull out any existing Gutenberg editions tied to the work with the proper Gutenberg ID
|
||||
try:
|
||||
edition = models.Identifier.objects.get( type='gtbg', value=gutenberg_etext_id ).edition
|
||||
except models.Identifier.DoesNotExist:
|
||||
edition = models.Edition()
|
||||
edition.title = title
|
||||
edition.work = work
|
||||
|
||||
edition.save()
|
||||
edition_id = models.Identifier.get_or_add(type='gtbg',value=gutenberg_etext_id, edition=edition, work=work)
|
||||
|
||||
# check to see whether the Edition hasn't already been loaded first
|
||||
# search by url
|
||||
ebooks = models.Ebook.objects.filter(url=url)
|
||||
|
||||
# format: what's the controlled vocab? -- from Google -- alternative would be mimetype
|
||||
|
||||
if len(ebooks):
|
||||
ebook = ebooks[0]
|
||||
elif len(ebooks) == 0: # need to create new ebook
|
||||
ebook = models.Ebook()
|
||||
|
||||
if len(ebooks) > 1:
|
||||
warnings.warn("There is more than one Ebook matching url {0}".format(url))
|
||||
|
||||
|
||||
ebook.format = format
|
||||
ebook.provider = 'gutenberg'
|
||||
ebook.url = url
|
||||
ebook.rights = license
|
||||
|
||||
# is an Ebook instantiable without a corresponding Edition? (No, I think)
|
||||
|
||||
ebook.edition = edition
|
||||
ebook.save()
|
||||
|
||||
return ebook
|
||||
|
||||
# get associated info from OL
|
||||
# book_loader.add_openlibrary(work)
|
||||
|
||||
|
||||
def load_moby_dick():
|
||||
"""Let's try this out for Moby Dick"""
|
||||
|
||||
title = "Moby Dick"
|
||||
ol_work_id = "/works/OL102749W"
|
||||
gutenberg_etext_id = 2701
|
||||
epub_url = "http://www.gutenberg.org/cache/epub/2701/pg2701.epub"
|
||||
license = 'http://www.gutenberg.org/license'
|
||||
lang = 'en'
|
||||
format = 'epub'
|
||||
publication_date = datetime.datetime(2001,7,1)
|
||||
|
||||
result = load_gutenberg_edition(title, gutenberg_etext_id, ol_work_id, epub_url, format, license, lang, publication_date)
|
||||
return result
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
from regluit.core import models
|
||||
from django.db.models import Count
|
||||
|
||||
from itertools import izip
|
||||
|
||||
def list_popular():
|
||||
"""Compare calculating popular works using QuerySets + distinct() and order_by() with an alternate approach """
|
||||
|
||||
w1 = models.Work.objects.filter(wishlists__isnull=False). \
|
||||
distinct().annotate(wished=Count('wishlists')).order_by('-wished', 'id')
|
||||
|
||||
# create a list of tuples of Works + the wishlist count, filter by non-zero wishlist counts, sort the list by descending
|
||||
# number of wishlists + Work.id and then blot out the wishlist count
|
||||
|
||||
w0 = map (lambda x: x[0],
|
||||
sorted(
|
||||
filter(lambda x: x[1] > 0,
|
||||
[(w, w.wishlists.count()) for w in models.Work.objects.all()]
|
||||
) ,
|
||||
key=lambda x: (-x[1],x[0].id)
|
||||
)
|
||||
)
|
||||
|
||||
print w1.count()
|
||||
print len(w0)
|
||||
print list(w1.all()) == w0
|
||||
|
||||
print "difference: ", filter(lambda item: item[1][0] != item[1][1], enumerate(izip(w0,w1)))
|
||||
|
||||
def list_new():
|
||||
"""Compare calculating new works using QuerySets + distinct() and order_by() with an alternate approach """
|
||||
w1 = models.Work.objects.filter(wishlists__isnull=False).distinct().order_by('-created', 'id')
|
||||
w0 = [w for w in models.Work.objects.order_by('-created', 'id') if w.wishlists.count()]
|
||||
|
||||
print w1.count()
|
||||
print len(w0)
|
||||
print list(w1.all()) == w0
|
||||
|
||||
print "difference: ", filter(lambda item: item[1][0] != item[1][1], enumerate(izip(w0,w1)))
|
Loading…
Reference in New Issue