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})
Exemplo n.º 2
0
def _document_delete(document_id, pha=None, record=None):
  """ Delete a document.

  **ARGUMENTS:**

  * *document_id*: The internal identifier of the document to delete.
  
  * *pha*: If the document to delete is scoped to an app, this
    :py:class:`~indivo.models.apps.PHA` instance refers to the app.

  * *record*: If the document to delete is scoped to a record, this
    :py:class:`~indivo.models.records_and_documents.Record` 
    instance refers to the record.
  
  **RETURNS:**
  
  * :http:statuscode:`200` on success.

  **RAISES:**
  
  * :py:exc:`django.http.Http404` if the arguments don't identify an existing
    document.

  """

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

  document.delete()
  return DONE
Exemplo n.º 3
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)
Exemplo n.º 4
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
    })
Exemplo n.º 5
0
def _document_create_by_rel(request,
                            record,
                            document_id,
                            rel,
                            pha=None,
                            external_id=None):
    """Create a document and relate it to an existing document, all in one call.
  
  FIXME: currently ignoring app_email
  """
    old_doc = _get_document(record=record, document_id=document_id)
    if not old_doc:
        raise Http404

    # no rels in app-specific docs
    full_external_id = Document.prepare_external_id(external_id,
                                                    pha=pha,
                                                    pha_specific=False)

    try:
        # create the doc
        new_doc = _document_create(record=record,
                                   creator=request.principal,
                                   pha=None,
                                   content=request.raw_post_data,
                                   external_id=full_external_id)
        # create the rel
        DocumentRels.objects.create(document_0=old_doc,
                                    document_1=new_doc,
                                    relationship=DocumentSchema.objects.get(
                                        type=DocumentSchema.expand_rel(rel)))
    except:
        raise Http404
    return DONE
def _document_create_by_rel(request, record, document_id, rel, pha=None, external_id=None):
  """Create a document and relate it to an existing document, all in one call.
  
  FIXME: currently ignoring app_email
  """
  old_doc = _get_document(record=record, document_id=document_id)
  if not old_doc:
    raise Http404

  # no rels in app-specific docs
  full_external_id = Document.prepare_external_id(external_id, pha=pha, pha_specific = False)

  try:
    # create the doc
    new_doc = _document_create( record = record, 
                                creator = request.principal,
                                pha = None,
                                content = request.raw_post_data,
                                external_id = full_external_id)
    # create the rel
    DocumentRels.objects.create(document_0 = old_doc, 
                                document_1 = new_doc, 
                                relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel)))
  except DocumentSchema.DoesNotExist:
    raise Http404
  except ValueError as e:
    return HttpResponseBadRequest(str(e))
  return DONE
Exemplo n.º 7
0
def record_app_document_meta(request, record, pha, document_id):
    """For 1:1 mapping of URLs to views. Calls _document_meta"""
    document = _get_document(record=record, pha=pha, document_id=document_id)
    return _document_meta(record=record,
                          document=document,
                          pha=pha,
                          app_specific=True)
Exemplo n.º 8
0
def carenet_document_delete(request, carenet, record, document_id):
  """ Unshare a document from a given carenet.

  If there is an autoshare of *document_id*'s type into *carenet*, this 
  call creates an exception for *document_id* in *carenet*. If *document_id*
  was shared individually into *carenet*, this call removes it. If *document_id*
  is not shared in *carenet* at all, this call does nothing immediately.
  
  In all cases, this call exempts *document_id* from any future autoshares into
  this carenet.

  Will return :http:statuscode:`200` on success, :http:statuscode:`404` if 
  *document_id* doesn't exist or if *document_id* or *carenet* don't belong
  to *record*.

  """

  document = _get_document(document_id=document_id, record=record)

  # this is always permission denied, so we can just handle it here
  # not in the access control system
  if not document or document.record != carenet.record:
    raise Http404

  doc_share, created_p = CarenetDocument.objects.get_or_create(document = document, carenet = carenet, defaults={'share_p':False})

  if not created_p and doc_share.share_p:
    doc_share.share_p = False
    doc_share.save()

  return DONE
