Exemple #1
0
def get_entities_in(dbm, geo_path, type_path=None):
    """
    Retrieve an entity within the given fully-qualified geographic placename.
    """
    assert isinstance(dbm, DatabaseManager)
    assert is_string(geo_path) or isinstance(geo_path, list)
    assert is_string(type_path) or isinstance(type_path, list) or type_path is None

    if is_string(geo_path):
        geo_path = [geo_path]
    if is_string(type_path):
        type_path = [type_path]

    entities = []

    # if type is unspecified, then return all entities
    if type_path is not None:
        # TODO: is the type field necessarily a heirarchy?
        # if not, then this needs to perform a query for each type and then take the intersection
        # of the result sets
        rows = dbm.load_all_rows_in_view(u'by_type_geo', key=(type_path + geo_path))
        entities = dbm.get_many([row.id for row in rows], Entity)

    # otherwise, filter by type
    if type_path is None:
        rows = dbm.load_all_rows_in_view(u'by_geo', key=geo_path)
        entities = dbm.get_many([row.id for row in rows], Entity)

    return entities
Exemple #2
0
    def __init__(self, dbm, entity_type=None, location=None, aggregation_paths=None,
                 geometry=None, centroid=None, gr_id=None, id=None, short_code=None, is_datasender=True):
        """
        Construct a new entity.

        Note: _couch_document is used for 'protected' factory methods and
        should not be passed in standard construction.

        If _couch_document is passed, the other args are ignored

        :param entity_type: may be a string (flat type) or sequence (hierarchical type)
        :param location: hierarchical list of location names
        :pram aggregation_paths: hierarchical list of aggregation path
        :pram geometry: hierarchical list of aggregation path
        :pram short_code: code for the entity
        """
        assert isinstance(dbm, DatabaseManager)
        assert entity_type is None or is_sequence(entity_type) or is_string(entity_type)
        assert location is None or is_sequence(location)
        assert aggregation_paths is None or isinstance(aggregation_paths, dict)
        assert geometry is None or isinstance(geometry, dict)
        assert centroid is None or isinstance(centroid, list)
        assert gr_id is None or is_string(gr_id)
        DataObject.__init__(self, dbm)

        # Are we being constructed from an existing doc, in which case all the work is
        # in _set_document?
        if entity_type is None:
            return

        # Not made from existing doc, so create a new one
        self._create_new_entity_doc(aggregation_paths, centroid, entity_type, geometry, gr_id, id, location, short_code,
                                    is_datasender)
Exemple #3
0
def get_entities_in(dbm, geo_path, type_path=None):
    """
    Retrieve an entity within the given fully-qualified geographic placename.
    """
    assert isinstance(dbm, DatabaseManager)
    assert is_string(geo_path) or isinstance(geo_path, list)
    assert is_string(type_path) or isinstance(type_path,
                                              list) or type_path is None

    if is_string(geo_path):
        geo_path = [geo_path]
    if is_string(type_path):
        type_path = [type_path]

    entities = []

    # if type is unspecified, then return all entities
    if type_path is not None:
        # TODO: is the type field necessarily a heirarchy?
        # if not, then this needs to perform a query for each type and then take the intersection
        # of the result sets
        rows = dbm.load_all_rows_in_view(u'by_type_geo',
                                         key=(type_path + geo_path))
        entities = dbm.get_many([row.id for row in rows], Entity)

    # otherwise, filter by type
    if type_path is None:
        rows = dbm.load_all_rows_in_view(u'by_geo', key=geo_path)
        entities = dbm.get_many([row.id for row in rows], Entity)

    return entities
Exemple #4
0
    def __init__(self, id=None, primitive_type=None, constraints=None, slug=None, name=None,
                 description=None, tags=None, **kwargs):
        """""Create a new CouchDB document that represents a DataDictType"""""
        DocumentBase.__init__(self, id, 'DataDict')

        assert primitive_type is None or is_string(primitive_type)
        assert constraints is None or isinstance(constraints, dict)
        assert slug is None or is_string(slug)
        assert name is None or is_string(name)
        assert description is None or is_string(description)
        assert tags is None or isinstance(tags, list)  # do we want to check that they are strings?
        # how to assert any kwargs?

        self.primitive_type = primitive_type
        if constraints is None:
            self.constraints = {}
        else:
            self.constraints = constraints
        if tags is None:
            self.tags = []
        else:
            self.tags = tags
        self.slug = slug
        self.name = name
        self.description = description
        for arg, value in kwargs.items():
            self[arg] = value
