Ejemplo n.º 1
0
def to_text(value):
    if value is None:
        return ''
    if not isinstance(value, six.string_types):
        return six.text_type(value)
    soft_assert_type_text(value)
    return value
Ejemplo n.º 2
0
    def handle(self, **options):
        prefix = os.getcwd()

        if options['clear']:
            print("clearing resource cache")
            rcache.delete_pattern(RESOURCE_PREFIX % '*')

        current_sha = self.current_sha()
        existing_resources = rcache.get(RESOURCE_PREFIX % current_sha, None)
        if existing_resources and not isinstance(existing_resources, six.string_types):
            print("getting resource dict from cache")
            self.output_resources(existing_resources)
            return
        if isinstance(existing_resources, six.string_types):
            soft_assert_type_text(existing_resources)

        resources = {}
        for finder in finders.get_finders():
            for path, storage in finder.list(['.*', '*~', '* *']):
                if not storage.location.startswith(prefix):
                    continue
                if not getattr(storage, 'prefix', None):
                    url = path
                else:
                    url = os.path.join(storage.prefix, path)
                filename = os.path.join(storage.location, path)
                resources[url] = self.get_hash(filename)
        self.overwrite_resources(resources, sha=current_sha)
Ejemplo n.º 3
0
def date(node):
    assert node.name == 'date'
    if len(node.args) != 1:
        raise XPathFunctionException(_("The \"date\" function only accepts a single argument"))
    arg = node.args[0]

    if isinstance(arg, int):
        return (datetime.date(1970, 1, 1) + datetime.timedelta(days=arg)).strftime("%Y-%m-%d")

    if isinstance(arg, six.string_types):
        soft_assert_type_text(arg)
        try:
            parsed_date = parse_date(arg)
        except ValueError:
            raise XPathFunctionException(_("{} is not a valid date").format(arg))

        if parsed_date is None:
            raise XPathFunctionException(
                _("The \"date\" function only accepts strings of the format \"YYYY-mm-dd\"")
            )

        return arg

    raise XPathFunctionException(
        "The \"date\" function only accepts integers or strings of the format \"YYYY-mm-dd\""
    )
Ejemplo n.º 4
0
    def _validate_toplevel_fields(self, data, field_defs):
        """
        :param data: any dictionary of data
        :param field_defs: a list of FieldDefinition namedtuples representing the names
        and types of the fields to validate
        """
        for field_def in field_defs:
            if field_def.name not in data and field_def.required:
                raise SelfRegistrationValidationException(
                    {field_def.name: 'This field is required'}
                )

            if field_def.name in data:
                if isinstance(data[field_def.name], six.string_types):
                    soft_assert_type_text(data[field_def.name])
                if not isinstance(data[field_def.name], field_def.type):
                    if isinstance(field_def.type, tuple):
                        if len(field_def.type) > 1:
                            raise SelfRegistrationValidationException(
                                {field_def.name: 'Expected type in {}'.format(
                                    ', '.join(t.__name__ for t in field_def.type)
                                )}
                            )
                        else:
                            type_name = field_def.type[0].__name__
                    else:
                        type_name = field_def.type.__name__
                    raise SelfRegistrationValidationException(
                        {field_def.name: 'Expected type: {}'.format(type_name)}
                    )
Ejemplo n.º 5
0
    def full_hydrate(self, bundle):
        if not self.is_valid(bundle):
            raise ImmediateHttpResponse(response=self.error_response(bundle.request, bundle.errors))

        data = bundle.data

        custom_registration_message = data.get('custom_registration_message')
        if isinstance(custom_registration_message, six.string_types):
            soft_assert_type_text(custom_registration_message)
            custom_registration_message = custom_registration_message.strip()
            if not custom_registration_message:
                custom_registration_message = None

        obj = SelfRegistrationInfo(
            data.get('app_id'),
            data.get('android_only', False),
            data.get('require_email', False),
            custom_registration_message
        )
        for user_info in data.get('users', []):
            obj.add_user(SelfRegistrationUserInfo(
                user_info.get('phone_number'),
                user_info.get('custom_user_data')
            ))
        bundle.obj = obj
        return bundle
Ejemplo n.º 6
0
def reverse_index_case_query(case_ids, identifier=None):
    """Fetches related cases related by `identifier`.

    For example, in a regular parent/child relationship, given a list of parent
    case ids, this will return all the child cases which point to the parents
    with identifier `parent`.

    """
    if isinstance(case_ids, six.string_types):
        soft_assert_type_text(case_ids)
        case_ids = [case_ids]

    if identifier is None:      # some old relationships don't have an identifier specified
        f = filters.term('{}.{}'.format(INDICES_PATH, REFERENCED_ID), list(case_ids)),
    else:
        f = filters.AND(
            filters.term('{}.{}'.format(INDICES_PATH, REFERENCED_ID), list(case_ids)),
            filters.term('{}.{}'.format(INDICES_PATH, IDENTIFIER), identifier),
        )
    return queries.nested(
        INDICES_PATH,
        queries.filtered(
            queries.match_all(),
            f
        )
    )