Exemplo n.º 9
0
def carenet_document_delete(request, carenet, record, document_id):
    """ Unshare a document from a given carenet.

  If there is an autoshare of *document_id*'s type into *carenet*, this 
  call creates an exception for *document_id* in *carenet*. If *document_id*
  was shared individually into *carenet*, this call removes it. If *document_id*
  is not shared in *carenet* at all, this call does nothing immediately.
  
  In all cases, this call exempts *document_id* from any future autoshares into
  this carenet.

  Will return :http:statuscode:`200` on success, :http:statuscode:`404` if 
  *document_id* doesn't exist or if *document_id* or *carenet* don't belong
  to *record*.

  """

    document = _get_document(document_id=document_id, record=record)

    # this is always permission denied, so we can just handle it here
    # not in the access control system
    if not document or document.record != carenet.record:
        raise Http404

    doc_share, created_p = CarenetDocument.objects.get_or_create(
        document=document, carenet=carenet, defaults={'share_p': False})

    if not created_p and doc_share.share_p:
        doc_share.share_p = False
        doc_share.save()

    return DONE
Exemplo n.º 10
0
def _document_delete(document_id, pha=None, record=None):
    """ Delete a document.

  **ARGUMENTS:**

  * *document_id*: The internal identifier of the document to delete.
  
  * *pha*: If the document to delete is scoped to an app, this
    :py:class:`~indivo.models.apps.PHA` instance refers to the app.

  * *record*: If the document to delete is scoped to a record, this
    :py:class:`~indivo.models.records_and_documents.Record` 
    instance refers to the record.
  
  **RETURNS:**
  
  * :http:statuscode:`200` on success.

  **RAISES:**
  
  * :py:exc:`django.http.Http404` if the arguments don't identify an existing
    document.

  """

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

    document.delete()
    return DONE
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 })
Exemplo n.º 12
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)
Exemplo n.º 13
0
def document_status_history(request, record, document_id):
  """ List all changes to a document's status over time.

  **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 identifier of the document for which to get 
    status history.

  **RETURNS:**

  * A :py:class:`django.http.HttpResponse` object containing an XML string
    listing status changes for the document.

  **RAISES:**

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

  """

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

  return render_template('document_status_history', 
          { 'document_id'      : document.id,
            'document_history' : DocumentStatusHistory.objects.filter(
                                  record    = record.id, 
                                  document  = document.id)})
Exemplo n.º 14
0
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 })
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
def _document_create_by_rel(request, record, document_id, rel, pha=None, external_id=None):
  """ Create a document and relate it to an existing document.

  **ARGUMENTS:**

  * *request*: The incoming Django HttpRequest object. ``request.POST`` must
    contain the raw content of the new document.
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` to
    which to scope the new document, and to which the source document is scoped.

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

  * *rel*: The relationship type to establish between the source document and the
    new document (as a string).

  * *pha*: The :py:class:`~indivo.models.apps.PHA` object that 
    scopes the external_id, if present.

  * *external_id*: The external identifier to assign to the newly created document.

  **RETURNS:**

  * :http:statuscode:`200` on success.

  * :http:statuscode:`400` if the new document content is invalid

  **RAISES:**

  * :py:exc:`django.http.Http404` if *document_id*
    doesn't identify an existing document scoped to *record*, or if
    *rel* doesn't identify an valid relationship type.  

  """

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

  # no rels in app-specific docs
  full_external_id = Document.prepare_external_id(external_id, pha=pha, pha_specific = False)

  try:
    # create the doc
    new_doc = _document_create( record = record, 
                                creator = request.principal,
                                pha = None,
                                content = request.raw_post_data,
                                external_id = full_external_id)
    # create the rel
    DocumentRels.objects.create(document_0 = old_doc, 
                                document_1 = new_doc, 
                                relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel)))
  except DocumentSchema.DoesNotExist:
    raise Http404
  except ValueError as e:
    return HttpResponseBadRequest(str(e))
  return DONE
