def send_low_balance(session, user, with_summary=False, force=False):
    if "email" not in user:
        return

    balance = Users.get_balance(user['id'])

    if not force and balance >= MINIMUM_BALANCE:
        # reset time
        user['meta']['last_emailed'] = time.time()
        Users.save(user)
        return

    last_emailed = user['meta'].get('last_emailed', 0)
    diff = time.time() - last_emailed
    diff_hours = diff / 60 / 60
    diff_days = diff_hours / 24

    if not force and diff_hours <= REMIND_MAIL_EVERY_X_HOURS:
        return

    logger.info("%s's low balance last emailed %.2f days (%.2f hours) ago. Mailing now.",
                user['name'], diff_days, diff_hours)
    content_text = (u"Du hast seit mehr als {diff_days} Tagen "
                    u"ein Guthaben von unter {minimum_balance}€!\n"
                    u"Aktueller Kontostand: {balance:.2f}€.\n"
                    u"Zum Aufladen im Space-Netz http://drinks-touch.fd/ besuchen.").format(
        diff_days=int(REMIND_MAIL_EVERY_X_HOURS / 24),
        minimum_balance=MINIMUM_BALANCE,
        balance=balance)
    content_html = render_jinja_html('low_balance.html',
                                     diff_days=REMIND_MAIL_EVERY_X_HOURS / 24,
                                     minimum_balance=MINIMUM_BALANCE,
                                     balance=balance,
                                     uid=user['id'])

    if not with_summary:
        content_text += FOOTER.format(uid=user['id'])
        content_html = render_jinja_html('main.html', prepend_html=content_html)

        send_notification(user['email'], "Negatives Guthaben", content_text, content_html, user['id'])
        return

    send_summary(session, user,
                 subject="Negatives Guthaben",
                 prepend_text=content_text,
                 prepend_html=content_html,
                 force=True)
    user['meta']['last_emailed'] = time.time()
    Users.save(user)
Exemplo n.º 2
0
def send_low_balance(session, user, with_summary=False, force=False):
    balance = Users.get_balance(user['id'])

    if not force and balance >= MINIMUM_BALANCE:
        # reset time
        user['meta']['last_emailed'] = time.time()
        Users.save(user)
        return

    last_emailed = user['meta'].get('last_emailed', 0)
    diff = time.time() - last_emailed
    diff_hours = diff / 60 / 60
    diff_days = diff_hours / 24

    if not force and diff_hours <= REMIND_MAIL_EVERY_X_HOURS:
        return

    logging.info("%s's low balance last emailed %.2f days (%.2f hours) ago. Mailing now.",
                 user['name'], diff_days, diff_hours)
    content_text = (u"Du hast seit mehr als {diff_days} Tagen "
                    u"ein Guthaben von unter {minimum_balance}€!\n"
                    u"Aktueller Kontostand: {balance:.2f}€.\n"
                    u"Zum Aufladen im Space-Netz http://drinks-touch.fd/ besuchen.").format(
        diff_days=REMIND_MAIL_EVERY_X_HOURS / 24,
        minimum_balance=MINIMUM_BALANCE,
        balance=balance)
    content_html = render_jinja_html('low_balance.html',
                                     diff_days=REMIND_MAIL_EVERY_X_HOURS / 24,
                                     minimum_balance=MINIMUM_BALANCE,
                                     balance=balance,
                                     uid=user['id'])

    if not with_summary:
        content_text += FOOTER.format(uid=user['id'])
        content_html = render_jinja_html('main.html', prepend_html=content_html)

        send_notification(user['email'], "Negatives Guthaben", content_text, content_html, user['id'])
        return

    send_summary(session, user,
                 subject="Negatives Guthaben",
                 prepend_text=content_text,
                 prepend_html=content_html,
                 force=True)
    user['meta']['last_emailed'] = time.time()
    Users.save(user)
 def set_id(self, ean):
     ean = ean.upper() if ean else ean
     self.user['id_card'] = ean
     Users.save(self.user)
     self.id_label.text = self.user['id_card']