Ejemplo n.º 7
0
def _is_list_like(val):
    if six.PY2 and isinstance(val, bytes):
        val = val.decode('utf-8')
    if isinstance(val, (six.text_type, bytes)):
        soft_assert_type_text(val)
    return (isinstance(val, collections.Iterable) and
            not isinstance(val, six.string_types))
Ejemplo n.º 8
0
 def configure(self, compiled_properties):
     for key in compiled_properties:
         if not isinstance(key, (six.text_type, bytes)):
             raise BadSpecError("Properties in a dict expression must be strings!")
         if six.PY3:
             soft_assert_type_text(key)
     self._compiled_properties = compiled_properties
Ejemplo n.º 9
0
    def __init__(self, domain, config_or_config_id, filters, aggregation_columns, columns, order_by):
        from corehq.apps.userreports.models import DataSourceConfiguration
        from corehq.apps.aggregate_ucrs.models import AggregateTableDefinition
        self.lang = None
        self.domain = domain
        if isinstance(config_or_config_id, DataSourceConfiguration):
            self._config = config_or_config_id
            self._config_id = self._config._id
        elif isinstance(config_or_config_id, AggregateTableDefinition):
            self._config = config_or_config_id
            self._config_id = config_or_config_id.id
        else:
            assert isinstance(config_or_config_id, six.string_types), \
                '{} is not an allowed type'.format(type(config_or_config_id))
            soft_assert_type_text(config_or_config_id)
            self._config = None
            self._config_id = config_or_config_id

        self._filters = {f['slug']: f for f in filters}
        self._filter_values = {}
        self._defer_fields = {}
        self._order_by = order_by
        self._aggregation_columns = aggregation_columns
        self._column_configs = OrderedDict()
        for column in columns:
            # should be caught in validation prior to reaching this
            assert column.column_id not in self._column_configs, \
                'Report {} in domain {} has more than one {} column defined!'.format(
                    self._config_id, self.domain, column.column_id,
                )
            self._column_configs[column.column_id] = column
        self._engine_id = None
Ejemplo n.º 10
0
def send_html_email_async(self, subject, recipient, html_content,
                          text_content=None, cc=None,
                          email_from=settings.DEFAULT_FROM_EMAIL,
                          file_attachments=None, bcc=None, smtp_exception_skip_list=None):
    """ Call with send_HTML_email_async.delay(*args, **kwargs)
    - sends emails in the main celery queue
    - if sending fails, retry in 15 min
    - retry a maximum of 10 times
    """
    try:
        send_HTML_email(subject, recipient, html_content,
                        text_content=text_content, cc=cc, email_from=email_from,
                        file_attachments=file_attachments, bcc=bcc,
                        smtp_exception_skip_list=smtp_exception_skip_list)
    except Exception as e:
        from corehq.util.python_compatibility import soft_assert_type_text
        if isinstance(recipient, six.string_types):
            soft_assert_type_text(recipient)
        recipient = list(recipient) if not isinstance(recipient, six.string_types) else [recipient]
        notify_exception(
            None,
            message="Encountered error while sending email",
            details={
                'subject': subject,
                'recipients': ', '.join(recipient),
                'error': e,
            }
        )
        self.retry(exc=e)
Ejemplo n.º 11
0
def edit_form_actions(request, domain, app_id, form_unique_id):
    app = get_app(domain, app_id)
    form = app.get_form(form_unique_id)
    module = form.get_module()
    old_load_from_form = form.actions.load_from_form
    form.actions = FormActions.wrap(json.loads(request.POST['actions']))
    add_properties_to_data_dictionary(domain, module.case_type, list(form.actions.update_case.update.keys()))
    if old_load_from_form:
        form.actions.load_from_form = old_load_from_form

    for condition in (form.actions.open_case.condition, form.actions.close_case.condition):
        if isinstance(condition.answer, six.string_types):
            soft_assert_type_text(condition.answer)
            condition.answer = condition.answer.strip('"\'')
    form.requires = request.POST.get('requires', form.requires)
    if actions_use_usercase(form.actions):
        if not is_usercase_in_use(domain):
            enable_usercase(domain)
        add_properties_to_data_dictionary(domain, USERCASE_TYPE, list(form.actions.usercase_update.update.keys()))

    response_json = {}
    app.save(response_json)
    response_json['propertiesMap'] = get_all_case_properties(app)
    response_json['usercasePropertiesMap'] = get_usercase_properties(app)
    return json_response(response_json)
Ejemplo n.º 12
0
def domain_save_callback(sender, domain, **kwargs):
    if isinstance(domain, six.string_types):
        soft_assert_type_text(domain)
        domain_name = domain
    else:
        domain_name = domain.name
    update_subscription_properties_by_domain(domain_name)
Ejemplo n.º 13
0
 def _convert_columns_to_properly_dicts(cols):
     for column in cols:
         if isinstance(column, six.string_types):
             soft_assert_type_text(column)
             yield {'column_id': column, 'display': column}
         else:
             yield column
Ejemplo n.º 14
0
 def digest(self, alphabet="0123456789"):
     b = len(alphabet)
     answer = [alphabet[i] for i in to_base(self.number, b)]
     if isinstance(alphabet, six.string_types):
         soft_assert_type_text(alphabet)
         answer = ''.join(answer)
     return answer
