14 |
14 |
# You should have received a copy of the GNU Affero General Public License
|
15 |
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 |
16 |
|
|
17 |
import logging
|
|
18 |
|
17 |
19 |
from django.conf import settings
|
18 |
20 |
from django.db import models
|
19 |
21 |
from django.forms import models as model_forms
|
20 |
22 |
from django.forms import Select
|
21 |
23 |
from django.utils.translation import ugettext_lazy as _
|
22 |
24 |
|
|
25 |
from requests import RequestException, HTTPError
|
|
26 |
|
23 |
27 |
from combo.data.models import CellBase
|
24 |
28 |
from combo.data.library import register_cell_class
|
25 |
29 |
from combo.utils import requests
|
26 |
30 |
|
|
31 |
logger = logging.getLogger(__name__)
|
|
32 |
|
27 |
33 |
|
28 |
34 |
@register_cell_class
|
29 |
35 |
class RecentDocumentsCell(CellBase):
|
... | ... | |
63 |
69 |
return hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('fargo')
|
64 |
70 |
|
65 |
71 |
def get_json(self, path, context):
|
66 |
|
remote_service = get_fargo_site(self.fargo_site)
|
67 |
|
response = requests.get(path,
|
68 |
|
remote_service=get_fargo_site(self.fargo_site),
|
69 |
|
user=self.get_concerned_user(context),
|
70 |
|
raise_if_not_cached=not(context.get('synchronous')),
|
71 |
|
headers={'accept': 'application/json'})
|
72 |
|
if response.status_code == 200:
|
|
72 |
user = self.get_concerned_user(context)
|
|
73 |
try:
|
|
74 |
response = requests.get(path,
|
|
75 |
remote_service=get_fargo_site(self.fargo_site),
|
|
76 |
user=user,
|
|
77 |
raise_if_not_cached=not(context.get('synchronous')),
|
|
78 |
headers={'accept': 'application/json'},
|
|
79 |
log_errors=True)
|
|
80 |
response.raise_for_status()
|
73 |
81 |
return response.json()
|
|
82 |
except HTTPError as e:
|
|
83 |
status_code = e.response.status_code
|
|
84 |
try:
|
|
85 |
content = e.response.json()
|
|
86 |
except Exception:
|
|
87 |
err = None
|
|
88 |
else:
|
|
89 |
err = content.get('err') if isinstance(content, dict) else None
|
|
90 |
if status_code == 401 and err == 'user-not-found':
|
|
91 |
log = logger.info
|
|
92 |
else:
|
|
93 |
log = logger.error
|
|
94 |
log('could not retrieve recent documents for user %s: status-code=%s err=%s', user, status_code, err)
|
|
95 |
except RequestException as e:
|
|
96 |
logger.error('could not retrieve recent documents for user %s: %s', user, e)
|
74 |
97 |
return {}
|
75 |
98 |
|
76 |
99 |
def render(self, context):
|
77 |
|
-
|