def timedelta_to_string(td): '''Convert a time delta to a Unicode string. Inspired by Recipe 498062 http://code.activestate.com/recipes/498062/ ARGUMENTS td: Time delta RETURNS A Unicode string, representing the time-delta. SIDE EFFECTS None''' assert isinstance(td, datetime.timedelta) deltas = [] s = int(td.days * 86400 + td.seconds) # Unit Seconds unitLimits = [("year", 31536000), ("month", 2592000), ("week", 604800), ("day", 86400), ("hour", 3600), ("minute", 60), ("second", 1)] for unit, limit in unitLimits: tdInUnit = s / limit if tdInUnit: if (tdInUnit > 1): # Plural deltas.append(u'%d %ss' % (tdInUnit, unit)) else: deltas.append(u'%d %s' % (tdInUnit, unit)) s = s - (tdInUnit * limit) retval = comma_comma_and(deltas) assert type(retval) == unicode return retval
def authors(self): '''The names of random recent authors, not including this person :returns: The names of five recent authors, seperated by commas :rtype: str''' authorNames = [u.name for u in self.specificAuthors] retval = comma_comma_and(authorNames) if (retval is not '') and (self.nAuthors != len(authorNames)): retval = 'including {0}'.format(retval) return retval
def specificKeywords(self): specificKeywords = [] n = 0 for keyword in self.keywords: if (len(keyword) > 31) or ('@' in keyword) or ('www' in keyword): continue n += len(keyword) if n >= self.maxTextLen: break specificKeywords.append(keyword) markedUpKeywords = self.markup_keywords(specificKeywords) shuffle(markedUpKeywords) retval = comma_comma_and(markedUpKeywords) return retval
def specificTopics(self): specificTopics = [] n = 0 for topic in self.topics: n += len(topic.name) if n >= self.maxTextLen: break specificTopics.append(topic) t = '<a href="{siteInfo.url}/r/topic/{topic.lastPostId}">'\ '{topic.name}</a>' topicLinks = [t.format(siteInfo=self.siteInfo, topic=topic) for topic in specificTopics] retval = comma_comma_and(topicLinks) return retval
def rolesToDescriptions(roles): k = list(roleMap.keys()) rs = [r.strip() for r in roles if (r.strip() in k)] if len(rs) > 1: try: rs.remove('Manager') except ValueError: pass if (('GroupAdmin' in roles) or ('DivisionAdmin' in roles)): rs.append('admins') ds = [r for r in [roleMap.get(r, '') for r in rs] if r] retval = comma_comma_and(ds) return retval
def xhtml(self): cssClass = 'audit-event groupserver-group-member-email-settings-%s'\ % self.code addrs = ['<code class="email">{0}</code>'.format(a) for a in self.instanceDatum.split()] addrStr = comma_comma_and(addrs) r = '<span class="{0}">Switched to use the email addresses {1} in '\ '{2}' retval = r.format(cssClass, addrStr, groupInfo_to_anchor(self.groupInfo)) if self.adminChanged: uu = userInfo_to_anchor(self.userInfo) retval = '{0} by {1}'.format(retval, uu) d = munge_date(self.context, self.date) retval = '{0}</span> ({1})'.format(retval, d) return retval
def specificTopics(self): specificTopics = [] n = 0 for topic in self.topics: n += len(topic.name) if n >= self.maxTextLen: break specificTopics.append(topic) t = '<a href="{siteInfo.url}/r/topic/{topic.lastPostId}">'\ '{topic.name}</a>' topicLinks = [ t.format(siteInfo=self.siteInfo, topic=topic) for topic in specificTopics ] retval = comma_comma_and(topicLinks) return retval
def __unicode__(self): addrs = self.instanceDatum.split() addrStr = comma_comma_and(addrs) if self.adminChanged: r = '{0} ({1}) has switched to the specific email addresses '\ '{6} for {2} ({3}) on ({4} {5})' retval = r.format(self.userInfo.name, self.userInfo.id, self.groupInfo.name, self.groupInfo.id, self.siteInfo.name, self.siteInfo.id, addrStr) else: r = '{0} ({1}) has switched {2} ({3}) to use the specific'\ 'addresses {8} for ({4} {5}) on ({6} {7})' retval = r.format(self.userInfo.name, self.userInfo.id, self.instanceUserInfo.name, self.instanceUserInfo.id, self.groupInfo.name, self.groupInfo.id, self.siteInfo.name, self.siteInfo.id, addrStr) return retval
def xhtml(self): cssClass = 'audit-event groupserver-group-member-email-settings-%s'\ % self.code addrs = [ '<code class="email">{0}</code>'.format(a) for a in self.instanceDatum.split() ] addrStr = comma_comma_and(addrs) r = '<span class="{0}">Switched to use the email addresses {1} in '\ '{2}' retval = r.format(cssClass, addrStr, groupInfo_to_anchor(self.groupInfo)) if self.adminChanged: uu = userInfo_to_anchor(self.userInfo) retval = '{0} by {1}'.format(retval, uu) d = munge_date(self.context, self.date) retval = '{0}</span> ({1})'.format(retval, d) return retval
def get_property(self, propertyId, default=''): p = self.props[propertyId].bind(self.context) if (hasattr(p, 'vocabulary') and (p.vocabulary is None)): # Deal with named vocabularies p.vocabulary = getUtility(IVocabularyFactory, p.vocabularyName, self.context) r = p.query(self.context, default) if hasattr(p, 'vocabulary'): try: retval = p.vocabulary.getTerm(r).title except (LookupError, AttributeError): retval = r elif hasattr(p, 'value_type') and type(r) == list: # Named vocabularies will cause a mare, like above. vocab = p.value_type.vocabulary s = [vocab.getTerm(v).title for v in r] retval = comma_comma_and(s) else: retval = r return retval
def test_three(self): testText = ['Dirk', 'Dinsdale', 'the local constable for the area'] r = comma_comma_and(testText) expected = 'Dirk, Dinsdale, and the local constable for the area' self.assertEqual(expected, r)
def test_double(self): testText = ['Dirk', 'Dinsdale'] r = comma_comma_and(testText) self.assertEqual('Dirk, and Dinsdale', r)
def handle_change(self, action, data): deliveryMethod = data['delivery'] defaultOrSpecific = data['default_or_specific'] emailAddresses = data['destination'] assert deliveryMethod in ('email', 'digest', 'web'), \ "Unexpected delivery option %s" % deliveryMethod if deliveryMethod != 'web': if defaultOrSpecific not in ('default', 'specific'): em = "Unexpected defaultOrSpecific %s" errMsg = em % defaultOrSpecific raise ValueError(errMsg) if not (isinstance(emailAddresses, list)): errM = "destination addresses {0} are not in a list" errMsg = errM.format(emailAddresses) raise TypeError(errMsg) if self.is_editing_self: name = '<a href="%s">You</a>' % self.userInfo.url else: name = '<a href="%s">%s</a>' % (self.userInfo.url, self.userInfo.name) groupName = '<a href="%s">%s</a>' % (self.groupInfo.relativeURL, self.groupInfo.name) m = "" self.groupEmailUser.set_default_delivery() auditor = SettingsAuditor(self.context, self.userInfo, self.loggedInUser, self.groupInfo) if deliveryMethod == 'email': auditor.info(EMAIL) m += '<strong>%s</strong> will receive an email message '\ 'every time someone posts to %s.' % (name, groupName) elif deliveryMethod == 'digest': self.groupEmailUser.set_digest() auditor.info(DIGEST) m += '<strong>%s</strong> will receive a daily digest of '\ 'topics posted to %s.' % (name, groupName) elif deliveryMethod == 'web': self.groupEmailUser.set_webonly() auditor.info(WEB_ONLY) m += '<strong>%s</strong> will not receive any email from ' \ '%s.' % (name, groupName) m += ' ' if deliveryMethod != 'web': # reset the specific addresses if defaultOrSpecific == 'specific' and emailAddresses: m += 'Email will be delivered to:\n<ul>' for address in emailAddresses: self.groupEmailUser.add_specific_address(address) m += '<li><code class="email">%s</code></li>' % address m += '</ul>\n' auditor.info(ADDRESS_SPECIFIC, ' '.join(emailAddresses)) else: pref = self.groupEmailUser.get_preferred_email_addresses() addrs = [ '<code class="email">{0}</code>'.format(a) for a in pref ] plural = 'address' if len(addrs) == 1 else 'addresses' isAre = 'is' if len(addrs) == 1 else 'are' m += 'Email will be delivered to the default {0}, which '\ '{1} {2}'.format(plural, isAre, comma_comma_and(addrs)) auditor.info(ADDRESS_DEFAULT) self.status = m
def handle_change(self, action, data): deliveryMethod = data['delivery'] defaultOrSpecific = data['default_or_specific'] emailAddresses = data['destination'] assert deliveryMethod in ('email', 'digest', 'web'), \ "Unexpected delivery option %s" % deliveryMethod if deliveryMethod != 'web': if defaultOrSpecific not in ('default', 'specific'): em = "Unexpected defaultOrSpecific %s" errMsg = em % defaultOrSpecific raise ValueError(errMsg) if not(isinstance(emailAddresses, list)): errM = "destination addresses {0} are not in a list" errMsg = errM.format(emailAddresses) raise TypeError(errMsg) if self.is_editing_self: name = '<a href="%s">You</a>' % self.userInfo.url else: name = '<a href="%s">%s</a>' % (self.userInfo.url, self.userInfo.name) groupName = '<a href="%s">%s</a>' % (self.groupInfo.relativeURL, self.groupInfo.name) m = "" self.groupEmailUser.set_default_delivery() auditor = SettingsAuditor(self.context, self.userInfo, self.loggedInUser, self.groupInfo) if deliveryMethod == 'email': auditor.info(EMAIL) m += '<strong>%s</strong> will receive an email message '\ 'every time someone posts to %s.' % (name, groupName) elif deliveryMethod == 'digest': self.groupEmailUser.set_digest() auditor.info(DIGEST) m += '<strong>%s</strong> will receive a daily digest of '\ 'topics posted to %s.' % (name, groupName) elif deliveryMethod == 'web': self.groupEmailUser.set_webonly() auditor.info(WEB_ONLY) m += '<strong>%s</strong> will not receive any email from ' \ '%s.' % (name, groupName) m += ' ' if deliveryMethod != 'web': # reset the specific addresses if defaultOrSpecific == 'specific' and emailAddresses: m += 'Email will be delivered to:\n<ul>' for address in emailAddresses: self.groupEmailUser.add_specific_address(address) m += '<li><code class="email">%s</code></li>' % address m += '</ul>\n' auditor.info(ADDRESS_SPECIFIC, ' '.join(emailAddresses)) else: pref = self.groupEmailUser.get_preferred_email_addresses() addrs = ['<code class="email">{0}</code>'.format(a) for a in pref] plural = 'address' if len(addrs) == 1 else 'addresses' isAre = 'is' if len(addrs) == 1 else 'are' m += 'Email will be delivered to the default {0}, which '\ '{1} {2}'.format(plural, isAre, comma_comma_and(addrs)) auditor.info(ADDRESS_DEFAULT) self.status = m