Ejemplo n.º 15
0
 def phone_number_is_valid(self):
     if isinstance(self.phone_number, six.string_types):
         soft_assert_type_text(self.phone_number)
     return (
         isinstance(self.phone_number, six.string_types) and
         re.compile(r'^\d+$').match(self.phone_number) is not None
     )
Ejemplo n.º 16
0
    def validate_string(self, data, field_name):
        value = data.get(field_name)
        error_msg = "Field '{}' is required and expected to be a string".format(field_name)

        if not isinstance(value, six.string_types):
            raise OrderStatusValidationError(error_msg)
        soft_assert_type_text(value)
Ejemplo n.º 17
0
 def _wrapped(*args, **kwargs):
     if 'domain' in kwargs:
         domain = kwargs['domain']
     elif (
             len(args) > 2 and
             isinstance(args[0], View) and
             isinstance(args[1], HttpRequest) and
             isinstance(args[2], six.string_types)
     ):
         soft_assert_type_text(args[2])
         # class-based view; args == (self, request, domain, ...)
         domain = args[2]
     elif (
             len(args) > 1 and
             isinstance(args[0], HttpRequest) and
             isinstance(args[1], six.string_types)
     ):
         soft_assert_type_text(args[1])
         # view function; args == (request, domain, ...)
         domain = args[1]
     else:
         domain = None
     if _soft_assert(
             domain,
             'Unable to track_domain_request("{prop}") on view "{view}". Unable to determine domain from '
             'args {args}.'.format(prop=calculated_prop, view=view_func.__name__, args=args)
     ):
         DomainAuditRecordEntry.update_calculations(domain, calculated_prop)
     return view_func(*args, **kwargs)
Ejemplo n.º 18
0
    def new_question(self, name, label, data_type='string', group=None, choices=None, label_safe=False, **params):
        """
        Adds a question to the XForm.

        Assumes that questions are added in a sane order. You can't add a
        question to a group before you add the group.

        :param name: Question name
        :param label: Question label
        :param data_type: The type of the question, or None for hidden values
        :param group: The name of the question's group, or an iterable of names if nesting is deeper than one
        :param choices: A dictionary of {name: label} pairs
        :param label_safe: Does the label contain (safe) XML?
        :param params: Supported question parameters: repeat_count, QUESTION_PARAMS
        :return: A Question instance, or QuestionGroup instance if `data_type` is a group type.
        """
        if data_type is not None and data_type not in ODK_TYPES + GROUP_TYPES:
            raise TypeError('Unknown question data type "{}"'.format(data_type))
        if isinstance(group, six.string_types):
            soft_assert_type_text(group)
        if group is not None and not isinstance(group, six.string_types) and not hasattr(group, '__iter__'):
            raise TypeError('group parameter needs to be a string or iterable')
        groups = [group] if isinstance(group, six.string_types) else group
        self._append_to_data(name, groups)
        self._append_to_model(name, data_type, groups, **params)
        if data_type is not None:
            self._append_to_translation(name, label, groups, choices, label_safe, **params)
            self._append_to_body(name, data_type, groups, choices, **params)
        if data_type in GROUP_TYPES:
            return QuestionGroup(name, self, groups)
        return Question(name, self, groups)
Ejemplo n.º 19
0
def _assert_user_types(user_types):
    if isinstance(user_types, six.string_types):
        soft_assert_type_text(user_types)
        user_types = [user_types]

    for type_ in user_types:
        assert type_ in USER_TYPES, "Expected user type to be in {}, got {}".format(USER_TYPES, type_)
Ejemplo n.º 20
0
def scroll_case_ids_by_domain_and_case_type(domain, case_type, chunk_size=100):
    """
    Retrieves the case ids in chunks, yielding a list of case ids each time
    until there are none left.

    Only filters on domain and case type, and includes both open and closed cases.
    """
    query = (CaseES()
             .domain(domain)
             .case_type(case_type)
             .exclude_source()
             .size(chunk_size))

    result = []

    for case_id in query.scroll():
        if not isinstance(case_id, six.string_types):
            raise ValueError("Something is wrong with the query, expected ids only")
        soft_assert_type_text(case_id)

        result.append(case_id)
        if len(result) >= chunk_size:
            yield result
            result = []

    if result:
        yield result
Ejemplo n.º 21
0
def verification_response_ok(text, verification_keywords):
    if not isinstance(text, six.string_types):
        return False
    soft_assert_type_text(text)

    text = text.lower()
    return any([keyword in text for keyword in verification_keywords])
Ejemplo n.º 22
0
def _id_from_doc(doc_or_doc_id):
    if isinstance(doc_or_doc_id, six.string_types):
        soft_assert_type_text(doc_or_doc_id)
        doc_id = doc_or_doc_id
    else:
        doc_id = doc_or_doc_id.get_id if doc_or_doc_id else None
    return doc_id
