Пример #1
0
def record_pha_setup(request, record, pha):
  """Set up a PHA in a record ahead of time

  FIXME: eventually, when there are permission restrictions on a PHA, make sure that
  any permission restrictions on the current PHA are transitioned accordingly
  """

  content = request.raw_post_data

  # if there is a document, create it
  if content:
    # is there already a setup doc
    setup_docs = Document.objects.filter(record=record, pha=pha, external_id='SETUP')
    if len(setup_docs) == 0:
      new_doc = _document_create( record      = record,
                                  creator     = request.principal,
                                  pha         = pha,
                                  content     = content,
                                  external_id = Document.prepare_external_id('SETUP', pha, pha_specific=True, record_specific=True))

  # preauthorize the token.
  access_token = OAUTH_SERVER.generate_and_preauthorize_access_token(pha, record=record)

  # return the token
  return HttpResponse(access_token.to_string(), mimetype="application/x-www-form-urlencoded")
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
Пример #3
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
    })
Пример #4
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 })
Пример #5
0
def document_rels(request, record, document_id_0, rel, document_id_1=None):
    """
  create a new document relationship, either with paylod of a new document, or between existing docs.
  2010-08-15: removed external_id and pha parameters as they are never set.
  That's for create_by_rel
  """
    try:
        document_0 = Document.objects.get(id=document_id_0)
        relationship = DocumentSchema.objects.get(
            type=DocumentSchema.expand_rel(rel))
        if document_id_1:
            document_1 = Document.objects.get(id=document_id_1)
        else:
            try:
                document_1 = _document_create(
                    record=record,
                    creator=request.principal,
                    content=request.raw_post_data,
                    mime_type=utils.get_content_type(request))
            except:
                raise Http404
        DocumentRels.objects.create(document_0=document_0,
                                    document_1=document_1,
                                    relationship=relationship)
    except:
        raise Http404
    return DONE
Пример #6
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
Пример #7
0
def record_pha_setup(request, record, pha):
    """Set up a PHA in a record ahead of time

  FIXME: eventually, when there are permission restrictions on a PHA, make sure that
  any permission restrictions on the current PHA are transitioned accordingly
  """

    content = request.raw_post_data

    # if there is a document, create it
    if content:
        # is there already a setup doc
        setup_docs = Document.objects.filter(record=record,
                                             pha=pha,
                                             external_id='SETUP')
        if len(setup_docs) == 0:
            new_doc = _document_create(
                record=record,
                creator=request.principal,
                pha=pha,
                content=content,
                external_id=Document.prepare_external_id('SETUP',
                                                         pha,
                                                         pha_specific=True,
                                                         record_specific=True))

    # preauthorize the token.
    access_token = OAUTH_SERVER.generate_and_preauthorize_access_token(
        pha, record=record)

    # return the token
    return HttpResponse(access_token.to_string(),
                        mimetype="application/x-www-form-urlencoded")
Пример #8
0
def document_rels(request, record, document_id_0, rel, document_id_1=None):
    """
  create a new document relationship, either with paylod of a new document, or between existing docs.
  2010-08-15: removed external_id and pha parameters as they are never set.
  That's for create_by_rel
  """
    try:
        document_0 = Document.objects.get(id=document_id_0)
        relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel))
        if document_id_1:
            document_1 = Document.objects.get(id=document_id_1)
        else:
            try:
                document_1 = _document_create(
                    record=record,
                    creator=request.principal,
                    content=request.raw_post_data,
                    mime_type=utils.get_content_type(request),
                )
            except:
                raise Http404
        DocumentRels.objects.create(document_0=document_0, document_1=document_1, relationship=relationship)
    except:
        raise Http404
    return DONE
Пример #9
0
    def save_as_document(self, account):
        """
        The account is the one who's doing the saving

        FIXME: need to check the external_id situation, which could cause problems if senders don't use it well.
        """
        if self.saved:
            raise Exception("this attachment already saved to record")

        record = self.message.about_record
        if record == None:
            raise Exception(
                "can only save attachments that pertain to a record")

        external_id = "SAVED_ATTACHMENT_%s_%s" % (
            self.message.external_identifier, str(self.attachment_num))

        # FIXME: this import shows that we should move the _document_create function to models from views.
        from indivo.views.documents.document import _document_create
        self.saved_to_document = _document_create(creator=account,
                                                  content=self.content,
                                                  pha=None,
                                                  record=record,
                                                  external_id=external_id,
                                                  mime_type='application/xml')
        self.save()
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 })
Пример #11
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
Пример #12
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
    })