Exemple #5
0
    def __init__(self,
                 dbm,
                 name=None,
                 slug=None,
                 primitive_type=None,
                 description=None,
                 constraints=None,
                 tags=None,
                 id=None,
                 **kwargs):
        """Create a new DataDictType.

        This represents a type of data that can be used to coordinate data collection and interoperability.
        """
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_string(name)
        assert slug is None or is_string(slug)
        assert primitive_type is None or is_string(primitive_type)
        assert description is None or is_string(description)
        assert constraints is None or isinstance(constraints, dict)
        # how to assert any kwargs?

        DataObject.__init__(self, dbm)

        # Are we being constructed from an existing doc?
        if name is None:
            return

        # Not made from existing doc, so create a new one
        doc = DataDictDocument(id, primitive_type, constraints, slug, name,
                               description, tags, **kwargs)
        self._set_document(doc)
Exemple #6
0
    def __init__(self, dbm, entity_type=None, location=None, aggregation_paths=None,
                 geometry=None, centroid=None, gr_id=None, id=None, short_code=None):
        """
        Construct a new entity.

        Note: _couch_document is used for 'protected' factory methods and
        should not be passed in standard construction.

        If _couch_document is passed, the other args are ignored

        entity_type may be a string (flat type) or sequence (hierarchical type)
        """
        assert isinstance(dbm, DatabaseManager)
        assert entity_type is None or is_sequence(entity_type) or is_string(entity_type)
        assert location is None or is_sequence(location)
        assert aggregation_paths is None or isinstance(aggregation_paths, dict)
        assert geometry is None or isinstance(geometry, dict)
        assert centroid is None or isinstance(centroid, list)
        assert gr_id is None or is_string(gr_id)
        DataObject.__init__(self, dbm)

        # Are we being constructed from an existing doc, in which case all the work is
        # in _set_document?
        if entity_type is None:
            return

        # Not made from existing doc, so create a new one
        doc = EntityDocument(id)
        self._set_document(doc)

        # add aggregation paths
        if is_string(entity_type):
            entity_type = [entity_type]
        doc.entity_type = entity_type

        if location is not None:
            doc.location = location

        if geometry is not None:
            doc.geometry = geometry

        if centroid is not None:
            doc.centroid = centroid

        if gr_id is not None:
            doc.gr_id = gr_id

        if short_code is not None:
            doc.short_code = short_code

        if aggregation_paths is not None:
            reserved_names = (attributes.TYPE_PATH, attributes.GEO_PATH)
            for name in aggregation_paths.keys():
                if name in reserved_names:
                    raise ValueError(u'Attempted to add an aggregation path with a reserved name')
                self.set_aggregation_path(name, aggregation_paths[name])
Exemple #7
0
def get_feature_by_id(id):
    assert is_string(id)
    query = _query('/feature/%s.json' % id)
    if query:
        return query['features'][0]
    else:
        return None
Exemple #8
0
def get_feature_by_id(id):
    assert is_string(id)
    query = _query('/feature/%s.json' % id)
    if query:
        return query['features'][0]
    else:
        return None
Exemple #9
0
def create_entity(dbm,
                  entity_type,
                  short_code,
                  location=None,
                  aggregation_paths=None,
                  geometry=None):
    """
    Initialize and save an entity to the database. Return the entity
    created unless the short code used is not unique or this entity
    type has not been defined yet.
    """
    assert is_string(short_code) and not is_empty(short_code)
    assert type(entity_type) is list and not is_empty(entity_type)
    type_hierarchy = [e_type for e_type in entity_type]
    if not entity_type_already_defined(dbm, type_hierarchy):
        raise EntityTypeDoesNotExistsException(entity_type)
    if _check_if_exists(dbm, type_hierarchy, short_code):
        raise DataObjectAlreadyExists("Entity",
                                      "Unique Identification Number (ID)",
                                      short_code)
    e = Entity(dbm,
               entity_type=type_hierarchy,
               location=location,
               aggregation_paths=aggregation_paths,
               short_code=short_code,
               geometry=geometry)
    e.save()
    return e