Ejemplo n.º 23
0
        def _get_any_select_question_node(tag, name_, groups_=None, choices_=None, **params_):
            """
            Return a question node for a single- or multiple-select multiple choice question

            e.g.
                <select ref="/data/multiple_answer_multichoice">
                    <label ref="jr:itext('multiple_answer_multichoice-label')" />
                    <item>
                        <label ref="jr:itext('multiple_answer_multichoice-choice1-label')" />
                        <value>choice1</value>
                    </item>
                    <item>
                        <label ref="jr:itext('multiple_answer_multichoice-choice2-label')" />
                        <value>choice2</value>
                    </item>
                </select>
            """
            node_ = etree.Element(tag, {'ref': self.get_data_ref(name_, groups_)})
            node_.append(E.label({'ref': "jr:itext('{}')".format(self.get_text_id(name_, groups_))}))
            if 'hint' in params_:
                node_.append(
                    E.hint({'ref': "jr:itext('{}')".format(self.get_text_id(name_, groups_, is_hint=True))})
                )
            for choice_name in choices_.keys():
                if isinstance(choice_name, six.string_types):
                    soft_assert_type_text(choice_name)
                node_.append(
                    E.item(
                        E.label({'ref': "jr:itext('{}')".format(self.get_text_id(name_, groups_, choice_name))}),
                        E.value(choice_name if isinstance(choice_name, six.string_types) else str(choice_name))
                    )
                )
            return node_
Ejemplo n.º 24
0
def get_case_ids_in_domain(domain, type=None):
    if type is None:
        type_keys = [[]]
    elif isinstance(type, (list, tuple)):
        soft_assert('skelly@{}'.format('dimagi.com'))(
            False, 'get_case_ids_in_domain called with typle / list arg for type'
        )
        type_keys = [[t] for t in type]
    elif isinstance(type, six.string_types):
        soft_assert_type_text(type)
        type_keys = [[type]]
    else:
        raise ValueError(
            "Argument type should be a string, tuple, or None: {!r}"
            .format(type)
        )
    return [
        res['id'] for type_key in type_keys
        for res in CommCareCase.get_db().view(
            'case_types_by_domain/view',
            startkey=[domain] + type_key,
            endkey=[domain] + type_key + [{}],
            reduce=False,
            include_docs=False,
        )
    ]
Ejemplo n.º 25
0
def populate_updated_fields(config, row):
    """
    Returns a dict map of fields that were marked to be updated
    due to the import. This can be then used to pass to the CaseBlock
    to trigger updates.
    """
    field_map = convert_custom_fields_to_struct(config)
    fields_to_update = {}
    for key in field_map:
        try:
            update_value = row[key]
        except Exception:
            continue

        if 'field_name' in field_map[key]:
            update_field_name = field_map[key]['field_name'].strip()
        else:
            # nothing was selected so don't add this value
            continue

        if update_field_name in RESERVED_FIELDS:
            raise InvalidCustomFieldNameException(_('Field name "{}" is reserved').format(update_field_name))

        if isinstance(update_value, six.string_types) and update_value.strip() == SCALAR_NEVER_WAS:
            soft_assert_type_text(update_value)
            # If we find any instances of blanks ('---'), convert them to an
            # actual blank value without performing any data type validation.
            # This is to be consistent with how the case export works.
            update_value = ''
        elif update_value is not None:
            update_value = convert_field_value(update_value)

        fields_to_update[update_field_name] = update_value

    return fields_to_update
Ejemplo n.º 26
0
    def render(self, name, value, attrs=None, renderer=None):
        if isinstance(value, six.string_types):
            soft_assert_type_text(value)
            # It's probably invalid JSON
            return super(JsonWidget, self).render(name, value, attrs, renderer)

        return super(JsonWidget, self).render(name, json.dumps(value, indent=2), attrs, renderer)
Ejemplo n.º 27
0
def strip_plus(phone_number):
    if (isinstance(phone_number, six.string_types) and len(phone_number) > 0
            and phone_number[0] == "+"):
        soft_assert_type_text(phone_number)
        return phone_number[1:]
    else:
        return phone_number
Ejemplo n.º 28
0
def get_referenced_class(class_or_str):
    # Simplified from https://github.com/toastdriven/django-tastypie/blob/master/tastypie/fields.py#L519

    if isinstance(class_or_str, six.string_types):
        soft_assert_type_text(class_or_str)
        return dimagi.utils.modules.to_function(class_or_str)
    else:
        return class_or_str
Ejemplo n.º 29
0
def _translate_setting(setting, prop):
    value = setting[prop]
    if not isinstance(value, six.string_types):
        return [ugettext(v) for v in value]
    else:
        if six.PY3:
            soft_assert_type_text(value)
        return ugettext(value)
Ejemplo n.º 30
0
    def __call__(self, v):
        if isinstance(self.attribute, six.string_types):
            soft_assert_type_text(self.attribute)
            accessor = lambda v: getattr(v, self.attribute)
        else:
            accessor = self.attribute

        return accessor(v)
Ejemplo n.º 31
0
def get_date(value):
    if isinstance(value, date):
        if isinstance(value, datetime):
            return value.date()

        return value

    if not isinstance(value, six.string_types):
        raise TypeError("Expected date, datetime, or string")
    soft_assert_type_text(value)

    if not re.match(r'^\d{4}-\d{2}-\d{2}', value):
        raise ValueError("Expected a date string")

    return datetime.strptime(value, '%Y-%m-%d').date()
Ejemplo n.º 32
0
 def split_path(path):
     table = []
     column = []
     id = []
     for k in path:
         if isinstance(k, six.string_types):
             soft_assert_type_text(k)
             if k:
                 column.append(k)
         else:
             table.extend(column)
             table.append(INT)
             column = []
             id.append(k)
     return (tuple(table), tuple(column), tuple(id))
