class ActionEditForm(EditForm): """An edit form for portal type conditions """ form_fields = form.FormFields(ITweetContent) label = _(u"Tweet new content's title and url.") description = _(u"Send a tweet containing the content's title and a " "shortened URL to it.") form_name = _(u"Select account")
class ActionAddForm(AddForm): """ The action's add form """ form_fields = form.FormFields(ITweetContent) label = _(u"Tweet new content's title and url.") description = _(u"Send a tweet containing the content's title and a " "shortened URL to it.") form_name = _(u"Select account") def create(self, data): c = Action() form.applyChanges(c, self.form_fields, data) return c
class ITweetContent(Interface): """ Content tweet config """ # url_shortener_vocabulary = SimpleVocabulary(["bitly", "tinyURL"]) # url_shortener = schema.Choice(title=_(u'URL Shortener service'), # description=_(u"Choose the service used to " # "shorten the URL"), # required=False, # vocabulary=url_shortener_vocabulary) tw_account = schema.Choice(title=_(u'Twitter account'), description=_(u"Choose a twitter account to " "use."), required=True, source=ValidAccounts) different_host = schema.TextLine(title=_(u'Separate host'), description=_(u"Specify here a different host to " "use for the link. (make sure to " "include the protocol. ie. http://, " "https://, etc.)"), required=False,)
def ValidAccounts(context): registry = getUtility(IRegistry) accounts = registry.get('collective.twitter.accounts', None) terms = [ SimpleTerm(value='first-valid-account', token='first-valid-account', title=_('First Valid Account')) ] if accounts: terms += [ SimpleTerm(value=key, token=key, title=key) for key in accounts.keys()] return SimpleVocabulary(terms)
def __call__(self): obj = aq_inner(self.event.object) uuid = IUUID(obj, None) # In a case where a previous action modified the object in any way if uuid: context = uuidToObject(uuid) else: # This should never happen, but just in case... context = obj request = context.REQUEST registry = getUtility(IRegistry) accounts = registry.get('collective.twitter.accounts', {}) account = None key = self.element.tw_account if accounts: if self.element.tw_account == 'first-valid-account': keys = accounts.keys() if keys: key = keys[0] account = accounts.get(key, {}) else: account = accounts.get(key, {}) if account: tw = twitter.Api( consumer_key=account.get('consumer_key'), consumer_secret=account.get('consumer_secret'), access_token_key=account.get('oauth_token'), access_token_secret=account.get('oauth_token_secret'), ) title = context.Title() url = None if self.element.different_host: if not self.element.different_host.endswith('/'): self.element.different_host += '/' try: portal_path = context.portal_url.getPortalObject( ).getPhysicalPath() location = '/'.join( context.getPhysicalPath()[len(portal_path):]) url = self.element.different_host + location except: return False else: url = context.absolute_url() # shortener = SHORTENER[self.element.url_shortener] url = getTinyURL(url) if url is None: return False text = "%s\n%s" % (title[:140 - (len(url) + 2)], url) try: status = tw.PostUpdate(text) msg = _("Tweet sent: ${tweet}", mapping=dict(tweet=text)) except (twitter.TwitterError, HTTPError, URLError), e: msg = _( "There was an error while sending the tweet: " "${error}", mapping=dict(error=str(e))) IStatusMessage(request).addStatusMessage(msg, "info") request.response.redirect(context.absolute_url())
def summary(self): return _(u"Twitter account: ${tw_account}", mapping=dict(tw_account=self.tw_account))
msg = _("Tweet sent: ${tweet}", mapping=dict(tweet=text)) except (twitter.TwitterError, HTTPError, URLError), e: msg = _( "There was an error while sending the tweet: " "${error}", mapping=dict(error=str(e))) IStatusMessage(request).addStatusMessage(msg, "info") request.response.redirect(context.absolute_url()) else: msg = _( "Could not publish to twitter, seems the account " "${account} was removed from the list of authorized " "accounts for this site.", mapping=dict(account=key)) IStatusMessage(request).addStatusMessage(msg, "info") request.response.redirect(context.absolute_url()) return True class ActionAddForm(AddForm): """ The action's add form """
def __call__(self): obj = aq_inner(self.event.object) uuid = IUUID(obj, None) # In a case where a previous action modified the object in any way if uuid: context = uuidToObject(uuid) else: # This should never happen, but just in case... context = obj request = context.REQUEST registry = getUtility(IRegistry) accounts = registry.get('collective.twitter.accounts', {}) account = None key = self.element.tw_account if accounts: if self.element.tw_account == 'first-valid-account': keys = accounts.keys() if keys: key = keys[0] account = accounts.get(key, {}) else: account = accounts.get(key, {}) if account: tw = twitter.Api( consumer_key = account.get('consumer_key'), consumer_secret = account.get('consumer_secret'), access_token_key = account.get('oauth_token'), access_token_secret = account.get('oauth_token_secret'),) title = context.Title() url = None if self.element.different_host: if not self.element.different_host.endswith('/'): self.element.different_host += '/' try: portal_path = context.portal_url.getPortalObject().getPhysicalPath() location = '/'.join(context.getPhysicalPath()[len(portal_path):]) url = self.element.different_host + location except: return False else: url = context.absolute_url() # shortener = SHORTENER[self.element.url_shortener] url = getTinyURL(url) if url is None: return False text = "%s\n%s" % ( title[:140-(len(url)+2)], url ) try: status = tw.PostUpdate(text) msg = _("Tweet sent: ${tweet}", mapping=dict(tweet=text)) except (twitter.TwitterError, HTTPError, URLError), e: msg = _("There was an error while sending the tweet: " "${error}", mapping=dict(error=str(e))) IStatusMessage(request).addStatusMessage(msg, "info") request.response.redirect(context.absolute_url())
try: status = tw.PostUpdate(text) msg = _("Tweet sent: ${tweet}", mapping=dict(tweet=text)) except (twitter.TwitterError, HTTPError, URLError), e: msg = _("There was an error while sending the tweet: " "${error}", mapping=dict(error=str(e))) IStatusMessage(request).addStatusMessage(msg, "info") request.response.redirect(context.absolute_url()) else: msg = _("Could not publish to twitter, seems the account " "${account} was removed from the list of authorized " "accounts for this site.", mapping=dict(account=key)) IStatusMessage(request).addStatusMessage(msg, "info") request.response.redirect(context.absolute_url()) return True class ActionAddForm(AddForm): """ The action's add form """ form_fields = form.FormFields(ITweetContent) label = _(u"Tweet new content's title and url.")