Пример #13
0
def _record_create(request, principal_email=None, external_id=None):
    """
  Create an Indivo record
  """

    # If the xml data is not valid return an HttpResponseBadRequest Obj
    xml_data = request.raw_post_data
    try:
        etree.XML(xml_data)
    except:
        return HttpResponseBadRequest("Contact XML not valid")

    record_external_id = Record.prepare_external_id(external_id,
                                                    principal_email)

    if external_id:
        record, created_p = Record.objects.get_or_create(
            external_id=record_external_id,
            defaults={
                'creator': request.principal,
                'label': Contacts.from_xml(xml_data).full_name,
                'owner': request.principal
            })
    else:
        record = Record.objects.create(
            external_id=record_external_id,
            creator=request.principal,
            label=Contacts.from_xml(xml_data).full_name,
            owner=request.principal)
        created_p = True

    # only set up the new contact document if the record is new
    # otherwise just return the existing record
    if created_p:
        # Create default carenets for this particular record
        record.create_default_carenets()

        # Create the contact document
        # use the same external ID as for the record
        # since those are distinct anyways
        doc_external_id = record_external_id

        doc = _document_create(record=record,
                               creator=request.principal,
                               pha=None,
                               content=xml_data,
                               external_id=doc_external_id)

        # save the contact document as the special contact doc
        record.contact = doc
        record.save()

    return render_template('record', {'record': record}, type='xml')
Пример #14
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})
Пример #15
0
def record_create(request, principal_email=None, external_id=None):
  """
  Create an Indivo record
  """

  # If the xml data is not valid return an HttpResponseBadRequest Obj
  xml_data = request.raw_post_data
  try:
    etree.XML(xml_data)
  except:
    return HttpResponseBadRequest("Contact XML not valid")

  record_external_id = Record.prepare_external_id(external_id, principal_email)
    
  if external_id:
    record , created_p = Record.objects.get_or_create(
      external_id = record_external_id,
      defaults = {
        'creator' : request.principal,
        'label' : Contacts.from_xml(xml_data).full_name,
        'owner' : request.principal})
  else:
    record = Record.objects.create(
      external_id = record_external_id,
      creator = request.principal,
      label = Contacts.from_xml(xml_data).full_name,
      owner = request.principal)
    created_p = True

  # only set up the new contact document if the record is new
  # otherwise just return the existing record
  if created_p:
    # Create default carenets for this particular record
    record.create_default_carenets()

    # Create the contact document
    # use the same external ID as for the record
    # since those are distinct anyways
    doc_external_id = record_external_id

    doc = _document_create( record      = record,
                            creator     = request.principal,
                            pha         = None,
                            content     = xml_data,
                            external_id = doc_external_id)
      
    # save the contact document as the special contact doc
    record.contact = doc
    record.save()

  return render_template('record', {'record' : record}, type='xml')
Пример #16
0
def record_pha_setup(request, record, pha):
    """ Bind an app to a record without user authorization.

  This call should be used to set up new records with apps required
  for this instance of Indivo to run (i.e. syncer apps that connect to 
  data sources). It can only be made by admins, since it skips the
  normal app authorization process.

  ``request.POST`` may contain raw content that will be used
  as a setup document for the record.

  Will return :http:statuscode:`200` with a valid access token for the
  app bound to the record on success.
  
  """

    # TODO: eventually, when there are permission restrictions on a PHA,
    # make sure that any permission restrictions on the current PHA are
    # transitioned accordingly.

    content = request.raw_post_data

    # if there is a document, create it
    if content:
        # is there already a setup doc
        setup_docs = Document.objects.filter(record=record,
                                             pha=pha,
                                             external_id='SETUP')
        if len(setup_docs) == 0:
            new_doc = _document_create(
                record=record,
                creator=request.principal,
                pha=pha,
                content=content,
                external_id=Document.prepare_external_id('SETUP',
                                                         pha,
                                                         pha_specific=True,
                                                         record_specific=True))

    # preauthorize the token.
    from indivo.accesscontrol.oauth_servers import OAUTH_SERVER
    access_token = OAUTH_SERVER.generate_and_preauthorize_access_token(
        pha, record=record)

    # return the token
    return HttpResponse(access_token.to_string(),
                        mimetype="application/x-www-form-urlencoded")
