Beispiel #1
0
def _document_version(request,
                      record,
                      document_id,
                      pha=None,
                      external_id=None):
    """Version a document, *cannot* be a non-record document"""

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

    full_external_id = Document.prepare_external_id(external_id, pha)
    try:
        new_doc = _document_create(record=record,
                                   creator=request.principal,
                                   content=request.raw_post_data,
                                   replaces_document=old_document,
                                   pha=None,
                                   external_id=full_external_id,
                                   mime_type=utils.get_content_type(request))
    except:
        raise Http404

    _set_doc_latest(new_doc)
    return render_template('document', {
        'record': record,
        'doc': new_doc,
        'pha': None
    })
def _document_version(request, record, document_id, pha=None, external_id=None):
  """Version a document, *cannot* be a non-record document"""

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

  # Can't version an already versioned document
  if old_document.replaced_by:
    return HttpResponseBadRequest("Can't version a document that has already been versioned. Get the latest version of the document.")


  full_external_id = Document.prepare_external_id(external_id, pha)
  try:
    new_doc = _document_create(record=record, 
                               creator=request.principal, 
                               content=request.raw_post_data,
                               replaces_document = old_document, 
                               pha=None,
                               external_id = full_external_id,
                               mime_type=utils.get_content_type(request))
  except:
    raise Http404

  _set_doc_latest(new_doc)
  return render_template('document', {'record'  : record, 
                                      'doc'     : new_doc, 
                                      'pha'     : None })
def _document_version(request, record, document_id, pha=None, external_id=None):
  """ Create a new version of a record-specific document. 

    **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    consist of a raw string containing the new document content.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` to which
    the old document is scoped, and to which the new document will be scoped.

  * *document_id*: The internal identifier of the old document. The old document
    must be at the latest version, or the call will fail.

  * *external_id*: The external identifier to assign to the new document.

  * *pha*: The :py:class:`~indivo.models.apps.PHA` object used
    to scope *external_id*, if present.

  **RETURNS:**

  * An HttpResponse object with an XML string containing metadata on the new
    document on success.

  * :http:statuscode:`400` if the old document has previously been replaced 
    by a newer version.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document_id* doesn't
    identify an existing document, or if document creation fails (odd behavior).

  """

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

  # Can't version an already versioned document
  if old_document.replaced_by:
    return HttpResponseBadRequest("Can't version a document that has already been versioned. Get the latest version of the document.")


  full_external_id = Document.prepare_external_id(external_id, pha)
  try:
    new_doc = _document_create(record=record, 
                               creator=request.principal, 
                               content=request.raw_post_data,
                               replaces_document = old_document, 
                               pha=None,
                               external_id = full_external_id,
                               mime_type=utils.get_content_type(request))
  except:
    raise Http404

  _set_doc_latest(new_doc)
  return render_template('document', {'record'  : record, 
                                      'doc'     : new_doc, 
                                      'pha'     : None })
def document_meta(request, docbox, document=None, pha=None, external_id=None, app_specific = False):
  """
  The metadata for a single document
  
  The app-specific parameter needs to be specified in the URL route
  """

  if not document:
    try:
      if docbox and docbox.record:
        full_external_id = Document.prepare_external_id(external_id, pha = pha, pha_specific = app_specific)

        # clear the pha argument if it's not app specific
        if not app_specific:
          pha = None

        document = docbox.record.documents.get(record=docbox.record, pha=pha, external_id = full_external_id)
      else:
        full_external_id = Document.prepare_external_id(external_id, pha = pha,
                                                        pha_specific = app_specific, record_specific=False)
        document = Document.objects.get(record=None, pha=pha, external_id = full_external_id)
    except Document.DoesNotExist:
      raise Http404
    except MultipleObjectsReturned:
      return HttpResponseBadRequest("Multiple external_ids returned, db is in a corrupted state")
  _set_doc_latest(document)

  # related stuff
  document.relates_to, document.is_related_from = _get_doc_relations(document)

  return render_template('single_document', {'doc' : document, 'record': document.record})
