def send_from_template(cls, addresses, subject, template_name, template_values=None, attachment=None, from_name=None, from_address=None, **kwags): """ Create HTML and text emails from template, then send. """ log.info("Emailer.send_from_template (%s)" % template_name) # Render email template as HTML. try: html = cls.render(template_name, template_values) except TemplateNotFound: log.warning("HTML template not found.") html = None try: text = cls.render(template_name, template_values, suffix="txt") except TemplateNotFound: log.warning("Text template not found.") text = None log.debug(html) return cls.send(addresses, subject, text, html, attachment, from_name, from_address)
def send(phone, message): log.info("Sending sms...") message = clean(message) settings = Config.get('twilio') account = twilio.Account(settings['sid'], settings['token']) callback = Config.base_url() if not callback: callback = Config.get('default_host') data = { 'From': settings['phone'], 'To': phone, 'Body': message, 'StatusCallback': "%stwilio/status" % callback } log.debug(data) try: response = account.request('/%s/Accounts/%s/SMS/Messages.json' % (settings['api'], settings['sid']), 'POST', data) log.info("--> %s" % response) response = json.loads(response) smsid = response['TwilioResponse']['SMSMessage']['Sid'] status = "passed" except Exception, e: log.error(e) smsid = None status = "blocked"
def dict_to_instance(self, data, need=None): if 'event_id' in data: try: int(data['event_id']) except ValueError: log.debug('Getting rid of an invalid event id: %r' % (data['event_id'],)) del data['event_id'] need = super(NeedModelRestController, self).dict_to_instance(data, need) return need
def dict_to_instance(self, data, need=None): if 'event_id' in data: try: int(data['event_id']) except ValueError: log.debug('Getting rid of an invalid event id: %r' % (data['event_id'], )) del data['event_id'] need = super(NeedModelRestController, self).dict_to_instance(data, need) return need
def instance_to_dict(self, row): if row is None: return None d = {} for columnName in row.__mapper__.columns.keys(): d[columnName] = getattr(row, columnName) try: if str(row.__mapper__.columns.get('name').type).startswith('VARCHAR'): d[columnName] = jinja2.Markup(d[columnName]).unescape() except Exception, e: log.debug("Exception decoding field %s: %s" % (columnName, e))
def instance_to_dict(self, row): if row is None: return None d = {} for columnName in row.__mapper__.columns.keys(): d[columnName] = getattr(row, columnName) try: col = row.__mapper__.columns.get('name') if col and str(col.type).startswith('VARCHAR'): d[columnName] = jinja2.Markup(d[columnName]).unescape() except Exception, e: log.debug("Exception decoding field %s: %s" % (columnName, e))
def dict_to_instance(self, data, instance=None): if instance is None: Model = self.get_model() instance = Model() for (key, val) in data.iteritems(): if key.startswith('_'): continue # All non-ascii (ond special) characters should be escaped with char-ref try: if key in Model.__table__.c.keys() and str(Model.__table__.c.get(key).type).startswith('VARCHAR'): val = jinja2.escape(val).encode('ascii','xmlcharrefreplace') if len(val) > 0: val = safeuni(val) except Exception, e: log.debug("Exception encoding field %s: %s" % (key, e)) pass setattr(instance, key, val)
def dict_to_instance(self, data, instance=None): if instance is None: Model = self.get_model() instance = Model() for (key, val) in data.iteritems(): if key.startswith('_'): continue # All non-ascii (ond special) characters should be escaped with char-ref try: if key in Model.__table__.c.keys() and str( Model.__table__.c.get(key).type).startswith('VARCHAR'): val = jinja2.escape(val).encode('ascii', 'xmlcharrefreplace') if len(val) > 0: val = safeuni(val) except Exception, e: log.debug("Exception encoding field %s: %s" % (key, e)) pass setattr(instance, key, val)
def __exit__(self, e_type=None, e_val=None, e_tb=None): from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Other exception with the ORM") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def __exit__(self, e_type=None, e_val=None, e_tb=None): # Since load_sqla is a processor, it catches bubbled-up exceptions from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Unhandled exception - check console logs for details") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def __enter__(self): log.debug("*** Loading the ORM") self.orm = OrmHolder().orm log.debug('***** loaded as %r' % self.orm) return self.orm