def _chapter_next_previous(document, chapter, dir='next'): '''Returns the next or previous data object from the OPF''' toc = document.get_toc() item = toc.find_item_by_id(chapter.idref) if dir == 'next': target_item = toc.find_next_item(item) else: target_item = toc.find_previous_item(item) if target_item is None: return None object = get_file_by_item(target_item, document) return object
def view(request, title, key, first=False, resume=False): '''If we view just a document, we want to either see our last chapter, or see the first item in the <opf:spine>, as required by the epub specification.''' log.debug("Looking up title %s, key %s" % (title, key)) document = _get_document(request, title, key) # If we got 'None' from get_document with an anonymous user, then prompt them # to login; this is probably just a bookmark with an unauthenticated user if document is None and request.user.is_anonymous(): return HttpResponseRedirect(reverse('user_signin')) if not request.user.is_anonymous(): uprofile = request.user.get_profile() last_chapter_read = document.get_last_chapter_read(request.user) else: last_chapter_read = None uprofile = None if resume and last_chapter_read is not None: chapter = last_chapter_read elif not first and uprofile and uprofile.open_to_last_chapter and last_chapter_read: chapter = last_chapter_read else: try: toc = document.get_toc() first_item = toc.first_item() chapter = get_file_by_item(first_item, document) except InvalidEpubException: # We got some kind of catastrophic error while trying to # parse this document message = _('There was a problem reading the metadata for this document.') return view_chapter(request, title, key, None, message=message) if chapter is None: log.error('Could not find an item with the id of %s' % first_item) raise Http404 if first: log.debug("Forcing first chapter") # Force an HTTP redirect so we get a clean URL but go to the correct chapter ID return HttpResponseRedirect(reverse('view_chapter', kwargs={'title':document.safe_title(), 'key': document.id, 'chapter_id':chapter.filename})) return view_chapter(request, title, key, None, chapter=chapter, document=document)