def should_sync(domain, last_sync, utcnow=None):
    # definitely sync if we haven't synced before
    if not last_sync or not last_sync.date:
        return True

    # utcnow only used in tests to mock other times
    utcnow = utcnow or datetime.utcnow()

    try:
        timezone = domain.get_default_timezone()
    except pytz.UnknownTimeZoneError:
        timezone = utc

    _assert = soft_assert(to=['droberts' + '@' + 'dimagi.com'])

    last_sync_utc = last_sync.date

    if not _assert(last_sync_utc.tzinfo is None,
                   'last_sync.date should be an offset-naive dt'):
        last_sync_utc = UserTime(last_sync_utc).server_time().done()

    # check if user has already synced today (in local timezone).
    # Indicators only change daily.
    last_sync_local = ServerTime(last_sync_utc).user_time(timezone).done()
    current_date_local = ServerTime(utcnow).user_time(timezone).done()

    if current_date_local.date() != last_sync_local.date():
        return True

    return False
Example #2
0
def should_sync(domain, last_sync, utcnow=None):
    # definitely sync if we haven't synced before
    if not last_sync or not last_sync.date:
        return True

    # utcnow only used in tests to mock other times
    utcnow = utcnow or datetime.utcnow()

    try:
        timezone = domain.get_default_timezone()
    except pytz.UnknownTimeZoneError:
        timezone = utc

    _assert = soft_assert(to=['droberts' + '@' + 'dimagi.com'])

    last_sync_utc = last_sync.date

    if not _assert(last_sync_utc.tzinfo is None,
                   'last_sync.date should be an offset-naive dt'):
        last_sync_utc = UserTime(last_sync_utc).server_time().done()

    # check if user has already synced today (in local timezone).
    # Indicators only change daily.
    last_sync_local = ServerTime(last_sync_utc).user_time(timezone).done()
    current_date_local = ServerTime(utcnow).user_time(timezone).done()

    if current_date_local.date() != last_sync_local.date():
        return True

    return False
Example #3
0
def should_sync(domain, last_sync, utcnow=None):
    # definitely sync if we haven't synced before
    if not last_sync or not last_sync.date:
        return True

    # utcnow only used in tests to mock other times
    utcnow = utcnow or datetime.utcnow()

    try:
        timezone = domain.get_default_timezone()
    except pytz.UnknownTimeZoneError:
        timezone = utc

    last_sync_utc = last_sync.date

    # check if user has already synced today (in local timezone).
    # Indicators only change daily.
    last_sync_local = ServerTime(last_sync_utc).user_time(timezone).done()
    current_date_local = ServerTime(utcnow).user_time(timezone).done()

    if current_date_local.date() != last_sync_local.date():
        return True

    return False
Example #4
0
def should_sync(domain, last_sync, utcnow=None):
    # definitely sync if we haven't synced before
    if not last_sync or not last_sync.date:
        return True

    # utcnow only used in tests to mock other times
    utcnow = utcnow or datetime.utcnow()

    try:
        timezone = domain.get_default_timezone()
    except pytz.UnknownTimeZoneError:
        timezone = utc

    last_sync_utc = last_sync.date

    # check if user has already synced today (in local timezone).
    # Indicators only change daily.
    last_sync_local = ServerTime(last_sync_utc).user_time(timezone).done()
    current_date_local = ServerTime(utcnow).user_time(timezone).done()

    if current_date_local.date() != last_sync_local.date():
        return True

    return False
Example #5
0
 def check_obs_props(obs, props):
     for k, v in props.items():
         if k.endswith("_date"):
             # datetime check
             obs_datetime = getattr(obs, k)
             val_datetime = dateutil.parser.parse(v)
             if k in ('completed_date', 'created_date'):
                 obs_datetime = ServerTime(obs_datetime).user_time(PACT_TIMEZONE).done()
             obs_date = obs_datetime.date()
             val_date = val_datetime.date()
             self.assertEquals(obs_date, val_date)
         else:
             self.assertEquals(getattr(obs, k), v,
                               msg="Error, observation %s\n\t%s didn't match: %s != %s" % (
                               json.dumps(obs.to_json(), indent=4), k, getattr(obs, k),
                               v))
Example #6
0
 def check_obs_props(obs, props):
     for k, v in props.items():
         if k.endswith("_date"):
             # datetime check
             obs_datetime = getattr(obs, k)
             val_datetime = dateutil.parser.parse(v)
             if k in ('completed_date', 'created_date'):
                 obs_datetime = ServerTime(obs_datetime).user_time(PACT_TIMEZONE).done()
             obs_date = obs_datetime.date()
             val_date = val_datetime.date()
             self.assertEquals(obs_date, val_date)
         else:
             self.assertEquals(getattr(obs, k), v,
                               msg="Error, observation %s\n\t%s didn't match: %s != %s" % (
                               json.dumps(obs.to_json(), indent=4), k, getattr(obs, k),
                               v))