Exemplo n.º 17
0
def _get_prefs_doc(account, pha):
    from indivo.models import Document
    from indivo.views.documents.document import _get_document
    prepared_id = Document.prepare_external_id(USER_PREFS_EXTID % account.id,
                                               pha,
                                               pha_specific=True,
                                               record_specific=False)
    return _get_document(pha=pha, external_id=prepared_id)
def _document_delete(document_id, pha=None, record=None):
  """Delete a document"""
  document = _get_document(record=record, pha=pha, document_id=document_id)
  if not document:
    raise Http404

  document.delete()
  return DONE
Exemplo n.º 19
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)
Exemplo n.º 20
0
def app_document_meta(request, pha, document_id):
  """ Fetch the metadata of an app-specific document.

  Calls into 
  :py:meth:`~indivo.views.documents.document_meta._document_meta`.

  """
  """For 1:1 mapping of URLs to views. Calls _document_meta"""
  document = _get_document(pha=pha, document_id=document_id)
  return _document_meta(pha=pha, document=document, app_specific=True)
Exemplo n.º 21
0
def record_app_document_meta(request, record, pha, document_id):
  """ Fetch the metadata of a record-app-specific document.

  Calls into 
  :py:meth:`~indivo.views.documents.document_meta._document_meta`.

  """

  document = _get_document(record=record, pha=pha, document_id=document_id)
  return _document_meta(record=record, document=document, pha=pha, app_specific=True)
Exemplo n.º 22
0
def record_document_meta(request, record, document_id):
    """ Fetch the metadata of a record-specific document.

  Calls into 
  :py:meth:`~indivo.views.documents.document_meta._document_meta`.

  """

    document = _get_document(record=record, document_id=document_id)
    return _document_meta(record=record, document=document)
Exemplo n.º 23
0
def carenet_document_meta(request, carenet, document_id):
  """ Fetch the metadata of a record-specific document via a carenet.

  Calls into 
  :py:meth:`~indivo.views.documents.document_meta._document_meta`.

  """

  document = _get_document(carenet=carenet, document_id=document_id)
  return _document_meta(carenet=carenet, document=document)
Exemplo n.º 24
0
def carenet_document_meta(request, carenet, document_id):
    """ Fetch the metadata of a record-specific document via a carenet.

  Calls into 
  :py:meth:`~indivo.views.documents.document_meta._document_meta`.

  """

    document = _get_document(carenet=carenet, document_id=document_id)
    return _document_meta(carenet=carenet, document=document)
Exemplo n.º 25
0
def document_status_history(request, record, document_id):
  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  return render_template('document_status_history', 
          { 'document_id'      : document.id,
            'document_history' : DocumentStatusHistory.objects.filter(
                                  record    = record.id, 
                                  document  = document.id)})
Exemplo n.º 26
0
def pha_document_update(request, pha, document_id, record=None):
  try:
    doc = _get_document(record=record, pha=pha, document_id=document_id)
  except Document.DoesNotExist:
    raise Http404

  doc.content = request.raw_post_data
  doc.save()

  return render_template('document', {'doc': doc, 'pha': pha, 'record' :record}, type="xml")
Exemplo n.º 27
0
def app_document_meta(request, pha, document_id):
    """ Fetch the metadata of an app-specific document.

  Calls into 
  :py:meth:`~indivo.views.documents.document_meta._document_meta`.

  """
    """For 1:1 mapping of URLs to views. Calls _document_meta"""
    document = _get_document(pha=pha, document_id=document_id)
    return _document_meta(pha=pha, document=document, app_specific=True)
Exemplo n.º 28
0
def document_set_nevershare(request, record, document_id):
    """
  Flag a document as nevershare
  """
    document = _get_document(document_id=document_id, record=record)
    if not document:
        raise Http404

    document.nevershare = True
    document.save()
    return DONE
Exemplo n.º 29
0
def document_remove_nevershare(request, record, document_id):
    """
  Remove nevershare flag
  """
    document = _get_document(document_id=document_id, record=record)
    if not document:
        raise Http404

    document.nevershare = False
    document.save()
    return DONE
