source: products/quintagroup.seoptimizer/branches/plone4/quintagroup/seoptimizer/browser/keywords.py @ 2958

Last change on this file since 2958 was 2958, checked in by mylan, 14 years ago

Merged 3.0.5 to 3.0.7 changes from trunk into plone-4 branch version.

Merged revisions 3942-3945,3966-3976,3982-3983,3994 via svnmerge from
http://svn.quintagroup.com/products/quintagroup.seoptimizer/trunk

........

r3942 | mylan | 2010-10-20 17:06:46 +0300 (Wed, 20 Oct 2010) | 1 line


#233: Bump version to 3.0.7, updated history

........

r3943 | olha | 2010-10-20 17:14:52 +0300 (Wed, 20 Oct 2010) | 1 line


HISTORY.txt checked

........

r3944 | olha | 2010-10-20 17:20:15 +0300 (Wed, 20 Oct 2010) | 1 line


internal README file updated

........

r3945 | mylan | 2010-10-20 18:16:14 +0300 (Wed, 20 Oct 2010) | 1 line


#233: Updated list of supported version in README.txt to 3.1+

........

r3966 | mylan | 2010-10-26 17:33:27 +0300 (Tue, 26 Oct 2010) | 1 line


#233: Reduce catch exceptions to URLError and HTTPError, log error to error_log, return link to error_log

........

r3967 | mylan | 2010-10-26 17:34:03 +0300 (Tue, 26 Oct 2010) | 1 line


#233: Update tests for catching errors on keywords calculation

........

r3968 | mylan | 2010-10-26 17:55:15 +0300 (Tue, 26 Oct 2010) | 1 line


#233: Make code more readable

........

r3969 | mylan | 2010-10-26 18:08:15 +0300 (Tue, 26 Oct 2010) | 1 line


#233: Force to get portal_transforms as utility

........

r3970 | mylan | 2010-10-26 18:14:46 +0300 (Tue, 26 Oct 2010) | 1 line


#233: little cleanup

........

r3971 | mylan | 2010-10-26 20:35:28 +0300 (Tue, 26 Oct 2010) | 1 line


#233: Force logging keywords calculation to nearest to context error_log object. Other minor fixes

........

r3972 | mylan | 2010-10-26 21:08:06 +0300 (Tue, 26 Oct 2010) | 1 line


#233: Return getting error_log with getToolByName as more flexible and accurate

........

r3973 | mylan | 2010-10-27 18:09:31 +0300 (Wed, 27 Oct 2010) | 1 line


#233: Correct HISTORY information

........

r3974 | mylan | 2010-10-27 18:16:40 +0300 (Wed, 27 Oct 2010) | 1 line


#233: Added plone.browserlayer to requirements for Plone-3.0 support

........

r3975 | mylan | 2010-10-27 18:18:13 +0300 (Wed, 27 Oct 2010) | 1 line


#233: Fixed testcases to emulate proper test environment for plone-3.0

........

r3976 | mylan | 2010-10-27 18:21:55 +0300 (Wed, 27 Oct 2010) | 1 line


#233: Described steps for use the package with plone-3.0.

........

r3982 | olha | 2010-10-29 13:16:55 +0300 (Fri, 29 Oct 2010) | 1 line


doc files corrected a bit

........

r3983 | olha | 2010-10-29 13:20:45 +0300 (Fri, 29 Oct 2010) | 1 line


wrong line spacing corrected

........

r3994 | mylan | 2010-11-01 16:59:34 +0200 (Mon, 01 Nov 2010) | 1 line


Removed devloper settings of the package configuration

........

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1import re, sys, urllib2
2from xml.dom import Node
3
4from zope.interface import implements
5from zope.component import getUtility
6from zope.component import queryAdapter
7
8from Acquisition import aq_acquire
9from Products.Five.browser import BrowserView
10
11from Products.CMFCore.utils import getToolByName
12from Products.CMFPlone.utils import safe_unicode, getSiteEncoding
13from Products.PortalTransforms.interfaces import IPortalTransformsTool
14
15from interfaces import IValidateSEOKeywordsView
16from quintagroup.seoptimizer import SeoptimizerMessageFactory as _
17from quintagroup.seoptimizer.browser.seo_configlet import ISEOConfigletSchema
18
19class ValidateSEOKeywordsView(BrowserView):
20
21    implements(IValidateSEOKeywordsView)
22
23    def validateKeywords(self):
24        """ see interface """
25        text = self.request.get('text')
26        ts = getToolByName(self.context, 'translation_service')
27        transforms = getUtility(IPortalTransformsTool)
28        portal = getToolByName(self.context, 'portal_url').getPortalObject()
29        isExternal = queryAdapter(portal, ISEOConfigletSchema).external_keywords_test
30        # extract keywords from text
31        enc = getSiteEncoding(self.context)
32        if text.lower().strip():
33            keywords = filter(None, map(lambda x: safe_unicode(x.strip(), enc),
34                                         text.lower().strip().split('\n')))
35        else:
36            return ts.utranslate(domain='quintagroup.seoptimizer',
37                                 msgid=_(u'Keywords list is empty!'),
38                                 context=self.context)
39        # Get html page internally or with external request
40        error_url = ""
41        if isExternal:
42            # Not pass timeout option because:
43            # 1. its value get from the global default timeout settings.
44            # 2. timeout option added in python 2.6 (so acceptable only in plone4+)
45            try:
46                resp = urllib2.urlopen(self.context.absolute_url())
47                try:
48                    html = resp.read()
49                finally:
50                    resp.close()
51            except (urllib2.URLError, urllib2.HTTPError), e:
52                # In case of exceed timeout period or other URL connection errors.
53                # Get nearest to context error_log object (stolen from Zope2/App/startup.py)
54                html = None
55                info = sys.exc_info()
56                elog = getToolByName(self.context, "error_log")
57                error_url = elog.raising(info)
58        else:
59            html = unicode(self.context()).encode(enc)
60
61        # If no html - information about problem with page retrieval should be returned
62        result = []
63        if html is None:
64            result.append("Problem with page retrieval.")
65            if error_url:
66                result.append("Details at %s." % error_url)
67        else:
68            page_text = transforms.convert("html_to_text", html).getData()
69            # check every keyword on appearing in body of html page
70            for keyword in keywords:
71                keyword_on_page = unicode(len(re.findall(u'\\b%s\\b' % keyword, page_text, re.I|re.U)))
72                result.append(' - '.join((keyword, keyword_on_page)))
73
74        return ts.utranslate(domain='quintagroup.seoptimizer',
75                             msgid=_(u'number_keywords',
76                               default=u'Number of keywords at page:\n${result}',
77                               mapping={'result':'\n'.join(result)}),
78                             context=self.context)
Note: See TracBrowser for help on using the repository browser.