Ejemplo n.º 1
0
 def __init__(self,
              channel=None,
              destination=None,
              values=None,
              id=None,
              status=None,
              error_message=None,
              form_model_id=None,
              form_model_revision=None,
              data_record_id=None,
              event_time=None,
              modified_by_id=None,
              owner_uid=None):
     DocumentBase.__init__(self, id, 'SurveyResponse')
     self.submitted_on = utcnow()
     self.channel = channel
     self.destination = destination
     self.created_by = self.modified_by = modified_by_id
     self.form_model_id = form_model_id
     self.form_model_revision = form_model_revision
     self.values = values
     self.status = status
     self.error_message = error_message
     self.data_record_id = data_record_id
     self.event_time = event_time if event_time is not None else self.created
     self.data_record_history = []
     self.owner_uid = owner_uid
Ejemplo n.º 2
0
 def __init__(self, id=None, document_type=None, **values):
     if id is None:
         id = uuid1().hex
     Document.__init__(self, id=id, **values)
     self.created = utcnow()
     self.document_type = document_type
     self.void = False
Ejemplo n.º 3
0
 def add_data(self, entity=None, data=(), event_time=None, submission=None, multiple_records=False):
     """
     Add a new datarecord to this Entity and return a UUID for the datarecord.
     Arguments:
         data: a sequence of ordered tuples, (label, value, type)
         event_time: the time at which the event occured rather than
             when it was reported
         submission_id: an id to a 'submission' document in the
             submission log from which this data came
     """
     assert is_sequence(data)
     assert event_time is None or isinstance(event_time, datetime)
     assert self.id is not None, u"id should never be none, even if haven't been saved,an entity should have a UUID."
     if event_time is None:
         event_time = utcnow()
     for (label, value) in data:
         if is_empty(label):
             raise ValueError(u'Empty label')
     if multiple_records:
         data_list = []
         for (label, value) in data:
             data_record = DataRecordDocument(
                 event_time=event_time,
                 data=[(label, value)],
                 submission=submission
             )
             data_list.append(data_record)
         return self._dbm._save_documents(data_list)
     else:
         data_record_doc = DataRecordDocument(
             event_time=event_time,
             data=data,
             submission=submission
         )
         return self._dbm._save_document(data_record_doc)
Ejemplo n.º 4
0
 def __init__(self, source, channel=None, destination=None, message=None, id=None):
     assert is_string(source)
     DocumentBase.__init__(self, id, 'RawSubmissionLog')
     self.source = source
     self.submitted_on = utcnow()
     self.channel = channel
     self.destination = destination
     self.message = message
Ejemplo n.º 5
0
 def __init__(self, source=None, channel=None, destination=None, values=None, id=None, status=None,
              error_message=None, form_code=None, data_record_id=None, voided=None, test=None):
     DocumentBase.__init__(self, id, 'SubmissionLog')
     self.source = source
     self.submitted_on = utcnow()
     self.channel = channel
     self.destination = destination
     self.form_code = form_code
     self.values = values
     self.status = status
     self.error_message = error_message
     self.data_record_id = data_record_id
     self.voided = voided
     self.test = test
Ejemplo n.º 6
0
 def add_data(self,
              data=(),
              event_time=None,
              submission=None,
              multiple_records=False):
     """
     Add a new datarecord to this Entity and return a UUID for the datarecord.
     Arguments:
         data: a sequence of ordered tuples, (label, value, type)
             where type is a DataDictType
         event_time: the time at which the event occured rather than
             when it was reported
         submission_id: an id to a 'submission' document in the
             submission log from which this data came
     """
     assert is_sequence(data)
     assert event_time is None or isinstance(event_time, datetime)
     assert self.id is not None, u"id should never be none, even if haven't been saved,an entity should have a UUID."
     # TODO: should we have a flag that says that this has been
     # saved at least once to avoid adding data records for an
     # Entity that may never be saved? Should docs just be saved on
     # init?
     if event_time is None:
         event_time = utcnow()
     for (label, value, dd_type) in data:
         if not isinstance(dd_type, DataDictType) or is_empty(label):
             raise ValueError(
                 u'Data must be of the form (label, value, DataDictType).')
     self.update_latest_data(data=data)
     if multiple_records:
         data_list = []
         for (label, value, dd_type) in data:
             data_record = DataRecordDocument(entity_doc=self._doc,
                                              event_time=event_time,
                                              data=[(label, value, dd_type)
                                                    ],
                                              submission=submission)
             data_list.append(data_record)
         return self._dbm._save_documents(data_list)
     else:
         data_record_doc = DataRecordDocument(entity_doc=self._doc,
                                              event_time=event_time,
                                              data=data,
                                              submission=submission)
         return self._dbm._save_document(data_record_doc)
