def test_absolutize_urls(self): text = """<img class="junk" src="/some.gif"> <img class="junk" src="/cat.gif"> <IMG SRC='/some.png'>""" #jinja register.filter decorator works in a weird way self.assertEqual( absolutize_urls(text), '<img class="junk" src="http://example.com/some.gif"> <img class="junk" src="http://example.com/cat.gif"> <IMG SRC="http://example.com/some.png">' ) text = """<a class="junk" href="/something">link</a> <A HREF='/something'>link</A>""" #jinja register.filter decorator works in a weird way self.assertEqual( absolutize_urls(text), '<a class="junk" href="http://example.com/something">link</a> <A HREF="http://example.com/something">link</A>' ) text = '<img src="/upfiles/13487900323638005.png" alt="" />' self.assertEqual( absolutize_urls(text), '<img src="http://example.com/upfiles/13487900323638005.png" alt="" />' ) text = 'ohaouhaosthoanstoahuaou<br /><img src="/upfiles/13487906221942257.png" alt="" /><img class="gravatar" title="Evgeny4" src="http://kp-dev.askbot.com/avatar/render_primary/5/32/" alt="Evgeny4 gravatar image" width="32" height="32" />' self.assertEqual( absolutize_urls(text), 'ohaouhaosthoanstoahuaou<br /><img src="http://example.com/upfiles/13487906221942257.png" alt="" /><img class="gravatar" title="Evgeny4" src="http://kp-dev.askbot.com/avatar/render_primary/5/32/" alt="Evgeny4 gravatar image" width="32" height="32" />' ) text = '<a href="/upfiles/13487909784287052.png"><img src="/upfiles/13487909942351405.png" alt="" /></a><img src="http://i2.cdn.turner.com/cnn/dam/assets/120927033530-ryder-cup-captains-wall-4-tease.jpg" alt="" width="160" height="90" border="0" />and some text<br />aouaosutoaehut' self.assertEqual( absolutize_urls(text), '<a href="http://example.com/upfiles/13487909784287052.png"><img src="http://example.com/upfiles/13487909942351405.png" alt="" /></a><img src="http://i2.cdn.turner.com/cnn/dam/assets/120927033530-ryder-cup-captains-wall-4-tease.jpg" alt="" width="160" height="90" border="0" />and some text<br />aouaosutoaehut' )
def mail_moderators( subject_line = '', body_text = '', raise_on_failure = False, headers = None ): """sends email to forum moderators and admins """ body_text = absolutize_urls(body_text) from django.db.models import Q from askbot.models import User recipient_list = User.objects.filter( Q(status='m') | Q(is_superuser=True) ).filter( is_active = True ).values_list('email', flat=True) recipient_list = set(recipient_list) send_mail( subject_line=subject_line, body_text=body_text, from_email=getattr(django_settings, 'DEFAULT_FROM_EMAIL', ''), recipient_list=recipient_list, raise_on_failure=raise_on_failure, headers=headers )
def send_mail( subject_line=None, body_text=None, from_email=None, recipient_list=None, activity_type=None, related_object=None, headers=None, raise_on_failure=False, ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ from_email = from_email or askbot_settings.ADMIN_EMAIL or django_settings.DEFAULT_FROM_EMAIL body_text = absolutize_urls(body_text) try: assert subject_line is not None subject_line = prefix_the_subject_line(subject_line) debug_emails = [] if hasattr(django_settings, "DEBUG_EMAIL_USERS"): debug_emails = django_settings.DEBUG_EMAIL_USERS if len(debug_emails) > 0: new_recipient_list = [] # Only allow recipients in the debug email list for allowed in debug_emails: if allowed in recipient_list: new_recipient_list.append(allowed) if len(new_recipient_list) == 0: return recipient_list = new_recipient_list logging.debug("Only Email Debug Users %s" % recipient_list) msg = mail.EmailMultiAlternatives( subject_line, clean_html_email(body_text), from_email, recipient_list, headers=headers ) msg.attach_alternative(body_text, "text/html") msg.send() logging.debug("sent update to %s" % ",".join(recipient_list)) if related_object is not None: assert activity_type is not None except Exception, error: sys.stderr.write("\n" + unicode(error).encode("utf-8") + "\n") if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line = None, body_text = None, from_email = django_settings.DEFAULT_FROM_EMAIL, recipient_list = None, activity_type = None, related_object = None, headers = None, raise_on_failure = False, ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ body_text = absolutize_urls(body_text) try: assert(subject_line is not None) subject_line = prefix_the_subject_line(subject_line) msg = mail.EmailMultiAlternatives( subject_line, clean_html_email(body_text), from_email, recipient_list, headers = headers ) msg.attach_alternative(body_text, "text/html") #sys.stderr.write('\n send_mail(before Send):' + unicode(msg.body).encode('utf-8') + '\n') #sys.stderr.write('\n send_mail(before Send2):' + unicode(smart_str(body_text,'utf-8')).encode('utf-8') + '\n') msg.send() #sys.stderr.write('\n send_mail(after Send):' + unicode(from_email).encode('utf-8') + '\n') if related_object is not None: assert(activity_type is not None) except Exception, error: sys.stderr.write('\n send_mail:' + unicode(error).encode('utf-8') + '\n') """ try: sys.stderr.write('\n send_mail(subject_line):' + unicode(subject_line).encode('utf-8') + '\n') sys.stderr.write('\n send_mail(body_text):' + unicode(body_text).encode('utf-8') + '\n') sys.stderr.write('\n send_mail(recipient_list):' + unicode(recipient_list).encode('utf-8') + '\n') sys.stderr.write('\n send_mail(headers):' + unicode(headers).encode('utf-8') + '\n') except: sys.stderr.write('\n send_mail(except):' + unicode(sys.exc_info()[0]).encode('utf-8') + '\n') """ if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line=None, body_text=None, from_email=None, recipient_list=None, activity_type=None, related_object=None, headers=None, raise_on_failure=False, attachments=None ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised `attachments` is a tuple of triples ((filename, filedata, mimetype), ...) """ from_email = from_email or askbot_settings.ADMIN_EMAIL or \ django_settings.DEFAULT_FROM_EMAIL body_text = absolutize_urls(body_text) try: assert(subject_line is not None) subject_line = prefix_the_subject_line(subject_line) _send_mail( subject_line, body_text, from_email, recipient_list, headers=headers, attachments=attachments ) logging.debug('sent update to %s' % ','.join(recipient_list)) if related_object is not None: assert(activity_type is not None) except Exception, error: sys.stderr.write('\n' + unicode(error).encode('utf-8') + '\n') if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line = None, body_text = None, from_email = django_settings.DEFAULT_FROM_EMAIL, recipient_list = None, activity_type = None, related_object = None, headers = None, raise_on_failure = False, ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ body_text = absolutize_urls(body_text) try: assert(subject_line is not None) subject_line = prefix_the_subject_line(subject_line) msg = mail.EmailMultiAlternatives( subject_line, clean_html_email(body_text), from_email, recipient_list, headers = headers ) msg.attach_alternative(body_text, "text/html") msg.send() if related_object is not None: assert(activity_type is not None) except Exception, error: logging.critical(unicode(error)) if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def mail_moderators( subject_line = '', body_text = '', raise_on_failure = False, headers = None ): """sends email to forum moderators and admins """ body_text = absolutize_urls(body_text) from askbot.models.user import get_moderator_emails recipient_list = get_moderator_emails() send_mail( subject_line=subject_line, body_text=body_text, from_email=django_settings.DEFAULT_FROM_EMAIL, recipient_list=recipient_list, raise_on_failure=raise_on_failure, headers=headers )
def mail_moderators( subject_line = '', body_text = '', raise_on_failure = False, headers = None ): """sends email to forum moderators and admins """ body_text = absolutize_urls(body_text) from django.db.models import Q from askbot.models import User recipient_list = User.objects.filter( Q(status='m') | Q(is_superuser=True) ).filter( is_active = True ).values_list('email', flat=True) recipient_list = set(recipient_list) from_email = '' if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'): from_email = django_settings.DEFAULT_FROM_EMAIL try: msg = mail.EmailMessage( subject_line, body_text, from_email, recipient_list, headers = headers or {} ) msg.content_subtype = 'html' msg.send() except smtplib.SMTPException, error: logging.critical(unicode(error)) if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def mail_moderators(subject_line="", body_text="", raise_on_failure=False, headers=None): """sends email to forum moderators and admins """ body_text = absolutize_urls(body_text) from django.db.models import Q from askbot.models import User recipient_list = ( User.objects.filter(Q(status="m") | Q(is_superuser=True)).filter(is_active=True).values_list("email", flat=True) ) recipient_list = set(recipient_list) from_email = "" if hasattr(django_settings, "DEFAULT_FROM_EMAIL"): from_email = django_settings.DEFAULT_FROM_EMAIL try: msg = mail.EmailMessage(subject_line, body_text, from_email, recipient_list, headers=headers or {}) msg.content_subtype = "html" msg.send() except smtplib.SMTPException, error: sys.stderr.write("\n" + unicode(error).encode("utf-8") + "\n") if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line=None, body_text=None, from_email=None, recipient_list=None, headers=None, raise_on_failure=False, attachments=None): """ TODO: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file if raise_on_failure is True, exceptions.EmailNotSent is raised `attachments` is a tuple of triples ((filename, filedata, mimetype), ...) """ from_email = from_email or askbot_settings.ADMIN_EMAIL or django_settings.DEFAULT_FROM_EMAIL body_text = absolutize_urls(body_text) try: assert(subject_line is not None) subject_line = prefix_the_subject_line(subject_line) _send_mail( subject_line, body_text, from_email, recipient_list, headers=headers, attachments=attachments ) logging.debug('sent update to %s' % ','.join(recipient_list)) except Exception as error: print('\n{}\n'.format(error), file=sys.stderr) if raise_on_failure is True: raise exceptions.EmailNotSent(six.text_type(error))
def render_body(self): template = get_template(self.template_path + '/body.html') body = template.render(Context(self.get_context())) return absolutize_urls(body)
def render_body(self, context=None): template = get_template(self.template_path + "/body.html") body = template.render(Context(self.get_context(context))) return absolutize_urls(body)
def render_body(self): body = render_to_string(self.template_path + '/body.jinja', self.get_context(), request=HttpRequest()) return absolutize_urls(body)
def send_mail( subject_line=None, body_text=None, from_email=None, recipient_list=None, activity_type=None, related_object=None, headers=None, raise_on_failure=False, ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ from_email = from_email or askbot_settings.ADMIN_EMAIL or \ django_settings.DEFAULT_FROM_EMAIL body_text = absolutize_urls(body_text) try: assert (subject_line is not None) subject_line = prefix_the_subject_line(subject_line) debug_emails = [] if hasattr(django_settings, 'DEBUG_EMAIL_USERS'): debug_emails = django_settings.DEBUG_EMAIL_USERS if len(debug_emails) > 0: new_recipient_list = [] # Only allow recipients in the debug email list for allowed in debug_emails: if allowed in recipient_list: new_recipient_list.append(allowed) if len(new_recipient_list) == 0: return recipient_list = new_recipient_list logging.debug("Only Email Debug Users %s" % recipient_list) msg = mail.EmailMultiAlternatives(subject_line, clean_html_email(body_text), from_email, recipient_list, headers=headers) msg.attach_alternative(body_text, "text/html") msg.send() logging.debug('sent update to %s' % ','.join(recipient_list)) if related_object is not None: assert (activity_type is not None) except Exception, error: sys.stderr.write('\n' + unicode(error).encode('utf-8') + '\n') if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))