Skip to content

Commit

Permalink
Updated status, requests, vals, changed request url generation.
Browse files Browse the repository at this point in the history
FINISHED
  • Loading branch information
a-baghirli committed Dec 1, 2017
1 parent 81e4ea3 commit 373f07c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
35 changes: 23 additions & 12 deletions pingen/pingen.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
100: 'Ready/Pending',
101: 'Processing',
102: 'Waiting for confirmation',
200: 'Sent',
1: 'Sent',
300: 'Some error occured and object wasn\'t sent',
400: 'Sending cancelled',
}
Expand Down Expand Up @@ -101,20 +101,31 @@ def _send(self, method, endpoint, **kwargs):
:param str endpoint: endpoint to call
:param kwargs: additional arguments forwarded to the requests method
"""

p_url = urlparse.urljoin(self.url, endpoint)
complete_url = p_url + '/token/' + self._token

if endpoint == 'document/get':
complete_url = '{}{}{}{}{}'.format(p_url,
'/id/',
kwargs['params']['id'],
'/token/',
self._token)
else:
complete_url = '{}{}{}'.format(p_url,
'/token/',
self._token)

response = method(complete_url, **kwargs)

if not response.ok:
raise ConnectionError(
"%s: %s" % (response.json['errorcode'],
response.json['errormessage']))
"%s: %s" % (response.json()['errorcode'],
response.json()['errormessage']))

if response.json['error']:
if response.json()['error']:
raise APIError(
"%s: %s" % (response.json['errorcode'],
response.json['errormessage']))
"%s: %s" % (response.json()['errorcode'],
response.json()['errormessage']))

return response

Expand Down Expand Up @@ -157,7 +168,7 @@ def push_document(self, filename, filestream,
headers={'Content-Type': content_type},
data=multipart)

rjson = response.json
rjson = response.json()

document_id = rjson['id']
if rjson.get('send'):
Expand Down Expand Up @@ -186,7 +197,7 @@ def send_document(self, document_id, speed=None, color=None):
params={'id': document_id},
data={'data': json.dumps(data)})

return response.json['id']
return response.json()['id']

def post_infos(self, post_id):
""" Return the information of a post
Expand All @@ -196,15 +207,15 @@ def post_infos(self, post_id):
"""
response = self._send(
self.session.get,
'post/get',
'document/get',
params={'id': post_id})

return response.json['item']
return response.json()['item']

@staticmethod
def is_posted(post_infos):
""" return True if the post has been sent
:param dict post_infos: post infos returned by `post_infos`
"""
return post_infos['status'] == 200
return post_infos['status'] == 1
24 changes: 12 additions & 12 deletions pingen/pingen_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ def _update_post_infos(self, pingen):
pingen of a document in the Sendcenter
:param Pingen pingen: pingen object to reuse
"""
if not self.post_id:
if not self.pingen_id:
return
try:
post_infos = pingen.post_infos(self.post_id)
post_infos = pingen.post_infos(self.pingen_id)
except ConnectionError:
_logger.exception(
'Connection Error when asking for '
Expand All @@ -277,19 +277,19 @@ def _update_post_infos(self, pingen):
'Pingen Document %s to %s.' %
(self.id, pingen.url))
raise
currency_ids = self.env['res.currency'].search(
[('name', '=', post_infos['currency'])])
country_ids = self.env['res.country'].search(
# currency_ids = self.env['res.currency'].search(
# [('name', '=', post_infos['currency'])])
country = self.env['res.country'].search(
[('code', '=', post_infos['country'])])
send_date = pingen_datetime_to_utc(post_infos['date'])
vals = {
'post_status': POST_SENDING_STATUS[post_infos['status']],
'cost': post_infos['cost'],
'currency_id': currency_ids[0] if currency_ids else False,
# 'post_status': POST_SENDING_STATUS[post_infos['status']],
# 'cost': post_infos['cost'],
# 'currency_id': currency_ids[0] if currency_ids else False,
'parsed_address': post_infos['address'],
'country_id': country_ids[0] if country_ids else False,
'country_id': country.id if country else False,
'send_date': fields.Datetime.to_string(send_date),
'pages': post_infos['pages'],
# 'pages': post_infos['pages'],
'last_error_message': False,
}
if pingen.is_posted(post_infos):
Expand Down Expand Up @@ -336,12 +336,12 @@ def update_post_infos(self):
document._update_post_infos(pingen=session)
except ConnectionError as e:
raise UserError(
_('Connection Error when updating' +
_('Connection Error when updating ' +
'the status of Document %s'
' from Pingen') % document.name)
except APIError as e:
raise UserError(
_('Error when updating the status' +
_('Error when updating the status ' +
'of Document %s from Pingen: '
'\n%s') % (document.name, e))
except BaseException as e:
Expand Down

0 comments on commit 373f07c

Please sign in to comment.