Пример #17
0
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})
Пример #18
0
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})
Пример #19
0
def record_pha_setup(request, record, pha):
    """ Bind an app to a record without user authorization.

  This call should be used to set up new records with apps required
  for this instance of Indivo to run (i.e. syncer apps that connect to 
  data sources). It can only be made by admins, since it skips the
  normal app authorization process.

  ``request.POST`` may contain raw content that will be used
  as a setup document for the record.

  Will return :http:statuscode:`200` with a valid access token for the
  app bound to the record on success.
  
  """

    # TODO: eventually, when there are permission restrictions on a PHA,
    # make sure that any permission restrictions on the current PHA are
    # transitioned accordingly.

    content = request.raw_post_data

    # if there is a document, create it
    if content:
        # is there already a setup doc
        setup_docs = Document.objects.filter(record=record, pha=pha, external_id="SETUP")
        if len(setup_docs) == 0:
            new_doc = _document_create(
                record=record,
                creator=request.principal,
                pha=pha,
                content=content,
                external_id=Document.prepare_external_id("SETUP", pha, pha_specific=True, record_specific=True),
            )

    # preauthorize the token.
    from indivo.accesscontrol.oauth_servers import OAUTH_SERVER

    access_token = OAUTH_SERVER.generate_and_preauthorize_access_token(pha, record=record)

    # return the token
    return HttpResponse(access_token.to_string(), mimetype="application/x-www-form-urlencoded")
Пример #20
0
    def save_as_document(self, account):
        """
        The account is the one who's doing the saving

        FIXME: need to check the external_id situation, which could cause problems if senders don't use it well.
        """
        if self.saved:
            raise Exception("this attachment already saved to record")
        
        record = self.message.about_record
        if record == None:
            raise Exception("can only save attachments that pertain to a record")

        external_id = "SAVED_ATTACHMENT_%s_%s" % (self.message.external_identifier , str(self.attachment_num))

        # FIXME: this import shows that we should move the _document_create function to models from views.
        from indivo.views.documents.document import _document_create
        self.saved_to_document = _document_create(creator = account, content = self.content,
                                                  pha = None, record = record, external_id = external_id)
        self.save()
Пример #21
0
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 })
Пример #22
0
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
    })
