示例#1
0
    def _test(self, name, any_id_ok=False, tz_differs=False):
        with open(os.path.join(os.path.dirname(__file__), 'data', '{name}.xml'.format(name=name))) as f:
            instance = f.read()

        if tz_differs and phone_timezones_should_be_processed():
            expected_name = name + '-tz'
        else:
            expected_name = name

        with open(os.path.join(os.path.dirname(__file__), 'data',
                               '{name}.json'.format(name=expected_name))) as f:
            result = json.load(f)

        xform = FormProcessorInterface.post_xform(instance)
        xform_json = json.loads(json.dumps(xform.to_json()))
        for key in ['is_archived', 'is_deprecated', 'is_duplicate', 'is_error']:
            del xform_json[key]

        result['received_on'] = xform_json['received_on']
        result['_rev'] = xform_json['_rev']
        result['_attachments'] = None
        xform_json['_attachments'] = None
        if any_id_ok:
            result['_id'] = xform_json['_id']
            result['id'] = xform_json['id']

        self.assertDictEqual(xform_json, result)
示例#2
0
def adjust_datetimes(data, parent=None, key=None):
    """
    find all datetime-like strings within data (deserialized json)
    and format them uniformly, in place.

    """
    # this strips the timezone like we've always done
    # todo: in the future this will convert to UTC
    if isinstance(data, basestring):
        if re_loose_datetime.match(data):
            if phone_timezones_should_be_processed():
                parent[key] = json_format_datetime(
                    iso8601.parse_date(data).astimezone(pytz.utc)
                    .replace(tzinfo=None)
                )
            else:
                parent[key] = json_format_datetime(
                    iso8601.parse_date(data).replace(tzinfo=None))

    elif isinstance(data, dict):
        for key, value in data.items():
            adjust_datetimes(value, parent=data, key=key)
    elif isinstance(data, list):
        for i, value in enumerate(data):
            adjust_datetimes(value, parent=data, key=i)

    # return data, just for convenience in testing
    # this is the original input, modified, not a new data structure
    return data
示例#3
0
    def _test(self, name, any_id_ok=False, tz_differs=False):
        with open(
                os.path.join(os.path.dirname(__file__), 'data',
                             '{name}.xml'.format(name=name))) as f:
            instance = f.read()

        if tz_differs and phone_timezones_should_be_processed():
            expected_name = name + '-tz'
        else:
            expected_name = name

        with open(
                os.path.join(os.path.dirname(__file__), 'data',
                             '{name}.json'.format(name=expected_name))) as f:
            result = json.load(f)

        with create_and_save_xform(instance) as doc_id:
            xform = XFormInstance.get(doc_id)
            try:
                xform_json = xform.to_json()
                result['received_on'] = xform_json['received_on']
                result['_rev'] = xform_json['_rev']
                if any_id_ok:
                    result['_id'] = xform_json['_id']
                self.assertDictEqual(xform_json, result)
            except Exception:
                # to help when bootstrapping a new test case
                print json.dumps(xform_json)
                raise
            finally:
                xform.delete()
示例#4
0
    def _test(self, name, any_id_ok=False, tz_differs=False):
        with open(os.path.join(os.path.dirname(__file__), 'data', '{name}.xml'.format(name=name))) as f:
            instance = f.read()

        if tz_differs and phone_timezones_should_be_processed():
            expected_name = name + '-tz'
        else:
            expected_name = name

        with open(os.path.join(os.path.dirname(__file__), 'data',
                               '{name}.json'.format(name=expected_name))) as f:
            result = json.load(f)

        with create_and_save_xform(instance) as doc_id:
            xform = XFormInstance.get(doc_id)
            try:
                xform_json = xform.to_json()
                result['received_on'] = xform_json['received_on']
                result['_rev'] = xform_json['_rev']
                if any_id_ok:
                    result['_id'] = xform_json['_id']
                self.assertDictEqual(xform_json, result)
            except Exception:
                # to help when bootstrapping a new test case
                print json.dumps(xform_json)
                raise
            finally:
                xform.delete()
