Example #1
0
 def __init__(self,
              type="",
              name="",
              code="",
              label='',
              instruction='',
              constraints=None,
              required=True,
              parent_field_code=None):
     if not constraints: constraints = []
     self._dict = {}
     self._dict = {
         'name': name,
         'type': type,
         'code': code,
         'instruction': instruction,
         'label': label,
         'required': required,
         'parent_field_code': parent_field_code
     }
     self.constraints = constraints
     self.errors = []
     self.value = None
     if not is_empty(constraints):
         self._dict['constraints'] = []
         for constraint in constraints:
             constraint_json = constraint._to_json()
             if not is_empty(constraint_json):
                 self._dict['constraints'].append(constraint_json)
Example #2
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
Example #3
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)
    if not entity_type_already_defined(dbm, entity_type):
        raise EntityTypeDoesNotExistsException(entity_type)
    existing = _check_if_entity_exists(dbm,
                                       entity_type,
                                       short_code,
                                       return_entity=True)
    if existing:
        entity_name = existing.data.get('name', {'value': ''}).get('value')
        raise DataObjectAlreadyExists(entity_type[0].capitalize(),
                                      "Unique ID Number",
                                      short_code,
                                      existing_name=entity_name)
    e = Entity(dbm,
               entity_type=entity_type,
               location=location,
               aggregation_paths=aggregation_paths,
               short_code=short_code,
               geometry=geometry)
    #e.save()
    return e
Example #4
0
 def _is_valid(self, answers):
     cleaned_answers = {}
     errors = {}
     short_code = self._find_code(answers, self.entity_question.code)
     if self.is_registration_form():
         entity_code = self._find_code(answers, ENTITY_TYPE_FIELD_CODE)
         if is_empty(entity_code):
             raise EntityTypeCodeNotSubmitted()
         if short_code is not None and len(short_code) > 12:
             raise ShortCodeTooLongException()
     else:
         if is_empty(short_code):
             raise EntityQuestionCodeNotSubmitted()
     for key in answers:
         field = self.get_field_by_code(key)
         if field is None:
             continue
         answer = answers[key]
         if is_empty(answer):
             continue
         is_valid, result = self._validate_answer_for_field(answer, field)
         if is_valid:
             cleaned_answers[field.code] = result
         else:
             errors[key] = result
     return cleaned_answers, errors
Example #5
0
    def test_should_initialize_response_from_form_submission(self):
        form_submission_mock = Mock(spec=FormSubmission)
        form_submission_mock.saved = True
        form_submission_mock.errors = []
        expected_data_record_id = 123
        form_submission_mock.data_record_id = expected_data_record_id
        expected_short_code = 456
        form_submission_mock.short_code = expected_short_code
        expected_cleanned_data = {'a': 1}
        form_submission_mock.cleaned_data = expected_cleanned_data
        form_submission_mock.is_registration = False
        expected_entity_type = 'entity_type'
        form_submission_mock.entity_type = expected_entity_type
        expected_form_code = '1'
        form_model_mock = Mock()
        form_model_mock.form_code = expected_form_code
        form_submission_mock.form_model = form_model_mock

        response = create_response_from_form_submission(
            reporters=None, form_submission=form_submission_mock)
        self.assertTrue(response.success)
        self.assertTrue(is_empty(response.errors))
        self.assertTrue(is_empty(response.reporters))
        self.assertTrue(response.success)
        self.assertEquals(expected_data_record_id, response.datarecord_id)
        self.assertEquals(expected_short_code, response.short_code)
        self.assertEquals(expected_cleanned_data, response.processed_data)
        self.assertFalse(response.is_registration)
        self.assertEquals(expected_entity_type, response.entity_type)
        self.assertEquals(expected_entity_type, response.entity_type)
        self.assertEquals(expected_form_code, response.form_code)
Example #6
0
 def _is_valid(self, answers):
     cleaned_answers = {}
     errors = {}
     short_code = self._find_code(answers, self.entity_question.code)
     if self.is_registration_form():
         entity_code = self._find_code(answers, ENTITY_TYPE_FIELD_CODE)
         if is_empty(entity_code):
             raise EntityTypeCodeNotSubmitted()
         if short_code is not None and len(short_code) > 12:
             raise ShortCodeTooLongException()
     else:
         if is_empty(short_code):
             raise EntityQuestionCodeNotSubmitted()
     for key in answers:
         field = self.get_field_by_code(key)
         if field is None:
             continue
         answer = answers[key]
         if is_empty(answer):
             continue
         is_valid, result = self._validate_answer_for_field(answer, field)
         if is_valid:
             cleaned_answers[field.code] = result
         else:
             errors[key] = result
     return cleaned_answers, errors