Ejemplo n.º 33
0
def parse_params(params, location=None):
    """
    Inserts date and OpenMRS location UUID into report params
    """
    today = datetime.today().strftime('%Y-%m-%d')
    location_uuid = location.metadata[LOCATION_OPENMRS] if location else None

    parsed = {}
    for key, value in params.items():
        if isinstance(value, six.string_types) and '{{' in value:
            soft_assert_type_text(value)
            template = Template(value)
            value = template.render(today=today, location=location_uuid)
        parsed[key] = value
    return parsed
Ejemplo n.º 34
0
 def _get_indicator(self,
                    ui_aggregation,
                    is_multiselect_chart_report=False):
     # ui_aggregation parameter is never used because we need not infer the data type
     # self._question_source is a tuple of (identifier, datatype)
     identifier = self._meta_property_spec[0]
     if isinstance(identifier, six.string_types):
         soft_assert_type_text(identifier)
         identifier = [identifier]
     identifier = "/".join(identifier)
     column_id = get_column_name(identifier.strip("/"))
     return make_form_meta_block_indicator(
         self._meta_property_spec,
         column_id,
         root_doc=is_multiselect_chart_report)
Ejemplo n.º 35
0
 def data_as_dict(self):
     """
     Convert 'a=b,c=d' to {'a': 'b', 'c': 'd'}
     """
     result = {}
     if isinstance(self.data, six.string_types):
         soft_assert_type_text(self.data)
         items = self.data.strip().split(',')
         for item in items:
             parts = item.partition('=')
             key = parts[0].strip().upper()
             value = parts[2].strip()
             if value:
                 result[key] = value
     return result
Ejemplo n.º 36
0
def send_sms(domain,
             contact,
             phone_number,
             text,
             metadata=None,
             logged_subevent=None):
    """
    Sends an outbound SMS. Returns false if it fails.
    """
    if phone_number is None:
        return False
    if isinstance(phone_number, six.integer_types):
        phone_number = str(phone_number)
    phone_number = clean_phone_number(phone_number)

    msg = get_sms_class()(domain=domain,
                          phone_number=phone_number,
                          direction=OUTGOING,
                          date=get_utcnow(),
                          backend_id=None,
                          location_id=get_location_id_by_contact(
                              domain, contact),
                          text=text)
    if contact:
        msg.couch_recipient = contact.get_id
        msg.couch_recipient_doc_type = contact.doc_type

    if domain and contact and is_commcarecase(contact):
        backend_name = contact.get_case_property('contact_backend_id')
        backend_name = backend_name.strip() if isinstance(
            backend_name, six.string_types) else ''
        soft_assert_type_text(backend_name)
        if backend_name:
            try:
                backend = SQLMobileBackend.load_by_name(
                    SQLMobileBackend.SMS, domain, backend_name)
            except BadSMSConfigException as e:
                if logged_subevent:
                    logged_subevent.error(
                        MessagingEvent.ERROR_GATEWAY_NOT_FOUND,
                        additional_error_text=six.text_type(e))
                return False

            msg.backend_id = backend.couch_id

    add_msg_tags(msg, metadata)

    return queue_outgoing_sms(msg)
Ejemplo n.º 37
0
    def wrap(cls, obj):
        if not obj["doc_type"] == "FixtureDataType":
            raise ResourceNotFound
        # Migrate fixtures without attributes on item-fields to fields with attributes
        if obj["fields"] and isinstance(obj['fields'][0], six.string_types):
            soft_assert_type_text(obj['fields'][0])
            obj['fields'] = [{
                'field_name': f,
                'properties': []
            } for f in obj['fields']]

        # Migrate fixtures without attributes on items to items with attributes
        if 'item_attributes' not in obj:
            obj['item_attributes'] = []

        return super(FixtureDataType, cls).wrap(obj)
Ejemplo n.º 38
0
def get_pillow_config_from_setting(section, pillow_config_string_or_dict):
    if isinstance(pillow_config_string_or_dict, six.string_types):
        soft_assert_type_text(pillow_config_string_or_dict)
        return PillowConfig(section,
                            pillow_config_string_or_dict.rsplit('.', 1)[1],
                            pillow_config_string_or_dict, None, {})
    else:
        assert 'class' in pillow_config_string_or_dict
        class_name = pillow_config_string_or_dict['class']
        return PillowConfig(
            section,
            pillow_config_string_or_dict.get('name', class_name),
            class_name,
            pillow_config_string_or_dict.get('instance', None),
            pillow_config_string_or_dict.get('params', {}),
        )
Ejemplo n.º 39
0
def to_omrs_date(value):
    """
    Drop the time and timezone to export date-only values

    >>> to_omrs_date('2017-06-27T12:00:00+0530') == '2017-06-27'
    True

    """
    if isinstance(value, six.string_types):
        soft_assert_type_text(value)
        if not re.match(r'\d{4}-\d{2}-\d{2}', value):
            raise ValueError(
                '"{}" is not recognised as a date or a datetime'.format(value))
        value = dateutil_parser.parse(value)
    if isinstance(value, (datetime.date, datetime.datetime)):
        return value.strftime('%Y-%m-%d')
