res_config_settings.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import logging
  4. import requests
  5. from odoo import api, fields, models, _, _lt
  6. from odoo.exceptions import UserError
  7. _logger = logging.getLogger(__name__)
  8. TWITTER_EXCEPTION = {
  9. 304: _lt('There was no new data to return.'),
  10. 400: _lt('The request was invalid or cannot be otherwise served. Requests without authentication are considered invalid and will yield this response.'),
  11. 401: _lt('Authentication credentials were missing or incorrect. Maybe screen name tweets are protected.'),
  12. 403: _lt('The request is understood, but it has been refused or access is not allowed. Please check your Twitter API Key and Secret.'),
  13. 429: _lt('Request cannot be served due to the applications rate limit having been exhausted for the resource.'),
  14. 500: _lt('Twitter seems broken. Please retry later. You may consider posting an issue on Twitter forums to get help.'),
  15. 502: _lt('Twitter is down or being upgraded.'),
  16. 503: _lt('The Twitter servers are up, but overloaded with requests. Try again later.'),
  17. 504: _lt('The Twitter servers are up, but the request could not be serviced due to some failure within our stack. Try again later.')
  18. }
  19. class ResConfigSettings(models.TransientModel):
  20. _inherit = 'res.config.settings'
  21. twitter_api_key = fields.Char(
  22. related='website_id.twitter_api_key', readonly=False,
  23. string='API Key',
  24. help='Twitter API key you can get it from https://apps.twitter.com/')
  25. twitter_api_secret = fields.Char(
  26. related='website_id.twitter_api_secret', readonly=False,
  27. string='API secret',
  28. help='Twitter API secret you can get it from https://apps.twitter.com/')
  29. twitter_screen_name = fields.Char(
  30. related='website_id.twitter_screen_name', readonly=False,
  31. string='Favorites From',
  32. help='Screen Name of the Twitter Account from which you want to load favorites.'
  33. 'It does not have to match the API Key/Secret.')
  34. twitter_server_uri = fields.Char(string='Twitter server uri', readonly=True)
  35. def _get_twitter_exception_message(self, error_code):
  36. if error_code in TWITTER_EXCEPTION:
  37. return TWITTER_EXCEPTION[error_code]
  38. else:
  39. return _('HTTP Error: Something is misconfigured')
  40. def _check_twitter_authorization(self):
  41. try:
  42. self.website_id.fetch_favorite_tweets()
  43. except requests.HTTPError as e:
  44. _logger.info("%s - %s" % (e.response.status_code, e.response.reason), exc_info=True)
  45. raise UserError("%s - %s" % (e.response.status_code, e.response.reason) + ':' + self._get_twitter_exception_message(e.response.status_code))
  46. except IOError:
  47. _logger.info('We failed to reach a twitter server.', exc_info=True)
  48. raise UserError(_('Internet connection refused: We failed to reach a twitter server.'))
  49. except Exception:
  50. _logger.info('Please double-check your Twitter API Key and Secret!', exc_info=True)
  51. raise UserError(_('Twitter authorization error! Please double-check your Twitter API Key and Secret!'))
  52. @api.model_create_multi
  53. def create(self, vals_list):
  54. configs = super().create(vals_list)
  55. for config in configs:
  56. if config.twitter_api_key or config.twitter_api_secret or config.twitter_screen_name:
  57. config._check_twitter_authorization()
  58. return configs
  59. def write(self, vals):
  60. TwitterConfig = super(ResConfigSettings, self).write(vals)
  61. if vals.get('twitter_api_key') or vals.get('twitter_api_secret') or vals.get('twitter_screen_name'):
  62. self._check_twitter_authorization()
  63. return TwitterConfig
  64. @api.model
  65. def get_values(self):
  66. res = super(ResConfigSettings, self).get_values()
  67. Params = self.env['ir.config_parameter'].sudo()
  68. res.update({
  69. 'twitter_server_uri': '%s/' % Params.get_param('web.base.url', default='http://yourcompany.odoo.com'),
  70. })
  71. return res