Exemple #10
0
    def __init__(self, dbm, name=None, label=None, form_code=None, fields=None, entity_type=None, type=None,
                 language="eng", state=attributes.ACTIVE_STATE):
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_not_empty(name)
        assert fields is None or is_sequence(fields)
        assert form_code is None or (is_string(form_code) and is_not_empty(form_code))
        assert type is None or is_not_empty(type)
        assert entity_type is None or is_sequence(entity_type)

        DataObject.__init__(self, dbm)

        self._form_fields = []
        self.errors = []

        # Are we being constructed from scratch or existing doc?
        if name is None:
            return

        # Not made from existing doc, so build ourselves up
        self._form_fields = fields
        self.validate_fields()

        doc = FormModelDocument()
        doc.name = name
        doc.add_label(language, label)
        doc.form_code = form_code
        doc.entity_type = entity_type
        doc.type = type
        doc.state = state
        doc.active_languages = language
        DataObject._set_document(self, doc)
Exemple #11
0
    def set_aggregation_path(self, name, path):
        assert self._doc is not None
        assert is_string(name) and is_not_empty(name)
        assert is_sequence(path) and is_not_empty(path)

        assert isinstance(self._doc[attributes.AGG_PATHS], dict)
        self._doc[attributes.AGG_PATHS][name] = list(path)
Exemple #12
0
    def set_aggregation_path(self, name, path):
        assert self._doc is not None
        assert is_string(name) and is_not_empty(name)
        assert is_sequence(path) and is_not_empty(path)

        assert isinstance(self._doc[attributes.AGG_PATHS], dict)
        self._doc[attributes.AGG_PATHS][name] = list(path)
Exemple #13
0
    def _create_new_entity_doc(self, aggregation_paths, centroid, entity_type,
                               geometry, gr_id, id, location, short_code,
                               is_data_sender):
        doc = ContactDocument(id)
        self._set_document(doc)
        # add aggregation paths
        if is_string(entity_type):
            entity_type = [entity_type]
        doc.entity_type = entity_type
        if location is not None:
            doc.location = location
        if geometry is not None:
            doc.geometry = geometry
        if centroid is not None:
            doc.centroid = centroid
        if gr_id is not None:
            doc.gr_id = gr_id
        if short_code is not None:
            doc.short_code = short_code
        if is_data_sender:
            doc.groups = ['datasender']
        else:
            doc.groups = ['contact']

        if aggregation_paths is not None:
            reserved_names = (attributes.TYPE_PATH, attributes.GEO_PATH)
            for name in aggregation_paths.keys():
                if name in reserved_names:
                    raise ValueError(
                        u'Attempted to add an aggregation path with a reserved name'
                    )
                self.set_aggregation_path(name, aggregation_paths[name])
Exemple #14
0
def _load_questionnaire(form_code, dbm):
    assert isinstance(dbm, DatabaseManager)
    assert is_string(form_code)
    rows = dbm.load_all_rows_in_view('questionnaire', key=form_code)
    if not len(rows):
        raise FormModelDoesNotExistsException(form_code)
    return rows[0]['value']
Exemple #15
0
 def value(self, label):
     """
         Returns the latest value for the given label.
     """
     assert is_string(label)
     field = self.data.get(label)
     return field.get('value') if field is not None else None
Exemple #16
0
 def update_questionnaire(self, dbm):
     form_model = self._load_form(dbm)
     form_model.name = self.name
     form_model.activeLanguages = [self.language]
     form_model.entity_type = [self.entity_type] if is_string(
         self.entity_type) else self.entity_type
     form_model.save()
Exemple #17
0
 def __init__(self,
              id=None,
              name=None,
              goals=None,
              project_type=None,
              entity_type=None,
              devices=None,
              state=ProjectState.INACTIVE,
              activity_report=None,
              sender_group=None,
              language='en'):
     assert entity_type is None or is_string(
         entity_type), "Entity type %s should be a string." % (
             entity_type, )
     DocumentBase.__init__(self, id=id, document_type='Project')
     self.devices = []
     self.name = name.lower() if name is not None else None
     self.goals = goals
     self.project_type = project_type
     self.entity_type = entity_type
     self.devices = devices
     self.state = state
     self.activity_report = activity_report
     self.sender_group = sender_group
     self.reminder_and_deadline = {
         "deadline_type": "Following",
         "should_send_reminder_to_all_ds": False,
         "has_deadline": True,
         "deadline_month": "5",
         "frequency_period": "month"
     }
     self.language = language
