Example #1
0
def get_documents_by_rel(request,
                         record,
                         document_id,
                         rel,
                         limit,
                         offset,
                         status,
                         order_by='id',
                         pha=None):
    """
  get all documents related to argument-document by rel-type defined by rel
  includes relationships to other versions of the argument-document
  (also limit, offset and status)
  """
    # Need to add limit, offset, order_by
    document = _get_document(record=record, document_id=document_id)
    if not document:
        raise Http404

    tdc = 0
    try:
        relationship = DocumentSchema.objects.get(
            type=DocumentSchema.expand_rel(rel))
        docs = Document.objects.filter(
            record=record,
            status=status,
            rels_as_doc_1__document_0__original=document.
            original_id,  # doc is related to passed document
            rels_as_doc_1__relationship=relationship
        )  # AND relation type is correct
        tdc = len(docs)
    except:
        docs = []
    return _render_documents(docs, record, pha, tdc)
def carenet_document_list(request, carenet, query_options):
  """List documents from a given carenet.

  request.GET may contain:
  
  * *type*: The document schema type to filter on.

  Returns both documents in the given carenet and documents with the same types 
  as in the record's autoshare, filtered by *type* if passed.

  Will return :http:statuscode:`200` with a document list on success,
  :http:statuscode:`404` if *type* doesn't exist.

  """
  
  try:
    doc_type_uri = request.GET.get('type', None)
    if doc_type_uri:
      requested_doc_type = DocumentSchema.objects.get(type = doc_type_uri)
    else:
      requested_doc_type = None
  except DocumentSchema.DoesNotExist:
    raise Http404

  documents = carenet_documents_filter(carenet, carenet.record.documents)
  tdc = documents.count()

  offset = query_options['offset']
  limit = query_options['limit']
  ret_documents = documents[offset:offset+limit]

  return _render_documents(ret_documents, carenet.record, None, tdc)
Example #3
0
def _document_label(request, record=None, document_id=None, external_id=None, pha=None, app_specific=False):
  """
  set the document label
  """
  label = request.raw_post_data

  # Get the document
  full_external_id = Document.prepare_external_id(external_id, pha, pha_specific = app_specific)
  if not app_specific:
    pha = None
  document = _get_document(record=record, document_id=document_id, pha=pha, external_id=full_external_id)
    
  if document:
    if pha and document.pha != pha:
      raise Http404

    if record and document.record != record:
      raise Http404
  else:
    raise Http404

  document.label = label
  document.save()

  return _render_documents([document], record, pha, 1)
def carenet_document_list(request, carenet, query_options):
    """List documents from a given carenet.

  request.GET may contain:
  
  * *type*: The document schema type to filter on.

  Returns both documents in the given carenet and documents with the same types 
  as in the record's autoshare, filtered by *type* if passed.

  Will return :http:statuscode:`200` with a document list on success,
  :http:statuscode:`404` if *type* doesn't exist.

  """

    try:
        doc_type_uri = request.GET.get('type', None)
        if doc_type_uri:
            requested_doc_type = DocumentSchema.objects.get(type=doc_type_uri)
        else:
            requested_doc_type = None
    except DocumentSchema.DoesNotExist:
        raise Http404

    documents = carenet_documents_filter(carenet, carenet.record.documents)
    tdc = documents.count()

    offset = query_options['offset']
    limit = query_options['limit']
    ret_documents = documents[offset:offset + limit]

    return _render_documents(ret_documents, carenet.record, None, tdc)
def get_documents_by_rel(request, record, document_id, rel, query_options, pha=None):
    """ Get all documents related to the passed document_id by a relation of the passed relation-type.

  Includes relationships to other versions of *document_id*.
  Paging operators are NOT IMPLEMENTED.

  **ARGUMENTS:**

  * *request*: The incoming Django HttpRequest object.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to.

  * *document_id*: The internal document identifier for the source document.

  * *rel*: The relationship type to filter related documents by (as a string).

  * *limit*, *offset*, *status*, *order_by*: Standard paging and filtering 
    arguments. See :py:func:`~indivo.lib.view_decorators.marsloader`
    or :doc:`/query-api`.

    .. Note:: 
    
       Paging operators are not implemented for this call currently. Passing
       them into the function will have no effect on output.

  * *pha*: The :py:class:`~indivo.models.apps.PHA` object that the
    source document is scoped to, if applicable.

  **RETURNS:**

  * An HttpResponse object with an XML string listing related documents
    on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document_id*
    doesn't identify an existing document scoped to *record*.

  """
    # Need to add limit, offset, order_by
    document = _get_document(record=record, document_id=document_id)
    if not document:
        raise Http404

    tdc = 0
    try:
        relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel))
        docs = Document.objects.filter(
            record=record,
            status=query_options["status"],
            rels_as_doc_1__document_0__original=document.original_id,  # doc is related to passed document
            rels_as_doc_1__relationship=relationship,
        )  # AND relation type is correct
        tdc = len(docs)
    except:
        docs = []
    return _render_documents(docs, record, pha, tdc)