def document_set_nevershare(request, record, document_id):
  """
  Flag a document as nevershare
  """
  document = _get_document(document_id=document_id, record=record)
  if not document:
    raise Http404

  document.nevershare = True
  document.save()
  return DONE
def document_remove_nevershare(request, record, document_id):
  """
  Remove nevershare flag
  """
  document = _get_document(document_id=document_id, record=record)
  if not document:
    raise Http404

  document.nevershare = False
  document.save()
  return DONE
Exemplo n.º 32
0
def app_document_delete(request, pha, document_id):
  """
  Delete an application specific document: no restrictions, since this storage is 
  managed by the app.
  """
  document = _get_document(pha=pha, document_id=document_id)
  if not document:
    raise Http404

  document.delete()
  return DONE
Exemplo n.º 33
0
def carenet_document_placement(request, record, carenet, document_id):
    """
  Place a document into a given carenet
  """
    document = _get_document(document_id=document_id, record=record)

    # don't allow this for nevershare documents
    if not document or document.nevershare:
        raise Http404

    CarenetDocument.objects.get_or_create(carenet=carenet, document=document)
    return DONE
Exemplo n.º 34
0
def carenet_document_placement(request, record, carenet, document_id):
    """
  Place a document into a given carenet
  """
    document = _get_document(document_id=document_id, record=record)

    # don't allow this for nevershare documents
    if not document or document.nevershare:
        raise Http404

    CarenetDocument.objects.get_or_create(carenet=carenet, document=document)
    return DONE
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))
Exemplo n.º 36
0
def document_set_status(request, record, document_id):
  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  status_str, reason_str = 'status', 'reason'
  if not (request.POST.has_key(status_str) and \
          request.POST.has_key(reason_str) and \
          document.set_status(request, 
                              request.POST[status_str], 
                              request.POST[reason_str])):
    return HttpResponseBadRequest()
  return DONE
def document_set_status(request, record, document_id):
  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  status_str, reason_str = 'status', 'reason'
  try:
    document.set_status(request.principal, 
                        request.POST[status_str],
                        request.POST[reason_str])
  except:
    return HttpResponseBadRequest()

  return DONE
Exemplo n.º 38
0
def document_carenets(request, record, document_id):
    """List all the carenets for a given document

    This view retrieves all the carenets in which  a given 
    document has been placed
  """
    document = _get_document(document_id=document_id, record=record)
    if not document:
        raise Http404

    # Get the carenets
    carenets = document_carenets_filter(document, Carenet.objects.all())

    return render_template("carenets", {"carenets": carenets, "record": record})
Exemplo n.º 39
0
def carenet_document(request, carenet, document_id):
    """Return a document given a record and carenet id

    Return the document if it is in the given carenet or 
    its type is in the record's autoshare
  """

    document = _get_document(document_id=document_id, carenet=carenet)
    if not document or document.nevershare:
        raise Http404

    if document_in_carenet(carenet, document_id):
        return _render_document(document)
    else:
        raise Http404
Exemplo n.º 40
0
def document_carenets(request, record, document_id):
  """List all the carenets into which a document has been shared.

  Will return :http:statuscode:`200` with a list of carenets on success,
  :http:statuscode:`404` if *document_id* is invalid.
  
  """
  document = _get_document(document_id=document_id, record=record)
  if not document:
    raise Http404

  # Get the carenets
  carenets = document_carenets_filter(document, Carenet.objects.all())

  return render_template('carenets', {'carenets' : carenets, 'record' : record})
Exemplo n.º 41
0
def carenet_document_placement(request, record, carenet, document_id):
  """ Place a document into a given carenet.

  Will return :http:statuscode:`200` on success, :http:statuscode:`404` if
  *document_id* doesn't exist or if *document_id* has a nevershare set
  on it.

  """
  document = _get_document(document_id=document_id, record=record)

  # don't allow this for nevershare documents
  if not document or document.nevershare:
    raise Http404

  CarenetDocument.objects.get_or_create(carenet=carenet, document=document)
  return DONE
