def viewer(request):
    work_uri = request.GET.get('work')
    parts = Work.get(work_uri).hasPart()
    part_uri = request.GET.get('part', parts[0].uri)
    cantos = WorkPart.get(part_uri).hasPart()
    canto_uri  = request.GET.get('canto')
    if canto_uri not in [str(c.uri) for c in cantos]:
        canto_uri = cantos[0].uri
    lines = WorkPart.get(canto_uri).hasPart()
    line_uri = request.GET.get('line')
    if line_uri not in [str(l.uri) for l in lines]:
        line_uri = lines[0].uri
    docs = []
    for text in Work.get(work_uri).hasTextOf():
        docs.extend(text.isTextIn())
    doc_uri = request.GET.get('doc', docs[0].uri)
    response_dict = {
        'part': {'selected': part_uri, 'items': [p.json() for p in parts]},
        'canto':{'selected': canto_uri,'items':[c.json() for c in cantos]},
        'line': {'selected': line_uri, 'items': [l.json() for l in lines]},
        'doc': {'selected': doc_uri, 'items': [d.json() for d in docs]}
    }
    if request.GET.get('format') == 'json':
        return HttpResponse(json.dumps(response_dict))
    response_dict['work_uri'] = work_uri
    return render_to_response(
        'rdfviewer/viewer.html', response_dict, 
        context_instance=RequestContext(request))
def line(request):
    part = request.GET.get('part', 'Inferno')
    canto = request.GET.get('canto', '1')
    line = request.GET.get('line', '1')
    line_uri = ns_dict['drc']['%s-%s-%s'] % (part, canto, line)
    data = {'witnesses': []}
    for l in WorkPart.get(line_uri).hasTextOf():
        url = l.hasTranscript()
        response = urllib2.urlopen(url)
        transcript = response.read()
        tokens = get_tokens(transcript)
        data['witnesses'].append(
            {'id': str(l.uri).split('-')[-1], 'tokens':tokens})
    request = urllib2.Request(
        'http://gregor.middell.net/collatex/api/collate',
        json.dumps(data),
        {'Content-Type': 'application/json', 'Accept': 'application/json'}
    )
    response = urllib2.urlopen(request)
    return HttpResponse(response.read(), mimetype='application/json')
def page(request):
    page_uri = request.GET.get('page')
    if page_uri:
        page = Page.get(uri=page_uri)
        func = request.GET.get('func')
        if func == 'next':
            page = page.next()
        elif func == 'prev':
            page = page.prev()
    else:
        line = WorkPart.get(request.GET.get('line'))
        doc = Document.get(request.GET.get('doc'))
        page = Page.get(line=line, doc=doc)
    url = page.hasTranscript()
    response = urllib2.urlopen(url)
    transcript = response.read()
    return HttpResponse(json.dumps({
        'page': page.uri,
        'transcript_url': url,
        'transcript': transcript,
        'image': page.hasImage()
    }))