Ejemplo n.º 7
0
 def values(self, aggregation_rules, asof=None):
     """
     Return a dictionary of aggregated values. The keys are the
     attribute label, each value is the aggregated value for the
     given fields using the aggregation function specified for data
     collected till a point in time.
     Eg: aggregation_rules={'arv':'latest', 'num_patients':'sum'}
     will return latest value for arv and sum the number of
     patients.
     """
     # todo: I think we need to simplify this method a bit and
     # expose some of the ViewResults goodness.
     asof = asof or utcnow()
     result = {}
     for field, aggregate_fn in aggregation_rules.items():
         view_name = self._translate(aggregate_fn)
         result[field] = self._get_aggregate_value(field, view_name, asof)
     return result
Ejemplo n.º 8
0
 def values(self, aggregation_rules, asof=None):
     """
     Return a dictionary of aggregated values. The keys are the
     attribute label, each value is the aggregated value for the
     given fields using the aggregation function specified for data
     collected till a point in time.
     Eg: aggregation_rules={'arv':'latest', 'num_patients':'sum'}
     will return latest value for arv and sum the number of
     patients.
     """
     # todo: I think we need to simplify this method a bit and
     # expose some of the ViewResults goodness.
     asof = asof or utcnow()
     result = {}
     for field, aggregate_fn in aggregation_rules.items():
         view_name = self._translate(aggregate_fn)
         result[field] = self._get_aggregate_value(field, view_name, asof)
     return result
Ejemplo n.º 9
0
 def add_data(self, data=(), event_time=None, submission=None, multiple_records=False):
     """
     Add a new datarecord to this Entity and return a UUID for the datarecord.
     Arguments:
         data: a sequence of ordered tuples, (label, value, type)
             where type is a DataDictType
         event_time: the time at which the event occured rather than
             when it was reported
         submission_id: an id to a 'submission' document in the
             submission log from which this data came
     """
     assert is_sequence(data)
     assert event_time is None or isinstance(event_time, datetime)
     assert self.id is not None, u"id should never be none, even if haven't been saved,an entity should have a UUID."
     # TODO: should we have a flag that says that this has been
     # saved at least once to avoid adding data records for an
     # Entity that may never be saved? Should docs just be saved on
     # init?
     if event_time is None:
         event_time = utcnow()
     for (label, value, dd_type) in data:
         if not isinstance(dd_type, DataDictType) or is_empty(label):
             raise ValueError(u'Data must be of the form (label, value, DataDictType).')
     self.update_latest_data(data=data)
     if multiple_records:
         data_list = []
         for (label, value, dd_type) in data:
             data_record = DataRecordDocument(
                 entity_doc=self._doc,
                 event_time=event_time,
                 data=[(label, value, dd_type)],
                 submission=submission
             )
             data_list.append(data_record)
         return self._dbm._save_documents(data_list)
     else:
         data_record_doc = DataRecordDocument(
             entity_doc=self._doc,
             event_time=event_time,
             data=data,
             submission=submission
         )
         return self._dbm._save_document(data_record_doc)
Ejemplo n.º 10
0
    def _save_documents(self, documents, modified=None):
        assert is_sequence(documents)
        assert modified is None or isinstance(modified, datetime)
        for doc in documents:
            assert isinstance(doc, DocumentBase)
            doc.modified = modified if modified is not None else dates.utcnow()

        # TODO: what should we return here? this is what is available from db.update()...
        # The return value of this method is a list containing a tuple for every
        # element in the `documents` sequence. Each tuple is of the form
        # ``(success, docid, rev_or_exc)``, where ``success`` is a boolean
        # indicating whether the update succeeded, ``docid`` is the ID of the
        # document, and ``rev_or_exc`` is either the new document revision, or
        # an exception instance (e.g. `ResourceConflict`) if the update failed.

        # Fix up rev, 'cause bulk update seems not to do that
        results = self.database.update(documents)
        for x in range(len(results)):
            if results[x][0]:
                documents[x]._data["_rev"] = results[x][2]
        return results
Ejemplo n.º 11
0
    def _save_documents(self, documents, modified=None):
        assert is_sequence(documents)
        assert modified is None or isinstance(modified, datetime)
        for doc in documents:
            assert isinstance(doc, DocumentBase)
            doc.modified = (modified
                            if modified is not None else dates.utcnow())

        # TODO: what should we return here? this is what is available from db.update()...
        # The return value of this method is a list containing a tuple for every
        # element in the `documents` sequence. Each tuple is of the form
        # ``(success, docid, rev_or_exc)``, where ``success`` is a boolean
        # indicating whether the update succeeded, ``docid`` is the ID of the
        # document, and ``rev_or_exc`` is either the new document revision, or
        # an exception instance (e.g. `ResourceConflict`) if the update failed.

        # Fix up rev, 'cause bulk update seems not to do that
        results = self.database.update(documents)
        for x in range(len(results)):
            if results[x][0]:
                documents[x]._data['_rev'] = results[x][2]
        return results