def _document_meta(record=None, carenet=None, document=None, pha=None, external_id=None, app_specific=False):
  """
  The metadata for a single document
  """
  if carenet:
    record = carenet.record

  if not document:
    full_external_id = Document.prepare_external_id(external_id, pha=pha, 
                                                    pha_specific=app_specific, 
                                                    record_specific=(record is not None))
    if not full_external_id:
      raise Http404

    if not app_specific:
      pha = None
    document = _get_document(record=record, pha=pha, external_id=full_external_id)
    if not document:
      raise Http404

  _set_doc_latest(document)

  # related stuff
  document.relates_to, document.is_related_from = _get_doc_relations(document)

  return render_template('single_document', {'doc' : document, 'record': document.record})
Beispiel #6
0
def set_demographics(request, record):
    """ Create or update demographics on a record.

  **ARGUMENTS:**

  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    consist of a raw string containing the demographics content.

  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` from 
    which to fetch the demographics.

  **RETURNS:**

  * a :py:class:`django.http.HttpResponse` containing Metadata XML on the
    newly created document. TODO: what should we return now that we have a model

  * :http:statuscode:`400` if the new demographics content didn't validate.

  """

    # grab existing demographics
    demographics = get_demographics(record)
    demographics_doc = (demographics.document if demographics else None)

    # build new demographics
    try:
        new_demographics = Demographics.from_xml(request.raw_post_data)
    except Exception as e:
        return HttpResponseBadRequest(str(e))

    # this will do the right thing in terms of replacement
    try:
        new_doc = _document_create(record=record,
                                   creator=request.principal,
                                   content=request.raw_post_data,
                                   pha=None,
                                   replaces_document=demographics_doc)
        new_demographics.document = new_doc
    except:
        return HttpResponseBadRequest(
            'Invalid document: special documents must be valid XML')

    # update the record pointer
    new_demographics.save()
    record.demographics = new_demographics
    record.save()
    #TODO: used to be changing the record label to reflect updated demographics

    _set_doc_latest(new_doc)
    return render_template('document', {
        'record': record,
        'doc': new_doc,
        'pha': None
    })
def get_doc_obj(doc_id):
  try:
    doc = Document.objects.get(id=doc_id)
    
    # append the doc metadata
    _set_doc_latest(doc)
    doc.relates_to, doc.is_related_from = _get_doc_relations(doc)

    return loader.get_template('document.xml').render(Context({'doc': doc, 'record': doc.record}))
  except:
    return ""
def set_demographics(request, record):
  """ Create or update demographics on a record.

  **ARGUMENTS:**

  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    consist of a raw string containing the demographics content.

  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` from 
    which to fetch the demographics.

  **RETURNS:**

  * a :py:class:`django.http.HttpResponse` containing Metadata XML on the
    newly created document. TODO: what should we return now that we have a model

  * :http:statuscode:`400` if the new demographics content didn't validate.

  """
  
  # grab existing demographics
  demographics = get_demographics(record)
  demographics_doc = (demographics.document if demographics else None)

  # build new demographics
  try:
    new_demographics = Demographics.from_xml(request.raw_post_data)
  except Exception as e:
    return HttpResponseBadRequest(str(e))  

  # this will do the right thing in terms of replacement
  try:
    new_doc = _document_create(record=record, 
                               creator=request.principal, 
                               content=request.raw_post_data,
                               pha=None,
                               replaces_document=demographics_doc)
    new_demographics.document = new_doc
  except:
    return HttpResponseBadRequest('Invalid document: special documents must be valid XML')
    
  # update the record pointer
  new_demographics.save()
  record.demographics = new_demographics
  record.save()
  #TODO: used to be changing the record label to reflect updated demographics

  _set_doc_latest(new_doc)
  return render_template('document', { 'record'  : record, 
                                              'doc'     : new_doc, 
                                              'pha'     : None})
Beispiel #9
0
def get_doc_obj(doc_id):
    try:
        doc = Document.objects.get(id=doc_id)

        # append the doc metadata
        _set_doc_latest(doc)
        doc.relates_to, doc.is_related_from = _get_doc_relations(doc)

        return loader.get_template('document.xml').render(
            Context({
                'doc': doc,
                'record': doc.record
            }))
    except:
        return ""