Ejemplo n.º 40
0
 def remove_user(self, couch_user_id):
     '''
     Returns True if it removed a user, False otherwise
     '''
     if not isinstance(couch_user_id, six.string_types):
         couch_user_id = couch_user_id.user_id
     else:
         soft_assert_type_text(couch_user_id)
     soft_assert_type_text(couch_user_id)
     if couch_user_id in self.users:
         for i in range(0, len(self.users)):
             if self.users[i] == couch_user_id:
                 del self.users[i]
                 self.removed_users.add(couch_user_id)
                 return True
     return False
Ejemplo n.º 41
0
def pformat_json(data):
    """
    Pretty-formats `data` for readability, or returns the original
    value if it can't be parsed as JSON.

    :return: A 2-space-indented string with sorted keys.
    """
    if data is None:
        return ''
    try:
        if isinstance(data, six.string_types):
            soft_assert_type_text(data)
        json_data = json.loads(data) if isinstance(data,
                                                   six.string_types) else data
        return json.dumps(json_data, indent=2, sort_keys=True)
    except ValueError:
        return data
Ejemplo n.º 42
0
    def __call__(self, item, context=None):
        string_value = self._string_expression(item, context)
        if not isinstance(string_value, six.string_types):
            return None
        soft_assert_type_text(string_value)

        index_value = None
        if self.index_expression is not None:
            index_value = self._index_expression(item, context)
            if not isinstance(index_value, int):
                return None

        try:
            split = string_value.split(self.delimiter)
            return split[index_value] if index_value is not None else split
        except IndexError:
            return None
Ejemplo n.º 43
0
def get_location_from_site_code(site_code, location_cache):
    if isinstance(site_code, six.string_types):
        soft_assert_type_text(site_code)
        site_code = site_code.lower()
    elif isinstance(site_code, six.integer_types):
        site_code = str(site_code)
    else:
        raise UserUploadError(
            _("Unexpected format received for site code '%(site_code)s'") %
            {'site_code': site_code})

    try:
        return location_cache.get(site_code)
    except SQLLocation.DoesNotExist:
        raise UserUploadError(
            _("Could not find organization with site code '%(site_code)s'") %
            {'site_code': site_code})
Ejemplo n.º 44
0
    def validate_and_clean_time(self, data, field_name):
        value = data.get(field_name)
        error_msg = "Field '{}' is required and expected to be a 24-hour time string HH:MM".format(field_name)

        if not isinstance(value, six.string_types):
            raise OrderStatusValidationError(error_msg)
        soft_assert_type_text(value)

        if not re.match(r'^\d\d?:\d\d$', value):
            raise OrderStatusValidationError(error_msg)

        try:
            value = parse_timestamp(value).time()
        except ValueError:
            raise OrderStatusValidationError(error_msg)

        data[field_name] = value
Ejemplo n.º 45
0
def adjust_datetimes(data, parent=None, key=None, process_timezones=None):
    """
    find all datetime-like strings within data (deserialized json)
    and format them uniformly, in place.

    this only processes timezones correctly if the call comes from a request with domain information
    otherwise it will default to not processing timezones.

    to force timezone processing, it can be called as follows

    >>> from corehq.apps.tzmigration.api import force_phone_timezones_should_be_processed
    >>> with force_phone_timezones_should_be_processed():
    >>>     adjust_datetimes(form_json)
    """
    process_timezones = process_timezones or phone_timezones_should_be_processed(
    )
    # this strips the timezone like we've always done
    # todo: in the future this will convert to UTC
    if isinstance(
            data,
            six.string_types) and jsonobject.re_loose_datetime.match(data):
        soft_assert_type_text(data)
        try:
            parent[key] = six.text_type(
                json_format_datetime(
                    adjust_text_to_datetime(
                        data, process_timezones=process_timezones)))
        except (iso8601.ParseError, ValueError):
            pass
    elif isinstance(data, dict):
        for key, value in data.items():
            adjust_datetimes(value,
                             parent=data,
                             key=key,
                             process_timezones=process_timezones)
    elif isinstance(data, list):
        for i, value in enumerate(data):
            adjust_datetimes(value,
                             parent=data,
                             key=i,
                             process_timezones=process_timezones)

    # return data, just for convenience in testing
    # this is the original input, modified, not a new data structure
    return data
Ejemplo n.º 46
0
 def __init__(self,
         sync_log_id='',
         version=V1,
         state_hash='',
         include_item_count=False,
         device_id=None,
         app=None,
         openrosa_version=None):
     self.sync_log_id = sync_log_id
     self.version = version
     self.state_hash = state_hash
     self.include_item_count = include_item_count
     self.app = app
     self.device_id = device_id
     if isinstance(openrosa_version, six.string_types):
         soft_assert_type_text(openrosa_version)
     self.openrosa_version = (LooseVersion(openrosa_version)
         if isinstance(openrosa_version, six.string_types) else openrosa_version)