Exemple #18
0
def get_submissions_made_for_form(dbm,
                                  form_code,
                                  start_time,
                                  end_time,
                                  page_number=0,
                                  page_size=20):
    assert is_string(form_code)
    end = [form_code] if start_time is None else [form_code, start_time]
    start = [form_code, {}] if end_time is None else [form_code, end_time, {}]
    if page_size is None:
        rows = dbm.load_all_rows_in_view('submissionlog',
                                         reduce=False,
                                         descending=True,
                                         startkey=start,
                                         endkey=end)
    else:
        rows = dbm.load_all_rows_in_view('submissionlog',
                                         reduce=False,
                                         descending=True,
                                         startkey=start,
                                         endkey=end,
                                         skip=page_number * page_size,
                                         limit=page_size)
    answers, ids = list(), list()
    for each in rows:
        answers.append(each.value)
        ids.append(each.value["data_record_id"])
    return answers, ids
Exemple #19
0
def create_questionnaire(post, dbm):
    reporting_period_dict_type = get_or_create_data_dict(
        dbm=dbm,
        name="rpd",
        slug="reporting_period",
        primitive_type="date",
        description="activity reporting period")
    entity_type = [post["entity_type"]] if is_string(
        post["entity_type"]) else post["entity_type"]
    entity_id_question = create_entity_id_question(dbm)
    activity_report_question = DateField(
        name="What is the reporting period for the activity?",
        code="rpd",
        label="Period being reported on",
        ddtype=reporting_period_dict_type,
        date_format="dd.mm.yyyy")
    fields = [entity_id_question]
    if entity_type == [REPORTER]:
        fields = [entity_id_question, activity_report_question]
    return FormModel(dbm,
                     entity_type=entity_type,
                     name=post["name"],
                     fields=fields,
                     form_code=generate_questionnaire_code(dbm),
                     type='survey',
                     state=attributes.INACTIVE_STATE)
Exemple #20
0
def create_contact(dbm,
                   short_code,
                   location=None,
                   aggregation_paths=None,
                   geometry=None,
                   is_datasender=True):
    """
    Initialize and save an entity to the database. Return the entity
    created unless the short code used is not unique or this entity
    type has not been defined yet.
    """
    contact_type = ["reporter"]
    assert is_string(short_code) and not is_empty(short_code)
    if not entity_type_already_defined(dbm, contact_type):
        raise EntityTypeDoesNotExistsException(contact_type)
    existing = _check_if_entity_exists(dbm,
                                       contact_type,
                                       short_code,
                                       return_entity=True)
    if existing:
        entity_name = existing.data.get('name', {'value': ''}).get('value')
        raise DataObjectAlreadyExists(contact_type[0].capitalize(),
                                      "Unique ID Number",
                                      short_code,
                                      existing_name=entity_name)
    e = Contact(dbm,
                entity_type=contact_type,
                location=location,
                aggregation_paths=aggregation_paths,
                short_code=short_code,
                geometry=geometry,
                is_datasender=is_datasender)
    #e.save()
    return e
Exemple #21
0
def aggregate_for_form(dbm, form_code, aggregates=None, aggregate_on=None, filter=None, starttime=None, endtime=None):
    assert is_string(form_code)
    result = {}
    aggregates = {} if aggregates is None else aggregates

    form = get_form_model_by_code(dbm, form_code)
    aggregate, group_level = _get_aggregate_strategy(aggregate_on, for_form_code=True)
    values = aggregate(dbm, form.entity_type, group_level, aggregate_on)

    interested_keys = _get_interested_keys_for_form_code(values, form_code)

    _parse_key = _get_key_strategy(aggregate_on, {'form_code': form_code})

    for key, val in values:
        result_key, field, filter_key = _parse_key(key)
        #        if filter and filter_key not in interested_keys:
        if filter_key not in interested_keys:
            continue
        interested_aggregate = None
        if field in aggregates:
            interested_aggregate = aggregates.get(field)
            #        * overrides field specific aggregation, returns the aggregation for all fields.
        if "*" in aggregates:
            interested_aggregate = aggregates.get("*")
        if interested_aggregate:
            try:
                result.setdefault(result_key, {})[field] = val[interested_aggregate]
            except KeyError:
                raise AggregationNotSupportedForTypeException(field, interested_aggregate)
    return result
