def annotation_container_for(cls, patron, identifier=None): if identifier: url = url_for('annotations_for_work', identifier_type=identifier.type, identifier=identifier.identifier, library_short_name=patron.library.short_name, _external=True) else: url = url_for("annotations", library_short_name=patron.library.short_name, _external=True) annotations = cls.annotations_for(patron, identifier=identifier) latest_timestamp = None if len(annotations) > 0: # patron.annotations is already sorted by timestamp, so the first # annotation is the most recent. latest_timestamp = annotations[0].timestamp container = dict() container["@context"] = [cls.JSONLD_CONTEXT, cls.LDP_CONTEXT] container["id"] = url container["type"] = ["BasicContainer", "AnnotationCollection"] container["total"] = len(annotations) container["first"] = cls.annotation_page_for(patron, identifier=identifier, with_context=False) return container, latest_timestamp
def annotation_page_for(cls, patron, identifier=None, with_context=True): if identifier: url = url_for( "annotations_for_work", identifier_type=identifier.type, identifier=identifier.identifier, library_short_name=patron.library.short_name, _external=True, ) else: url = url_for( "annotations", library_short_name=patron.library.short_name, _external=True, ) annotations = cls.annotations_for(patron, identifier=identifier) details = [ cls.detail(annotation, with_context=with_context) for annotation in annotations ] page = dict() if with_context: page["@context"] = cls.JSONLD_CONTEXT page["id"] = url page["type"] = "AnnotationPage" page["items"] = details return page
def detail(cls, annotation, with_context=True): item = dict() if with_context: item["@context"] = cls.JSONLD_CONTEXT item["id"] = url_for( "annotation_detail", annotation_id=annotation.id, library_short_name=annotation.patron.library.short_name, _external=True, ) item["type"] = "Annotation" item["motivation"] = annotation.motivation item["body"] = annotation.content if annotation.target: target = json.loads(annotation.target) compacted = jsonld.compact(target, cls.JSONLD_CONTEXT) del compacted["@context"] item["target"] = compacted if annotation.content: body = json.loads(annotation.content) compacted = jsonld.compact(body, cls.JSONLD_CONTEXT) del compacted["@context"] item["body"] = compacted return item
def link_template_header(self): """Generate the Link Template that explains how to deregister a specific DRM device ID. """ url = url_for("adobe_drm_device", device_id="{id}", _external=True) # The curly brackets in {id} were escaped. Un-escape them to # get a Link Template. url = url.replace("%7Bid%7D", "{id}") return {"Link-Template": '<%s>; rel="item"' % url}
def annotation_page_for(cls, patron, identifier=None, with_context=True): if identifier: url = url_for('annotations_for_work', identifier_type=identifier.type, identifier=identifier.identifier, library_short_name=patron.library.short_name, _external=True) else: url = url_for("annotations", library_short_name=patron.library.short_name, _external=True) annotations = cls.annotations_for(patron, identifier=identifier) details = [cls.detail(annotation, with_context=with_context) for annotation in annotations] page = dict() if with_context: page["@context"] = cls.JSONLD_CONTEXT page["id"] = url page["type"] = "AnnotationPage" page["items"] = details return page
def detail(cls, annotation, with_context=True): item = dict() if with_context: item["@context"] = cls.JSONLD_CONTEXT item["id"] = url_for("annotation_detail", annotation_id=annotation.id, library_short_name=annotation.patron.library.short_name, _external=True) item["type"] = "Annotation" item["motivation"] = annotation.motivation item["body"] = annotation.content if annotation.target: target = json.loads(annotation.target) compacted = jsonld.compact(target, cls.JSONLD_CONTEXT) del compacted["@context"] item["target"] = compacted if annotation.content: body = json.loads(annotation.content) compacted = jsonld.compact(body, cls.JSONLD_CONTEXT) del compacted["@context"] item["body"] = compacted return item