def get_charset(): """Character set to use for encoding the email. If encoding fails we will try some other encodings. We hope to get utf-8 here always actually. The getSiteEncoding call also works when portal is None, falling back to utf-8. But that is only on Plone 4, not Plone 3. So we handle that ourselves. """ charset = None portal = getSite() if portal is None: return DEFAULT_CHARSET if IMailSchema is None: # Plone 4 charset = portal.getProperty('email_charset', '') else: # Plone 5.0 and higher registry = getUtility(IRegistry) mail_settings = registry.forInterface( IMailSchema, prefix='plone', check=False) charset = mail_settings.email_charset if not charset: charset = getSiteEncoding(portal) return charset
def get_charset(): """Character set to use for encoding the email. If encoding fails we will try some other encodings. We hope to get utf-8 here always actually. The getSiteEncoding call also works when portal is None, falling back to utf-8. But that is only on Plone 4, not Plone 3. So we handle that ourselves. """ charset = None portal = getSite() if portal is None: return DEFAULT_CHARSET if IMailSchema is None: # Plone 4 charset = portal.getProperty('email_charset', '') else: # Plone 5.0 and higher registry = getUtility(IRegistry) mail_settings = registry.forInterface(IMailSchema, prefix='plone', check=False) charset = mail_settings.email_charset if not charset: charset = getSiteEncoding(portal) return charset
def __init__(self, context): super(PDFOptions, self).__init__(context) self.portal = context pprop = getToolByName(self.portal, 'portal_properties') self.context = getattr(pprop, PROPERTYSHEET) self.encoding = getSiteEncoding(context) return
def __init__(self, context): super(PDFOptions, self).__init__(context) self.portal = context pprop = getToolByName(self.portal, 'portal_properties') self.context = getattr(pprop, PROPERTYSHEET) self.encoding = getSiteEncoding(context) return
def validateKeywords(self): """ see interface """ text = self.request.get('text') ts = getToolByName(self.context, 'translation_service') transforms = getUtility(IPortalTransformsTool) portal = getToolByName(self.context, 'portal_url').getPortalObject() isExternal = queryAdapter(portal, ISEOConfigletSchema).external_keywords_test # extract keywords from text enc = getSiteEncoding(self.context) if text.lower().strip(): keywords = filter(None, map(lambda x: safe_unicode(x.strip(), enc), text.lower().strip().split('\n'))) else: return ts.utranslate(domain='quintagroup.seoptimizer', msgid=_(u'Keywords list is empty!'), context=self.context) # Get html page internally or with external request error_url = "" if isExternal: # Not pass timeout option because: # 1. its value get from the global default timeout settings. # 2. timeout option added in python 2.6 (so acceptable only in plone4+) try: resp = urllib2.urlopen(self.context.absolute_url()) try: html = resp.read() finally: resp.close() except (urllib2.URLError, urllib2.HTTPError), e: # In case of exceed timeout period or other URL connection errors. # Get nearest to context error_log object (stolen from Zope2/App/startup.py) html = None info = sys.exc_info() elog = getToolByName(self.context, "error_log") error_url = elog.raising(info)
def render(self): TEMPLATE = '<meta name="%s" content="%s"/>' enc = getSiteEncoding(self.context) sfuncd = lambda x, enc=enc: safe_unicode(x, enc) return u'\n'.join([ TEMPLATE % tuple(map(sfuncd, (k, v))) for k, v in self.listMetaTags().items() ])
def getPropertyStopWords(self): """ Get property 'stop_words' from SEO Properties tool. """ enc = getSiteEncoding(self.context) # self.gseo.stop_words return list of unicode objects, # and may contains stop words in different languages. # So we must return encoded strings. sw = map(lambda x: unicode.encode(x, enc), self.gseo.stop_words) return str(sw)
def pleaseDontCache(context, request): """Requests the proxy (if any) and user browser to avoid caching the response """ charset = getSiteEncoding(context) setHeader = request.RESPONSE.setHeader setHeader('Content-Type', 'text/html;;charset=%s' % charset) setHeader('Expires', 'Sat, 1 Jan 2000 00:00:00 GMT') setHeader('Last-Modified', 'Sat, 1 Jan 2000 00:00:00 GMT') setHeader('Cache-control', 'max-age=0,s-maxage=0,must-revalidate') setHeader('Pragma', 'no-cache') return
def get_charset(): """Character set to use for encoding the email. """ charset = None portal = api.portal.get() if portal is None: return DEFAULT_CHARSET charset = portal.getProperty('email_charset', '') if not charset: charset = getSiteEncoding(portal) return charset
def pleaseDontCache(context, request): """Requests the proxy (if any) and user browser to avoid caching the response """ charset = getSiteEncoding(context) setHeader = request.RESPONSE.setHeader setHeader("Content-Type", "text/html;;charset=%s" % charset) setHeader("Expires", "Sat, 1 Jan 2000 00:00:00 GMT") setHeader("Last-Modified", "Sat, 1 Jan 2000 00:00:00 GMT") setHeader("Cache-control", "max-age=0,s-maxage=0,must-revalidate") setHeader("Pragma", "no-cache") return
def pleaseDontCache(context, request): """Requests the proxy (if any) and user browser to avoid caching the response """ charset = getSiteEncoding(context) setHeader = request.RESPONSE.setHeader setHeader('Content-Type', 'text/html;;charset=%s' % charset) setHeader('Expires', 'Sat, 1 Jan 2000 00:00:00 GMT') setHeader('Last-Modified', 'Sat, 1 Jan 2000 00:00:00 GMT') setHeader('Cache-control', 'max-age=0,s-maxage=0,must-revalidate') setHeader('Pragma', 'no-cache') return
def get_charset(): """Character set to use for encoding the email. """ charset = None portal = api.portal.get() if portal is None: return DEFAULT_CHARSET charset = portal.getProperty('email_charset', '') if not charset: charset = getSiteEncoding(portal) return charset
def __init__(self, context, request): self.context = context self.request = request self.response = self.request.RESPONSE self.encoding = getSiteEncoding(self.context) self.delimiter = ';' self.stringdelimiter = '"' date = DateTime().strftime("%Y%m%d_%H%M") folder_title = normalizeString(self.context.Title(), encoding=self.encoding) self.filename = date + '_' + folder_title + '.csv' self.moneyfmt = getUtility(IMoneyFormat)
def __init__(self, context, request): self.context = context self.request = request self.response = self.request.RESPONSE self.encoding = getSiteEncoding(self.context) self.delimiter = ';' self.stringdelimiter = '"' date = DateTime().strftime("%Y%m%d_%H%M") folder_title = normalizeString(self.context.Title(), encoding=self.encoding) self.filename = date + '_' + folder_title + '.csv' self.moneyfmt = getUtility(IMoneyFormat)
def deleteObjectsByPaths(self, paths, handle_errors=True, REQUEST=None): """Copy of deleteObjectsByPaths of the method of same name in ``Products.CMFPlone.PloneTool.PloneTool``. We just know that """ failure = {} success = [] # use the portal for traversal in case we have relative paths portal = getSite() traverse = portal.restrictedTraverse charset = getSiteEncoding(self.context) for path in paths: # Skip and note any errors if handle_errors: sp = transaction.savepoint(optimistic=True) try: obj = traverse(path) # Check for the case where a path to a nonexisting object # deletes an acquired object if path.startswith('/'): absolute_path = path else: portal_path = '/'.join(portal.getPhysicalPath()) absolute_path = "%s/%s" % (portal_path, path) if '/'.join(obj.getPhysicalPath()) != absolute_path: raise # end check obj_parent = aq_parent(aq_inner(obj)) obj_parent.manage_delObjects([obj.getId()]) # PATCH: support for content with non ASCII chars title_or_id = obj.title_or_id() if not isinstance(title_or_id, unicode): title_or_id = unicode(title_or_id, charset, 'ignore') # Transform in plain ASCII title_or_id = unicodedata.normalize('NFKD', title_or_id).encode( 'ascii', 'ignore') success.append('%s (%s)' % (title_or_id, path)) # /PATCH except ConflictError: raise except LinkIntegrityNotificationException: raise except Exception, e: if handle_errors: sp.rollback() failure[path] = e else: raise
def getMemberInfoFromId(self, mid): mtool = getToolByName(self.context.context, 'portal_membership') portal_url = getToolByName(mtool, 'portal_url')() portal_url = portal_url.endswith('/') and portal_url or portal_url + '/' author_url = portal_url + 'author/' # Plone 2.5 does not like the id to be unicode encoding = getSiteEncoding(self.context.context) mid = mid.encode(encoding) m = mtool.getMemberById(mid) name = m.getUserName() return {'id': m.getId(), 'name': name, 'fullname': m.getProperty('fullname', ''), 'url': author_url+name}
def deleteObjectsByPaths(self, paths, handle_errors=True, REQUEST=None): """Copy of deleteObjectsByPaths of the method of same name in ``Products.CMFPlone.PloneTool.PloneTool``. We just know that """ failure = {} success = [] # use the portal for traversal in case we have relative paths portal = getSite() traverse = portal.restrictedTraverse charset = getSiteEncoding(self.context) for path in paths: # Skip and note any errors if handle_errors: sp = transaction.savepoint(optimistic=True) try: obj = traverse(path) # Check for the case where a path to a nonexisting object # deletes an acquired object if path.startswith('/'): absolute_path = path else: portal_path = '/'.join(portal.getPhysicalPath()) absolute_path = "%s/%s" % (portal_path, path) if '/'.join(obj.getPhysicalPath()) != absolute_path: raise # end check obj_parent = aq_parent(aq_inner(obj)) obj_parent.manage_delObjects([obj.getId()]) # PATCH: support for content with non ASCII chars title_or_id = obj.title_or_id() if not isinstance(title_or_id, unicode): title_or_id = unicode(title_or_id, charset, 'ignore') # Transform in plain ASCII title_or_id = unicodedata.normalize( 'NFKD', title_or_id).encode('ascii', 'ignore') success.append('%s (%s)' % (title_or_id, path)) # /PATCH except ConflictError: raise except LinkIntegrityNotificationException: raise except Exception, e: if handle_errors: sp.rollback() failure[path] = e else: raise
def __init__(self, context, widget): super(NavtreeStrategy, self).__init__(context, None) self.context = context self.widget = widget # set rootPath to the query path if the source query is not a # navtree query in this case we want the widget to only show # elements below the query path if 'path' in widget.source.navigation_tree_query: queryPath = widget.source.navigation_tree_query['path'] if 'navtree' not in queryPath or not queryPath['navtree']: self.rootPath = queryPath['query'] self.showAllParents = False # override from base class self.site_encoding = utils.getSiteEncoding(self.context)
def __init__(self, context, widget): super(NavtreeStrategy, self).__init__(context, None) self.context = context self.widget = widget # set rootPath to the query path if the source query is not a # navtree query in this case we want the widget to only show # elements below the query path if 'path' in widget.source.navigation_tree_query: queryPath = widget.source.navigation_tree_query['path'] if 'navtree' not in queryPath or not queryPath['navtree']: self.rootPath = queryPath['query'] self.showAllParents = False # override from base class self.site_encoding = utils.getSiteEncoding(self.context)
def getMemberInfoFromId(self, mid): mtool = getToolByName(self.context.context, 'portal_membership') portal_url = getToolByName(mtool, 'portal_url')() portal_url = portal_url.endswith( '/') and portal_url or portal_url + '/' author_url = portal_url + 'author/' # Plone 2.5 does not like the id to be unicode encoding = getSiteEncoding(self.context.context) mid = mid.encode(encoding) m = mtool.getMemberById(mid) name = m.getUserName() return { 'id': m.getId(), 'name': name, 'fullname': m.getProperty('fullname', ''), 'url': author_url + name }
def __call__(self): data = self._data() content = self.index(**data) # IE6 encoding bug workaround (IE6 sucks but...) if self.request.get('USER_AGENT', '').find('MSIE 6.0') > 0: # response must be latin-1 self.request.RESPONSE.setHeader("Content-Type", "text/html; charset=ISO-8859-1") encoding = getSiteEncoding(self.context.context) if not isinstance(content, unicode): content = content.decode(encoding) # convert special characters to HTML entities since we're # recoding to latin-1 content = escape_to_entities(content).encode('latin-1') return content
def cooked_price(self): """Return formatted price""" pr = unicode(aq_inner(self.context.price)) elements = [] if len(pr) > 9: elements.append(pr[-12:-9]) if len(pr) > 6: elements.append(pr[-9:-6]) if len(pr) > 3: elements.append(pr[-6:-3]) elements.append(pr[-3:]) currency = safe_unicode(self.properties.getProperty('currency'), getSiteEncoding(self.context)) return currency + u' ' + u'.'.join(elements)
def get_charset(): """Character set to use for encoding the email. If encoding fails we will try some other encodings. We hope to get utf-8 here always actually. The getSiteEncoding call also works when portal is None, falling back to utf-8. But that is only on Plone 4, not Plone 3. So we handle that ourselves. """ charset = None portal = getSite() if portal is None: return DEFAULT_CHARSET charset = portal.getProperty('email_charset', '') if not charset: charset = getSiteEncoding(portal) return charset
def cropText(self, text, length, ellipsis='...'): """Crop text on a word boundary """ converted = False if not isinstance(text, unicode): encoding = utils.getSiteEncoding(aq_inner(self.context)) text = unicode(text, encoding) converted = True if len(text)>length: text = text[:length] l = text.rfind(' ') if l > length/2: text = text[:l+1] text += ellipsis if converted: # encode back from unicode text = text.encode(encoding) return text
def toggle(self, value=None): """ same as __call__ but without the redirect """ if value is None: self.outdated = not self.outdated else: self.outdated = not not value if self.outdated: msg = u"Marked '%s' as outdated." else: msg = u"Removed outdated flag from '%s'." event = ObjectOutdatedToggleEvent(self.context, self.outdated) notify(event) name = self.context.title_or_id() if not isinstance(name, unicode): name = name.decode(getSiteEncoding(self.context)) msg = msg % name self.context.reindexObject() return msg
def cropText(self, text, length, ellipsis='...'): """Crop text on a word boundary """ converted = False if not isinstance(text, unicode): encoding = utils.getSiteEncoding(aq_inner(self.context)) text = unicode(text, encoding) converted = True if len(text) > length: text = text[:length] l = text.rfind(' ') if l > length / 2: text = text[:l + 1] text += ellipsis if converted: # encode back from unicode text = text.encode(encoding) return text
def __call__(self): data = self._data() content = self.index(**data) # IE6 encoding bug workaround (IE6 sucks but...) if self.request.get('USER_AGENT', '').find('MSIE 6.0') > 0: # response must be latin-1 self.request.RESPONSE.setHeader("Content-Type", "text/html; charset=ISO-8859-1") encoding = getSiteEncoding(self.context.context) if not isinstance(content, unicode): content = content.decode(encoding) # convert special characters to HTML entities since we're # recoding to latin-1 content = escape_to_entities(content).encode('latin-1') return content
def get_charset(): """Character set to use for encoding the email. If encoding fails we will try some other encodings. We hope to get utf-8 here always actually. The getSiteEncoding call also works when portal is None, falling back to utf-8. But that is only on Plone 4, not Plone 3. So we handle that ourselves. """ charset = None portal = getSite() if portal is None: return DEFAULT_CHARSET charset = portal.getProperty('email_charset', '') if not charset: charset = getSiteEncoding(portal) return charset
def Description(self): """Return the explicit description or the details. If a description is set manually, return that. Else returns the first 200 characters (defined in config.py) of the 'details' field. We must avoid returning a string in one case and a unicode in a different case. So we always return unicode. See http://plone.org/products/poi/issues/135 """ value = self.getField('description').get(self) if not value: value = self.getRawDetails() if len(value) > DESCRIPTION_LENGTH: value = value[:DESCRIPTION_LENGTH] + "..." # Always return unicode. if not isinstance(value, unicode): encoding = getSiteEncoding(self) value = safe_unicode(value, encoding) return value
def Description(self): """Return the explicit description or the details. If a description is set manually, return that. Else returns the first 200 characters (defined in config.py) of the 'details' field. We must avoid returning a string in one case and a unicode in a different case. So we always return unicode. See http://plone.org/products/poi/issues/135 """ value = self.getField('description').get(self) if not value: value = self.getRawDetails() if len(value) > DESCRIPTION_LENGTH: value = value[:DESCRIPTION_LENGTH] + "..." # Always return unicode. if not isinstance(value, unicode): encoding = getSiteEncoding(self) value = safe_unicode(value, encoding) return value
def render(self): seo = ISEOSettings(self.context, None) if not seo: return '' TEMPLATE = '<meta %(key)s content="%(content)s"/>' enc = getSiteEncoding(self.context) meta_tags = [] for seodict, name in METATAGS: content = getattr(seo, name, None) if not content: continue if isinstance(content, list) or isinstance(content, tuple): content = ', '.join(content).strip().strip(',') # each metatag can have more then one name/property for k,v in seodict.items(): opts = {'key': '%s="%s"' % (k,v), # i.e. name="twitter:card" 'content': escape(safe_unicode(content, enc))} meta_tags.append(TEMPLATE % opts) return u'\n'.join(meta_tags)
def role_settings(self): """Only return current settings""" results = self.existing_role_settings() encoding = getSiteEncoding(aq_inner(self.context)) static = {} index = None # Puts AuthenticatedUsers group to position 0 for item in results: if item['id'] == 'AuthenticatedUsers': index = results.index(item) break if index != -1 and index is not None: static = results.pop(index) results.sort(lambda x, y: cmp( safe_unicode(x["title"], encoding).lower(), safe_unicode(y["title"], encoding).lower())) # Only show AuthenticatedUsers group users with ManagePortal # perission # XXX: This should be configurable if static and self.has_manage_portal(): results.insert(0, static) return results
def handleOSHMailUrls(self, text, id=""): """ turn relative URLs into absolute URLs based on the context's URL; append google analytics code """ encoding = getSiteEncoding(self) if type(text) == unicode: text = text.encode(encoding) links = [] isURL = validation.validatorFor("isURL") if isURL(text) == 1: links.append(text) else: for link in retrieveSTX(text): links.append(link) for link in self.retrieveLinksFromHTML(text): links.append(link) au = self.context.absolute_url() for link in links: if not link.startswith("mailto"): joinchar = link.find("?") > 0 and "&" or "?" newlink = "%(link)s%(joinchar)sutm_source=oshmail&" "utm_medium=email&utm_campaign=%(campaign)s" % dict( link=urljoin(au, link), joinchar=joinchar, campaign=id != "" and id or "oshmail" ) # below is a fix for the problem that you have two links in # text, one being a prefix of the # other and appearing below it. In this case, the smaller # one will be replaced with GA postfix in the longer one # as fix, only replace links which terminate with a ". # Of course this requires that all links are closed properly # if the text already is a URL however, we don't want this hack if isURL(text) == 1: text = text.replace(link, newlink) else: text = text.replace(link + '"', newlink + '"') text = text.decode(encoding) return text
def unescapeOldTitle(setuptool): """ Upgrade quintagroup.seoptimizer title and comments properties. """ portal = getToolByName(setuptool, "portal_url").getPortalObject() types = ISEOConfigletSchema(portal).types_seo_enabled catalog = getToolByName(portal, "portal_catalog") brains = catalog(portal_type=types) for b in brains: obj = b.getObject() obj_enc = getSiteEncoding(obj) if obj.hasProperty("qSEO_title"): uni_qSEO_title = safe_unicode(obj.qSEO_title, encoding=obj_enc) fixed_title = unescape(uni_qSEO_title).encode(obj_enc) obj._updateProperty("qSEO_title", fixed_title) if obj.hasProperty("qSEO_html_comment"): uni_qSEO_html_comment = safe_unicode(obj.qSEO_html_comment, encoding=obj_enc) fixed_comment = unescape(uni_qSEO_html_comment).encode(obj_enc) obj._updateProperty("qSEO_html_comment", fixed_comment)
def role_settings(self): """Only return current settings""" results = self.existing_role_settings() encoding = getSiteEncoding(aq_inner(self.context)) static = {} index = None # Puts AuthenticatedUsers group to position 0 for item in results: if item['id'] == 'AuthenticatedUsers': index = results.index(item) break if index != -1 and index is not None: static = results.pop(index) results.sort( lambda x, y: cmp( safe_unicode(x["title"], encoding).lower(), safe_unicode(y["title"], encoding).lower())) # Only show AuthenticatedUsers group users with ManagePortal # perission # XXX: This should be configurable if static and self.has_manage_portal(): results.insert(0, static) return results
def unescapeOldTitle(setuptool): """ Upgrade quintagroup.seoptimizer title and comments properties. """ portal = getToolByName(setuptool, "portal_url").getPortalObject() types = ISEOConfigletSchema(portal).types_seo_enabled catalog = getToolByName(portal, "portal_catalog") brains = catalog(portal_type=types) for b in brains: obj = b.getObject() obj_enc = getSiteEncoding(obj) if obj.hasProperty("qSEO_title"): uni_qSEO_title = safe_unicode(obj.qSEO_title, encoding=obj_enc) fixed_title = unescape(uni_qSEO_title).encode(obj_enc) obj._updateProperty("qSEO_title", fixed_title) if obj.hasProperty("qSEO_html_comment"): uni_qSEO_html_comment = safe_unicode(obj.qSEO_html_comment, encoding=obj_enc) fixed_comment = unescape(uni_qSEO_html_comment).encode(obj_enc) obj._updateProperty("qSEO_html_comment", fixed_comment)
def validateKeywords(self): """ see interface """ text = self.request.get('text') ts = getToolByName(self.context, 'translation_service') # extract keywords from text enc = getSiteEncoding(self.context) if text.lower().strip(): keywords = filter(None, map(lambda x: safe_unicode(x.strip(), enc), text.lower().strip().split('\n'))) else: return ts.utranslate(domain='quintagroup.seoptimizer', msgid=_(u'Keywords list is empty!'), context=self.context) # request html page of context object url = '%s?without_metatag_keywords=1' % self.context.absolute_url() # extract words from url page using lynx browser (test page by 'url' # randered without metatag keywords) page_text = commands.getoutput('lynx --dump --nolist %s' % url).lower() if page_text and page_text != 'sh: lynx: command not found': page_text = safe_unicode(page_text, 'utf-8') else: return ts.utranslate(domain='quintagroup.seoptimizer', msgid=_(u'Could not find lynx browser!'), context=self.context) # check every keyword on appearing in body of html page result = [] for keyword in keywords: keyword_on_page = unicode(len(re.findall(u'\\b%s\\b' % keyword, page_text, re.I|re.U))) result.append(' - '.join((keyword, keyword_on_page))) return ts.utranslate(domain='quintagroup.seoptimizer', msgid=_(u'number_keywords', default=u'Number of keywords at page:\n${result}', mapping={'result':'\n'.join(result)}), context=self.context)
def getTranslatedString(self, value, preflang, default=u''): if not isinstance(value, unicode): value = value.decode(getSiteEncoding(self)) msg = _(value, default) return translate(msg, target_language=preflang)
def set_max_price(self, value): value = safe_unicode(value, getSiteEncoding(self.context)) self.context._updateProperty("max_price", value)
def get_max_price(self): value = getattr(self.context, "max_price", u"") value = safe_unicode(value, getSiteEncoding(self.context)) return value
def string_converter(value): return safe_unicode(value, getSiteEncoding(getSite()))
def createSubscribersFromCSV(self, file_upload): """ Create all subscribers objects from csv file uploaded """ # Reset old log self._csv_import_log = '' reader = csv.DictReader(file_upload, ['email', 'active', 'format']) # get target folder for subscribers object, or create it if not exist subscriber_folder = self.getSubscriberFolder() if subscriber_folder is None: self.invokeFactory('NewsletterBTree', 'subscribers') self.subscriber_folder_id = 'subscribers' subscriber_folder = getattr(self, 'subscribers') # remove headers first_row = reader.next() if first_row['email'] != 'email': return "You must add headers to the csv file : email, active, format ('email' at least)" # for each row, create a subscriber object default_format = self.default_format k = 0 already_used = [] not_valid = [] for row in reader: # get the field value, or the default value if row['active'] == '1': active = True elif row['active'] == '0': active = False else: active = False if row['format']: if row['format'].lower() == 'html': format = 'HTML' elif row['format'].lower() == 'text': format = 'Text' else: format = default_format else: format = default_format email = row['email'] email = email.strip() # check mail address validity if not checkMailAddress(self, email): not_valid.append(email) else: # check if subscriber already exist if self.alreadySubscriber(email): already_used.append(email) else: newId = self._getRandomIdForSubscriber() subscriber = self.createSubscriberObject(newId) subscriber.edit(format=format, active=active, email=email) k += 1 self._subscribersCount += 1 self._logCSVImportResult(not_valid, already_used) msg = '' if k: msg += '%s subscribers created. ' % str(k) if len(already_used): msg += '%s users were already subscriber on this newsletter theme. ' % len(already_used) if len(not_valid): msg += '%s emails were not valid. ' % len(not_valid) charset = getSiteEncoding(self) return msg.encode(charset)
def __init__(self, context): """Initialize adapter.""" self.context = context self.flag = True self.site_encoding = getSiteEncoding(context)
def encoding(): if HAS_SITE: return getSiteEncoding(getSite()) else: return 'utf-8'
def set_residential_storage(self, value): value = safe_unicode(value, getSiteEncoding(self.context)) self.context._updateProperty("residential_storage", value)
def write(self, msg): if self.response: if isinstance(msg, unicode): msg = msg.encode(getSiteEncoding(self)) self.response.write(msg + br) logger.info(msg)
def get_commercial_parking(self): value = getattr(self.context, "commercial_parking", u"") value = safe_unicode(value, getSiteEncoding(self.context)) return value
def __init__(self, context): """Initialize adapter.""" self.context = context self.flag = True self.site_encoding = getSiteEncoding(context)
def set_commercial_parking(self, value): value = safe_unicode(value, getSiteEncoding(self.context)) self.context._updateProperty("commercial_parking", value)
def search_result(self): """Returns only the search result for the new search result table""" # XXX: This method is inefficient. To many interations and # manipulations. # If someone really understands what to do, # please do it. mtool = getToolByName(self.context, 'portal_membership') gtool = getToolByName(self.context, 'portal_groups') user_results = self.user_search_results() group_results = self.group_search_results() current_settings = user_results + group_results requested = self.request.form.get('entries', []) # Remove real/saved roles from requested for item in self.existing_role_settings(): index = None for r_item in requested: if item['id'] == r_item['id']: index = requested.index(r_item) break if index is not None: requested.pop(index) if requested is not None: knownroles = [r['id'] for r in self.roles()] settings = {} result_like_settings = {} for entry in requested: roles = [] roles_for_settings = {} for r in knownroles: if entry.get('role_%s' % r, False): roles.append(r) roles_for_settings[r] = True else: roles_for_settings[r] = False settings[(entry['id'], entry['type'])] = roles # Add requested entries to current_settings if there is one or # more roles selected # This is kind a temporary storage for search results. # It allows you to do multible search queries and you will not # lose allready selected roles for user/groups if roles and not self.request.get('form.button.Save', None): # get group title or user fullname if entry['type'] == 'user': member = mtool.getMemberById(entry['id']) title = '%s' % (member.getProperty( 'fullname', entry['id'])) else: group = gtool.getGroupById(entry['id']) title = group.getGroupTitleOrName() result_like_settings = { 'id': entry['id'], 'type': entry['type'], 'title': title, 'roles': roles_for_settings } # Add or update settings updated = False for given_entry in current_settings: if result_like_settings['id'] == given_entry['id']: given_entry.update(result_like_settings) updated = True break if not updated: current_settings.append(result_like_settings) for entry in current_settings: desired_roles = settings.get((entry['id'], entry['type']), None) if desired_roles is None: continue for role in entry["roles"]: if entry["roles"][role] in [True, False]: entry["roles"][role] = role in desired_roles encoding = getSiteEncoding(aq_inner(self.context)) current_settings.sort(lambda x, y: cmp( safe_unicode(x["title"], encoding).lower(), safe_unicode(y["title"], encoding).lower())) return current_settings
def site_encoding(self): return utils.getSiteEncoding(self.context)
def encoding(): if HAS_SITE: return getSiteEncoding(getSite()) else: return 'utf-8'
def _get_default_encoding(site): from Products.CMFPlone.utils import getSiteEncoding return getSiteEncoding(site)
def render(self): TEMPLATE = '<meta name="%s" content="%s"/>' enc = getSiteEncoding(self.context) sfuncd = lambda x, enc=enc: safe_unicode(x, enc) return u'\n'.join([TEMPLATE % tuple(map(sfuncd, (k, v))) \ for k, v in self.listMetaTags().items()])
def validateKeywords(self): """ see interface """ text = self.request.get('text') ts = getToolByName(self.context, 'translation_service') transforms = getUtility(IPortalTransformsTool) portal = getToolByName(self.context, 'portal_url').getPortalObject() query_adapter = queryAdapter(portal, ISEOConfigletSchema) isExternal = query_adapter.external_keywords_test # extract keywords from text enc = getSiteEncoding(self.context) if text.lower().strip(): keywords = filter( None, map(lambda x: safe_unicode(x.strip(), enc), text.lower().strip().split('\n'))) else: return ts.utranslate(domain='quintagroup.seoptimizer', msgid=_(u'Keywords list is empty!'), context=self.context) # Get html page internally or with external request error_url = "" if isExternal: # Not pass timeout option because: # 1. its value get from the global default timeout settings. # 2. timeout option added in python 2.6 # (so acceptable only in plone4+) try: resp = urllib2.urlopen(self.context.absolute_url()) try: html = resp.read() finally: resp.close() except (urllib2.URLError, urllib2.HTTPError): # In case of exceed timeout period or # other URL connection errors. # Get nearest to context error_log object # (stolen from Zope2/App/startup.py) html = None info = sys.exc_info() elog = getToolByName(self.context, "error_log") error_url = elog.raising(info) else: html = unicode(self.context()).encode(enc) # If no html - information about problem with page retrieval # should be returned result = [] if html is None: result.append("Problem with page retrieval.") if error_url: result.append("Details at %s." % error_url) else: page_text = transforms.convert("html_to_text", html).getData() # check every keyword on appearing in body of html page for keyword in keywords: keyword_on_page = unicode( len( re.findall(u'\\b%s\\b' % keyword, page_text, re.I | re.U))) result.append(' - '.join((keyword, keyword_on_page))) return ts.utranslate(domain='quintagroup.seoptimizer', msgid=_(u'number_keywords', default=u'Number of keywords at page:\n' '${result}', mapping={'result': '\n'.join(result)}), context=self.context)