@task(base=TaskAlertIfFail)
def send_email_if_new_diffs(profile):

    # profile = db.session.query(Profile).get(profile_id)

    status = "started"
    now = datetime.datetime.utcnow()    
    latest_diff_timestamp = profile.latest_diff_ts
    if not latest_diff_timestamp:
        status = "no diff timestamp"
        return status

    status = "checking diffs"

    if (not profile.last_email_check) or (latest_diff_timestamp > profile.last_email_check):
        # logger.info(u"has diffs since last email check! calling send_email report for {url_slug}".format(url_slug=profile.url_slug))
        status = send_email_report(profile, now)
    else:
        # logger.info(u"not sending, no new diffs since last email sent for {url_slug}".format(url_slug=profile.url_slug))
        status = "no new diffs"

    # set last email check date
    db.session.merge(profile)
    profile.last_email_check = now
    try:
        db.session.commit()
        # logger.info(u"updated profile object in send_email_if_new_diffs for {url_slug}".format(url_slug=profile.url_slug))
    except InvalidRequestError:
        logger.info(u"rollback, trying again to update profile object in send_email_if_new_diffs for {url_slug}".format(url_slug=profile.url_slug))
        db.session.rollback()
        db.session.commit()
        logger.info(u"after rollback updated profile object in send_email_if_new_diffs for {url_slug}".format(url_slug=profile.url_slug))

    return status
dataset/ETHPy150Open Impactstory/total-impact-webapp/tasks.py/send_email_if_new_diffs/original