def update_state(bug):
    log.debug('Starting bug "{}" update'.format(bug.externalId))
    now = datetime.utcnow()
    td = now - datetime.replace(bug.updated, tzinfo=None)
    diff = (td.microseconds +
            (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6

    if bug.state in settings.BUG_STATE_EXPIRED:
        old_state = bug.state
        new_state = \
            get_issue_fields_from_bts(bug.externalId)['status']['name']
        log.debug('Comparing bug state,'
                  '"{0}" and "{1}"'.format(old_state, new_state))
        if old_state == new_state and diff > float(settings.BUG_TIME_EXPIRED):
            log.debug(
                'Bug "{}" expired, deleting it from DB'.format(bug.externalId))
            bug.delete()
        elif old_state == new_state \
                and diff < float(settings.BUG_TIME_EXPIRED):
            log.debug(
                'Bug "{}" not updated, '
                'because {} seconds not expired'.format(
                    bug.externalId, settings.BUG_TIME_EXPIRED))
        else:
            bug.state = new_state
            bug.updated = now
            log.debug('Saving bug "{}"'.format(bug.externalId))
            bug.save()
    if bug.state not in settings.BUG_STATE_EXPIRED \
            and diff > float(settings.TIME_BEFORE_UPDATE_BUG_INFO):
        log.debug("%s > %s time to update bug state.", diff,
                  settings.TIME_BEFORE_UPDATE_BUG_INFO)
        bug.updated = now
        bug.state = \
            get_issue_fields_from_bts(bug.externalId)['status']['name']
        log.debug('Saving bug "{}"'.format(bug.externalId))
        bug.save()
dataset/ETHPy150Open 2gis/badger-api/testreport/tasks.py/update_state/original