示例#5
0
文件: util.py 项目: ekush/commcare-hq
def adjust_datetimes(data, parent=None, key=None):
    """
    find all datetime-like strings within data (deserialized json)
    and format them uniformly, in place.

    """
    # this strips the timezone like we've always done
    # todo: in the future this will convert to UTC
    if isinstance(data, basestring) and re_loose_datetime.match(data):
        try:
            matching_datetime = iso8601.parse_date(data)
        except iso8601.ParseError:
            pass
        else:
            if phone_timezones_should_be_processed():
                parent[key] = json_format_datetime(
                    matching_datetime.astimezone(pytz.utc).replace(tzinfo=None)
                )
            else:
                parent[key] = json_format_datetime(
                    matching_datetime.replace(tzinfo=None))

    elif isinstance(data, dict):
        for key, value in data.items():
            adjust_datetimes(value, parent=data, key=key)
    elif isinstance(data, list):
        for i, value in enumerate(data):
            adjust_datetimes(value, parent=data, key=i)

    # return data, just for convenience in testing
    # this is the original input, modified, not a new data structure
    return data
示例#6
0
    def _get_expected_name(self, name, tz_differs):
        expected_name = name
        if tz_differs and phone_timezones_should_be_processed():
            expected_name = name + '-tz'

        if getattr(settings, 'TESTS_SHOULD_USE_SQL_BACKEND', False):
            expected_name += '-sql'
        return expected_name
示例#7
0
    def _get_expected_name(self, name, tz_differs):
        expected_name = name
        if tz_differs and phone_timezones_should_be_processed():
            expected_name = name + '-tz'

        if getattr(settings, 'TESTS_SHOULD_USE_SQL_BACKEND', False):
            expected_name += '-sql'
        return expected_name
示例#8
0
 def test_strip_tz(self):
     if phone_timezones_should_be_processed():
         self.assertEqual(
             adjust_datetimes({'datetime': '2013-03-09T06:30:09.007+03'}),
             {'datetime': '2013-03-09T03:30:09.007000Z'})
     else:
         self.assertEqual(
             adjust_datetimes({'datetime': '2013-03-09T06:30:09.007+03'}),
             {'datetime': '2013-03-09T06:30:09.007000Z'})
 def test_strip_tz(self):
     if phone_timezones_should_be_processed():
         self.assertEqual(
             adjust_datetimes({'datetime': '2013-03-09T06:30:09.007+03'}),
             {'datetime': '2013-03-09T03:30:09.007000Z'}
         )
     else:
         self.assertEqual(
             adjust_datetimes({'datetime': '2013-03-09T06:30:09.007+03'}),
             {'datetime': '2013-03-09T06:30:09.007000Z'}
         )
示例#10
0
def adjust_datetimes(data, parent=None, key=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 import force_phone_timezones_should_be_processed
    >>> with force_phone_timezones_should_be_processed():
    >>>     adjust_datetimes(form_json)
    """
    # this strips the timezone like we've always done
    # todo: in the future this will convert to UTC
    if isinstance(data,
                  basestring) and jsonobject.re_loose_datetime.match(data):
        try:
            matching_datetime = iso8601.parse_date(data)
        except iso8601.ParseError:
            pass
        else:
            if phone_timezones_should_be_processed():
                parent[key] = unicode(
                    json_format_datetime(
                        matching_datetime.astimezone(
                            pytz.utc).replace(tzinfo=None)))
            else:
                parent[key] = unicode(
                    json_format_datetime(
                        matching_datetime.replace(tzinfo=None)))

    elif isinstance(data, dict):
        for key, value in data.items():
            adjust_datetimes(value, parent=data, key=key)
    elif isinstance(data, list):
        for i, value in enumerate(data):
            adjust_datetimes(value, parent=data, key=i)

    # return data, just for convenience in testing
    # this is the original input, modified, not a new data structure
    return data
示例#11
0
def adjust_datetimes(data, parent=None, key=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 import force_phone_timezones_should_be_processed
    >>> with force_phone_timezones_should_be_processed():
    >>>     adjust_datetimes(form_json)
    """
    # this strips the timezone like we've always done
    # todo: in the future this will convert to UTC
    if isinstance(data, basestring) and jsonobject.re_loose_datetime.match(data):
        try:
            matching_datetime = iso8601.parse_date(data)
        except iso8601.ParseError:
            pass
        else:
            if phone_timezones_should_be_processed():
                parent[key] = unicode(json_format_datetime(
                    matching_datetime.astimezone(pytz.utc).replace(tzinfo=None)
                ))
            else:
                parent[key] = unicode(json_format_datetime(
                    matching_datetime.replace(tzinfo=None)))

    elif isinstance(data, dict):
        for key, value in data.items():
            adjust_datetimes(value, parent=data, key=key)
    elif isinstance(data, list):
        for i, value in enumerate(data):
            adjust_datetimes(value, parent=data, key=i)

    # return data, just for convenience in testing
    # this is the original input, modified, not a new data structure
    return data