def document_versions(request, record, document, limit, offset, status, order_by='created_at', pha=None):
  """Retrieve the versions of a document"""
  try:
    docs = Document.objects.filter( original  = document.original_id, 
                                    status    = status).order_by(order_by)
  except:
    raise Http404
  return _render_documents(request, docs[offset:offset+limit], record, pha, len(docs))
Example #7
0
def get_documents_by_rel(request, record, document_id, rel, limit, offset, status, order_by='id', pha=None):
  """ Get all documents related to the passed document_id by a relation of the passed relation-type.

  Includes relationships to other versions of *document_id*.
  Paging operators are NOT IMPLEMENTED.

  **ARGUMENTS:**

  * *request*: The incoming Django HttpRequest object.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to.

  * *document_id*: The internal document identifier for the source document.

  * *rel*: The relationship type to filter related documents by (as a string).

  * *limit*, *offset*, *status*, *order_by*: Standard paging and filtering 
    arguments. See :py:func:`~indivo.lib.view_decorators.marsloader`
    or :doc:`/query-api`.

    .. Note:: 
    
       Paging operators are not implemented for this call currently. Passing
       them into the function will have no effect on output.

  * *pha*: The :py:class:`~indivo.models.apps.PHA` object that the
    source document is scoped to, if applicable.

  **RETURNS:**

  * An HttpResponse object with an XML string listing related documents
    on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document_id*
    doesn't identify an existing document scoped to *record*.

  """
  # Need to add limit, offset, order_by
  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  tdc = 0
  try:
    relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel))
    docs = Document.objects.filter(record=record,
                                   status=status,
                                   rels_as_doc_1__document_0__original=document.original_id, # doc is related to passed document
                                   rels_as_doc_1__relationship=relationship) # AND relation type is correct
    tdc = len(docs)
  except:
    docs = []
  return _render_documents(docs, record, pha, tdc)
def document_versions(request, record, document_id, limit, offset, status, order_by='created_at'):
  """Retrieve the versions of a document"""
  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  try:
    docs = Document.objects.filter( original  = document.original_id, 
                                    status    = status).order_by(order_by)
  except:
    raise Http404
  return _render_documents(docs[offset:offset+limit], record, None, len(docs))
def carenet_document_list(request, carenet, limit, offset, status, order_by):
    """List documents from a given carenet

    Return both documents in the given carenet and 
    documents with the same types as in the record's autoshare

    FIXME: this needs refactoring so it's one query for the whole thing,
    rather than one query per auto-share doctype
  """

    try:
        doc_type_uri = request.GET.get('type', None)
        if doc_type_uri:
            requested_doc_type = DocumentSchema.objects.get(type=doc_type_uri)
        else:
            requested_doc_type = None
    except DocumentSchema.DoesNotExist:
        raise Http404

    # we want to select
    # the documents that are explicitly shared
    # plus the documents that are auto-shared
    # minus the documents that are explicitly un-shared

    record = carenet.record

    # documents explicitly shared
    explicitly_shared = record.documents.filter(
        carenetdocument__share_p=True,
        carenetdocument__carenet=carenet,
        nevershare=False)
    if requested_doc_type:
        explicitly_shared = explicitly_shared.filter(type=requested_doc_type)

    # auto-shared documents that are not in the negatively shared space
    autoshared_types = DocumentSchema.objects.filter(
        carenetautoshare__carenet=carenet).values('id')
    implicitly_shared = record.documents.filter(
        type__in=autoshared_types,
        nevershare=False).exclude(carenetdocument__share_p=False,
                                  carenetdocument__carenet=carenet)

    if requested_doc_type:
        implicitly_shared = implicitly_shared.filter(type=requested_doc_type)

    # FIXME: we should make this lazy so that it's not evaluated right now
    all_documents = [d for d in explicitly_shared
                     ] + [d for d in implicitly_shared]

    documents = all_documents[offset:offset + limit]

    return _render_documents(documents, record, None, len(documents))
Example #10
0
def get_documents_by_rel(request, record, document, rel, limit, offset, status, order_by='id', pha=None):
  tdc = 0 
  try:
    # SZ: Make more readable!
    # SZ: add limit, offset, order_by support
    relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel))
    docs = [x for x in [[relation.document_1 \
                for relation in doc.rels_as_doc_0.filter(relationship=relationship).select_related()] \
                  for doc in [document \
                    for document in Document.objects.filter(status=status, 
                      original=document.original_id)]] \
                        if len(x) > 0][0]
    tdc = len(docs)
  except:
    docs = []
  return _render_documents(request, docs, record, pha, tdc)