Ejemplo n.º 47
0
    def from_v2(cls, block):
        attachments = {}
        if not isinstance(block, dict):
            return cls(block, attachments)

        for id, data in block.items():
            if isinstance(data, six.string_types):
                soft_assert_type_text(data)
                attachment_src = None
                attachment_from = None
                attachment_name = None
            else:
                attachment_src = data.get('@src', None)
                attachment_from = data.get('@from', None)
                attachment_name = data.get('@name', None)
            attachments[id] = CaseAttachment(id, attachment_src,
                                             attachment_from, attachment_name)
        return cls(block, attachments)
Ejemplo n.º 48
0
    def handle_response(self, msg, response):
        if not isinstance(response, six.string_types):
            raise SyniverseException(
                "Unrecognized response received from Syniverse "
                "backend %s" % self.pk
            )
        soft_assert_type_text(response)

        response = response.strip().upper()
        if response.startswith('+OK'):
            return
        elif response.startswith('-ERR'):
            self.handle_error_response(msg, response)
        else:
            raise SyniverseException(
                "Unrecognized response received from Syniverse "
                "backend %s" % self.pk
            )
Ejemplo n.º 49
0
def fit_to_schema(doc, schema):
    def log(msg):
        raise SchemaMismatchException("doc-schema mismatch: %s (%s)" %
                                      (msg, doc))

    if schema is None:
        if doc:
            log("%s is not null" % doc)
        return None
    if isinstance(schema, list):
        if not doc:
            doc = []
        if not isinstance(doc, list):
            doc = [doc]
        answ = []
        schema_, = schema
        for doc_ in doc:
            answ.append(fit_to_schema(doc_, schema_))
        return answ
    if isinstance(schema, dict):
        if not doc:
            doc = {}
        if not isinstance(doc, dict):
            doc = {'': doc}
        doc_keys = set(doc.keys())
        schema_keys = set(schema.keys())
        if doc_keys - schema_keys:
            log("doc has keys not in schema: '%s'" %
                ("', '".join(doc_keys - schema_keys)))
        answ = {}
        for key in schema:
            #if schema[key] == unknown_type: continue
            if key in doc:
                answ[key] = fit_to_schema(doc.get(key), schema[key])
            else:
                answ[key] = render_never_was(schema[key])
        return answ
    if schema == "string":
        if not doc:
            doc = ""
        if not isinstance(doc, six.string_types):
            doc = six.text_type(doc)
        soft_assert_type_text(doc)
        return doc
Ejemplo n.º 50
0
def get_timezone_for_user(couch_user_or_id, domain):
    if couch_user_or_id and not isinstance(couch_user_or_id,
                                           AnonymousCouchUser):
        if isinstance(couch_user_or_id, CouchUser):
            requesting_user = couch_user_or_id
        else:
            assert isinstance(couch_user_or_id, six.string_types)
            soft_assert_type_text(couch_user_or_id)
            try:
                requesting_user = WebUser.get_by_user_id(couch_user_or_id)
            except CouchUser.AccountTypeError:
                requesting_user = None

        if requesting_user:
            domain_membership = requesting_user.get_domain_membership(domain)
            if domain_membership:
                return coerce_timezone_value(domain_membership.timezone)

    return get_timezone_for_domain(domain)
Ejemplo n.º 51
0
def to_omrs_datetime(value):
    """
    Converts CommCare dates and datetimes to OpenMRS datetimes.

    >>> to_omrs_datetime('2017-06-27') == '2017-06-27T00:00:00.000+0000'
    True

    """
    if isinstance(value, six.string_types):
        soft_assert_type_text(value)
        if not re.match(r'\d{4}-\d{2}-\d{2}', value):
            raise ValueError(
                '"{}" is not recognised as a date or a datetime'.format(value))
        value = dateutil_parser.parse(value)
    if isinstance(value, (datetime.date, datetime.datetime)):
        micros = value.strftime('%f')[:3]  # Only 3 digits for OpenMRS
        tz = value.strftime('%z') or '+0000'  # If we don't know, lie
        return value.strftime('%Y-%m-%dT%H:%M:%S.{f}{z}'.format(f=micros,
                                                                z=tz))
Ejemplo n.º 52
0
def format_date(date_string, OUTPUT_FORMAT, localize=None):
    if date_string is None or date_string == '' or date_string == " " or date_string == EMPTY_FIELD \
            or isinstance(date_string, (int, float)):
        return ''

    if isinstance(date_string, six.string_types):
        soft_assert_type_text(date_string)
        try:
            date_string = dateutil.parser.parse(date_string)
        except (AttributeError, ValueError):
            return ''

    if localize:
        tz = Domain.get_by_name(SUCCEED_DOMAIN).get_default_timezone()
        if date_string.tzname() is None:
            date_string = timezone('UTC').localize(date_string)
        date_string = date_string.astimezone(tz)

    return date_string.strftime(OUTPUT_FORMAT)
Ejemplo n.º 53
0
def export_for_group(export_id_or_group, last_access_cutoff=None):
    if isinstance(export_id_or_group, six.string_types):
        soft_assert_type_text(export_id_or_group)
        try:
            config = GroupExportConfiguration.get(export_id_or_group)
        except ResourceNotFound:
            raise Exception("Couldn't find an export with id %s" % export_id_or_group)
    else:
        config = export_id_or_group

    for subconfig, schema in config.all_exports:
        try:
            rebuild_export(subconfig, schema, last_access_cutoff=last_access_cutoff)
        except ExportRebuildError:
            continue
        except Exception as e:
            notify_exception(None, 'Problem building export {} in domain {}: {}'.format(
                subconfig.index, getattr(config, 'domain', 'unknown'), e
            ))