Example #7
0
 def _handle_tokens_with_only_separators(self, tokens):
     new_tokens = []
     for token in tokens:
         if is_empty(token): continue
         if is_empty("".join(token.split("."))): continue
         new_tokens.append(token.strip())
     return new_tokens
Example #8
0
 def test_should_delete_the_datasender_user_if_it_exists(self):
     self.assertTrue(User.objects.get(username=self.username))
     self.assertTrue(NGOUserProfile.objects.get(reporter_id=self.reporter_id, org_id=self.org_id))
     entity_ids = [self.reporter_id, 'test_id']
     organization = Mock(spec=Organization)
     organization.org_id = self.org_id
     delete_datasender_users_if_any(entity_ids, organization)
     self.assertTrue(is_empty(User.objects.filter(username=self.username)))
     self.assertTrue(is_empty(NGOUserProfile.objects.filter(reporter_id=self.reporter_id, org_id=self.org_id)))
Example #9
0
 def _add_text_length_constraint(self, post_dict):
     max_length_from_post = post_dict.get("max_length")
     min_length_from_post = post_dict.get("min_length")
     max_length = max_length_from_post if not is_empty(max_length_from_post) else None
     min_length = min_length_from_post if not is_empty(min_length_from_post) else None
     constraints = []
     if not (max_length is None and min_length is None):
         constraints.append(TextLengthConstraint(min=min_length, max=max_length))
     return constraints
Example #10
0
 def _create_integer_question(self, post_dict, code):
     max_range_from_post = post_dict.get("range_max")
     min_range_from_post = post_dict.get("range_min")
     max_range = max_range_from_post if not is_empty(max_range_from_post) else None
     min_range = min_range_from_post if not is_empty(min_range_from_post) else None
     range = NumericRangeConstraint(min=min_range, max=max_range)
     return IntegerField(name=self._get_name(post_dict), code=code, label=post_dict["title"],
                         constraints=[range], instruction=post_dict.get("instruction"),
                         required=post_dict.get("required"), parent_field_code=post_dict.get('parent_field_code'))
    def validate(self, values, fields=None, dbm=None):
        from mangrove.form_model.form_model import GEO_CODE, LOCATION_TYPE_FIELD_CODE

        if is_empty(case_insensitive_lookup(values, GEO_CODE)) and is_empty(
                case_insensitive_lookup(values, LOCATION_TYPE_FIELD_CODE)):
            errors = OrderedDict()
            errors[GEO_CODE] = 'Please fill out at least one location field correctly.'
            #errors[LOCATION_TYPE_FIELD_CODE] = 'Please fill out at least one location field correctly.'
            return errors
        return OrderedDict()
Example #12
0
def _create_integer_question(post_dict, ddtype):
    max_range_from_post = post_dict.get("range_max")
    min_range_from_post = post_dict.get("range_min")
    max_range = max_range_from_post if not is_empty(
        max_range_from_post) else None
    min_range = min_range_from_post if not is_empty(
        min_range_from_post) else None
    range = NumericConstraint(min=min_range, max=max_range)
    return IntegerField(name=post_dict["title"],
                        code=post_dict["code"].strip(),
                        label="default",
                        range=range,
                        ddtype=ddtype,
                        instruction=post_dict.get("instruction"))
Example #13
0
def _create_text_question(post_dict, ddtype):
    max_length_from_post = post_dict.get("max_length")
    min_length_from_post = post_dict.get("min_length")
    max_length = max_length_from_post if not is_empty(
        max_length_from_post) else None
    min_length = min_length_from_post if not is_empty(
        min_length_from_post) else None
    length = TextConstraint(min=min_length, max=max_length)
    return TextField(name=post_dict["title"],
                     code=post_dict["code"].strip(),
                     label="default",
                     entity_question_flag=post_dict.get("is_entity_question"),
                     length=length,
                     ddtype=ddtype,
                     instruction=post_dict.get("instruction"))