Exemple #22
0
def aggregate_by_form_code_python(dbm, form_code, aggregates=None, aggregate_on=None, filter=None,
                                  starttime=None, endtime=None):
    assert is_string(form_code)
    aggregates = [] if aggregates is None else aggregates

    form = get_form_model_by_code(dbm, form_code)
    values = _map(dbm, form.entity_type, BY_VALUES_FORM_CODE_INDEX, form_code, starttime, endtime)
    return _reduce(aggregates, values)
Exemple #23
0
def get_by_short_code(dbm, short_code, entity_type):
    assert is_string(short_code)
    assert is_sequence(entity_type)
    rows = dbm.load_all_rows_in_view("by_short_codes", key=[entity_type, short_code], reduce=False)
    if is_empty(rows):
        raise DataObjectNotFound("Entity", "Unique Identification Number (ID)", short_code)
    doc_id = rows[0].id
    return Entity.get(dbm, doc_id)
Exemple #24
0
def entities_exists_with_value(dbm, entity_type, label, value):
    """
    Returns true if entity with the given value for the label exists
    """
    assert isinstance(dbm, DatabaseManager)
    assert is_string(label)
    rows = dbm.load_all_rows_in_view(u'entity_by_label_value', key=[entity_type, label, value])
    return is_not_empty(rows)
Exemple #25
0
 def _to_string(self, errors):
     if is_string(errors):
         return errors
     if isinstance(errors, dict):
         return sequence_to_str(errors.values())
     if is_sequence(errors):
         return sequence_to_str(errors)
     return None
Exemple #26
0
def get_submission_count_for_form(dbm, form_code, start_time, end_time):
    assert is_string(form_code)
    start = [form_code] if start_time is  None else [form_code, start_time]
    end = [form_code,{}] if end_time  is None else [form_code, end_time, {}]
    rows = dbm.load_all_rows_in_view('submissionlog', startkey=start, endkey=end,
                                     group=True, group_level=1, reduce=True)
    count = _get_row_count(rows) if rows else 0
    return count
Exemple #27
0
def project_by_form_model_id(dbm, form_model_id):
    assert isinstance(dbm, DatabaseManager)
    assert is_string(form_model_id)
    rows = dbm.load_all_rows_in_view('project_by_form_model_id', key=form_model_id)
    if not len(rows):
        raise ProjectNotFoundException("project does not exist for form model id %s " % form_model_id)

    return Project._wrap_row(rows[0])
Exemple #28
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
Exemple #29
0
 def value(self, label):
     """
         Returns the latest value for the given label.
     """
     assert isinstance(label, DataDictType) or is_string(label)
     if isinstance(label, DataDictType):
         label = label.slug
     field = self.data.get(label)
     return field.get('value') if field is not None else None
def create_questionnaire(post, manager, entity_type, name, language):
    entity_type = [entity_type] if is_string(entity_type) else entity_type
    questionnaire_code = post['questionnaire-code'].lower()
    json_string = post['question-set']
    question_set = json.loads(json_string)
    form_model = FormModel(manager, entity_type=entity_type, name=name, type='survey',
                           state=post['project_state'], fields=[], form_code=questionnaire_code, language=language)
    QuestionnaireBuilder(form_model, manager).update_questionnaire_with_questions(question_set)
    return form_model
Exemple #31
0
 def value(self, label):
     """
         Returns the latest value for the given label.
     """
     assert isinstance(label, DataDictType) or is_string(label)
     if isinstance(label, DataDictType):
         label = label.slug
     field = self.data.get(label)
     return field.get('value') if field is not None else None
Exemple #32
0
 def data_types(self, tags=None):
     """
     Returns a list of each type of data that is stored on this entity
     """
     assert tags is None or isinstance(tags, list) or is_string(tags)
     if tags is None or is_empty(tags):
         rows = self._dbm.load_all_rows_in_view(u'entity_datatypes', key=self.id)
         result = get_datadict_types(self._dbm, [row[u'value'] for row in rows])
     else:
         if is_string(tags):
             tags = [tags]
         keys = []
         for tag in tags:
             rows = self._dbm.load_all_rows_in_view(u'entity_datatypes_by_tag', key=[self.id, tag])
             keys.append([row[u'value'] for row in rows])
         ids_with_all_tags = list(set.intersection(*map(set, keys)))
         result = get_datadict_types(self._dbm, ids_with_all_tags)
     return result
