commit
124fb63a45
|
|
@ -29,71 +29,73 @@ class ParsedRequest(object):
|
|||
def __init__(self, data):
|
||||
self.__dict__ = json.loads(data)
|
||||
|
||||
serverIP = "http://160.153.0.122:8008"
|
||||
loginUrl = serverIP + "/jwt-api-token-auth/"
|
||||
refreshUrl = serverIP + "/jwt-api-token-refresh/"
|
||||
employeeUrl = serverIP + "/personnel/api/employee/"
|
||||
departmentsUrl = serverIP + "/personnel/api/departments/"
|
||||
terminalsUrl = serverIP + "/iclock/api/terminals/"
|
||||
areasUrl = serverIP + "/personnel/api/areas/"
|
||||
positionsUrl = serverIP + "/personnel/api/positions/"
|
||||
transctionsUrl = serverIP + "/iclock/api/transactions/"
|
||||
|
||||
# serverIP = "http://160.153.0.122:8008"
|
||||
# loginUrl = serverIP + "/jwt-api-token-auth/"
|
||||
# refreshUrl = serverIP + "/jwt-api-token-refresh/"
|
||||
# employeeUrl = serverIP + "/personnel/api/employee/"
|
||||
# departmentsUrl = serverIP + "/personnel/api/departments/"
|
||||
# terminalsUrl = serverIP + "/iclock/api/terminals/"
|
||||
# areasUrl = serverIP + "/personnel/api/areas/"
|
||||
# positionsUrl = serverIP + "/personnel/api/positions/"
|
||||
# transctionsUrl = serverIP + "/iclock/api/transactions/"
|
||||
# defaultHeaders = {'Content-Type': 'application/json'}
|
||||
defaultHeaders = {'Content-Type': 'application/json'}
|
||||
|
||||
|
||||
class HttpHelper(object):
|
||||
|
||||
def login(self, username, password, url=loginUrl, headers=defaultHeaders):
|
||||
def login(self, username, password, url, headers=defaultHeaders):
|
||||
data = {'username': username, 'password': password}
|
||||
return requests.post(url, data=json.dumps(data), headers=headers)
|
||||
|
||||
def refresh(self, token, url=refreshUrl, headers=defaultHeaders):
|
||||
def refresh(self, token, url, headers=defaultHeaders):
|
||||
data = {'token': token}
|
||||
return requests.post(url, data=json.dumps(data), headers=headers)
|
||||
|
||||
def fetch_employees(self, data, token, url=employeeUrl):
|
||||
def fetch_employees(self, data, token, url):
|
||||
headers = {
|
||||
'Authorization': 'JWT ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
return requests.get(url, data=json.dumps(data), headers=headers)
|
||||
|
||||
def fetch_departments(self, data, token, url=departmentsUrl):
|
||||
def fetch_departments(self, data, token, url):
|
||||
headers = {
|
||||
'Authorization': 'JWT ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
return requests.get(url, data=json.dumps(data), headers=headers)
|
||||
|
||||
def fetch_terminals(self, data, token, url=terminalsUrl):
|
||||
def fetch_terminals(self, data, token, url):
|
||||
headers = {
|
||||
'Authorization': 'JWT ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
return requests.get(url, data=json.dumps(data), headers=headers)
|
||||
|
||||
def fetch_areas(self, data, token, url=areasUrl):
|
||||
def fetch_areas(self, data, token, url):
|
||||
headers = {
|
||||
'Authorization': 'JWT ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
return requests.get(url, data=json.dumps(data), headers=headers)
|
||||
|
||||
def fetch_positions(self, data, token, url=positionsUrl):
|
||||
def fetch_positions(self, data, token, url):
|
||||
headers = {
|
||||
'Authorization': 'JWT ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
return requests.get(url, data=json.dumps(data), headers=headers)
|
||||
|
||||
def fetch_transctions(self, token, url=transctionsUrl):
|
||||
def fetch_transctions(self, token, url):
|
||||
headers = {
|
||||
'Authorization': 'JWT ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
return requests.get(url, headers=headers)
|
||||
|
||||
def fetch_empl_transctions(self, data, token, url=transctionsUrl):
|
||||
def fetch_empl_transctions(self, data, token, url):
|
||||
headers = {
|
||||
'Authorization': 'JWT ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
|
|
|
|||
|
|
@ -156,7 +156,8 @@ class BiotimeAPI(models.Model):
|
|||
print(tx)
|
||||
|
||||
def login(self):
|
||||
res = httpHelper.login(self.username, self.password)
|
||||
loginUrl, _, _, _, _, _, _, _, _, _ = self._calc_urls()
|
||||
res = httpHelper.login(self.username, self.password, loginUrl)
|
||||
|
||||
if res.status_code == 200:
|
||||
data = res.json()
|
||||
|
|
@ -172,7 +173,8 @@ class BiotimeAPI(models.Model):
|
|||
raise ValidationError(err)
|
||||
|
||||
def refresh(self):
|
||||
res = httpHelper.refresh(self.token)
|
||||
_, refreshUrl, _, _, _, _, _, _, _, _ = self._calc_urls()
|
||||
res = httpHelper.refresh(self.token, refreshUrl)
|
||||
if res.status_code == 200:
|
||||
data = res.json()
|
||||
token = data.get('token', False)
|
||||
|
|
@ -209,8 +211,9 @@ class BiotimeAPI(models.Model):
|
|||
def sync_employees(self):
|
||||
if not self.token:
|
||||
self.refresh()
|
||||
res = httpHelper.fetch_employees({}, self.token)
|
||||
_, _, employeeUrl, _, _, _, _, _, _, _ = self._calc_urls()
|
||||
res = httpHelper.fetch_employees({}, self.token, employeeUrl)
|
||||
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
self.create_employee(da.data)
|
||||
|
|
@ -241,8 +244,9 @@ class BiotimeAPI(models.Model):
|
|||
def sync_terminals(self):
|
||||
if not self.token:
|
||||
self.refresh()
|
||||
res = httpHelper.fetch_terminals({}, self.token)
|
||||
_, _, _, _, _, _, _, terminalsUrl, _, _ = self._calc_urls()
|
||||
res = httpHelper.fetch_terminals({}, self.token, terminalsUrl)
|
||||
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
self.create_termainl(da.data)
|
||||
|
|
@ -262,8 +266,8 @@ class BiotimeAPI(models.Model):
|
|||
def sync_departments(self):
|
||||
if not self.token:
|
||||
self.refresh()
|
||||
res = httpHelper.fetch_departments({}, self.token)
|
||||
_, _, _, departmentsUrl, _, _, _, _, _, _ = self._calc_urls()
|
||||
res = httpHelper.fetch_departments({}, self.token, departmentsUrl)
|
||||
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
|
|
@ -284,8 +288,8 @@ class BiotimeAPI(models.Model):
|
|||
def sync_areas(self):
|
||||
if not self.token:
|
||||
self.refresh()
|
||||
res = httpHelper.fetch_areas({}, self.token)
|
||||
_, _, _, _, _, _, areasUrl, _, _, _ = self._calc_urls()
|
||||
res = httpHelper.fetch_areas({}, self.token, areasUrl)
|
||||
try:
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
|
|
@ -293,7 +297,7 @@ class BiotimeAPI(models.Model):
|
|||
next = 2
|
||||
while da.next:
|
||||
res = httpHelper.fetch_areas(
|
||||
{}, self.token, url=areasUrl + "?page=" + str(next))
|
||||
{}, self.token, areasUrl + "?page=" + str(next))
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
self.create_area(da.data)
|
||||
|
|
@ -308,8 +312,8 @@ class BiotimeAPI(models.Model):
|
|||
def sync_positions(self):
|
||||
if not self.token:
|
||||
self.refresh()
|
||||
res = httpHelper.fetch_positions({}, self.token)
|
||||
_, _, _, _, positionsUrl, _, _, _, _, _ = self._calc_urls()
|
||||
res = httpHelper.fetch_positions({}, self.token, positionsUrl)
|
||||
|
||||
try:
|
||||
if res.status_code == 200:
|
||||
|
|
@ -318,7 +322,7 @@ class BiotimeAPI(models.Model):
|
|||
next = 2
|
||||
while da.next:
|
||||
res = httpHelper.fetch_positions(
|
||||
{}, self.token, url=positionsUrl + "?page=" + str(next))
|
||||
{}, self.token, positionsUrl + "?page=" + str(next))
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
self.create_position(da.data)
|
||||
|
|
@ -341,8 +345,6 @@ class BiotimeAPI(models.Model):
|
|||
next = 2
|
||||
self.create_attendance(da.data)
|
||||
while da.next:
|
||||
# res = httpHelper.fetch_empl_transctions(
|
||||
# {}, self.token, url=transctionsUrl + "?page=" + str(next))
|
||||
res = httpHelper.fetch_empl_transctions({}, self.token, da.next)
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
|
|
@ -350,7 +352,6 @@ class BiotimeAPI(models.Model):
|
|||
else:
|
||||
da.next = None
|
||||
next = next + 1
|
||||
# print('&&&&&&&&&&&&&&&&&&&&& pages &&&&&&&&&&&&&&&&&&&',da.next)
|
||||
else:
|
||||
self.errorHandler(res)
|
||||
if res.status_code == 401:
|
||||
|
|
@ -364,10 +365,11 @@ class BiotimeAPI(models.Model):
|
|||
self.login()
|
||||
if not self.token:
|
||||
self.refresh()
|
||||
res = httpHelper.fetch_empl_transctions({}, self.token)
|
||||
_, _, _, _, _, _, _, _, _, transctionsUrl = self._calc_urls()
|
||||
res = httpHelper.fetch_empl_transctions({}, self.token, transctionsUrl)
|
||||
if not self.loaded_employees:
|
||||
self.sync_employees()
|
||||
_, _, _, _, _, _, _, _, _, transctionsUrl = self._calc_urls()
|
||||
|
||||
try:
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
|
|
@ -375,19 +377,19 @@ class BiotimeAPI(models.Model):
|
|||
self.create_attendance(da.data)
|
||||
while da.next:
|
||||
res = httpHelper.fetch_empl_transctions(
|
||||
{}, self.token, url=transctionsUrl + "?page=" + str(next))
|
||||
{}, self.token, transctionsUrl + "?page=" + str(next))
|
||||
if res.status_code == 200:
|
||||
da = ParsedRequest(res.content)
|
||||
self.create_attendance(da.data)
|
||||
else:
|
||||
da.next = None
|
||||
next = next + 1
|
||||
# print('&&&&&&&&&&&&&&&&&&&&& pages 222 &&&&&&&&&&&&&&&&&&&',next)
|
||||
else:
|
||||
self.errorHandler(res)
|
||||
except Exception as e:
|
||||
ValidationError(str(e))
|
||||
|
||||
|
||||
def create_termainl(self, termainls):
|
||||
TerminalsModel = self.env['finger.terminal']
|
||||
for tx in termainls:
|
||||
|
|
|
|||
Loading…
Reference in New Issue