def save_special_document(request, record, special_document):
  """Save a new special document """
  doc = get_special_doc(record, carenet=None, special_document=special_document)

  # this will do the right thing in terms of replacement
  new_doc = _document_create(record=record, 
                             creator=request.principal, 
                             content=request.raw_post_data,
                             pha=None,
                             replaces_document=doc)

  # update the record pointer
  set_special_doc(record, special_document, new_doc)

  _set_doc_latest(new_doc)
  return render_template('document', { 'record'  : record, 
                                              'doc'     : new_doc, 
                                              'pha'     : None})
def save_special_document(request, record, special_document):
  """ Create or update a special document on a record.

  **ARGUMENTS:**

  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    consist of a raw string containing the new document content.

  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` from 
    which to fetch the special document.

  * *special_document*: The type of special document to update. Options are 
    ``demographics`` or ``contact``.

  **RETURNS:**

  * a :py:class:`django.http.HttpResponse` containing Metadata XML on the
    newly created document.

  * :http:statuscode:`400` if the new document content didn't validate.

  """

  doc = get_special_doc(record, carenet=None, special_document=special_document)

  # this will do the right thing in terms of replacement
  try:
    new_doc = _document_create(record=record, 
                               creator=request.principal, 
                               content=request.raw_post_data,
                               pha=None,
                               replaces_document=doc)
  except:
    return HttpResponseBadRequest('Invalid document: special documents must be valid XML')
    
  # update the record pointer
  set_special_doc(record, special_document, new_doc)

  _set_doc_latest(new_doc)
  return render_template('document', { 'record'  : record, 
                                              'doc'     : new_doc, 
                                              'pha'     : None})
def document_version(request, record, document, pha=None, external_id=None):
  """Version a document, *cannot* be a non-record document"""

  full_external_id = Document.prepare_external_id(external_id, pha)

  try:
    new_doc = _document_create(record=record, 
                               creator=request.principal, 
                               content=request.raw_post_data,
                               replaces_document = document, 
                               pha=None,
                               external_id = full_external_id,
                               mime_type=utils.get_content_type(request))
  except:
    raise Http404

  _set_doc_latest(new_doc)
  return render_template('document', {'record'  : record, 
                                      'doc'     : new_doc, 
                                      'pha'     : None })
def save_special_document(request, record, special_document):
    """Save a new special document """
    doc = get_special_doc(record,
                          carenet=None,
                          special_document=special_document)

    # this will do the right thing in terms of replacement
    new_doc = _document_create(record=record,
                               creator=request.principal,
                               content=request.raw_post_data,
                               pha=None,
                               replaces_document=doc)

    # update the record pointer
    set_special_doc(record, special_document, new_doc)

    _set_doc_latest(new_doc)
    return render_template('document', {
        'record': record,
        'doc': new_doc,
        'pha': None
    })
def _document_meta(record=None,
                   carenet=None,
                   document=None,
                   pha=None,
                   external_id=None,
                   app_specific=False):
    """
  The metadata for a single document
  """
    if carenet:
        record = carenet.record

    if not document:
        full_external_id = Document.prepare_external_id(
            external_id,
            pha=pha,
            pha_specific=app_specific,
            record_specific=(record is not None))
        if not full_external_id:
            raise Http404

        if not app_specific:
            pha = None
        document = _get_document(record=record,
                                 pha=pha,
                                 external_id=full_external_id)
        if not document:
            raise Http404

    _set_doc_latest(document)

    # related stuff
    document.relates_to, document.is_related_from = _get_doc_relations(
        document)

    return render_template('single_document', {
        'doc': document,
        'record': document.record
    })
