def getContainer(self, default=None): """ Get the container object where new objects from Salesforce should be stored. Since the location can be specified using an instance method of the object, it is usually best to populate the object before attempting to get the container. """ container_value = self._queryTaggedValue('salesforce.container', None) if not container_value: return default # First we see if this is an instance method of the object. If so, # it is responsible for returning the container object. container_factory = getattr(self.context, container_value, None) if callable(container_factory): return container_factory() # Otherwise, we assume this is a path relative to the site root # and attempt to traverse to it. if container_value.startswith('/'): container_value = container_value[1:] container = getSite().restrictedTraverse(container_value, None) if container: return container return default
def getRecaptchaSettings(): registry = getUtility(IRegistry) if TRY_REGISTRY: # if plone.formwidget.recaptcha is installed, try getting # its settings from the registry try: settings = registry.forInterface(IReCaptchaSettings) if settings.public_key and settings.private_key \ and settings.multilingual and settings.default_language\ and settings.default_theme and settings.fallback: return settings except: pass # try getting settings from the registry first try: settings = registry.forInterface(IRecaptchaSettings) if settings.public_key and settings.private_key \ and settings.multilingual and settings.default_language \ and settings.default_theme and settings.fallback: return settings except KeyError: # fall back to our storage of an annotation on the site if the settings # haven't been configured site = getSite() return IRecaptchaSettings(site)
def getRoles(context): """ Return a list of user roles. """ pmemb = getToolByName(getSite(), 'portal_membership') roles = [role for role in pmemb.getPortalRoles() if role != 'Owner'] return SimpleVocabulary.fromValues(roles)
def getWebProperties(context): """ Return list of Google Analytics profiles and web property IDs (e.g. UA-30481-22). """ analytics_tool = getToolByName(getSite(), "portal_analytics") # short circuit if user hasn't authorized yet if not analytics_tool.auth_token: return SimpleVocabulary([]) try: accounts = analytics_tool.getAccountsFeed("accounts/~all/webproperties/~all/profiles") except error.BadAuthenticationError: choices = [ ( "Please authorize with Google in the Google Analytics \ control panel.", None, ) ] return SimpleVocabulary.fromItems(choices) except error.RequestTimedOutError: choices = [ ( "The request to Google Analytics timed out. Please try \ again later.", None, ) ] return SimpleVocabulary.fromItems(choices) if accounts: unique_choices = {} # In vocabularies, both the terms and the values must be unique. Since # there can be more than one profile for a given web property, we create a list # of all the profiles for each property. (Ideally we would use the URL for the # web property, but Google doesn't expose it through the Analytics API.) for entry in accounts.entry: for prop in entry.property: if prop.name == "ga:profileName": title = prop.value if not isinstance(title, unicode): title = unicode(title, "utf-8") if prop.name == "ga:webPropertyId": webPropertyId = prop.value if not webPropertyId in unique_choices.keys(): unique_choices.update({webPropertyId: title}) else: unique_choices[webPropertyId] += ", " + title # After we reverse the terms so that the profile name(s) is now the key, we need # to ensure that these keys are unique. So, we pass the resulting list through # dict() and then output a list of items. choices = dict([(title, property_id) for (property_id, title) in unique_choices.items()]).items() else: choices = [("No profiles available", None)] return SimpleVocabulary([SimpleTerm(c[1], c[1], c[0]) for c in choices])
def getWebProperties(context): """ Return list of Google Analytics profiles and web property IDs (e.g. UA-30481-22). """ custom_connection = (_(u'custom tracking code'), '_TRACKING_CODE_CUSTOM') analytics_tool = getToolByName(getSite(), 'portal_analytics') # short circuit if user hasn't authorized yet if not analytics_tool.auth_token: return SimpleVocabulary([ SimpleTerm(custom_connection[1], custom_connection[1], custom_connection[0]) ]) try: accounts = analytics_tool.getAccountsFeed( 'accounts/~all/webproperties/~all/profiles') except error.BadAuthenticationError: choices = [('Please authorize with Google in the Google Analytics \ control panel.', None)] return SimpleVocabulary.fromItems(choices) except error.RequestTimedOutError: choices = [('The request to Google Analytics timed out. Please try \ again later.', None)] return SimpleVocabulary.fromItems(choices) if accounts: unique_choices = {} # In vocabularies, both the terms and the values must be unique. Since # there can be more than one profile for a given web property, we create a list # of all the profiles for each property. (Ideally we would use the URL for the # web property, but Google doesn't expose it through the Analytics API.) for entry in accounts.entry: for prop in entry.property: if prop.name == 'ga:profileName': title = prop.value if not isinstance(title, unicode): title = unicode(title, 'utf-8') if prop.name == 'ga:webPropertyId': webPropertyId = prop.value if not webPropertyId in unique_choices.keys(): unique_choices.update({webPropertyId: title}) else: unique_choices[webPropertyId] += ', ' + title # After we reverse the terms so that the profile name(s) is now the key, we need # to ensure that these keys are unique. So, we pass the resulting list through # dict() and then output a list of items. choices = dict([(crop(title, 40), property_id) for (property_id, title) in unique_choices.items() ]).items() else: choices = [('No profiles available', None)] choices.append(custom_connection) return SimpleVocabulary([SimpleTerm(c[1], c[1], c[0]) for c in choices])
def getReports(context, category=None): """ Return list of Google Analytics reports. """ analytics_tool = getToolByName(getSite(), "portal_analytics") reports = analytics_tool.getReports(category=category) choices = [] if reports: choices = [(report.title, report.id) for report in reports] return SimpleVocabulary.fromItems(choices)
def getReports(context, category=None): """ Return list of Google Analytics reports. """ analytics_tool = getToolByName(getSite(), 'portal_analytics') reports = analytics_tool.getReports(category=category) choices = [] if reports: choices = [SimpleTerm(value=report.id, token=report.id, title=report.title) for report in reports] return SimpleVocabulary(choices)
def getPluginNameChoices(self): """ Return the list of plugin names. """ gsm = getGlobalSiteManager() global_plugins = set([p.name for p in gsm.registeredAdapters() if p.provided == IAnalyticsPlugin]) lsm = getSite().getSiteManager() local_plugins = set([p.name for p in lsm.registeredAdapters() if p.provided == IAnalyticsPlugin]) return sorted(list(global_plugins | local_plugins))
def getTrackingPluginNames(context): """ Return a list of the names of the available tracking plugins. """ gsm = getGlobalSiteManager() global_plugins = set([p.name for p in gsm.registeredAdapters() if p.provided == IAnalyticsTrackingPlugin]) lsm = getSite().getSiteManager() local_plugins = set([p.name for p in lsm.registeredAdapters() if p.provided == IAnalyticsTrackingPlugin]) values = sorted(list(global_plugins | local_plugins)) return SimpleVocabulary.fromValues(values)
def getReports(context, category=None): """ Return list of Google Analytics reports. """ analytics_tool = getToolByName(getSite(), 'portal_analytics') reports = analytics_tool.getReports(category=category) choices = [] if reports: choices = [( report.title, report.id, ) for report in reports] return SimpleVocabulary.fromItems(choices)
def getTrackingPluginNames(context): """ Return a list of the names of the available tracking plugins. """ gsm = getGlobalSiteManager() global_plugins = set([p.name for p in gsm.registeredAdapters() \ if p.provided == IAnalyticsTrackingPlugin]) lsm = getSite().getSiteManager() local_plugins = set([p.name for p in lsm.registeredAdapters() \ if p.provided == IAnalyticsTrackingPlugin]) values = sorted(list(global_plugins | local_plugins)) return SimpleVocabulary.fromValues(values)
def getProfiles(context): """ Return list of Google Analytics profiles and corresponding account IDs (e.g. ga:30481). """ analytics_tool = getToolByName(getSite(), "portal_analytics") # short circuit if user hasn't authorized yet if not analytics_tool.auth_token: return SimpleVocabulary([]) try: accounts = analytics_tool.getAccountsFeed("accounts/~all/webproperties/~all/profiles") except error.BadAuthenticationError: choices = [ ( "Please authorize with Google in the Google Analytics \ control panel.", None, ) ] return SimpleVocabulary.fromItems(choices) except error.RequestTimedOutError: choices = [ ( "The request to Google Analytics timed out. Please try \ again later.", None, ) ] return SimpleVocabulary.fromItems(choices) if accounts: unique_choices = {} for entry in accounts.entry: for prop in entry.property: if prop.name == "ga:profileName": title = prop.value if not isinstance(title, unicode): title = unicode(title, "utf-8") if prop.name == "dxp:tableId": tableId = prop.value unique_choices.update({title: tableId}) choices = unique_choices.items() else: choices = [("No profiles available", None)] return SimpleVocabulary([SimpleTerm(c[1], c[1], c[0]) for c in choices])
def getPluginNameChoices(self): """ Return the list of plugin names. """ gsm = getGlobalSiteManager() global_plugins = set([ p.name for p in gsm.registeredAdapters() if p.provided == IAnalyticsPlugin ]) lsm = getSite().getSiteManager() local_plugins = set([ p.name for p in lsm.registeredAdapters() if p.provided == IAnalyticsPlugin ]) return sorted(list(global_plugins | local_plugins))
def getProfiles(context): """ Return list of Google Analytics profiles and corresponding account IDs (e.g. ga:30481). """ analytics_tool = getToolByName(getSite(), 'portal_analytics') # short circuit if user hasn't authorized yet if not analytics_tool.is_auth(): return SimpleVocabulary([]) try: accounts = analytics_tool.getAccountsFeed('accounts/~all/webproperties/~all/profiles') except error.BadAuthenticationError: choices = [('Please authorize with Google in the Google Analytics \ control panel.', None)] return SimpleVocabulary.fromItems(choices) except error.RequestTimedOutError: choices = [('The request to Google Analytics timed out. Please try \ again later.', None)] return SimpleVocabulary.fromItems(choices) except RequestError: choices = [('Request to Google Analytics errored, you might need to ' 'authenticate again.', None)] return SimpleVocabulary.fromItems(choices) if accounts: unique_choices = {} for entry in accounts.entry: for prop in entry.property: if prop.name == 'ga:profileName': title = prop.value if not isinstance(title, unicode): title = unicode(title, 'utf-8') title = crop(title, 40) if prop.name == 'dxp:tableId': tableId = prop.value unique_choices.update({title: tableId}) choices = unique_choices.items() else: choices = [('No profiles available', None)] return SimpleVocabulary([SimpleTerm(c[1], c[1], c[0]) for c in choices])
def getRecaptchaSettings(): registry = getUtility(IRegistry) if TRY_REGISTRY: # if plone.formwidget.recaptcha is installed, try getting # its settings from the registry try: settings = registry.forInterface(IReCaptchaSettings) if settings.public_key and settings.private_key: return settings except: pass # try getting settings from the registry first try: settings = registry.forInterface(IRecaptchaSettings) if settings.public_key and settings.private_key: return settings except KeyError: # fall back to our storage of an annotation on the site if the settings # haven't been configured site = getSite() return IRecaptchaSettings(site)
def getProfiles(context): """ Return list of Google Analytics profiles and corresponding account IDs (e.g. ga:30481). """ analytics_tool = getToolByName(getSite(), 'portal_analytics') # short circuit if user hasn't authorized yet if not analytics_tool.auth_token: return SimpleVocabulary([]) try: accounts = analytics_tool.getAccountsFeed( 'accounts/~all/webproperties/~all/profiles') except error.BadAuthenticationError: choices = [('Please authorize with Google in the Google Analytics \ control panel.', None)] return SimpleVocabulary.fromItems(choices) except error.RequestTimedOutError: choices = [('The request to Google Analytics timed out. Please try \ again later.', None)] return SimpleVocabulary.fromItems(choices) if accounts: unique_choices = {} for entry in accounts.entry: for prop in entry.property: if prop.name == 'ga:profileName': title = prop.value if not isinstance(title, unicode): title = unicode(title, 'utf-8') if prop.name == 'dxp:tableId': tableId = prop.value unique_choices.update({title: tableId}) choices = unique_choices.items() else: choices = [('No profiles available', None)] return SimpleVocabulary([SimpleTerm(c[1], c[1], c[0]) for c in choices])
def getRecaptchaSettings(): site = getSite() return IRecaptchaSettings(site)