def getAccessToken(self, code): """ Returns the access token """ key = self.customer_key secret = self.customer_secret token = self.access_token if token: return {'error': _(u'token_configured'), 'error_description': _(u'access token already configured')} if not key or not secret: return {'error': _(u'no_customer'), 'error_description': _(u'no customer_key or customer_secret configured')} headers = {'Content-Type': 'application/json'} params = {'code': code, 'grant_type': 'authorization_code', 'redirect_uri': '%s/collective_flattr' % (self.context.absolute_url())} req = urllib2.Request(self.access_token_url, data=simplejson.dumps(params), headers=headers) ret = {} try: retf = self.opener.open(req) if retf: ret = retf.read() except urllib2.HTTPError, e: ret = e.read()
def handleSave(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return changes = self.applyChanges(data) IStatusMessage(self.request).add(_(u"Changes saved."), "info") self.request.response.redirect("%s/%s" % (self.context.absolute_url(), self.view_name))
def flattrLanguageVocab(context): portal = getToolByName(context, "portal_url").getPortalObject() flattr = getMultiAdapter((portal, context.REQUEST), name="collective_flattr") languages = flattr.getLanguages() terms = [SimpleVocabulary.createTerm("sysdefault", "sysdefault", _(u"System default"))] for lang in languages: terms.append(SimpleVocabulary.createTerm(lang["id"], lang["id"], lang["text"])) return SimpleVocabulary(terms)
def update_flattr_thing(event): obj = event.object portal = getToolByName(obj, 'portal_url').getPortalObject() flattr = getMultiAdapter((portal, obj.REQUEST), name='collective_flattr') if safe_hasattr(obj, 'flattrCreateThing')\ and obj.flattrCreateThing\ and safe_hasattr(obj, 'flattrThingId'): if obj.flattrCreateThing and not obj.flattrThingId: create_flattr_thing(event) return category = None language = None tags = None hidden = 'false' if safe_hasattr(obj, 'flattrCategory') and obj.flattrCategory: category = obj.flattrCategory if safe_hasattr(obj, 'flattrLanguage') and obj.flattrLanguage != 'sysdefault': language = obj.flattrLanguage if safe_hasattr(obj, 'flattrHidden') and obj.flattrHidden: hidden = 'true' if not language: default_lang = obj.restrictedTraverse('@@plone_portal_state')\ .default_language() language = '%s_%s' % (default_lang, default_lang.upper()) if obj.Subject(): tags = ','.join(obj.Subject()) thingid = obj.flattrThingId.split('/')[-1] ret = flattr.updateThing(obj.Title(), thingid, description=obj.Description(), category=category, language=language, tags=tags, hidden=hidden) if 'message' in ret and ret['message'] == u'ok': obj.reindexObject() IStatusMessage(obj.REQUEST).addStatusMessage( _(u'Updated Flattr thing'), type='info') else: IStatusMessage(obj.REQUEST).addStatusMessage( _(u'Was not able to update the thing on Flattr'), type='error')
def _handleTestAccessToken(self): """ Handle click on form.button.TestAccessToken """ headers = {'Content-Type': 'application/json'} access_token = self.flattr.access_token if not access_token: IStatusMessage(self.request).add( _(u'No access token configured'), type='error') self.test_access_token = u'' return False req = urllib2.Request('https://api.flattr.com/rest/v2/user', headers=headers) ret = False try: retf = self.flattr.opener.open(req) if retf: self.test_access_token = pformat(json.loads(retf.read())) return True except urllib2.HTTPError, e: IStatusMessage(self.request).add( _(u'Configured access token does not work :('), type='error') self.test_access_token = _(u'Configured access token does not work :(')
def __call__(self): """ """ code = self.request.get('code', None) error = self.request.get('error', None) error_description = self.request.get('error_description', u'') if code and not isinstance(code, unicode): code = safe_unicode(code) if error and not isinstance(error, unicode): error = safe_unicode(error) if error_description and not isinstance(error_description, unicode): error_description = safe_unicode(error_description) status = IStatusMessage(self.request) if code and not error: ret = self.getAccessToken(code) if u'access_token' in ret and u'token_type' in ret\ and u'error' not in ret: self._setAccessToken(ret['access_token'], ret['token_type']) status.addStatusMessage( _(u'collective.flattr successfully configured'), type='info') else: if u'error' in ret and u'error_description' in ret: error = ret['error'] error_description = ret['error_description'] else: error = _(u'undefined') error_description = _(u'Undefined error while getting access token') if error: status.addStatusMessage('%s: %s' % (error, error_description), type='error') self.request.response.redirect(self.portal.absolute_url()) return
def _handleAuthenticate(self): """ Handle click on form.button.Save """ client_id = self.flattr.customer_key if client_id: callback_uri = '%s/collective_flattr' %\ self.context.absolute_url() self.request.response.redirect('%s?scope=thing&response_type=code' '&redirect_uri=%s&client_id=%s' % ( self.flattr.authorize_url, callback_uri, client_id)) return True IStatusMessage(self.request).add(_( u'Unable to create authorize ' 'url. consumer and consumer_secret not configured :('), type='error') return False
def handleCancel(self, action): IStatusMessage(self.request).add(_(u"Edit cancelled."), "info") self.request.response.redirect("%s/%s" % (self.context.absolute_url(), self.view_name))
def _handleClearAccessToken(self): """ Handle click on form.button.ClearAccessToken """ self.flattr._setAccessToken(u'', u'') IStatusMessage(self.request).add( _(u'Cleared access token'), type='info') return True
from Products.Archetypes.Widget import StringWidget from Products.Archetypes.Registry import registerWidget from collective.flattr import flattrMessageFactory as _ class FlattrThingWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro': 'flattrthing', 'helper_js': ('flattrthing.js',), }) registerWidget(FlattrThingWidget, title=_(u'Flattr Thing'), description=_(u'Allows to create or select a flattr thing'), used_for=('Products.Archetypes.Field.StringField'))
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm from collective.flattr import flattrMessageFactory as _ from Products.CMFCore.utils import getToolByName from zope.component import getMultiAdapter buttons_vocab = SimpleVocabulary( [ SimpleTerm(value=u"Large Counter", title=_(u"Large Counter")), SimpleTerm(value=u"Compact Counter", title=_(u"Compact Counter")), SimpleTerm(value=u"Static", title=_(u"Static")), ] ) def flattrCategoryVocab(context): portal = getToolByName(context, "portal_url").getPortalObject() flattr = getMultiAdapter((portal, context.REQUEST), name="collective_flattr") categories = flattr.getCategories() terms = [] for cat in categories: terms.append(SimpleVocabulary.createTerm(cat["id"], cat["id"], cat["text"])) return SimpleVocabulary(terms) def flattrLanguageVocab(context): portal = getToolByName(context, "portal_url").getPortalObject() flattr = getMultiAdapter((portal, context.REQUEST), name="collective_flattr") languages = flattr.getLanguages() terms = [SimpleVocabulary.createTerm("sysdefault", "sysdefault", _(u"System default"))] for lang in languages: terms.append(SimpleVocabulary.createTerm(lang["id"], lang["id"], lang["text"]))