def handle(self, *args, **kwargs): set_script_prefix(SITE_URL) category_users = {} for profile in Profile.objects.all(): if profile.subscribed_category: category_users.setdefault(profile.subscribed_category, []).append(profile.user.email) for subscribed_category in category_users: recipients = category_users[subscribed_category] articles = Article.objects.filter( type=subscribed_category, publish__date=timezone.now()).all() if len(articles): subject = 'Microlearning: {} daily new articles'\ .format(dict(Article.ARTICLE_TYPES).get(subscribed_category)) message = render_to_string('email/new_articles.html', {'articles': articles}) for recipient in recipients: email = EmailMessage(subject=subject, body=message, from_email=EMAIL_HOST_USER, to=[recipient]) email.content_subtype = 'html' email.send() clear_script_prefix()
def test_add_script_name_prefix(self): tests = ( # Relative paths. ("/somesubpath", "path/", "/somesubpath/path/"), ("/somesubpath/", "path/", "/somesubpath/path/"), ("/", "path/", "/path/"), # Invalid URLs. ( "/somesubpath/", "htp://myhost.com/path/", "/somesubpath/htp://myhost.com/path/", ), # Blank settings. ("/somesubpath/", "", "/somesubpath/"), ) for setting in ("MEDIA_URL", "STATIC_URL"): for script_name, path, expected_path in tests: new_settings = {setting: path} with self.settings(**new_settings): with self.subTest(script_name=script_name, **new_settings): try: self.set_script_name(script_name) self.assertEqual(getattr(settings, setting), expected_path) finally: clear_script_prefix()
def test_get_canonical_path_with_prefix(self): set_script_prefix('/v2/') rsrc_key = DogSerializer().get_resource_key() self.assertEqual( '/v2/dogs', DynamicRouter.get_canonical_path(rsrc_key) ) clear_script_prefix()
def test_not_prefixed(self): # Don't add SCRIPT_NAME prefix to valid URLs, absolute paths or None. tests = ( '/path/', 'http://myhost.com/path/', None, ) for setting in ('MEDIA_URL', 'STATIC_URL'): for path in tests: new_settings = {setting: path} with self.settings(**new_settings): for script_name in ['/somesubpath', '/somesubpath/', '/', '', None]: with self.subTest(script_name=script_name, **new_settings): try: self.set_script_name(script_name) self.assertEqual(getattr(settings, setting), path) finally: clear_script_prefix()
def test_not_prefixed(self): # Don't add SCRIPT_NAME prefix to absolute paths, URLs, or None. tests = ( "/path/", "http://myhost.com/path/", "http://myhost/path/", "https://myhost/path/", None, ) for setting in ("MEDIA_URL", "STATIC_URL"): for path in tests: new_settings = {setting: path} with self.settings(**new_settings): for script_name in [ "/somesubpath", "/somesubpath/", "/", "", None ]: with self.subTest(script_name=script_name, **new_settings): try: self.set_script_name(script_name) self.assertEqual(getattr(settings, setting), path) finally: clear_script_prefix()
def set_script_name(self, val): clear_script_prefix() if val is not None: set_script_prefix(val)