Exemple #33
0
 def parse(self, message):
     assert is_string(message)
     try:
         form_code, tokens = self.form_code(message)
         question_codes, form_model = self.get_question_codes(form_code)
         submission, extra_data = self._parse_ordered_tokens(tokens, question_codes, form_code)
     except SMSParserInvalidFormatException as ex:
         raise SMSParserInvalidFormatException(ex.data)
     return form_code, submission, extra_data
Exemple #34
0
def get_by_short_code(dbm, short_code, entity_type):
    """
    Finds Entity with a given short code
    :param dbm: DatabaseManager
    :param short_code: short code of the Entity
    :param entity_type: hierarchical list of entity types
    """
    assert is_string(short_code)
    assert is_sequence(entity_type)
    return by_short_code(dbm, short_code.lower(), entity_type)
Exemple #35
0
def get_form_model_by_code(dbm, code):
    assert isinstance(dbm, DatabaseManager)
    assert is_string(code)
    rows = dbm.load_all_rows_in_view('questionnaire', key=code)
    if not len(rows):
        raise FormModelDoesNotExistsException(code)

    doc = dbm._load_document(rows[0]['value']['_id'], FormModelDocument)
    form = FormModel.new_from_doc(dbm, doc)
    return form
    def set_data_for(self, node, dikt):
        """""
        Note: all keys in the data dictionary 'dikt' must be strings

        If not, this raises a value error
        """""
        assert is_string(node)
        if not self._verify_dict_keys_are_strings(dikt):
            raise ValueError('Keys in a nodes data-dictionary must be strings')
        self.graph.node[node] = dikt
Exemple #37
0
 def parse(self, csv_data):
     assert is_string(csv_data)
     csv_data = self._clean(csv_data)
     dict_reader = csv.DictReader(self._to_list(csv_data), restkey=self.EXTRA_VALUES)
     dict_reader.fieldnames = self._parse_header(dict_reader)
     parsedData = []
     form_code_fieldname = dict_reader.fieldnames[0]
     for row in dict_reader:
         parsedData.append(self._parse_row(form_code_fieldname, row))
     return parsedData
Exemple #38
0
    def set_data_for(self, node, dikt):
        """""
        Note: all keys in the data dictionary 'dikt' must be strings

        If not, this raises a value error
        """ ""
        assert is_string(node)
        if not self._verify_dict_keys_are_strings(dikt):
            raise ValueError('Keys in a nodes data-dictionary must be strings')
        self.graph.node[node] = dikt
Exemple #39
0
def get_form_model_by_code(dbm, code):
    assert isinstance(dbm, DatabaseManager)
    assert is_string(code)
    rows = dbm.load_all_rows_in_view('questionnaire', key=code)
    if not len(rows):
        raise FormModelDoesNotExistsException(code)

    doc = dbm._load_document(rows[0]['value']['_id'], FormModelDocument)
    form = FormModel.new_from_doc(dbm, doc)
    return form
Exemple #40
0
 def parse(self, message):
     assert is_string(message)
     try:
         form_code, tokens = self.form_code(message)
         submission, extra_data = self._parse_tokens(tokens, form_code)
     except SMSParserInvalidFormatException as ex:
         raise SMSParserInvalidFormatException(ex.data)
     except MultipleSubmissionsForSameCodeException as ex:
         raise MultipleSubmissionsForSameCodeException(ex.data[0])
     return form_code, submission, extra_data
Exemple #41
0
 def parse(self, csv_data):
     assert is_string(csv_data)
     csv_data = self._clean(csv_data)
     dict_reader = csv.DictReader(self._to_list(csv_data), restkey=self.EXTRA_VALUES)
     dict_reader.fieldnames = self._parse_header(dict_reader)
     parsedData = []
     form_code_fieldname = dict_reader.fieldnames[0]
     for row in dict_reader:
         parsedData.append(self._parse_row(form_code_fieldname, row))
     return parsedData