def _document_version(request,
                      record,
                      document_id,
                      pha=None,
                      external_id=None):
    """ Create a new version of a record-specific document. 

    **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    consist of a raw string containing the new document content.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` to which
    the old document is scoped, and to which the new document will be scoped.

  * *document_id*: The internal identifier of the old document. The old document
    must be at the latest version, or the call will fail.

  * *external_id*: The external identifier to assign to the new document.

  * *pha*: The :py:class:`~indivo.models.apps.PHA` object used
    to scope *external_id*, if present.

  **RETURNS:**

  * An HttpResponse object with an XML string containing metadata on the new
    document on success.

  * :http:statuscode:`400` if the old document has previously been replaced 
    by a newer version.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document_id* doesn't
    identify an existing document, or if document creation fails (odd behavior).

  """

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

    # Can't version an already versioned document
    if old_document.replaced_by:
        return HttpResponseBadRequest(
            "Can't version a document that has already been versioned. Get the latest version of the document."
        )

    full_external_id = Document.prepare_external_id(external_id, pha)
    try:
        new_doc = _document_create(record=record,
                                   creator=request.principal,
                                   content=request.raw_post_data,
                                   replaces_document=old_document,
                                   pha=None,
                                   external_id=full_external_id,
                                   mime_type=utils.get_content_type(request))
    except:
        raise Http404

    _set_doc_latest(new_doc)
    return render_template('document', {
        'record': record,
        'doc': new_doc,
        'pha': None
    })
Beispiel #16
0
def _document_meta(record=None, carenet=None, document=None, pha=None, external_id=None, app_specific=False):
  """ Fetch the metadata of a single document.
  
  Metadata includes:
  
  * id

  * date created
  
  * creator 

  * the document that replaced this one

  * the document that this one replaces

  * the original document in the version chain

  * the latest document in the version chain

  * label

  * current status

  * nevershare status

  * related documents
  

  **ARGUMENTS:**
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to, if applicable.

  * *carenet*: The 
    :py:class:`~indivo.models.shares.Carenet` that
    the document is shared into, if applicable.

  * *document*: The document to get metadata for, if it has been prefetched.

    .. Note::

       One of *external_id* or *document* 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``.

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

    .. Note::

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

  * *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 document metadata
    on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document* isn't passed and *external_id*
    doesn't identify an existing document.

  """

  if carenet:
    record = carenet.record

  if not document:
    full_external_id = Document.prepare_external_id(external_id, pha=pha, 
                                                    pha_specific=app_specific, 
                                                    record_specific=(record is not None))
    if not full_external_id:
      raise Http404

    if not app_specific:
      pha = None
    document = _get_document(record=record, pha=pha, external_id=full_external_id)
    if not document:
      raise Http404

  _set_doc_latest(document)

  # related stuff
  document.relates_to, document.is_related_from = _get_doc_relations(document)

  return render_template('single_document', {'doc' : document, 'record': document.record})
def _document_meta(record=None,
                   carenet=None,
                   document=None,
                   pha=None,
                   external_id=None,
                   app_specific=False):
    """ Fetch the metadata of a single document.
  
  Metadata includes:
  
  * id

  * date created
  
  * creator 

  * the document that replaced this one

  * the document that this one replaces

  * the original document in the version chain

  * the latest document in the version chain

  * label

  * current status

  * nevershare status

  * related documents
  

  **ARGUMENTS:**
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to, if applicable.

  * *carenet*: The 
    :py:class:`~indivo.models.shares.Carenet` that
    the document is shared into, if applicable.

  * *document*: The document to get metadata for, if it has been prefetched.

    .. Note::

       One of *external_id* or *document* 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``.

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

    .. Note::

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

  * *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 document metadata
    on success.

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document* isn't passed and *external_id*
    doesn't identify an existing document.

  """

    if carenet:
        record = carenet.record

    if not document:
        full_external_id = Document.prepare_external_id(
            external_id,
            pha=pha,
            pha_specific=app_specific,
            record_specific=(record is not None))
        if not full_external_id:
            raise Http404

        if not app_specific:
            pha = None
        document = _get_document(record=record,
                                 pha=pha,
                                 external_id=full_external_id)
        if not document:
            raise Http404

    _set_doc_latest(document)

    return render_template('single_document', {
        'doc': document,
        'record': document.record
    })