def test01_send_mail(self): # Capture sent emails mock_smtp = Mock() self.msg_list = None def send_messages(msg_list): self.msg_list = msg_list mock_smtp.send_messages.side_effect = send_messages context = { 'available_games': [{'game_id': 6161, 'owner_name': u'david', 'sentence': u'this is an available story çà'}], 'completed_games': [{'game_id': 6162, 'owner_name': u'goliath', 'sentence': u'this is a completed story éè'}], 'game_activities': [{'game_id': 6159, 'state': u'vote', 'owner_name': 'You', 'events': [u'emzie3001 voted', u'Cathy Jewson voted', u'Kazzi voted', u'nara voted', u'Matjaz voted'], 'sentence': u'story with activity ù'}], 'unsubscribe_path': '/unsubscribe-path' } send.send_mail(mock_smtp, '*****@*****.**', context) mock_smtp.send_messages.assert_called_once() mock_smtp.reset_mock() self.assertEqual(len(self.msg_list), 1) msg_formatted = str(self.msg_list[0].message()) self.assertTrue('From: Card Stories <*****@*****.**>' in msg_formatted) self.assertTrue('Content-Type: text/plain' in msg_formatted) self.assertTrue('Content-Type: text/html' in msg_formatted) self.assertTrue('Content-Type: image/jpeg' in msg_formatted) self.assertTrue("Content-Disposition: inline; filename=\"umbrella.png\"\nContent-Type: image/png; name=\"umbrella.png\"\nContent-ID: <umbrella.png>" in msg_formatted) self.assertTrue('"story with activity =C3=B9" (Game Master: You, State: vote)' in msg_formatted) self.assertTrue("\"this is an available story =C3=A7=C3=A0\" (Game Master: david)" in msg_formatted) self.assertTrue("\"this is a completed story =C3=A9=C3=A8\" (Game Master: goliath)" in msg_formatted) self.assertTrue("emzie3001 voted" in msg_formatted) self.assertTrue("/?game_id=3D6161" in msg_formatted) self.assertTrue("/?game_id=3D6162" in msg_formatted) self.assertTrue("/?game_id=3D6159" in msg_formatted) self.assertTrue("/unsubscribe-path" in msg_formatted)
def loop(ws_db_path, django_db_path, email_list=None, verbose=False): django_conn = sqlite3.connect(django_db_path) cursor = django_conn.cursor() players_list = aggregate.get_all_players(cursor) aggregate.seed_playerid2name(players_list) cursor.close() django_conn.close() smtp = send.smtp_open() ws_conn = sqlite3.connect(ws_db_path) cursor = ws_conn.cursor() count = 0 for id, email, name, unsubscribed in players_list: if email_list and not email in email_list: continue if unsubscribed: continue game_ids = aggregate.get_player_game_ids(cursor, id) last_active = aggregate.get_players_last_activity(cursor, id) yesterday = datetime.now() - timedelta(days=1) recent_game_activities = aggregate.get_game_activities( cursor, game_ids, id, happened_since=yesterday) should_send = should_send_email(last_active, recent_game_activities) if should_send: context = get_context(cursor, id, game_ids, last_active) # Don't send if there isn't any new info in the context. if not is_context_empty(context): if verbose: print 'Sending email to %s' % email send.send_mail(smtp, email, context) count += 1 cursor.close() ws_conn.close() smtp.close() return count
def loop(ws_db_path, django_db_path, email_list=None, verbose=False): django_conn = sqlite3.connect(django_db_path) cursor = django_conn.cursor() players_list = aggregate.get_all_players(cursor) aggregate.seed_playerid2name(players_list) cursor.close() django_conn.close() smtp = send.smtp_open() ws_conn = sqlite3.connect(ws_db_path) cursor = ws_conn.cursor() count = 0 for id, email, name, unsubscribed in players_list: if email_list and not email in email_list: continue if unsubscribed: continue game_ids = aggregate.get_player_game_ids(cursor, id) last_active = aggregate.get_players_last_activity(cursor, id) yesterday = datetime.now() - timedelta(days=1) recent_game_activities = aggregate.get_game_activities(cursor, game_ids, id, happened_since=yesterday) should_send = should_send_email(last_active, recent_game_activities) if should_send: context = get_context(cursor, id, game_ids, last_active) # Don't send if there isn't any new info in the context. if not is_context_empty(context): if verbose: print 'Sending email to %s' % email send.send_mail(smtp, email, context) count += 1 cursor.close() ws_conn.close() smtp.close() return count
def loop(ws_db_path, django_db_path): django_conn = sqlite3.connect(django_db_path) cursor = django_conn.cursor() players_list = aggregate.get_all_players(cursor) aggregate.seed_playerid2name(players_list) cursor.close() django_conn.close() ws_conn = sqlite3.connect(ws_db_path) cursor = ws_conn.cursor() count = 0 for id, email, name in players_list: last_active = aggregate.get_players_last_activity(cursor, id) if should_send(id, last_active): context = get_context(cursor, id, last_active) # TODO: Make send_mail work something like this: send.send_mail(email, name, context) count += 1 cursor.close() ws_conn.close() return count
def test01_send_mail(self): # Capture sent emails mock_smtp = Mock() self.msg_list = None def send_messages(msg_list): self.msg_list = msg_list mock_smtp.send_messages.side_effect = send_messages context = { 'available_games': [{ 'game_id': 6161, 'owner_name': u'david', 'sentence': u'this is an available story çà' }], 'completed_games': [{ 'game_id': 6162, 'owner_name': u'goliath', 'sentence': u'this is a completed story éè' }], 'game_activities': [{ 'game_id': 6159, 'state': u'vote', 'owner_name': 'You', 'events': [ u'emzie3001 voted', u'Cathy Jewson voted', u'Kazzi voted', u'nara voted', u'Matjaz voted' ], 'sentence': u'story with activity ù' }], 'unsubscribe_path': '/unsubscribe-path' } send.send_mail(mock_smtp, '*****@*****.**', context) mock_smtp.send_messages.assert_called_once() mock_smtp.reset_mock() self.assertEqual(len(self.msg_list), 1) msg_formatted = str(self.msg_list[0].message()) self.assertTrue( 'From: Card Stories <*****@*****.**>' in msg_formatted) self.assertTrue('Content-Type: text/plain' in msg_formatted) self.assertTrue('Content-Type: text/html' in msg_formatted) self.assertTrue('Content-Type: image/jpeg' in msg_formatted) self.assertTrue( "Content-Disposition: inline; filename=\"umbrella.png\"\nContent-Type: image/png; name=\"umbrella.png\"\nContent-ID: <umbrella.png>" in msg_formatted) self.assertTrue( '"story with activity =C3=B9" (Game Master: You, State: vote)' in msg_formatted) self.assertTrue( "\"this is an available story =C3=A7=C3=A0\" (Game Master: david)" in msg_formatted) self.assertTrue( "\"this is a completed story =C3=A9=C3=A8\" (Game Master: goliath)" in msg_formatted) self.assertTrue("emzie3001 voted" in msg_formatted) self.assertTrue("/?game_id=3D6161" in msg_formatted) self.assertTrue("/?game_id=3D6162" in msg_formatted) self.assertTrue("/?game_id=3D6159" in msg_formatted) self.assertTrue("/unsubscribe-path" in msg_formatted)