def update_questionnaire(questionnaire, post, entity_type, name, manager, language):
    questionnaire.name = name
    questionnaire.activeLanguages = [language]
    questionnaire.entity_type = [entity_type] if is_string(entity_type) else entity_type
    questionnaire.form_code = post['questionnaire-code'].lower()
    json_string = post['question-set']
    question_set = json.loads(json_string)
    QuestionnaireBuilder(questionnaire, manager).update_questionnaire_with_questions(question_set)
    questionnaire.deactivate() if post['project_state'] == ProjectState.INACTIVE else questionnaire.set_test_mode()
    return questionnaire
Exemple #43
0
def get_entities_by_value(dbm, label, value, as_of=None):
    assert isinstance(dbm, DatabaseManager)
    assert isinstance(label, DataDictType) or is_string(label)
    assert as_of is None or isinstance(as_of, datetime)
    if isinstance(label, DataDictType):
        label = label.slug

    rows = dbm.load_all_rows_in_view(u'by_label_value', key=[label, value])
    entities = dbm.get_many([row[u'value'] for row in rows], Entity)

    return [e for e in entities if e.values({label: u'latest'}, asof=as_of) == {label: value}]
    def _verify_dict_keys_are_strings(self, dikt):
        """""
        Returns True if all keys are strings, false otherwise

        if 'None' is passed, returns True 'cause there are no keys to NOT be strings!
        """""
        if dikt is not None:
            for k in dikt.keys():
                if not is_string(k):
                    return False
        return True
Exemple #45
0
    def _verify_dict_keys_are_strings(self, dikt):
        """""
        Returns True if all keys are strings, false otherwise

        if 'None' is passed, returns True 'cause there are no keys to NOT be strings!
        """ ""
        if dikt is not None:
            for k in dikt.keys():
                if not is_string(k):
                    return False
        return True
Exemple #46
0
 def parse(self, message):
     assert is_string(message)
     form_code = None
     try:
         message = self._clean(message)
         self._validate_format(message)
         tokens = message.split(self.SEPARATOR)
         form_code = self._pop_form_code(tokens)
         submission = self._parse_tokens(tokens)
     except MangroveException as ex:
         raise SubmissionParseException(form_code, ex.message)
     return form_code, submission
Exemple #47
0
def get_datadict_type_by_slug(dbm,slug):
    assert isinstance(dbm, DatabaseManager)
    assert is_string(slug)

    rows = dbm.load_all_rows_in_view('by_datadict_type', key=slug,include_docs='true')
    if not len(rows):
        raise DataObjectNotFound("DataDictType","slug",slug)
    assert len(rows) == 1, "More than one item found for slug %s" % (slug,)

    #  include_docs = 'true' returns the doc as a dict, which has to be wrapped into a DataDictDocument, and then into a DataDictType
    _doc = DataDictDocument.wrap(rows[0].doc)
    return DataDictType.new_from_doc(dbm,_doc)
Exemple #48
0
 def __init__(self, id=None, name=None, goals=None, project_type=None, entity_type=None, devices=None, state=ProjectState.INACTIVE, activity_report=None, sender_group=None):
     assert entity_type is None or is_string(entity_type), "Entity type %s should be a string." % (entity_type,)
     DocumentBase.__init__(self, id=id, document_type='Project')
     self.devices = []
     self.name = name.lower() if name is not None else None
     self.goals = goals
     self.project_type = project_type
     self.entity_type = entity_type
     self.devices = devices
     self.state = state
     self.activity_report = activity_report
     self.sender_group = sender_group
Exemple #49
0
def get_entities_by_type(dbm, entity_type):
    """
    Return a list of all entities with this type.
    """
    # TODO: change this?  for now it assumes _type is
    # non-heirarchical. Might also benefit from using get_many.
    assert isinstance(dbm, DatabaseManager)
    assert is_string(entity_type)

    rows = dbm.load_all_rows_in_view(u'by_type', key=entity_type)
    entities = dbm.get_many([row.id for row in rows], Entity)

    return entities