Example #14
0
 def _to_json(self):
     dict = {}
     if self.min is not None:
         dict[ConstraintAttributes.MIN] = self.min
     if self.max is not None:
         dict[ConstraintAttributes.MAX] = self.max
     return ("length", dict) if not is_empty(dict) else ()
 def _get_chars_constraints(self, field):
     constraints = {}
     if not is_empty(field.constraints):
         constraint = field.constraints[0]
         if constraint.max is not None: constraints["max_length"] = constraint.max
         if constraint.min is not None: constraints["min_length"] = constraint.min
     return constraints
Example #16
0
def submit(request):
    dbm = get_database_manager(request)
    post = _get_submission(request.POST)
    success = True
    try:
        web_player = WebPlayer(dbm, SubmissionHandler(dbm))
        message = {
            k: v
            for (k, v) in post.get('message').items() if not is_empty(v)
        }
        if message.get(LOCATION_TYPE_FIELD_CODE) is not None:
            message[LOCATION_TYPE_FIELD_CODE] += COUNTRY
        request = Request(message=message,
                          transportInfo=TransportInfo(
                              transport=post.get('transport'),
                              source=post.get('source'),
                              destination=post.get('destination')))
        response = web_player.accept(request)
        if response.success:
            message = get_success_msg_for_registration_using(response, "web")
        else:
            message = get_submission_error_message_for(response.errors)
        entity_id = response.datarecord_id
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel="web")
        success = False
        entity_id = None
    return HttpResponse(
        json.dumps({
            'success': success,
            'message': message,
            'entity_id': entity_id
        }))
Example #17
0
    def payment_details(self, obj):
        organization = obj.organization
        payment_details = PaymentDetails.objects.filter(organization=organization)
        if not is_empty(payment_details):
            return payment_details[0].preferred_payment

        return "--"
Example #18
0
def count_projects(dbm, include_voided_projects=True):
    if include_voided_projects:
        rows = dbm.load_all_rows_in_view('count_projects', reduce=True, group_level=0)
    else:
        rows = dbm.load_all_rows_in_view('count_projects', reduce=True, group_level=1, key=False)

    return rows[0]['value'] if not is_empty(rows) else 0
Example #19
0
def _validate_post_data(dbm, request):
    form = ReporterRegistrationForm(request.POST)
    message = None
    success = False
    form_errors = []
    form_errors.extend(form.non_field_errors())
    if form.is_valid():
        form_errors = []
        form_data = {k: v for (k, v) in form.cleaned_data.items() if not is_empty(v)}
        try:
            entered_telephone_number = form_data.get("telephone_number")
            tel_number = _get_telephone_number(entered_telephone_number)
            if not helper.unique(dbm, tel_number):
                raise MultipleReportersForANumberException(entered_telephone_number)

            web_player = WebPlayer(dbm, SubmissionHandler(dbm))
            response = web_player.accept(
                Request(message=_get_data(form_data),
                        transportInfo=TransportInfo(transport='web', source='web', destination='mangrove')))
            message = get_success_msg_for_registration_using(response, "web")
            success = True
        except MangroveException as exception:
            form_errors.append(exception.message)
            success = False
    return form, form_errors, message, success
Example #20
0
def import_subjects_from_project_wizard(request, form_code):
    manager = get_database_manager(request.user)
    error_message, failure_imports, success_message, short_code_subject_details_dict = import_module.import_data(
        request, manager, default_parser=XlsOrderedParser, form_code=form_code)
    subject_details = {}

    if len(short_code_subject_details_dict) != 0:
        detail_dict = dict()
        form_model = get_form_model_by_code(manager, form_code)
        entity_type = form_model.entity_type[0]
        short_codes = []
        for short_code in short_code_subject_details_dict.keys():
            short_codes.append(short_code)

        subject_details = _format_imported_subjects_datetime_field_to_str(
            form_model, short_code_subject_details_dict)
        detail_dict.update({entity_type: "[%s]" % ", ".join(short_codes)})
        UserActivityLog().log(request,
                              action=IMPORTED_IDENTIFICATION_NUMBER,
                              detail=json.dumps(detail_dict))

    return HttpResponse(
        json.dumps({
            'success':
            error_message is None and is_empty(failure_imports),
            'message':
            success_message,
            'error_message':
            error_message,
            'failure_imports':
            failure_imports,
            'successful_imports':
            subject_details,
        }))