def send_summary(session, user, subject, prepend_text=None, prepend_html=None, force=False):
    if 'email' not in user:
        return

    frequency_str = user['meta']['drink_notification']
    balance = Users.get_balance(user['id'])

    if not force and frequency_str not in FREQUENCIES.keys():
        return
    elif force:
        freq_secs = 0
    else:
        freq_secs = FREQUENCIES[frequency_str]

    last_emailed = user['meta']['last_drink_notification']
    if type(last_emailed) not in [int, float]:
        last_emailed = 0

    last_emailed_str = datetime.fromtimestamp(last_emailed).strftime("%d.%m.%Y %H:%M:%S")
    diff = time.time() - last_emailed
    diff_hours = diff / 60 / 60
    diff_days = diff_hours / 24

    if force:
        logger.info("Forcing mail.")
    else:
        if diff <= freq_secs:
            return

    logger.info("%s's summary last emailed %.2f days (%.2f hours) ago. Mailing now.",
                user['name'], diff_days, diff_hours)

    content_text = u""

    if prepend_text:
        content_text += prepend_text + u"\n==========\n"

    content_text += u"Hier ist deine Getränkeübersicht seit {since}.\n" \
                    u"Dein aktuelles Guthaben beträgt EUR {balance:.2f}.\n" \
        .format(since=last_emailed_str, balance=balance)

    drinks_consumed = get_drinks_consumed(session, user, last_emailed)
    recharges = get_recharges(session, user, last_emailed)

    if drinks_consumed:
        content_text += format_drinks(drinks_consumed)

    if recharges:
        content_text += format_recharges(recharges)

    content_text += FOOTER.format(uid=user['id'])
    content_html = render_jinja_html('main.html',
                                     with_report=True,
                                     balance=balance,
                                     since_text=last_emailed_str,
                                     drinks=drinks_consumed,
                                     recharges=recharges,
                                     prepend_html=prepend_html,
                                     limit_days=REMIND_MAIL_EVERY_X_HOURS / 24,
                                     minimum_balance=MINIMUM_BALANCE,
                                     uid=user['id'])

    email = user['email']

    if not email:
        logger.warning("User %s has no email. skipping.", user)
        return

    if len(drinks_consumed) == 0 and len(recharges) == 0 and not force:
        logger.debug("got no rows. skipping.")
        return

    logger.info("Got %d drinks and %d recharges. Mailing.", len(drinks_consumed), len(recharges))
    send_notification(email, subject, content_text, content_html, user['id'])
    user['meta']['last_drink_notification'] = time.time()
    Users.save(user)
def main():
    for user in Users.get_all():
        user['meta']['last_emailed'] = 0
        user['meta']['last_drink_notification'] = 0
        Users.save(user)
Exemplo n.º 6
0
def main(args):
    for user in Users.get_all():
        user['meta']['last_emailed'] = 0
        user['meta']['last_drink_notification'] = 0
        Users.save(user)
 def set_id(self, ean):
     ean = ean.upper() if ean else ean
     self.user['id_card'] = ean
     Users.save(self.user)
     self.id_label.text = self.user['id_card']
Exemplo n.º 8
0
def send_summary(session, user, subject, prepend_text=None, prepend_html=None, force=False):
    frequency_str = user['meta']['drink_notification']
    balance = Users.get_balance(user['id'])

    if not force and frequency_str not in FREQUENCIES.keys():
        return
    elif force:
        freq_secs = 0
    else:
        freq_secs = FREQUENCIES[frequency_str]

    last_emailed = user['meta']['last_drink_notification']
    if type(last_emailed) not in [int, float]:
        last_emailed = 0

    last_emailed_str = datetime.fromtimestamp(last_emailed).strftime("%d.%m.%Y %H:%M:%S")
    diff = time.time() - last_emailed
    diff_hours = diff / 60 / 60
    diff_days = diff_hours / 24

    if force:
        logging.info("Forcing mail.")
    else:
        if diff <= freq_secs:
            return

    logging.info("%s's summary last emailed %.2f days (%.2f hours) ago. Mailing now.",
                 user['name'], diff_days, diff_hours)

    content_text = u""

    if prepend_text:
        content_text += prepend_text + u"\n==========\n"

    content_text += u"Hier ist deine Getränkeübersicht seit {since}.\n" \
                    u"Dein aktuelles Guthaben beträgt EUR {balance:.2f}.\n" \
        .format(since=last_emailed_str, balance=balance)

    drinks_consumed = get_drinks_consumed(session, user, last_emailed)
    recharges = get_recharges(session, user, last_emailed)

    if drinks_consumed:
        content_text += format_drinks(drinks_consumed)

    if recharges:
        content_text += format_recharges(recharges)

    content_text += FOOTER.format(uid=user['id'])
    content_html = render_jinja_html('main.html',
                                     with_report=True,
                                     balance=balance,
                                     since_text=last_emailed_str,
                                     drinks=drinks_consumed,
                                     recharges=recharges,
                                     prepend_html=prepend_html,
                                     limit_days=REMIND_MAIL_EVERY_X_HOURS / 24,
                                     minimum_balance=MINIMUM_BALANCE,
                                     uid=user['id'])

    email = user['email']

    if not email:
        logging.warn("User %s has no email. skipping.", user)
        return

    if len(drinks_consumed) == 0 and len(recharges) == 0 and not force:
        logging.debug("got no rows. skipping.")
        return

    logging.info("Got %d drinks and %d recharges. Mailing.", len(drinks_consumed), len(recharges))
    send_notification(email, subject, content_text, content_html, user['id'])
    user['meta']['last_drink_notification'] = time.time()
    Users.save(user)