Exemple #50
0
    def __init__(self, dbm, name=None, slug=None, primitive_type=None, description=None,
                 constraints=None, tags=None, id=None, **kwargs):

        """Create a new DataDictType.

        This represents a type of data that can be used to coordinate data collection and interoperability.
        """
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_string(name)
        assert slug is None or is_string(slug)
        assert primitive_type is None or is_string(primitive_type)
        assert description is None or is_string(description)
        assert constraints is None or isinstance(constraints, dict)
        # how to assert any kwargs?

        DataObject.__init__(self, dbm)

        # Are we being constructed from an existing doc?
        if name is None:
            return

        # Not made from existing doc, so create a new one
        doc = DataDictDocument(id, primitive_type, constraints, slug, name, description, tags, **kwargs)
        self._set_document(doc)
Exemple #51
0
def define_type(dbm, entity_type):
    """
    Add this entity type to the tree of all entity types and save it
    to the database. entity_type may be a string or a list of
    strings.
    """
    assert is_not_empty(entity_type)
    assert is_sequence(entity_type)
    type_path = ([entity_type] if is_string(entity_type) else entity_type)
    type_path = [item.strip() for item in type_path]
    if entity_type_already_defined(dbm, type_path):
        raise EntityTypeAlreadyDefined(u"Type: %s is already defined" % u'.'.join(entity_type))
        # now make the new one
    entity_tree = _get_entity_type_tree(dbm)
    entity_tree.add_path([atree.AggregationTree.root_id] + entity_type)
    entity_tree.save()
    def _add_node(self, name, data=None):
        """""
        Adds a node and data the tree.

        NOTE: Because of JSON encoding issues, the node Names and all data dict keys must be strings.
        If not a ValueError is raised!

        """""
        assert data is None or isinstance(data, dict)
        if not is_string(name):
            raise ValueError('Node names must be strings')

        if not self._verify_dict_keys_are_strings(data):
            raise ValueError('Keys in a nodes data-dictionary must be strings')

        self.graph.add_node(name, data)
Exemple #53
0
def get_submissions_made_for_form(dbm, form_code, start_time, end_time, page_number=0, page_size=20):
    assert is_string(form_code)
    end = [form_code] if start_time is  None else [form_code, start_time]
    start = [form_code,{}] if end_time  is None else [form_code, end_time, {}]
    if page_size is None:
        rows = dbm.load_all_rows_in_view('submissionlog', reduce=False, descending=True,
                                         startkey=start,
                                         endkey=end)
    else:
        rows = dbm.load_all_rows_in_view('submissionlog', reduce=False, descending=True,
                                         startkey=start,
                                         endkey=end, skip=page_number * page_size, limit=page_size)
    answers, ids = list(), list()
    for each in rows:
        answers.append(each.value)
        ids.append(each.value["data_record_id"])
    return answers, ids
Exemple #54
0
def create_entity(dbm, entity_type, short_code, location=None, aggregation_paths=None, geometry=None):
    """
    Initialize and save an entity to the database. Return the entity
    created unless the short code used is not unique or this entity
    type has not been defined yet.
    """
    assert is_string(short_code) and not is_empty(short_code)
    assert type(entity_type) is list and not is_empty(entity_type)
    type_hierarchy = [e_type for e_type in entity_type]
    if not entity_type_already_defined(dbm, type_hierarchy):
        raise EntityTypeDoesNotExistsException(entity_type)
    if _check_if_exists(dbm,type_hierarchy,short_code):
        raise DataObjectAlreadyExists("Entity", "Unique Identification Number (ID)", short_code)
    e = Entity(dbm, entity_type=type_hierarchy, location=location,
               aggregation_paths=aggregation_paths, short_code=short_code, geometry=geometry)
    e.save()
    return e
Exemple #55
0
def get_locations_tree(country_code, limit=GEOREGISTRY_API_DEFAULT_LIMIT):
    assert is_string(country_code)
    assert is_number(int(limit))
    return _query('/features/locations', country_code=country_code, limit=limit)
Exemple #56
0
 def _strip_field_values(self, row):
     for key, value in row.items():
         if value is not None and is_string(value):
             row[unicode(key, encoding='utf-8')] = unicode(value.strip(), encoding='utf-8')
Exemple #57
0
 def update_questionnaire(self, dbm):
     form_model = dbm.get(self.qid, FormModel)
     form_model.name = self.name
     form_model.entity_type =  [self.entity_type] if is_string(self.entity_type) else self.entity_type
     form_model.save()