Exemplo n.º 42
0
def carenet_document_placement(request, record, carenet, document_id):
    """ Place a document into a given carenet.

  Will return :http:statuscode:`200` on success, :http:statuscode:`404` if
  *document_id* doesn't exist or if *document_id* has a nevershare set
  on it.

  """
    document = _get_document(document_id=document_id, record=record)

    # don't allow this for nevershare documents
    if not document or document.nevershare:
        raise Http404

    CarenetDocument.objects.get_or_create(carenet=carenet, document=document)
    return DONE
Exemplo n.º 43
0
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))
Exemplo n.º 44
0
def document_set_status(request, record, document_id):
  """ Set the status of a record-specific document.

  **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    contain:
    
    * *status* The new status for the document. Must identify an existing
      :py:class:`~indivo.models.status.StatusName` object.
    
    * *reason* The reason for the status change
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to.

  * *document_id*: The internal identifier of the document whose status is being
    altered.

  **RETURNS:**

  * :http:statuscode:`200` on success.

  * :http:statuscode:`400` if ``request.POST`` is missing arguments.

  **RAISES:**

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

  """

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

  status_str, reason_str = 'status', 'reason'
  try:
    document.set_status(request.principal, 
                        request.POST[status_str],
                        request.POST[reason_str])
  except:
    return HttpResponseBadRequest()

  return DONE
Exemplo n.º 45
0
def carenet_document_delete(request, carenet, record, document_id):
  """Delete a document into a given carenet"""

  document = _get_document(document_id=document_id, record=record)

  # this is always permission denied, so we can just handle it here
  # not in the access control system
  if not document or document.record != carenet.record:
    raise Http404

  doc_share, created_p = CarenetDocument.objects.get_or_create(document = document, carenet = carenet, defaults={'share_p':False})

  if not created_p and doc_share.share_p:
    doc_share.share_p = True
    doc_share.save()

  return DONE
Exemplo n.º 46
0
def document_carenets(request, record, document_id):
  """List all the carenets for a given document

    This view retrieves all the carenets in which  a given 
    document has been placed
  """
  document = _get_document(document_id=document_id, record=record)
  if not document:
    raise Http404

  if document.nevershare:
    carenets = []
  else:
    # the carenets for which a carenetdocument share exists with that particular document
    carenets = Carenet.objects.filter(carenetdocument__document = document, carenetdocument__share_p=True)
    
  return render_template('carenets', {'carenets' : carenets, 'record' : record})
Exemplo n.º 47
0
def document_set_status(request, record, document_id):
    """ Set the status of a record-specific document.

  **ARGUMENTS:**
  
  * *request*: The incoming Django HttpRequest object. ``request.POST`` must 
    contain:
    
    * *status* The new status for the document. Must identify an existing
      :py:class:`~indivo.models.status.StatusName` object.
    
    * *reason* The reason for the status change
  
  * *record*: The 
    :py:class:`~indivo.models.records_and_documents.Record` that
    the document is scoped to.

  * *document_id*: The internal identifier of the document whose status is being
    altered.

  **RETURNS:**

  * :http:statuscode:`200` on success.

  * :http:statuscode:`400` if ``request.POST`` is missing arguments.

  **RAISES:**

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

  """

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

    status_str, reason_str = 'status', 'reason'
    try:
        document.set_status(request.principal, request.POST[status_str],
                            request.POST[reason_str])
    except:
        return HttpResponseBadRequest()

    return DONE
Exemplo n.º 48
0
def carenet_document_delete(request, carenet, record, document_id):
    """Delete a document into a given carenet"""

    document = _get_document(document_id=document_id, record=record)

    # this is always permission denied, so we can just handle it here
    # not in the access control system
    if not document or document.record != carenet.record:
        raise Http404

    doc_share, created_p = CarenetDocument.objects.get_or_create(
        document=document, carenet=carenet, defaults={'share_p': False})

    if not created_p and doc_share.share_p:
        doc_share.share_p = True
        doc_share.save()

    return DONE
