def handle(self, *args, **options): """ """ # get user data if options.get('users_str') is not None: # explicitly-specified users user_ids = [v.strip() for v in options['users_str'].split(',')] users = self.get_specific_users(user_ids) else: # get all the users subscribed to notifications users = get_digest_subscribers() # generator if options.get('show_users'): self.show_users(users) return # determine time window if options.get('to_datetime'): to_datetime = date_parse(options['to_datetime']) else: to_datetime = datetime.datetime.utcnow().replace( hour=0, minute=0, second=0) from_datetime = to_datetime - \ datetime.timedelta(minutes=options['minutes']) if options.get('show_content'): self.show_content(users, from_datetime, to_datetime) return if options.get('show_text'): self.show_rendered('text', users, from_datetime, to_datetime) return if options.get('show_html'): self.show_rendered('html', users, from_datetime, to_datetime) return # invoke `tasks.generate_and_send_digests` via celery, in groups of # 10 def queue_digests(some_users): generate_and_send_digests.delay( some_users, from_datetime, to_datetime, language=settings.LANGUAGE_CODE ) user_batch = [] for user in users: user_batch.append(user) if len(user_batch) == settings.FORUM_DIGEST_TASK_BATCH_SIZE: queue_digests(user_batch) user_batch = [] # get the remainder if any if user_batch: queue_digests(user_batch)
def batch_digest_subscribers(): batch = [] for v in get_digest_subscribers(): batch.append(v) if len(batch) == settings.FORUM_DIGEST_TASK_BATCH_SIZE: yield batch batch = [] if batch: yield batch
def batch_digest_subscribers(): batch = [] for v in get_digest_subscribers(): batch.append(v) if len(batch)==settings.FORUM_DIGEST_TASK_BATCH_SIZE: yield batch batch = [] if batch: yield batch
def handle(self, *args, **options): """ """ # get user data if options.get('users_str') is not None: # explicitly-specified users user_ids = [v.strip() for v in options['users_str'].split(',')] users = self.get_specific_users(user_ids) else: # get all the users subscribed to notifications users = get_digest_subscribers() # generator if options.get('show_users'): self.show_users(users) return # determine time window if options.get('to_datetime'): to_datetime = date_parse(options['to_datetime']) else: to_datetime = datetime.datetime.utcnow().replace( hour=0, minute=0, second=0) from_datetime = to_datetime - \ datetime.timedelta(minutes=options['minutes']) if options.get('show_content'): self.show_content(users, from_datetime, to_datetime) return if options.get('show_text'): self.show_rendered('text', users, from_datetime, to_datetime) return if options.get('show_html'): self.show_rendered('html', users, from_datetime, to_datetime) return # invoke `tasks.generate_and_send_digests` via celery, in groups of # 10 def queue_digests(some_users): generate_and_send_digests.delay( some_users, from_datetime, to_datetime, language=settings.LANGUAGE_CODE ) user_batch = [] for user in users: user_batch.append(user) if len(user_batch) == settings.FORUM_DIGEST_TASK_BATCH_SIZE: queue_digests(user_batch) user_batch = [] # get the remainder if any if user_batch: queue_digests(user_batch)
def test_get_digest_subscribers_empty(self): """ """ # empty result expected_empty = {"count": 0, "next": None, "previous": None, "results": []} with patch("requests.get", return_value=Mock(json=expected_empty)) as p: res = list(get_digest_subscribers()) p.assert_called_once_with(self.expected_api_url, params=self.expected_params, headers=self.expected_headers) self.assertEqual(0, len(res))
def test_get_digest_subscribers_multi_page(self): """ """ # multi page result expected_multi_p1 = { "count": 5, "next": "not none", "previous": None, "results": [mkresult(1), mkresult(2), mkresult(3)] } expected_multi_p2 = { "count": 5, "next": None, "previous": "not none", "results": [mkresult(4), mkresult(5)] } expected_pages = [expected_multi_p1, expected_multi_p2] def side_effect(*a, **kw): return expected_pages.pop(0) m = Mock() with patch('requests.get', return_value=m) as p: res = [] m.json = expected_multi_p1 g = get_digest_subscribers() res.append(g.next()) p.assert_called_once_with( self.expected_api_url, params=self.expected_params, headers=self.expected_headers) res.append(g.next()) res.append(g.next()) # result 3, end of page self.assertEqual([ mkexpected(mkresult(1)), mkexpected(mkresult(2)), mkexpected(mkresult(3))], res) # still should only have called requests.get() once self.assertEqual(1, p.call_count) p.reset_mock() # reset call count self.expected_params['page']=2 m.json = expected_multi_p2 self.assertEqual(mkexpected(mkresult(4)), g.next()) p.assert_called_once_with( self.expected_api_url, params=self.expected_params, headers=self.expected_headers) self.assertEqual(mkexpected(mkresult(5)), g.next()) self.assertEqual(1, p.call_count) self.assertRaises(StopIteration, g.next)
def test_get_digest_subscribers_multi_page(self): """ """ # multi page result expected_multi_p1 = { "count": 5, "next": "not none", "previous": None, "results": [mkresult(1), mkresult(2), mkresult(3)] } expected_multi_p2 = { "count": 5, "next": None, "previous": "not none", "results": [mkresult(4), mkresult(5)] } expected_pages = [expected_multi_p1, expected_multi_p2] def side_effect(*a, **kw): return expected_pages.pop(0) mock_response = make_mock_json_response(json=expected_multi_p1) with patch('requests.get', return_value=mock_response) as p: res = [] g = get_digest_subscribers() res.append(g.next()) p.assert_called_once_with(self.expected_api_url, params=self.expected_params, headers=self.expected_headers) res.append(g.next()) res.append(g.next()) # result 3, end of page self.assertEqual([ mkexpected(mkresult(1)), mkexpected(mkresult(2)), mkexpected(mkresult(3)) ], res) # still should only have called requests.get() once self.assertEqual(1, p.call_count) mock_response = make_mock_json_response(json=expected_multi_p2) with patch('requests.get', return_value=mock_response) as p: self.expected_params['page'] = 2 self.assertEqual(mkexpected(mkresult(4)), g.next()) p.assert_called_once_with(self.expected_api_url, params=self.expected_params, headers=self.expected_headers) self.assertEqual(mkexpected(mkresult(5)), g.next()) self.assertEqual(1, p.call_count) self.assertRaises(StopIteration, g.next)
def test_get_digest_subscribers_single_page(self): """ """ # single page result expected_single = { "count": 3, "next": None, "previous": None, "results": [mkresult(1), mkresult(2), mkresult(3)], } with patch("requests.get", return_value=Mock(json=expected_single)) as p: res = list(get_digest_subscribers()) p.assert_called_once_with(self.expected_api_url, params=self.expected_params, headers=self.expected_headers) self.assertEqual([mkexpected(mkresult(1)), mkexpected(mkresult(2)), mkexpected(mkresult(3))], res)
def test_get_digest_subscribers_empty(self): """ """ # empty result mock_response = make_mock_json_response(json={ "count": 0, "next": None, "previous": None, "results": [] }) with patch('requests.get', return_value=mock_response) as p: res = list(get_digest_subscribers()) p.assert_called_once_with(self.expected_api_url, params=self.expected_params, headers=self.expected_headers) self.assertEqual(0, len(res))
def test_get_digest_subscribers_empty(self): """ """ # empty result mock_response = make_mock_json_response(json={ "count": 0, "next": None, "previous": None, "results": [] }) with patch('requests.get', return_value=mock_response) as p: res = list(get_digest_subscribers()) p.assert_called_once_with( self.expected_api_url, params=self.expected_params, headers=self.expected_headers) self.assertEqual(0, len(res))
def test_get_digest_subscribers_basic_auth(self): """ """ # single page result mock_response = make_mock_json_response(json={ "count": 3, "next": None, "previous": None, "results": [mkresult(1), mkresult(2), mkresult(3)] }) with patch('requests.get', return_value=mock_response) as p: res = list(get_digest_subscribers()) p.assert_called_once_with( self.expected_api_url, params=self.expected_params, headers=self.expected_headers, auth=('someuser', 'somepass')) self.assertEqual([ mkexpected(mkresult(1)), mkexpected(mkresult(2)), mkexpected(mkresult(3))], res)
def test_get_digest_subscribers_single_page(self): """ """ # single page result mock_response = make_mock_json_response( json={ "count": 3, "next": None, "previous": None, "results": [mkresult(1), mkresult(2), mkresult(3)] }) with patch('requests.get', return_value=mock_response) as p: res = list(get_digest_subscribers()) p.assert_called_once_with(self.expected_api_url, params=self.expected_params, headers=self.expected_headers) self.assertEqual([ mkexpected(mkresult(1)), mkexpected(mkresult(2)), mkexpected(mkresult(3)) ], res)