def save(self): for field_name, value in self._fields.iteritems(): if field_name.upper().startswith("SITE_"): value = self._fields[field_name].data app.config[field_name] = value op = Option(id=field_name) op.value = value op.put()
def get(self, action): # Depending on the action requested /admin/<action>/ if action == 'register': # Register a Twitter user with the application using # OAuth. Works with the Option model to store the request and # access tokens. # Remove all the previous tokens matching oauth-* from the datastore. options = Option.all() options.filter('name >=', 'oauth-').filter('name <', 'oauth-' + u'\ufffd') options.fetch(1000) for option in options: option.delete() # Request an OAuth token and show the authorization URL on Twitter. twitter = OAuthApi(consumer_key, consumer_secret) credentials = twitter.getRequestToken() url = twitter.getAuthorizationURL(credentials) rendertext(self, '<a href="%s">%s</a><br />' % (url, url)) rendertext( self, '<form action="/admin/verify/" method="GET"><input type="text" name="oauth_verifier" /><input type="submit" /></form>' ) # Save the tokens to the datastore for later authentication oauth_token = credentials['oauth_token'] oauth_token_secret = credentials['oauth_token_secret'] # Record the tokens opt = Option(name='oauth-request-token', value=oauth_token) opt.put() opt = Option(name='oauth-request-token-secret', value=oauth_token_secret) opt.put() elif action == 'verify': # Used to verify an initiated registration request. Request tokens should # by now be stored in the data store. Retrieve them and initiate a change. twitter = OAuthApi(consumer_key, consumer_secret) oauth_verifier = self.request.get('oauth_verifier') options = Option.all() options.filter('name =', 'oauth-request-token') oauth_token = options.get() options = Option.all() options.filter('name =', 'oauth-request-token-secret') oauth_token_secret = options.get() # Form a request and ask Twitter to exchange request tokens for access tokens using # an OAuth verifier (PIN code). credentials = { 'oauth_token': oauth_token.value, 'oauth_token_secret': oauth_token_secret.value, 'oauth_callback_confirmed': 'true' } credentials = twitter.getAccessToken(credentials, oauth_verifier) # Record the access tokens and remove the previously stored request tokens. access_token = Option(name='oauth-access-token', value=credentials['oauth_token']) access_token_secret = Option( name='oauth-access-token-secret', value=credentials['oauth_token_secret']) oauth_token.delete() oauth_token_secret.delete() access_token.put() access_token_secret.put() # Tokens are now saved, getTwitterObject can be used. self.response.out.write("You are now registered as @%s!" % credentials['screen_name']) # Uses the Task Queues API elif action == 'cron': if self.request.get('task') == 'geocode': for i in range(2, 140, 2): deferred.defer(tasks.geocode, _countdown=i, _queue='geocoding') rendertext(self, "Geo task added to queue")
def get(self, action): # Depending on the action requested /admin/<action>/ if action == 'register': # Register a Twitter user with the application using # OAuth. Works with the Option model to store the request and # access tokens. # Remove all the previous tokens matching oauth-* from the datastore. options = Option.all() options.filter('name >=', 'oauth-').filter('name <', 'oauth-' + u'\ufffd') options.fetch(1000) for option in options: option.delete() # Request an OAuth token and show the authorization URL on Twitter. twitter = OAuthApi(consumer_key, consumer_secret) credentials = twitter.getRequestToken() url = twitter.getAuthorizationURL(credentials) rendertext(self, '<a href="%s">%s</a><br />' % (url, url)) rendertext(self, '<form action="/admin/verify/" method="GET"><input type="text" name="oauth_verifier" /><input type="submit" /></form>') # Save the tokens to the datastore for later authentication oauth_token = credentials['oauth_token'] oauth_token_secret = credentials['oauth_token_secret'] # Record the tokens opt = Option(name='oauth-request-token', value=oauth_token) opt.put() opt = Option(name='oauth-request-token-secret', value=oauth_token_secret) opt.put() elif action == 'verify': # Used to verify an initiated registration request. Request tokens should # by now be stored in the data store. Retrieve them and initiate a change. twitter = OAuthApi(consumer_key, consumer_secret) oauth_verifier = self.request.get('oauth_verifier') options = Option.all() options.filter('name =', 'oauth-request-token') oauth_token = options.get() options = Option.all() options.filter('name =', 'oauth-request-token-secret') oauth_token_secret = options.get() # Form a request and ask Twitter to exchange request tokens for access tokens using # an OAuth verifier (PIN code). credentials = {'oauth_token': oauth_token.value, 'oauth_token_secret': oauth_token_secret.value, 'oauth_callback_confirmed': 'true'} credentials = twitter.getAccessToken(credentials, oauth_verifier) # Record the access tokens and remove the previously stored request tokens. access_token = Option(name='oauth-access-token', value=credentials['oauth_token']) access_token_secret = Option(name='oauth-access-token-secret', value=credentials['oauth_token_secret']) oauth_token.delete() oauth_token_secret.delete() access_token.put() access_token_secret.put() # Tokens are now saved, getTwitterObject can be used. self.response.out.write("You are now registered as @%s!" % credentials['screen_name']) # Uses the Task Queues API elif action == 'cron': if self.request.get('task') == 'geocode': for i in range(2, 140, 2): deferred.defer(tasks.geocode, _countdown=i, _queue='geocoding') rendertext(self, "Geo task added to queue")