Ejemplo n.º 54
0
def apply_leniency(contact_phone_number):
    """
    The documentation says that contact_phone_number should be
    in international format and consist of only digits. However,
    we can apply some leniency to avoid common mistakes.
    Returns None if an unsupported data type is passed in.
    """
    from corehq.apps.sms.util import strip_plus
    # Decimal preserves trailing zeroes, so it's ok 
    if isinstance(contact_phone_number, six.integer_types + (Decimal,)):
        contact_phone_number = six.text_type(contact_phone_number)
    if isinstance(contact_phone_number, six.string_types):
        soft_assert_type_text(contact_phone_number)
        chars = re.compile(r"[()\s\-.]+")
        contact_phone_number = chars.sub("", contact_phone_number)
        contact_phone_number = strip_plus(contact_phone_number)
    else:
        contact_phone_number = None
    return contact_phone_number
Ejemplo n.º 55
0
def ordering_config_validator(value):

    error = ValidationError(_('The config format is invalid'),
                            params={'value': value})

    if not isinstance(value, list):
        raise error
    for group in value:
        if not isinstance(group, list) or len(group) != 2:
            raise error
        if not isinstance(group[0], six.string_types):
            raise error
        else:
            soft_assert_type_text(group[0])
        if not isinstance(group[1], list):
            raise error
        for report in group[1]:
            if not isinstance(report, six.string_types):
                raise error
            soft_assert_type_text(report)
Ejemplo n.º 56
0
 def __init__(self,
              domain,
              couch_user,
              case_id_or_case,
              app,
              form,
              delegation=False):
     super(CaseSessionDataHelper, self).__init__(domain, couch_user)
     self.form = form
     self.app = app
     if case_id_or_case is None or isinstance(case_id_or_case,
                                              six.string_types):
         if case_id_or_case is not None:
             soft_assert_type_text(case_id_or_case)
         self.case_id = case_id_or_case
         self._case = None
     else:
         self.case_id = case_id_or_case.case_id
         self._case = case_id_or_case
     self._delegation = delegation
Ejemplo n.º 57
0
    def clean_assigned_locations(self):
        # select2 (< 4.0) doesn't format multiselect for remote data as an array
        #   but formats it as comma-seperated list, so we need to clean the returned data
        from corehq.apps.locations.models import SQLLocation
        from corehq.apps.locations.util import get_locations_from_ids

        value = self.cleaned_data.get('assigned_locations')
        if not isinstance(value, six.string_types) or value.strip() == '':
            return []
        soft_assert_type_text(value)

        location_ids = [
            location_id.strip() for location_id in value.split(',')
        ]
        try:
            locations = get_locations_from_ids(location_ids, self.domain)
        except SQLLocation.DoesNotExist:
            raise ValidationError(
                _('One or more of the locations was not found.'))

        return [location.location_id for location in locations]
Ejemplo n.º 58
0
    def get_translated_value(display_lang, app_langs, obj):
        """
        Given a list of lang codes and a dictionary of lang codes to strings, output
        the value of the current display lang or the first lang available.

        If obj is a string, just output that string.
        """
        if isinstance(obj, six.string_types):
            soft_assert_type_text(obj)
            return obj
        if not obj:
            return _('Untitled')

        val = obj.get(display_lang)
        if val:
            return val
        for lang in app_langs:
            val = obj.get(lang)
            if val:
                return val
        return obj.get(list(obj)[0], _('Untitled'))
Ejemplo n.º 59
0
def get_modules_and_forms_row(row_type, sheet_name, languages, media_image, media_audio, unique_id):
    """
    assemble the various pieces of data that make up a row in the
    {sheet_name} sheet into a single row (a flat tuple).

    This function is meant as the single point of truth for the
    column ordering of {sheet_name}

    """.format(sheet_name=MODULES_AND_FORMS_SHEET_NAME)
    assert row_type is not None
    assert sheet_name is not None
    assert isinstance(languages, list)
    assert isinstance(media_image, list)
    assert isinstance(media_audio, list)
    assert isinstance(unique_id, six.string_types)
    soft_assert_type_text(unique_id)

    return [item if item is not None else "" for item in
            ([row_type, sheet_name] +
             get_menu_row(languages, media_image, media_audio) +
             [unique_id])]
Ejemplo n.º 60
0
def le_days_diff(max_days, date1, date2):
    """
    Returns True if date1 is less than or equal to max_days away from
    date2, otherwise returns False

    >>> from datetime import date
    >>> le_days_diff(364, '2018-01-01', '2017-01-20')
    True
    >>> le_days_diff(364, date(2017, 1, 20), date(2018, 1, 1))
    True
    >>> le_days_diff(364, '2018-01-01', '2020-01-20')
    False

    """
    if isinstance(date1, six.string_types):
        soft_assert_type_text(date1)
        date1 = parse_date(date1)
    if isinstance(date2, six.string_types):
        soft_assert_type_text(date2)
        date2 = parse_date(date2)
    return abs(date1 - date2) <= timedelta(max_days)