def document_versions(request,
                      record,
                      document_id,
                      limit,
                      offset,
                      status,
                      order_by='created_at'):
    """ Retrieve the versions of a document.

  **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` to which
    the document is scoped.

  * *document_id*: The internal identifier of the document. 

  * *limit*, *offset*, *status*, *order_by*: Standard paging and filtering 
    arguments. See :py:func:`~indivo.lib.view_decorators.marsloader`
    or :doc:`/query-api`.

  **RETURNS:**

  * An HttpResponse object with an XML string containing metadata on all versions
    of the document, including the passed *document_id*, on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document_id* doesn't
    identify an existing document.

  """

    document = _get_document(record=record, document_id=document_id)
    if not document:
        raise Http404

    try:
        docs = Document.objects.filter(original=document.original_id,
                                       status=status).order_by(order_by)
    except:
        raise Http404
    return _render_documents(docs[offset:offset + limit], record, None,
                             len(docs))
Example #12
0
def carenet_document_list(request, docbox, limit, offset, status, order_by):
  """List documents from a given carenet

    Return both documents in the given carenet and 
    documents with the same types as in the record's autoshare

    FIXME: this needs refactoring so it's one query for the whole thing,
    rather than one query per auto-share doctype
  """
  
  try:
    doc_type_uri = request.GET.get('type', None)
    if doc_type_uri:
      requested_doc_type = DocumentSchema.objects.get(type = doc_type_uri)
    else:
      requested_doc_type = None
  except DocumentSchema.DoesNotExist:
    raise Http404

  # we want to select
  # the documents that are explicitly shared
  # plus the documents that are auto-shared
  # minus the documents that are explicitly un-shared
  
  carenet = docbox.carenet
  record = carenet.record
  
  # documents explicitly shared
  explicitly_shared = record.documents.filter(carenetdocument__share_p = True, carenetdocument__carenet = carenet, nevershare= False)
  if requested_doc_type:
    explicitly_shared = explicitly_shared.filter(type = requested_doc_type)

  # auto-shared documents that are not in the negatively shared space
  autoshared_types = DocumentSchema.objects.filter(carenetautoshare__carenet = carenet).values('id')
  implicitly_shared = record.documents.filter(type__in = autoshared_types, nevershare=False).exclude(carenetdocument__share_p = False, carenetdocument__carenet = carenet)

  if requested_doc_type:
    implicitly_shared = implicitly_shared.filter(type = requested_doc_type)

  # FIXME: we should make this lazy so that it's not evaluated right now
  all_documents = [d for d in explicitly_shared] + [d for d in implicitly_shared]

  documents = all_documents[offset:offset+limit]

  return _render_documents(request, documents, docbox.record, None, len(documents))
Example #13
0
def document_versions(request,
                      record,
                      document_id,
                      limit,
                      offset,
                      status,
                      order_by='created_at'):
    """Retrieve the versions of a document"""
    document = _get_document(record=record, document_id=document_id)
    if not document:
        raise Http404

    try:
        docs = Document.objects.filter(original=document.original_id,
                                       status=status).order_by(order_by)
    except:
        raise Http404
    return _render_documents(docs[offset:offset + limit], record, None,
                             len(docs))
def document_versions(request, record, document_id, query_options):
  """ Retrieve the versions of a document.

  **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` to which
    the document is scoped.

  * *document_id*: The internal identifier of the document. 

  * *limit*, *offset*, *status*, *order_by*: Standard paging and filtering 
    arguments. See :py:func:`~indivo.lib.view_decorators.marsloader`
    or :doc:`/query-api`.

  **RETURNS:**

  * An HttpResponse object with an XML string containing metadata on all versions
    of the document, including the passed *document_id*, on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document_id* doesn't
    identify an existing document.

  """

  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  try:
    docs = Document.objects.filter( original  = document.original_id, 
                                    status    = query_options['status']).order_by(query_options['order_by'])
  except:
    raise Http404

  offset = query_options['offset']
  limit = query_options['limit']
  return _render_documents(docs[offset:offset+limit], record, None, len(docs))
def get_documents_by_rel(request, record, document_id, rel, limit, offset, status, order_by='id', pha=None):
  """
  get all documents related to argument-document by rel-type defined by rel
  includes relationships to other versions of the argument-document
  (also limit, offset and status)
  """
  # Need to add limit, offset, order_by
  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  tdc = 0
  try:
    relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel))
    docs = Document.objects.filter(record=record,
                                   status=status,
                                   rels_as_doc_1__document_0__original=document.original_id, # doc is related to passed document
                                   rels_as_doc_1__relationship=relationship) # AND relation type is correct
    tdc = len(docs)
  except:
    docs = []
  return _render_documents(docs, record, pha, tdc)
