diff options
| author | Rohan Patel <rop@odoo.com> | 2015-08-31 11:01:25 +0530 |
|---|---|---|
| committer | David Monjoie <dmo@odoo.com> | 2016-04-07 14:18:05 +0200 |
| commit | c3ff15b147d8ba394dc9a608583f4c3a1de08a48 (patch) | |
| tree | 9915197d1da3419764a05aef261795995f280f72 /addons/website_twitter/models | |
| parent | 12bd44fd96be26df90a63a75c4520ded77f85f97 (diff) | |
[MIG] website_twitter: migration to new api
Diffstat (limited to 'addons/website_twitter/models')
| -rw-r--r-- | addons/website_twitter/models/__init__.py | 3 | ||||
| -rw-r--r-- | addons/website_twitter/models/website_twitter.py | 73 | ||||
| -rw-r--r-- | addons/website_twitter/models/website_twitter_config.py | 91 | ||||
| -rw-r--r-- | addons/website_twitter/models/website_twitter_tweet.py | 26 |
4 files changed, 92 insertions, 101 deletions
diff --git a/addons/website_twitter/models/__init__.py b/addons/website_twitter/models/__init__.py index 592c8f484a5..21fb047fa67 100644 --- a/addons/website_twitter/models/__init__.py +++ b/addons/website_twitter/models/__init__.py @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + import website_twitter import website_twitter_config import website_twitter_tweet diff --git a/addons/website_twitter/models/website_twitter.py b/addons/website_twitter/models/website_twitter.py index ea8faf2638b..cf0fe5d4d07 100644 --- a/addons/website_twitter/models/website_twitter.py +++ b/addons/website_twitter/models/website_twitter.py @@ -1,13 +1,12 @@ # -*- coding: utf-8 -*- - -from urllib2 import urlopen, Request, HTTPError +# Part of Odoo. See LICENSE file for full copyright and licensing details. import base64 import json import logging import werkzeug - -from openerp.osv import fields, osv +from urllib2 import urlopen, Request, HTTPError +from odoo import api, fields, models API_ENDPOINT = 'https://api.twitter.com' API_VERSION = '1.1' @@ -17,15 +16,15 @@ URLOPEN_TIMEOUT = 10 _logger = logging.getLogger(__name__) -class TwitterClient(osv.osv): - _inherit = "website" - _columns = { - 'twitter_api_key': fields.char('Twitter API key', help="Twitter API Key"), - 'twitter_api_secret': fields.char('Twitter API secret', help="Twitter API Secret"), - 'twitter_screen_name': fields.char('Get favorites from this screen name'), - } +class WebsiteTwitter(models.Model): + _inherit = 'website' + + twitter_api_key = fields.Char(string='Twitter API key', help='Twitter API Key') + twitter_api_secret = fields.Char(string='Twitter API secret', help='Twitter API Secret') + twitter_screen_name = fields.Char(string='Get favorites from this screen name') + @api.model def _request(self, website, url, params=None): """Send an authenticated request to the Twitter API.""" access_token = self._get_access_token(website) @@ -41,50 +40,44 @@ class TwitterClient(osv.osv): e.code, e.msg, e.fp.read()) raise - def _refresh_favorite_tweets(self, cr, uid, context=None): + @api.model + def _refresh_favorite_tweets(self): ''' called by cron job ''' - website = self.pool['website'] - ids = self.pool['website'].search(cr, uid, [('twitter_api_key', '!=', False), - ('twitter_api_secret', '!=', False), - ('twitter_screen_name', '!=', False)], - context=context) - _logger.debug("Refreshing tweets for website IDs: %r", ids) - website.fetch_favorite_tweets(cr, uid, ids, context=context) + website = self.env['website'].search([('twitter_api_key', '!=', False), + ('twitter_api_secret', '!=', False), + ('twitter_screen_name', '!=', False)]) + _logger.debug("Refreshing tweets for website IDs: %r", website.ids) + website.fetch_favorite_tweets() - def fetch_favorite_tweets(self, cr, uid, ids, context=None): - website_tweets = self.pool['website.twitter.tweet'] + @api.multi + def fetch_favorite_tweets(self): + WebsiteTweets = self.env['website.twitter.tweet'] tweet_ids = [] - for website in self.browse(cr, uid, ids, context=context): - if not all((website.twitter_api_key, website.twitter_api_secret, - website.twitter_screen_name)): - _logger.debug("Skip fetching favorite tweets for unconfigured website %s", - website) + for website in self: + if not all((website.twitter_api_key, website.twitter_api_secret, website.twitter_screen_name)): + _logger.debug("Skip fetching favorite tweets for unconfigured website %s", website) continue params = {'screen_name': website.twitter_screen_name} - last_tweet = website_tweets.search_read( - cr, uid, [('website_id', '=', website.id), - ('screen_name', '=', website.twitter_screen_name)], - ['tweet_id'], - limit=1, order='tweet_id desc', context=context) + last_tweet = WebsiteTweets.search([('website_id', '=', website.id), + ('screen_name', '=', website.twitter_screen_name)], + limit=1, order='tweet_id desc') if last_tweet: - params['since_id'] = int(last_tweet[0]['tweet_id']) + params['since_id'] = int(last_tweet.tweet_id) _logger.debug("Fetching favorite tweets using params %r", params) response = self._request(website, REQUEST_FAVORITE_LIST_URL, params=params) for tweet_dict in response: - tweet_id = tweet_dict['id'] # unsigned 64-bit snowflake ID - tweet_ids = website_tweets.search(cr, uid, [('tweet_id', '=', tweet_id)]) + tweet_id = tweet_dict['id'] # unsigned 64-bit snowflake ID + tweet_ids = WebsiteTweets.search([('tweet_id', '=', tweet_id)]).ids if not tweet_ids: - new_tweet = website_tweets.create( - cr, uid, + new_tweet = WebsiteTweets.create( { 'website_id': website.id, 'tweet': json.dumps(tweet_dict), - 'tweet_id': tweet_id, # stored in NUMERIC PG field + 'tweet_id': tweet_id, # stored in NUMERIC PG field 'screen_name': website.twitter_screen_name, - }, - context=context) + }) _logger.debug("Found new favorite: %r, %r", tweet_id, tweet_dict) - tweet_ids.append(new_tweet) + tweet_ids.append(new_tweet.id) return tweet_ids def _get_access_token(self, website): diff --git a/addons/website_twitter/models/website_twitter_config.py b/addons/website_twitter/models/website_twitter_config.py index 115eda5cd57..bc7df946fa5 100644 --- a/addons/website_twitter/models/website_twitter_config.py +++ b/addons/website_twitter/models/website_twitter_config.py @@ -1,62 +1,57 @@ # -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. import logging from urllib2 import URLError, HTTPError - -from openerp.osv import fields, osv -from openerp.tools.translate import _ -from openerp.exceptions import UserError +from odoo import api, fields, models, _ +from odoo.exceptions import UserError _logger = logging.getLogger(__name__) +TWITTER_EXCEPTION = { + 304: _('There was no new data to return.'), + 400: _('The request was invalid or cannot be otherwise served. Requests without authentication are considered invalid and will yield this response.'), + 401: _('Authentication credentials were missing or incorrect. Maybe screen name tweets are protected.'), + 403: _('The request is understood, but it has been refused or access is not allowed. Please check your Twitter API Key and Secret.'), + 429: _('Request cannot be served due to the applications rate limit having been exhausted for the resource.'), + 500: _('Twitter seems broken. Please retry later. You may consider posting an issue on Twitter forums to get help.'), + 502: _('Twitter is down or being upgraded.'), + 503: _('The Twitter servers are up, but overloaded with requests. Try again later.'), + 504: _('The Twitter servers are up, but the request could not be serviced due to some failure within our stack. Try again later.') +} + -class twitter_config_settings(osv.TransientModel): +class WebsiteTwitterConfig(models.TransientModel): _inherit = 'website.config.settings' - _columns = { - 'twitter_api_key': fields.related( - 'website_id', 'twitter_api_key', type="char", - string='Twitter API Key', - help="Twitter API key you can get it from https://apps.twitter.com/app/new"), - 'twitter_api_secret': fields.related( - 'website_id', 'twitter_api_secret', type="char", - string='Twitter API secret', - help="Twitter API secret you can get it from https://apps.twitter.com/app/new"), - 'twitter_tutorial': fields.dummy( - type="boolean", string="Show me how to obtain the Twitter API Key and Secret"), - 'twitter_screen_name': fields.related( - 'website_id', 'twitter_screen_name', - type="char", string='Get favorites from this screen name', - help="Screen Name of the Twitter Account from which you want to load favorites." - "It does not have to match the API Key/Secret."), - } + twitter_api_key = fields.Char( + related='website_id.twitter_api_key', + string='Twitter API Key', + help='Twitter API key you can get it from https://apps.twitter.com/app/new') + twitter_api_secret = fields.Char( + related='website_id.twitter_api_secret', + string='Twitter API secret', + help='Twitter API secret you can get it from https://apps.twitter.com/app/new') + twitter_tutorial = fields.Boolean(string='Show me how to obtain the Twitter API Key and Secret') + twitter_screen_name = fields.Char( + related='website_id.twitter_screen_name', + string='Get favorites from this screen name', + help='Screen Name of the Twitter Account from which you want to load favorites.' + 'It does not have to match the API Key/Secret.') - def _get_twitter_exception_message(self, error_code, context=None): - TWITTER_EXCEPTION = { - 304: _('There was no new data to return.'), - 400: _('The request was invalid or cannot be otherwise served. Requests without authentication are considered invalid and will yield this response.'), - 401: _('Authentication credentials were missing or incorrect. Maybe screen name tweets are protected.'), - 403: _('The request is understood, but it has been refused or access is not allowed. Please check your Twitter API Key and Secret.'), - 429: _('Request cannot be served due to the applications rate limit having been exhausted for the resource.'), - 500: _('Twitter seems broken. Please retry later. You may consider posting an issue on Twitter forums to get help.'), - 502: _('Twitter is down or being upgraded.'), - 503: _('The Twitter servers are up, but overloaded with requests. Try again later.'), - 504: _('The Twitter servers are up, but the request could not be serviced due to some failure within our stack. Try again later.') - } + def _get_twitter_exception_message(self, error_code): if error_code in TWITTER_EXCEPTION: return TWITTER_EXCEPTION[error_code] else: return _('HTTP Error: Something is misconfigured') - def _check_twitter_authorization(self, cr, uid, config_id, context=None): - website_obj = self.pool['website'] - website_config = self.browse(cr, uid, config_id, context=context) + def _check_twitter_authorization(self): try: - website_obj.fetch_favorite_tweets(cr, uid, [website_config.website_id.id], context=context) + self.website_id.fetch_favorite_tweets() except HTTPError, e: _logger.info("%s - %s" % (e.code, e.reason), exc_info=True) - raise UserError("%s - %s" % (e.code, e.reason) + ':' + self._get_twitter_exception_message(e.code, context)) + raise UserError("%s - %s" % (e.code, e.reason) + ':' + self._get_twitter_exception_message(e.code)) except URLError, e: _logger.info(_('We failed to reach a twitter server.'), exc_info=True) raise UserError(_('Internet connection refused') + ' ' + _('We failed to reach a twitter server.')) @@ -64,14 +59,16 @@ class twitter_config_settings(osv.TransientModel): _logger.info(_('Please double-check your Twitter API Key and Secret!'), exc_info=True) raise UserError(_('Twitter authorization error!') + ' ' + _('Please double-check your Twitter API Key and Secret!')) - def create(self, cr, uid, vals, context=None): - res_id = super(twitter_config_settings, self).create(cr, uid, vals, context=context) + @api.model + def create(self, vals): + TwitterConfig = super(WebsiteTwitterConfig, self).create(vals) if vals.get('twitter_api_key') or vals.get('twitter_api_secret') or vals.get('twitter_screen_name'): - self._check_twitter_authorization(cr, uid, res_id, context=context) - return res_id + TwitterConfig._check_twitter_authorization() + return TwitterConfig - def write(self, cr, uid, ids, vals, context=None): - res_id = super(twitter_config_settings, self).write(cr, uid, ids, vals, context=context) + @api.multi + def write(self, vals): + TwitterConfig = super(WebsiteTwitterConfig, self).write(vals) if vals.get('twitter_api_key') or vals.get('twitter_api_secret') or vals.get('twitter_screen_name'): - self._check_twitter_authorization(cr, uid, ids, context=context) - return res_id + self._check_twitter_authorization() + return TwitterConfig diff --git a/addons/website_twitter/models/website_twitter_tweet.py b/addons/website_twitter/models/website_twitter_tweet.py index 8efc4a65079..267c6ac8db3 100644 --- a/addons/website_twitter/models/website_twitter_tweet.py +++ b/addons/website_twitter/models/website_twitter_tweet.py @@ -1,20 +1,18 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from openerp.osv import fields, osv +from odoo import fields, models -class WebsiteTwitterTweet(osv.osv): - _name = "website.twitter.tweet" - _description = "Twitter Tweets" - _columns = { - 'website_id': fields.many2one('website', string="Website"), - 'screen_name': fields.char("Screen Name"), - 'tweet': fields.text('Tweets'), +class WebsiteTwitterTweet(models.Model): + _name = 'website.twitter.tweet' - # Twitter IDs are 64-bit unsigned ints, so we need to store them in - # unlimited precision NUMERIC columns, which can be done with a - # float field. Used digits=(0,0) to indicate unlimited. - # Using VARCHAR would work too but would have sorting problems. - 'tweet_id': fields.float("Tweet ID", digits=(0,0)), # Twitter - } + website_id = fields.Many2one('website', string='Website') + screen_name = fields.Char(string='Screen Name') + tweet = fields.Text(string='Tweets') + + # Twitter IDs are 64-bit unsigned ints, so we need to store them in + # unlimited precision NUMERIC columns, which can be done with a + # float field. Used digits=(0,0) to indicate unlimited. + # Using VARCHAR would work too but would have sorting problems. + tweet_id = fields.Float(string='Tweet ID', digits=(0, 0)) # Twitter |