Пример #23
0
def document_create_by_rel(request, record, document, 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
  """

  # 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 = document, 
                                document_1 = new_doc, 
                                relationship = DocumentSchema.objects.get(type=DocumentSchema.expand_rel(rel)))
  except:
    raise Http404
  return DONE
Пример #24
0
def _record_create(request, principal_email=None, external_id=None):
  """ Create an Indivo record.

  request.POST must contain raw XML that is a valid Indivo Demographics
  document (see :doc:`/schemas/demographics-schema`).
  
  This call will create a new record containing the following 
  information:

  * *creator*: Corresponds to ``request.principal``.

  * *label*: The full name of the new record, specified in the
    demographics document.

  * *owner*: Corresponds to ``request.principal``.

  * *external_id* An external identifier for the record, if 
    passed in.

  Additionally, this call will create a Demographics document for the record.

  Will return :http:statuscode:`200` with information about the record on
  success, :http:statuscode:`400` if the demographics data in request.POST was
  empty or invalid XML.
  
  """

  # If the xml data is not valid return an HttpResponseBadRequest Obj
  xml_data = request.raw_post_data
  try:
    etree.XML(xml_data)
  except:
    return HttpResponseBadRequest("Demographics XML not valid")

  demographics = Demographics.from_xml(xml_data)
  label = demographics.name_given + ' ' + demographics.name_family

  record_external_id = Record.prepare_external_id(external_id, principal_email)
    
  if external_id:
    record , created_p = Record.objects.get_or_create(
      external_id = record_external_id,
      defaults = {
        'creator' : request.principal,
        'label' : label,
        'owner' : request.principal})
  else:
    record = Record.objects.create(
      external_id = record_external_id,
      creator = request.principal,
      label = label,
      owner = request.principal)
    created_p = True

  # only set up the new demographics document if the record is new
  # otherwise just return the existing record
  if created_p:
    # Create default carenets for this particular record
    record.create_default_carenets()

    # Create the demographics document
    # use the same external ID as for the record
    # since those are distinct anyways
    doc_external_id = record_external_id

    doc = _document_create( record      = record,
                            creator     = request.principal,
                            pha         = None,
                            content     = xml_data,
                            external_id = doc_external_id)
      
    # set up demographics model and add it to the record
    demographics.document = doc
    demographics.save()
    record.demographics = demographics
    record.save()

  return render_template('record', {'record' : record}, type='xml')
Пример #25
0
def _record_create(request, principal_email=None, external_id=None):
    """ Create an Indivo record.

  request.POST must contain raw XML that is a valid Indivo Contact
  document (see :doc:`/schemas/contact-schema`).
  
  This call will create a new record containing the following 
  information:

  * *creator*: Corresponds to ``request.principal``.

  * *label*: The full name of the new record, specified in the
    contact document.

  * *owner*: Corresponds to ``request.principal``.

  * *external_id* An external identifier for the record, if 
    passed in.

  Additionally, this call will create a Contact document for the record.

  Will return :http:statuscode:`200` with information about the record on
  success, :http:statuscode:`400` if the contact data in request.POST was
  empty or invalid XML.
  
  """

    # If the xml data is not valid return an HttpResponseBadRequest Obj
    xml_data = request.raw_post_data
    try:
        etree.XML(xml_data)
    except:
        return HttpResponseBadRequest("Contact XML not valid")

    record_external_id = Record.prepare_external_id(external_id, principal_email)

    if external_id:
        record, created_p = Record.objects.get_or_create(
            external_id=record_external_id,
            defaults={
                "creator": request.principal,
                "label": Contacts.from_xml(xml_data).full_name,
                "owner": request.principal,
            },
        )
    else:
        record = Record.objects.create(
            external_id=record_external_id,
            creator=request.principal,
            label=Contacts.from_xml(xml_data).full_name,
            owner=request.principal,
        )
        created_p = True

    # only set up the new contact document if the record is new
    # otherwise just return the existing record
    if created_p:
        # Create default carenets for this particular record
        record.create_default_carenets()

        # Create the contact document
        # use the same external ID as for the record
        # since those are distinct anyways
        doc_external_id = record_external_id

        doc = _document_create(
            record=record, creator=request.principal, pha=None, content=xml_data, external_id=doc_external_id
        )

        # save the contact document as the special contact doc
        record.contact = doc
        record.save()

    return render_template("record", {"record": record}, type="xml")
Пример #26
0
def _record_create(request, principal_email=None, external_id=None):
    """ Create an Indivo record.

  request.POST must contain raw XML that is a valid Indivo Contact
  document (see :doc:`/schemas/contact-schema`).
  
  This call will create a new record containing the following 
  information:

  * *creator*: Corresponds to ``request.principal``.

  * *label*: The full name of the new record, specified in the
    contact document.

  * *owner*: Corresponds to ``request.principal``.

  * *external_id* An external identifier for the record, if 
    passed in.

  Additionally, this call will create a Contact document for the record.

  Will return :http:statuscode:`200` with information about the record on
  success, :http:statuscode:`400` if the contact data in request.POST was
  empty or invalid XML.
  
  """

    # If the xml data is not valid return an HttpResponseBadRequest Obj
    xml_data = request.raw_post_data
    try:
        etree.XML(xml_data)
    except:
        return HttpResponseBadRequest("Contact XML not valid")

    record_external_id = Record.prepare_external_id(external_id,
                                                    principal_email)

    if external_id:
        record, created_p = Record.objects.get_or_create(
            external_id=record_external_id,
            defaults={
                'creator': request.principal,
                'label': Contacts.from_xml(xml_data).full_name,
                'owner': request.principal
            })
    else:
        record = Record.objects.create(
            external_id=record_external_id,
            creator=request.principal,
            label=Contacts.from_xml(xml_data).full_name,
            owner=request.principal)
        created_p = True

    # only set up the new contact document if the record is new
    # otherwise just return the existing record
    if created_p:
        # Create default carenets for this particular record
        record.create_default_carenets()

        # Create the contact document
        # use the same external ID as for the record
        # since those are distinct anyways
        doc_external_id = record_external_id

        doc = _document_create(record=record,
                               creator=request.principal,
                               pha=None,
                               content=xml_data,
                               external_id=doc_external_id)

        # save the contact document as the special contact doc
        record.contact = doc
        record.save()

    return render_template('record', {'record': record}, type='xml')
Пример #27
0
def _record_create(request, principal_email=None, external_id=None):
  """ Create an Indivo record.

  request.POST must contain raw XML that is a valid Indivo Demographics
  document (see :doc:`/schemas/demographics-schema`).
  
  This call will create a new record containing the following 
  information:

  * *creator*: Corresponds to ``request.principal``.

  * *label*: The full name of the new record, specified in the
    demographics document.

  * *owner*: Corresponds to ``request.principal``.

  * *external_id* An external identifier for the record, if 
    passed in.

  Additionally, this call will create a Demographics document for the record.

  Will return :http:statuscode:`200` with information about the record on
  success, :http:statuscode:`400` if the demographics data in request.POST was
  empty or invalid XML.
  
  """

  # If the xml data is not valid return an HttpResponseBadRequest Obj
  xml_data = request.raw_post_data
  root_temp = etree.XML(xml_data) 

  try:
            with open(('/media/data/hatzimin/web/indivo_server/indivo/schemas/data/core/demographics/schema.xsd'), 'r') as schema_file:
                schema = etree.XMLSchema(etree.parse(schema_file))
            schema.assertValid(root_temp)
  except etree.DocumentInvalid as e:
        return HttpResponseBadRequest("Demographics XML not valid based on indivo model schema.Input document didn't validate, error was: %s"%(str(e)))



  try:
    etree.XML(xml_data)
  except:
    return HttpResponseBadRequest("Demographics XML not valid")

  demographics = Demographics.from_xml(xml_data)
  label = demographics.name_given + ' ' + demographics.name_family

  record_external_id = Record.prepare_external_id(external_id, principal_email)
    
  if external_id:
    record , created_p = Record.objects.get_or_create(
      external_id = record_external_id,
      defaults = {
        'creator' : request.principal,
        'label' : label,
        'owner' : request.principal})
  else:
    record = Record.objects.create(
      external_id = record_external_id,
      creator = request.principal,
      label = label,
      owner = request.principal)
    created_p = True

  # only set up the new demographics document if the record is new
  # otherwise just return the existing record
  if created_p:
    # Create default carenets for this particular record
    record.create_default_carenets()

    # Create the demographics document
    # use the same external ID as for the record
    # since those are distinct anyways
    doc_external_id = record_external_id

    doc = _document_create( record      = record,
                            creator     = request.principal,
                            pha         = None,
                            content     = xml_data,
                            external_id = doc_external_id)
      
    # set up demographics model and add it to the record
    demographics.document = doc
    demographics.save()
    record.demographics = demographics
    record.save()

    #import subprocess
    #stri=str("python /media/data/hatzimin/web/enableapps.py "+record.id)

    #subprocess.call(stri, shell=True)

#    return HttpResponse(subprocess.check_output(stri, shell=True))
#    headersAdmin={'IMC-TOKEN':'1d430b8a8da9c50a6dbd','ADMIN':''}

#    url='https://www.iphr.care:443/api/records/'+record.id+'/apps/[email protected]/setup'

#    r=requests.post(url,headers=headersAdmin,verify=True)
#    return HttpResponse(r.text)
  #  headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

   # r=requests.get('https://www.iphr.care/enableapps/?record_id='+record.id,headers=headers,verify=True) 
#    return HttpResponse(r)
#    url='https://www.iphr.care/enableapps/?record_id='+record.id
#    r = requests.post(url,verify=True)
#    return HttpResponse(record.id)
  return render_template('record', {'record' : record}, type='xml')
Пример #28
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
    })