2
2
# For copyright and license notices, see __manifest__.py file in module root
3
3
# directory
4
4
##############################################################################
5
- from odoo import fields , models , api , _
5
+ import hashlib
6
6
import logging
7
- from odoo .exceptions import UserError
8
- import dateutil .parser
9
- import pytz
10
- import odoo .tools as tools
11
7
import os
12
- import hashlib
13
- import time
14
8
import sys
9
+ import time
15
10
import traceback
16
11
12
+ import dateutil .parser
13
+ import odoo .tools as tools
14
+ import pytz
15
+ from odoo import _ , api , fields , models
16
+ from odoo .exceptions import UserError
17
+
17
18
_logger = logging .getLogger (__name__ )
18
19
19
20
20
21
class ResCompany (models .Model ):
21
-
22
22
_inherit = "res.company"
23
23
24
24
alias_ids = fields .One2many (
@@ -46,9 +46,7 @@ def _get_environment_type(self):
46
46
* 'test' or 'develop' --> homologation
47
47
* other or no parameter --> production
48
48
"""
49
- parameter_env_type = (
50
- self .env ["ir.config_parameter" ].sudo ().get_param ("afip.ws.env.type" )
51
- )
49
+ parameter_env_type = self .env ["ir.config_parameter" ].sudo ().get_param ("afip.ws.env.type" )
52
50
if parameter_env_type == "production" :
53
51
environment_type = "production"
54
52
elif parameter_env_type == "homologation" :
@@ -112,9 +110,9 @@ def get_key_and_certificate(self, environment_type):
112
110
if pkey_path and cert_path :
113
111
try :
114
112
if os .path .isfile (pkey_path ) and os .path .isfile (cert_path ):
115
- with open (pkey_path , "r" ) as pkey_file :
113
+ with open (pkey_path ) as pkey_file :
116
114
pkey = pkey_file .read ()
117
- with open (cert_path , "r" ) as cert_file :
115
+ with open (cert_path ) as cert_file :
118
116
cert = cert_file .read ()
119
117
msg = "Could not find %s or %s files" % (pkey_path , cert_path )
120
118
except Exception :
@@ -127,9 +125,7 @@ def get_key_and_certificate(self, environment_type):
127
125
128
126
def get_connection (self , afip_ws ):
129
127
self .ensure_one ()
130
- _logger .info (
131
- "Getting connection for company %s and ws %s" % (self .name , afip_ws )
132
- )
128
+ _logger .info ("Getting connection for company %s and ws %s" % (self .name , afip_ws ))
133
129
now = fields .Datetime .now ()
134
130
environment_type = self ._get_environment_type ()
135
131
@@ -173,14 +169,10 @@ def _create_connection(self, afip_ws, environment_type):
173
169
)
174
170
175
171
auth_data ["generationtime" ] = (
176
- dateutil .parser .parse (auth_data ["generationtime" ])
177
- .astimezone (pytz .utc )
178
- .replace (tzinfo = None )
172
+ dateutil .parser .parse (auth_data ["generationtime" ]).astimezone (pytz .utc ).replace (tzinfo = None )
179
173
)
180
174
auth_data ["expirationtime" ] = (
181
- dateutil .parser .parse (auth_data ["expirationtime" ])
182
- .astimezone (pytz .utc )
183
- .replace (tzinfo = None )
175
+ dateutil .parser .parse (auth_data ["expirationtime" ]).astimezone (pytz .utc ).replace (tzinfo = None )
184
176
)
185
177
186
178
_logger .info ("Successful Connection to AFIP." )
@@ -213,23 +205,15 @@ def authenticate(
213
205
DEFAULT_TTL = 60 * 60 * 5
214
206
215
207
# make md5 hash of the parameter for caching...
216
- fn = (
217
- "%s.xml"
218
- % hashlib .md5 (
219
- (service + certificate + private_key ).encode ("utf-8" )
220
- ).hexdigest ()
221
- )
208
+ fn = "%s.xml" % hashlib .md5 ((service + certificate + private_key ).encode ("utf-8" )).hexdigest ()
222
209
if cache :
223
210
fn = os .path .join (cache , fn )
224
211
else :
225
212
fn = os .path .join (wsaa .InstallDir , "cache" , fn )
226
213
227
214
try :
228
215
# read the access ticket (if already authenticated)
229
- if (
230
- not os .path .exists (fn )
231
- or os .path .getmtime (fn ) + (DEFAULT_TTL ) < time .time ()
232
- ):
216
+ if not os .path .exists (fn ) or os .path .getmtime (fn ) + (DEFAULT_TTL ) < time .time ():
233
217
# access ticket (TA) outdated, create new access request
234
218
# ticket (TRA)
235
219
tra = wsaa .CreateTRA (service = service , ttl = DEFAULT_TTL )
@@ -245,7 +229,7 @@ def authenticate(
245
229
open (fn , "w" ).write (ta )
246
230
else :
247
231
# get the access ticket from the previously written file
248
- ta = open (fn , "r" ).read ()
232
+ ta = open (fn ).read ()
249
233
# analyze the access ticket xml and extract the relevant fields
250
234
wsaa .AnalizarXml (xml = ta )
251
235
token = wsaa .ObtenerTagXml ("token" )
@@ -260,12 +244,8 @@ def authenticate(
260
244
err_msg = wsaa .Excepcion
261
245
else :
262
246
# avoid encoding problem when reporting exceptions to the user:
263
- err_msg = traceback .format_exception_only (sys .exc_type , sys .exc_value )[
264
- 0
265
- ]
266
- raise UserError (
267
- _ ("Could not connect. This is the what we received: %s" ) % (err_msg )
268
- )
247
+ err_msg = traceback .format_exception_only (sys .exc_type , sys .exc_value )[0 ]
248
+ raise UserError (_ ("Could not connect. This is the what we received: %s" ) % (err_msg ))
269
249
return {
270
250
"uniqueid" : uniqueId ,
271
251
"generationtime" : generationTime ,
0 commit comments