def _required_message(self, params, fields): missing = [s for s in fields if not params.get(s, '').strip()] if missing: return '{} {} required'.format( readable_join([s.replace('_', ' ').title() for s in missing]), 'is' if len(missing) == 1 else 'are') return ''
def get_twilio_client(twilio_sid, twilio_token): if c.SEND_SMS: try: if twilio_sid and twilio_token: return TwilioRestClient(twilio_sid, twilio_token) else: log.info('Twilio: could not create twilio client. Missing twilio {}.'.format( readable_join(['' if twilio_sid else 'SID', '' if twilio_token else 'TOKEN']))) except Exception: log.error('Twilio: could not create twilio client', exc_info=True) return None
def humanize_timedelta(*args, granularity='seconds', separator=None, now='right now', prefix='', suffix='', past_prefix='', past_suffix='', **kwargs): """ Converts a time interval into a nicely formatted human readable string. Accepts either a single `datetime.timedelta` instance, or a series of keyword arguments specifying time units: - years - months - days - hours - minutes - seconds Args: delta (datetime.timedelta): The timedelta to format granularity (str): The smallest unit of time that should be included. Defaults to "seconds". years (int, float): A number of years. months (int, float): A number of months. days (int, float): A number of days. hours (int, float): A number of hours. minutes (int, float): A number of minutes. seconds (int, float): A number of seconds. Returns: str: A human readable string, like "2 hours and 45 minutes", or "right now" if the timedelta is equal to zero. """ if args and isinstance(args[0], timedelta): delta = relativedelta(seconds=args[0].total_seconds()).normalized() else: delta = relativedelta(**kwargs).normalized() units = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] if past_prefix or past_suffix: is_past = False for unit in reversed(units): unit = int(getattr(delta, unit)) if unit != 0: is_past = unit < 0 if is_past: prefix = past_prefix suffix = past_suffix time_units = [] for unit in units: time = abs(int(getattr(delta, unit))) if time: plural = pluralize(time) time_units.append('{} {}{}'.format(time, unit[:-1], plural)) if unit == granularity: break if time_units: if separator is None: humanized = readable_join(time_units) else: humanized = separator.join(time_units) return '{}{}{}'.format(prefix, humanized, suffix) return now
def humanize_timedelta( *args, granularity='seconds', separator=None, now='right now', prefix='', suffix='', past_prefix='', past_suffix='', **kwargs): """ Converts a time interval into a nicely formatted human readable string. Accepts either a single `datetime.timedelta` instance, or a series of keyword arguments specifying time units: - years - months - days - hours - minutes - seconds Args: delta (datetime.timedelta): The timedelta to format granularity (str): The smallest unit of time that should be included. Defaults to "seconds". years (int, float): A number of years. months (int, float): A number of months. days (int, float): A number of days. hours (int, float): A number of hours. minutes (int, float): A number of minutes. seconds (int, float): A number of seconds. Returns: str: A human readable string, like "2 hours and 45 minutes", or "right now" if the timedelta is equal to zero. """ if args and isinstance(args[0], timedelta): delta = relativedelta(seconds=args[0].total_seconds()).normalized() else: delta = relativedelta(**kwargs).normalized() units = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] if past_prefix or past_suffix: is_past = False for unit in reversed(units): unit = int(getattr(delta, unit)) if unit != 0: is_past = unit < 0 if is_past: prefix = past_prefix suffix = past_suffix time_units = [] for unit in units: time = abs(int(getattr(delta, unit))) if time: plural = pluralize(time) time_units.append('{} {}{}'.format(time, unit[:-1], plural)) if unit == granularity: break if time_units: if separator is None: humanized = readable_join(time_units) else: humanized = separator.join(time_units) return '{}{}{}'.format(prefix, humanized, suffix) return now
def required_roles_labels(self): return readable_join([r.name for r in self.required_roles])