Exemplo n.º 49
0
def document_remove_nevershare(request, record, document_id):
    """ Remove the nevershare flag from a document.

  If a document has was shared via autoshare or explicitly, then marked
  as nevershare, this call will reactivate all previously existing shares.

  Will return :http:statuscode:`200` on success, :http:statuscode:`404` if
  *document_id* is invalid.
  
  """

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

    document.nevershare = False
    document.save()
    return DONE
Exemplo n.º 50
0
def document_carenets(request, record, document_id):
    """List all the carenets into which a document has been shared.

  Will return :http:statuscode:`200` with a list of carenets on success,
  :http:statuscode:`404` if *document_id* is invalid.
  
  """
    document = _get_document(document_id=document_id, record=record)
    if not document:
        raise Http404

    # Get the carenets
    carenets = document_carenets_filter(document, Carenet.objects.all())

    return render_template('carenets', {
        'carenets': carenets,
        'record': record
    })
Exemplo n.º 51
0
def document_delete(request, document_id, record):
  """Delete a recently added document, only if it was recently added by the same person"""
  document = _get_document(record=record, document_id=document_id)
  if not document:
    raise Http404

  # FIXME: modularize timedelta(hours=1)
  # For now, 1 hour
  if document.creator == request.principal.effective_principal and \
      datetime.datetime.now() - document.created_at < datetime.timedelta(hours=1):

    # we mean explicitly for this to fail 
    # if the document is referenced by anything in the DB

    document.delete()
    return DONE
  else:
    raise Exception("document was inserted too long ago to allow this, or was not created by you")
Exemplo n.º 52
0
def carenet_document(request, carenet, document_id):
    """Return a document from a carenet.

  Will only return the document if it exists within the carenet.
  
  Will return :http:statuscode:`200` with the document content on success,
  :http:statuscode:`404` if *document_id* is invalid or if the indicated
  document is not shared in *carenet*.

  """

    document = _get_document(document_id=document_id, carenet=carenet)
    if not document or document.nevershare:
        raise Http404

    if document_in_carenet(carenet, document_id):
        return _render_document(document)
    else:
        raise Http404
Exemplo n.º 53
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))
Exemplo n.º 54
0
def carenet_document(request, carenet, document_id):
    """Return a document given a record and carenet id

    Return the document if it is in the given carenet or 
    its type is in the record's autoshare
  """

    document = _get_document(document_id=document_id, carenet=carenet)
    if not document or document.nevershare:
        raise Http404

    try:
        if CarenetDocument.objects.filter(carenet = carenet, document = document, carenet__record = carenet.record, share_p=True) or \
              (CarenetAutoshare.objects.filter(carenet = carenet, record = carenet.record, type = document.type) and \
                 not CarenetDocument.objects.filter(carenet = carenet, document = document, share_p=False)):
            return _render_document(document)
        else:
            raise Http404
    except Carenet.DoesNotExist:
        raise Http404
Exemplo n.º 55
0
def document_set_nevershare(request, record, document_id):
    """ Flag a document to never be shared, anywhere.

  This overrides autoshares and existing shares, and prevents
  sharing the document in the future, until
  :py:meth:`~indivo.views.shares.shares_nevershare.document_remove_nevershare` 
  is called.

  Will return :http:statuscode:`200` on success, :http:statuscode:`404` if 
  *document_id* is invalid.

  """

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

    document.nevershare = True
    document.save()
    return DONE
Exemplo n.º 56
0
def document_carenets(request, record, document_id):
    """List all the carenets for a given document

    This view retrieves all the carenets in which  a given 
    document has been placed
  """
    document = _get_document(document_id=document_id, record=record)
    if not document:
        raise Http404

    if document.nevershare:
        carenets = []
    else:
        # the carenets for which a carenetdocument share exists with that particular document
        carenets = Carenet.objects.filter(carenetdocument__document=document,
                                          carenetdocument__share_p=True)

    return render_template('carenets', {
        'carenets': carenets,
        'record': record
    })