[IMP] website_slides: improve error message when google API key is invalid
Currently when there is an issue with the google API key, a generic message is send to the user, with the error details embedded in it in an ugly way. Now a message is displayed: "Your Google API key is invalid, please update it into your settings. Settings > Website > Features > API Key". This is done by parsing the JSON google sends back after request and determining if the error is linked to the API key. Task #2082857 closes odoo/odoo#40547 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
committed by
Thibault Delavallée
parent
7b0df05706
commit
01dadaecca
@@ -883,7 +883,7 @@ class WebsiteSlides(WebsiteProfile):
|
||||
return preview
|
||||
values = Slide._parse_document_url(data['url'], only_preview_fields=True)
|
||||
if values.get('error'):
|
||||
preview['error'] = _('Could not fetch data from url. Document or access right not available.\nHere is the received response: %s') % values['error']
|
||||
preview['error'] = values['error']
|
||||
return preview
|
||||
return values
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import io
|
||||
import re
|
||||
import requests
|
||||
import PyPDF2
|
||||
import json
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from PIL import Image
|
||||
@@ -337,7 +338,7 @@ class Slide(models.Model):
|
||||
if self.url:
|
||||
res = self._parse_document_url(self.url)
|
||||
if res.get('error'):
|
||||
raise Warning(_('Could not fetch data from url. Document or access right not available:\n%s') % res['error'])
|
||||
raise Warning(res.get('error'))
|
||||
values = res['values']
|
||||
if not values.get('document_id'):
|
||||
raise Warning(_('Please enter valid Youtube or Google Doc URL'))
|
||||
@@ -720,7 +721,7 @@ class Slide(models.Model):
|
||||
key = self.env['website'].get_current_website().website_slide_google_app_key
|
||||
fetch_res = self._fetch_data('https://www.googleapis.com/youtube/v3/videos', {'id': document_id, 'key': key, 'part': 'snippet,contentDetails', 'fields': 'items(id,snippet,contentDetails)'}, 'json')
|
||||
if fetch_res.get('error'):
|
||||
return fetch_res
|
||||
return {'error': self._extract_google_error_message(fetch_res.get('error'))}
|
||||
|
||||
values = {'slide_type': 'video', 'document_id': document_id}
|
||||
items = fetch_res['values'].get('items')
|
||||
@@ -754,6 +755,22 @@ class Slide(models.Model):
|
||||
})
|
||||
return {'values': values}
|
||||
|
||||
def _extract_google_error_message(self, error):
|
||||
"""
|
||||
See here for Google error format
|
||||
https://developers.google.com/drive/api/v3/handle-errors
|
||||
"""
|
||||
try:
|
||||
error = json.loads(error)
|
||||
error = (error.get('error', {}).get('errors', []) or [{}])[0].get('reason')
|
||||
except json.decoder.JSONDecodeError:
|
||||
error = str(error)
|
||||
|
||||
if error == 'keyInvalid':
|
||||
return _('Your Google API key is invalid, please update it into your settings.\nSettings > Website > Features > API Key')
|
||||
|
||||
return _('Could not fetch data from url. Document or access right not available:\n%s') % error
|
||||
|
||||
@api.model
|
||||
def _parse_google_document(self, document_id, only_preview_fields):
|
||||
def get_slide_type(vals):
|
||||
@@ -781,7 +798,7 @@ class Slide(models.Model):
|
||||
|
||||
fetch_res = self._fetch_data('https://www.googleapis.com/drive/v2/files/%s' % document_id, params, "json")
|
||||
if fetch_res.get('error'):
|
||||
return fetch_res
|
||||
return {'error': self._extract_google_error_message(fetch_res.get('error'))}
|
||||
|
||||
google_values = fetch_res['values']
|
||||
if only_preview_fields:
|
||||
|
||||
Reference in New Issue
Block a user