Example #21
0
    def post(self, request, *args, **kwargs):
        parser_dict = {
            '.xls': XlsDatasenderParser,
            '.xlsx': XlsxDataSenderParser
        }
        manager = get_database_manager(request.user)
        file_extension = os.path.splitext(request.GET["qqfile"])[1]
        parser = parser_dict.get(file_extension, None)
        is_update = request.GET.get('is_update') == 'true'
        error_message, failure_imports, success_message, successful_imports = import_module.import_data(
            request, manager, default_parser=parser, is_update=is_update)

        imported_data_senders = parse_successful_imports(successful_imports)
        self._convert_anonymous_submissions_to_registered(
            imported_data_senders, manager)

        self.update_activity_log(request, successful_imports)
        transaction.commit(
        )  #Mandatory for manually managed transaction blocks. Here it won't save anything

        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'successful_imports':
                imported_data_senders
            }))
Example #22
0
def _validate_post_data(dbm, request):
    form = ReporterRegistrationForm(request.POST)
    message = None
    success = False
    form_errors = []
    form_errors.extend(form.non_field_errors())
    if form.is_valid():
        form_errors = []
        form_data = {
            k: v
            for (k, v) in form.cleaned_data.items() if not is_empty(v)
        }
        try:
            entered_telephone_number = form_data.get("telephone_number")
            tel_number = _get_telephone_number(entered_telephone_number)
            if not helper.unique(dbm, tel_number):
                raise MultipleReportersForANumberException(
                    entered_telephone_number)

            web_player = WebPlayer(dbm, SubmissionHandler(dbm))
            response = web_player.accept(
                Request(message=_get_data(form_data),
                        transportInfo=TransportInfo(transport='web',
                                                    source='web',
                                                    destination='mangrove')))
            message = get_success_msg_for_registration_using(response, "web")
            success = True
        except MangroveException as exception:
            form_errors.append(exception.message)
            success = False
    return form, form_errors, message, success
Example #23
0
def import_subjects_from_project_wizard(request, form_code):
    manager = get_database_manager(request.user)
    error_message, failure_imports, success_message, imported_entities, successful_imports = import_module.import_data(
        request, manager, default_parser=XlsOrderedParser, form_code=form_code)
    if len(imported_entities) != 0:
        detail_dict = dict()
        for short_code, entity_type in imported_entities.items():
            entity_type = entity_type.capitalize()
            if detail_dict.get(entity_type) is not None:
                detail_dict.get(entity_type).append(short_code)
            else:
                detail_dict.update({entity_type: [short_code]})
        for key, detail in detail_dict.items():
            detail_dict.update({key: "[%s]" % ", ".join(detail)})
        UserActivityLog().log(request,
                              action=IMPORTED_SUBJECTS,
                              detail=json.dumps(detail_dict))

    subjects_data = import_module.load_all_subjects(manager)

    return HttpResponse(
        json.dumps({
            'success':
            error_message is None and is_empty(failure_imports),
            'message':
            success_message,
            'error_message':
            error_message,
            'failure_imports':
            failure_imports,
            'all_data':
            subjects_data,
            'imported':
            imported_entities.keys()
        }))
Example #24
0
    def handle(self, *args, **options):
        if is_empty(args):
            print "give atleast one organization id. the command is python manage.py delete_org <id1 id2 ...>"

        for org_id in args:
            try:
                organization = Organization.objects.get(org_id=org_id)
                organization_setting = OrganizationSetting.objects.get(
                    organization=organization)
                document_store = organization_setting.document_store
                self._delete_users_and_related_data(org_id)

                print "deleted organization users and related information for %s" % (
                    org_id, )

                organization_setting.delete()
                organization.delete()

                print "deleted organization registration information for %s" % (
                    org_id, )

                self._delete_couchdb_database(document_store)

                print "deleted submission, datarecords, form models for %s" % (
                    org_id, )
                print "deleted organization complete for %s" % (org_id, )

            except Exception as e:
                print e
                print "error in deleting organization %s" % (org_id, )
 def _get_number_constraints(self, field):
     constraints = {}
     if not is_empty(field.constraints):
         constraint = field.constraints[0]
         if constraint.max is not None: constraints["max_value"] = float(constraint.max)
         if constraint.min is not None: constraints["min_value"] = float(constraint.min)
     return constraints
Example #26
0
def get_success_msg_for_submission_using(response):
    reporters = response.reporters
    thanks = success_messages[SUBMISSION] % reporters[0].get(
        NAME_FIELD) if not is_empty(
            reporters) else success_messages[SUBMISSION] % ""
    expanded_response = get_expanded_response(response.processed_data)
    return thanks + expanded_response
