Example #1
0
def metadata(request):
    """
    This view serves the metadata window.
    """
    w = int(request.GET["id"])
    ent = get_by_ismi_id(w)
    title = get_name(ent)

    def adder(clss, l):
        for a in clss.objects.all():
            if a.show != clss.NEVER_SHOW:
                k, vals = get_keyvals(a, w)
                if a.show == clss.ALWAYS_SHOW:
                    for val in vals:
                        l.append((k, val))
                else:  # show if set
                    if len(vals) == 1:
                        val, = vals
                        if val == NO_DATA_MSG:
                            continue
                    for val in vals:
                        l.append((k, val))

    md = []
    adder(AttDisplaySetting, md)
    adder(RelDisplaySetting, md)

    data = {"title": title, "md": md}
    return render(request, "templates/metadata.html", data)
Example #2
0
def manuscript(request, ms_id):
    """The view for displaying a specific manuscript."""
    if request.user.is_anonymous():
        u = User.objects.get(pk=-1)  # select the "AnonymousUser" object
    else:
        u = request.user

    # We break the response down into a couple steps here.
    # First, check the number of manuscripts groups they have permissions to. This is just to
    # be able to exit with a 404 if they're trying to access a manuscript in a group that doesn't exist.

    # Then, we check against the manuscripts themselves. This allows us to catch the user and redirect them
    # to a log in page if they need to log in to see the MSS.
    manuscript_groups = get_objects_for_user(u, "imageserve.view_manuscript_group")
    manuscripts = Manuscript.objects.filter(manuscriptgroup__in=manuscript_groups).distinct()
    if not manuscripts.exists():
        raise Http404

    has_permission = manuscripts.filter(id=ms_id)

    if manuscripts and not has_permission.exists():
        return redirect("/login/?next={0}".format(request.path))

    m = has_permission[0]
    curr_wit = request.GET.get("curr_wit")
    try:
        curr_wit = int(curr_wit)
    except (ValueError, TypeError):
        curr_wit = -1

    # witnesses = None
    titles = None
    ismi_data = False
    wits = PageNotAnInteger

    if m.ismi_id is not None:
        codex_title = get_name(get_by_ismi_id(m.ismi_id))
        ismi_data = True
        wits = m.witnesses
        if wits:
            if not curr_wit in wits:
                curr_wit = -1
            titles = [(w, get_rel(w, "is_exemplar_of")[0], get_att(w, "folios")) for w in wits]
    else:
        codex_title = m.directory

    data = {
        "ms_title": codex_title,
        "witnesses": bool(wits),
        "divaserve_url": DIVASERVE_URL,
        "iipserver_url": IIPSERVER_URL,
        "image_root": conf.IMG_DIR,
        "curr_wit": curr_wit,
        "ms_name": m.directory,
        "titles": titles,
        "ismi_data": ismi_data,
        "ms_id": ms_id,
        "ismi_id": m.ismi_id,
        "path": quote_plus(request.get_full_path()),
        "num_files": m.num_files,
    }
    return render(request, "templates/diva.html", data)
Example #3
0
 def handle(self, *args, **options):
     all_rels = {}
     ocs = []
     for r in RelDisplaySetting.objects.all():
         print u'retrieving {0} relations...'.format(r.name)
         u = urlopen(JSON_INTERFACE + "method=get_rels&rel_name={0}".format(r.name))
         rels = loads(u.read())['rels']
         u.close()
         d = dict((e.get('src_id'), []) for e in rels)
         for rel in rels:
             for key in ['src_oc', 'tar_oc']:
                 if key in rel:
                     if not rel[key] in ocs:
                         ocs.append(rel[key])
             d[rel.get('src_id')].append((rel.get('tar_id'), rel.get('tar_oc')))
         all_rels[r.name] = d
     objs = {}
     for oc in ocs:
         print u'retrieving {0} objects...'.format(oc)
         u = urlopen(JSON_INTERFACE + "method=get_ents&oc={0}".format(oc))
         ents = loads(u.read())['ents']
         u.close()
         objs.update(dict((e['id'], get_name(e)) for e in ents))
         del ents
     print 'compiling connections...'
     mapper = {}
     for m in Manuscript.objects.all():
         for w in m.witnesses:
             mapper[w] = {}
             for r in RelDisplaySetting.objects.all():
                 if r.on_ent == u'self':
                     want_id = w
                 else:
                     print r.name, r.on_ent
                     if w in all_rels[r.on_ent].keys():
                         (want_id, _), = all_rels[r.on_ent][w]
                     else:
                         matches = [k for k,v in all_rels[r.on_ent].iteritems() if w in
                                     [i for i,c in v]]
                         if matches:
                             want_id, = matches
                         else:
                             mapper[w][r.name] = [NO_DATA_MSG]
                             continue
                 if want_id in all_rels[r.name].keys():
                     matches = [i for i,c in all_rels[r.name][want_id]]
                 else:
                     matches = [k for k,v in all_rels[r.name].iteritems() if want_id in
                                 [i for i,c in v]]
                 if not matches:
                     mapper[w][r.name] = [NO_DATA_MSG]
                     continue
                 mapper[w][r.name] = \
                     [objs.get(get_id, NO_DATA_MSG) for get_id in matches]
     print 'updating the cache...'
     d = cache.get('rels', {})
     for wit, rels in mapper.iteritems():
         wit_dict = d.get(wit, {})
         for r, vals in rels.iteritems():
             wit_dict[r] = vals
         d[wit] = wit_dict
     cache.set('rels', d)