Example #16
0
def carenet_document_list(request, carenet, limit, offset, status, order_by):
    """List documents from a given carenet

    Return both documents in the given carenet and 
    documents with the same types as in the record's autoshare

  """

    try:
        doc_type_uri = request.GET.get("type", None)
        if doc_type_uri:
            requested_doc_type = DocumentSchema.objects.get(type=doc_type_uri)
        else:
            requested_doc_type = None
    except DocumentSchema.DoesNotExist:
        raise Http404

    documents = carenet_documents_filter(carenet, carenet.record.documents)
    tdc = documents.count()

    ret_documents = documents[offset : offset + limit]

    return _render_documents(ret_documents, carenet.record, None, tdc)
Example #17
0
def document_label(request, record, document=None, external_id=None, pha=None, app_specific=False):
  """
  set the document label
  """
  label = request.raw_post_data

  # set up the full external id and potentially clear the PHA variable
  # if it was just meant for selecting the external ID
  full_external_id = Document.prepare_external_id(external_id, pha, pha_specific = app_specific)
  if not app_specific:
    pha = None

  if document:
    if pha and document.pha != pha:
      raise Http404

    if record and document.record != record:
      raise Http404
  else:
    document = record.documents.get(external_id=full_external_id, pha=pha)
  document.label = label
  document.save()

  return _render_documents(request, [document], record, pha, 1)
def _document_label(request, record=None, document_id=None, external_id=None, pha=None, app_specific=False):
  """ Set a document's label.

  **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    consist of a raw string containing the new label to assign.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to, if applicable.

  * *document_id*: The internal identifier of the document to re-label.

    .. Note::

       One of *external_id* or *document_id* MUST be passed to this function, 
       or it cannot retrieve a unique document.

  * *external_id*: The external identifier of the document to re-label.

    .. Note::

       One of *external_id* or *document_id* MUST be passed to this function, 
       or it cannot retrieve a unique document.

  * *pha*: The :py:class:`~indivo.models.apps.PHA` object that the
    document is scoped to. Also serves to scope *external_id*, if present and
    *app_specific* is ``True``.

  * *app_specific*: Whether or not the document is app-specific. The mere presence
    of the *pha* argument isn't enough to satisfy this question, as *pha* might
    have been passed in only to scope an external id for a non-app-specific
    document.

  **RETURNS:**

  * An HttpResponse object with an XML string describing the re-labeled document
    on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if neither *document_id* nor *external_id*
    identify an existing document.

  """

  label = request.raw_post_data

  # Get the document
  full_external_id = Document.prepare_external_id(external_id, pha, pha_specific = app_specific)
  if not app_specific:
    pha = None
  document = _get_document(record=record, document_id=document_id, pha=pha, external_id=full_external_id)
    
  if not document:
    raise Http404

  document.label = label
  document.save()

  return _render_documents([document], record, pha, 1)
def _document_label(request,
                    record=None,
                    document_id=None,
                    external_id=None,
                    pha=None,
                    app_specific=False):
    """ Set a document's label.

  **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    consist of a raw string containing the new label to assign.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to, if applicable.

  * *document_id*: The internal identifier of the document to re-label.

    .. Note::

       One of *external_id* or *document_id* MUST be passed to this function, 
       or it cannot retrieve a unique document.

  * *external_id*: The external identifier of the document to re-label.

    .. Note::

       One of *external_id* or *document_id* MUST be passed to this function, 
       or it cannot retrieve a unique document.

  * *pha*: The :py:class:`~indivo.models.apps.PHA` object that the
    document is scoped to. Also serves to scope *external_id*, if present and
    *app_specific* is ``True``.

  * *app_specific*: Whether or not the document is app-specific. The mere presence
    of the *pha* argument isn't enough to satisfy this question, as *pha* might
    have been passed in only to scope an external id for a non-app-specific
    document.

  **RETURNS:**

  * An HttpResponse object with an XML string describing the re-labeled document
    on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if neither *document_id* nor *external_id*
    identify an existing document.

  """

    label = request.raw_post_data

    # Get the document
    full_external_id = Document.prepare_external_id(external_id,
                                                    pha,
                                                    pha_specific=app_specific)
    if not app_specific:
        pha = None
    document = _get_document(record=record,
                             document_id=document_id,
                             pha=pha,
                             external_id=full_external_id)

    if not document:
        raise Http404

    document.label = label
    document.save()

    return _render_documents([document], record, pha, 1)