Example #27
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)
Example #28
0
 def forwards(self, orm):
     for org in orm.Organization.objects.all():
         if not org.status:
             status = 'Activated'
             status_changed_datetime = datetime.datetime.now() if is_empty(org.active_date) else org.active_date
             org.status_changed_datetime = status_changed_datetime
             org.status = status
             org.save()
Example #29
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)
Example #30
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
Example #31
0
 def _get_location_data(self, values):
     display_location, geo_code = values.get(LOCATION_TYPE_FIELD_CODE), values.get(GEO_CODE)
     location_hierarchy = self._get_location_heirarchy_from_location_name(display_location)
     tree = self.location_tree
     if location_hierarchy is None and not is_empty(geo_code):
         try:
             lat_string, long_string = tuple(geo_code.split())
             location_hierarchy = tree.get_location_hierarchy_for_geocode(lat=float(lat_string), long=float(long_string))
         except ValueError as e:
             raise GeoCodeFormatException(e.args)
     elif location_hierarchy is not None and is_empty(geo_code):
         try:
             translated_geo_code = tree.get_centroid(display_location.split(',')[0],len(location_hierarchy)-1)
             values[GEO_CODE] = "%s %s" % (translated_geo_code[1], translated_geo_code[0])
         except Exception:
             pass
     values[LOCATION_TYPE_FIELD_CODE] = location_hierarchy
Example #32
0
def _entity_by_short_code(dbm, short_code, entity_type):
    rows = dbm.view.entity_by_short_code(key=[entity_type, short_code],
                                         include_docs=True)
    if is_empty(rows):
        raise DataObjectNotFound("Entity", "Unique Identification Number (ID)",
                                 short_code)
    doc = EntityDocument.wrap(rows[0]['doc'])
    return Entity.new_from_doc(dbm, doc)
Example #33
0
 def _geo_code_validations(self, b):
     msg = "Incorrect GPS format. The GPS coordinates must be in the following format: xx.xxxx yy.yyyy. Example -18.8665 47.5315"
     geo_code_string = b.strip()
     geo_code_string = (' ').join(geo_code_string.split())
     if not is_empty(geo_code_string):
         lat_long = geo_code_string.split(' ')
         self._geo_code_format_validations(lat_long, msg)
         self.cleaned_data['geo_code'] = geo_code_string
Example #34
0
 def _geo_code_validations(self, b):
     msg = "Incorrect GPS format. The GPS coordinates must be in the following format: xx.xxxx yy.yyyy. Example -18.8665 47.5315"
     geo_code_string = b.strip()
     geo_code_string = (' ').join(geo_code_string.split())
     if not is_empty(geo_code_string):
         lat_long = geo_code_string.split(' ')
         self._geo_code_format_validations(lat_long, msg)
         self.cleaned_data['geo_code'] = geo_code_string
Example #35
0
def crs_model_creator(data_record_id, submission_data, model, question_mapping=None, defaults=None):
    exists = model.objects.filter(data_record_id=data_record_id)
    if not is_empty(exists):
        return
    if not question_mapping: question_mapping = {}
    data_record = {'data_record_id': data_record_id}
    if not defaults: defaults = data_record
    else:   defaults.update(data_record)
    _save_submission_via_model( submission_data, model,question_mapping, defaults)
Example #36
0
 def forwards(self, orm):
     for org in orm.OrganizationSetting.objects.all():
         if org.smsc is not None and not is_empty(org.sms_tel_number):
             outgoing_obj = orm.OutgoingNumberSetting.objects.create(
                 phone_number=org.sms_tel_number.split(',')[0].strip(),
                 smsc=org.smsc)
             outgoing_obj.save()
             org.outgoing_number = outgoing_obj
             org.save()
Example #37
0
 def _parse_tokens(self, tokens):
     tokens = [token.strip() for token in tokens if token]
     submission = {}
     for token in tokens:
         if is_empty(token): continue
         field_code, answer = self._parse_token(token)
         if field_code in submission.keys():
             raise MultipleSubmissionsForSameCodeException(field_code)
         submission[field_code] = answer
     return submission
Example #38
0
 def _validate_unique_phone_number_for_reporter(self, submission):
     if submission.cleaned_data.get(ENTITY_TYPE_FIELD_CODE) == [REPORTER] and submission.form_model.is_registration_form():
         phone_number = submission.cleaned_data.get(MOBILE_NUMBER_FIELD_CODE)
         if is_empty(phone_number):
             raise MobileNumberMissing()
         actual_number = self._get_telephone_number(phone_number)
         try:
             find_reporter_entity(self.dbm, actual_number)
             raise MultipleReportersForANumberException(from_number=phone_number)
         except NumberNotRegisteredException:
             submission.cleaned_data[MOBILE_NUMBER_FIELD_CODE] = actual_number
