def tag(request, tag_name): """ Show all references for single tag and related tags. """ if request.method == 'DELETE': return tag_delete(request, tag_name) elif request.method == 'PUT': return tag_update(request, tag_name) t = get_object_or_404(Tag, tag=tag_name, user=request.user) related_refs = get_refs_with_tag(request.user, t) clean_refs = [] related_tags = [] texts = [] ref_paths = [] ids = [] for ref in related_refs: related_tags.append(get_tags_for_ref(request.user, ref)) ids.append(ref.id) resource = ref.resource try: pybook_ref = library.get(resource).reference(ref.pretty_ref()) text = pybook_ref.text() clean_ref = pybook_ref.pretty() except Exception: log.warning("Exception for " + ref.pretty_ref() + " = " + traceback.format_exc()) text, clean_ref = ('unknown', ref.pretty_ref()) clean_refs.append(clean_ref) texts.append(text) ref_paths.append(reverse('resource', args=(resource, ref.pretty_ref()))) related_refs_n_tags = zip(ids, clean_refs, ref_paths, related_tags, texts) return render_to_response('tags/tag_detail.html', {'tag': t, 'related_refs_n_tags': related_refs_n_tags}, context_instance=RequestContext(request))
def get_resource(request, res_name, ref_str=None, highlights=None): """ Top-level function for displaying a textbites resource. If a reference is given within that resource, then it shows that particular scope. This handler is also used to front-end searches, which are redirected to another handler. @param Request may be None in the case where search is calling back to here, in order to display a reference. @param ref_str from the path, if given. The reference within the resource to be retrieved like 'John 5:1' @param highlights are reference strings which should be highlighted, which should be 'sub-references' to make sense. """ try: resource = library.get(res_name) if ref_str: ref_obj = resource.reference(ref_str) else: ref_obj = resource.top_reference() # see if they want a search if request: query = request.GET.get('q', None) if query: return lib_resource_search(request, resource, res_name, ref_obj, query, ref_str) return render_resource(request, res_name, ref_str, highlights) except Exception: log.warning("Problem getting resource: %s", traceback.format_exc()) raise Http404
def get_export_tsv(user): out = StringIO() for ref in Reference.objects.filter(user=user_pk(user)): offset_start = ref.offset_start offset_end = ref.offset_end resource = str(ref.resource) ref_str = str(ref.reference) # if offsets aren't known, look them up if not offset_start or not offset_end: try: pybook_ref = library.get(resource).reference(ref_str) except: log.info("Couldn't find refernce: %s", ref_str) continue try: offset_start, offset_end = pybook_ref.indices() except: log.warning("Problem with index: %s %s", ref_str, traceback.format_exc()) print >> out, '\t'.join([ ref.tag.tag, resource, ref_str, str(offset_start), str(offset_end) ]) return out.getvalue()
def tagref_create(request, tag_name): """ Create a single tag reference. The resource must exist and the reference must be valid, but if the tag doesn't exist, then then it is created. """ try: # resource MUST exist res_str = request.POST['resource'].strip() resource = library.get(res_str) # reference must be valid ref_str = request.POST['reference'] ref = resource.reference(ref_str) except: log.info("User provided bad resource or reference. (res: %s; ref: )", res_str, ref_str) raise Http404 # if tag name doesn't exist, create it try: t = get_exact_tag(request.user, tag_name) except: # TODO: move to models t = Tag(tag=tag_name, user=request.user) t.save() log.debug("Saving new tag %s %s %s %s", ref_str, tag_name, t, ref.pretty()) # TODO: move to models new_ref = Reference(tag=t, resource=res_str, reference=ref.pretty(), offset_start=ref.indices().start, offset_end=ref.indices().end, user=request.user) new_ref.save() return HttpResponseRedirect(reverse('tagref_detail', args=(tag_name, str(new_ref.id))));
def test_library(self): r = Resource() self.assertEqual(len(Library.list()), 0) Library.add("r1", r) self.assertEqual(len(Library.list()), 1) r1 = Library.get("r1") self.assertEqual(r1, r)
def load_library(resources): """ Load textual resources given by command-line args else all default. """ if len(resources) > 0: import os for arg in resources: f = os.path.realpath( os.path.join(os.path.dirname(library.__file__), "data/{}.bible.json".format(arg.upper()))) library.load(f) else: # load everything we find library.load_resources() print "Loading into library:", library.list() # map to lowercase resource names return dict([(k.lower(), library.get(k)) for k in library.list()])
def load_library(resources): """ Load textual resources given by command-line args else all default. """ if len(resources) > 0: import os for arg in resources: f = os.path.realpath( os.path.join(os.path.dirname(library.__file__), "data/{}.bible.json".format(arg.upper()))) library.load(f) else: # load everything we find library.load_resources() print "Loading into library:", library.list() # map to lowercase resource names return dict([(k.lower(),library.get(k)) for k in library.list()])
def get_export_tsv(user): out = StringIO() for ref in Reference.objects.filter(user=user_pk(user)): offset_start = ref.offset_start offset_end = ref.offset_end resource = str(ref.resource) ref_str = str(ref.reference) # if offsets aren't known, look them up if not offset_start or not offset_end: try: pybook_ref = library.get(resource).reference(ref_str) except: log.info("Couldn't find refernce: %s", ref_str) continue try: offset_start, offset_end = pybook_ref.indices() except: log.warning("Problem with index: %s %s", ref_str, traceback.format_exc()) print >> out, "\t".join([ref.tag.tag, resource, ref_str, str(offset_start), str(offset_end)]) return out.getvalue()
def render_resource(request, res_name, ref_str=None, highlights=None): """ Render a textbites resource. Uses one of several templates depending on what kind of a reference it is based on its attributes (e.g. book, chapter, line). Doesn't check for query parameters for search, see get_resource. """ try: resource = library.get(res_name) if ref_str: ref_obj = resource.reference(ref_str) else: ref_obj = resource.top_reference() # inspect if the reference has children # should return None if it doesn't have them. children = ref_obj.children() # inspect if the children have text # show child text IF the children don't have children show_child_text = (not children[0].children() if children else False) # get text if possible try: text = ref_obj.text() except: text = None # requesting context is legal if there are no children, AND, it # is a text-bearing reference if text and not children: context_size = safe_int(request.GET.get('ctx', 0)) if request else None # get the amount of context needed if context_size and context_size > 0: # get reference try: context = ref_obj.context(context_size) # set the highlight on our center line return render_resource(request, res_name, context.pretty(), [ref_obj.pretty()]) except Exception as e: log.warning("Problem rendering with context: %s", e) # navigation references parent_ref, previous_ref, next_ref = None, None, None res_path = (reverse('resource', kwargs={'res_name': res_name})) if ref_obj: def rel_url(rel_fct): # TODO: could make this into a reverse() return res_path + rel_fct().path() if rel_fct() else None parent_ref = rel_url(ref_obj.parent) previous_ref = rel_url(ref_obj.previous) next_ref = rel_url(ref_obj.next) context = { 'resource_name': res_name, 'title': ref_obj.pretty(), 'resource_path': res_path, 'parent_ref': parent_ref, 'previous_ref': previous_ref, 'next_ref': next_ref, 'children': children, 'highlights': highlights, 'text': text, 'sub_ref': ref_str if ref_str else "" } # determine which template to use if children: if show_child_text: return render_to_response( 'texts/ref_text_list.html', context, context_instance=RequestContext(request)) else: return render_to_response( 'texts/ref_index.html', context, context_instance=RequestContext(request)) else: return render_to_response('texts/ref_detail.html', context, context_instance=RequestContext(request)) except Exception: log.warning("Problem rendering resource: %s", traceback.format_exc()) raise Http404
def render_resource(request, res_name, ref_str=None, highlights=None): """ Render a textbites resource. Uses one of several templates depending on what kind of a reference it is based on its attributes (e.g. book, chapter, line). Doesn't check for query parameters for search, see get_resource. """ try: resource = library.get(res_name) if ref_str: ref_obj = resource.reference(ref_str) else: ref_obj = resource.top_reference() # inspect if the reference has children # should return None if it doesn't have them. children = ref_obj.children() # inspect if the children have text # show child text IF the children don't have children show_child_text = (not children[0].children() if children else False) # get text if possible try: text = ref_obj.text() except: text = None # requesting context is legal if there are no children, AND, it # is a text-bearing reference if text and not children: context_size = safe_int(request.GET.get('ctx', 0)) if request else None # get the amount of context needed if context_size and context_size > 0: # get reference try: context = ref_obj.context(context_size) # set the highlight on our center line return render_resource( request, res_name, context.pretty(), [ref_obj.pretty()]) except Exception as e: log.warning("Problem rendering with context: %s", e) # navigation references parent_ref, previous_ref, next_ref = None, None, None res_path = (reverse('resource', kwargs={'res_name':res_name})) if ref_obj: def rel_url(rel_fct): # TODO: could make this into a reverse() return res_path + rel_fct().path() if rel_fct() else None parent_ref = rel_url(ref_obj.parent) previous_ref = rel_url(ref_obj.previous) next_ref = rel_url(ref_obj.next) context = { 'resource_name': res_name, 'title': ref_obj.pretty(), 'resource_path': res_path, 'parent_ref': parent_ref, 'previous_ref': previous_ref, 'next_ref': next_ref, 'children': children, 'highlights': highlights, 'text': text, 'sub_ref': ref_str if ref_str else "" } # determine which template to use if children: if show_child_text: return render_to_response('texts/ref_text_list.html', context, context_instance=RequestContext(request)) else: return render_to_response('texts/ref_index.html', context, context_instance=RequestContext(request)) else: return render_to_response('texts/ref_detail.html', context, context_instance=RequestContext(request)) except Exception: log.warning("Problem rendering resource: %s", traceback.format_exc()) raise Http404