Example #39
0
 def _get_location_heirarchy_from_location_name(self, display_location):
     if is_empty(display_location):
         return None
     display_location_list = display_location.lower().split(',')
     if len(display_location_list) > 1:
         display_location_list.reverse()
         return display_location_list
     lowest_level_location = display_location_list[0]
     tree = self.location_tree
     location_hierarchy = tree.get_hierarchy_path(lowest_level_location)
     return location_hierarchy
Example #40
0
    def _load_document(self, id, document_class=DocumentBase):
        """
        Load a document from the DB into an in memory document object.

        Low level interface does not create wrapping DataObject

        """
        if is_empty(id):
            return None
        else:
            return document_class.load(self.database, id=id)
Example #41
0
    def _parse_header(self, dict_reader):
        field_header = dict_reader.fieldnames

        if is_empty(field_header):
            raise CSVParserInvalidHeaderFormatException()

        self._remove_trailing_empty_header_field(field_header)

        if self._has_empty_values(field_header):
            raise CSVParserInvalidHeaderFormatException()

        return [field.strip().lower() for field in field_header]
Example #42
0
def get_exception_message_for(exception, channel=None):
    ex_type = type(exception)
    if channel is not None:
        message_dict = exception_messages.get(ex_type)
        if message_dict is None:
            return exception.message
        message = message_dict.get(channel)
        if is_empty(message):
            message = exception_messages[ex_type].get(DEFAULT)
    else:
        message = exception_messages[ex_type][DEFAULT]
    if isinstance(exception,MangroveException) and exception.data is not None and "%s" in message:
        return message % exception.data
    return message
Example #43
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
Example #44
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)
Example #45
0
def submit(request):
    dbm = get_database_manager(request)
    post = _get_submission(request.POST)
    success = True
    try:
        web_player = WebPlayer(dbm, SubmissionHandler(dbm))
        message = {k: v for (k, v) in post.get('message').items() if not is_empty(v)}
        if message.get(LOCATION_TYPE_FIELD_CODE) is not None:
            message[LOCATION_TYPE_FIELD_CODE]+= COUNTRY
        request = Request(message=message,
                          transportInfo=TransportInfo(transport=post.get('transport'), source=post.get('source'),
                                                      destination=post.get('destination')))
        response = web_player.accept(request)
        if response.success:
            message = get_success_msg_for_registration_using(response, "web")
        else:
            message = get_submission_error_message_for(response.errors)
        entity_id = response.datarecord_id
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel="web")
        success = False
        entity_id = None
    return HttpResponse(json.dumps({'success': success, 'message': message, 'entity_id': entity_id}))
Example #46
0
def _short_code_not_in(entity_q_code, values):
    return is_empty(values.get(entity_q_code))
Example #47
0
 def _is_empty(self, row):
     return len([value for value in row if not is_empty(value)]) == 0
Example #48
0
 def _is_header_row(self, row):
     if is_empty(row[0]):
         return None, False
     self._remove_trailing_empty_header_field(row)
     return [unicode(value).strip().lower() for value in row], True
Example #49
0
 def _remove_trailing_empty_header_field(self, field_header):
     for field in field_header[::-1]:
         if is_empty(field):
             field_header.pop()
         else:
             break
Example #50
0
 def test_should_return_empty_dict_for_empty_integer_constraint(self):
     constraint = NumericConstraint()
     actual_dict = constraint._to_json()
     self.assertTrue(is_empty(actual_dict))
Example #51
0
def get_success_msg_for_submission_using(response):
    reporters = response.reporters
    thanks = success_messages[SUBMISSION] % reporters[0].get(NAME_FIELD) if not is_empty(reporters) else success_messages[SUBMISSION] % ""
    expanded_response = get_expanded_response(response.processed_data)
    return thanks + expanded_response
Example #52
0
 def test_should_return_empty_dict_for_empty_text_constraint(self):
     constraint = TextConstraint()
     actual_dict = constraint._to_json()
     self.assertTrue(is_empty(actual_dict))
Example #53
0
 def _has_empty_values(self, values_list):
     for value in values_list:
         if is_empty(value):
             return True
     return False