data_employee.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import requests
  2. import json
  3. from datetime import datetime, timedelta
  4. class DataEmployee:
  5. DATA = []
  6. API = "http://http-service:http-service@172.16.10.231/acms/hs/api/getbyfio/"
  7. def __init__(self, fios, startDate, endDate):
  8. self.fios = fios
  9. self.startDate = startDate
  10. self.endDate = endDate
  11. self.get_correct_data_to_table()
  12. class EmployeesTable():
  13. Headers = [
  14. {
  15. 'text': 'День недели',
  16. 'value': 'wd'
  17. },{
  18. 'text': 'Дата',
  19. 'value': 'date'
  20. },{
  21. 'text': 'Вход',
  22. 'value': 'entrance'
  23. },{
  24. 'text': 'Выход',
  25. 'value': 'exit'
  26. },{
  27. 'text': 'Время пребывания',
  28. 'value': 'diffTime'
  29. }
  30. ]
  31. class DateAttendance:
  32. WEEKDAYS = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']
  33. def __init__(self, date, startDate = None, endDate = None):
  34. dateD = datetime.fromisoformat(date)
  35. self.wd = self.WEEKDAYS[dateD.weekday()],
  36. self.date = dateD.strftime('%d.%m.%Y'),
  37. if startDate is None and endDate is None:
  38. self.entrance = None,
  39. self.exit = None
  40. self.diffTime = None
  41. else:
  42. self.entrance = startDate.split('T')[1],
  43. self.exit = endDate.split('T')[1]
  44. self.diffTime = str(self.get_diff_time(startDate, endDate))
  45. def get_diff_time(self, startDate, endDate):
  46. start = datetime.fromisoformat(startDate)
  47. end = datetime.fromisoformat(endDate)
  48. return abs(end-start)
  49. def get_range_date(self):
  50. start = datetime.fromisoformat(self.startDate)
  51. end = datetime.fromisoformat(self.endDate)
  52. range_date = []
  53. while start <= end:
  54. range_date.append(start)
  55. start += timedelta(days=1)
  56. return range_date
  57. def get_correct_data_to_table(self):
  58. self.DATA = []
  59. date_range = self.get_range_date()
  60. for data in self.get_data_from_api():
  61. employee = self.EmployeesTable()
  62. employee.FIO = data['fio']
  63. employee.DatesAttendance = []
  64. employee.EmptyDatesAttendance = []
  65. dates = [x['d'] for x in data['data']]
  66. print(dates)
  67. for date in self.get_range_date():
  68. if date.strftime('%Y-%m-%d') not in dates:
  69. employee.EmptyDatesAttendance.append(self.DateAttendance(str(date)))
  70. for d_attandence in data['data']:
  71. employee.DatesAttendance.append(self.DateAttendance(d_attandence['d'],
  72. d_attandence['d_first'],
  73. d_attandence['d_last']))
  74. self.DATA.append(employee)
  75. def get_data_from_api(self):
  76. data = []
  77. for link_api in self.create_links_api():
  78. response = requests.get(link_api)
  79. try:
  80. data.append(json.loads(response.text))
  81. except json.decoder.JSONDecodeError:
  82. response.encoding = 'utf-8-sig'
  83. data.append(json.loads(response.text))
  84. return data
  85. def get_json(self):
  86. obj_json = []
  87. for data in self.DATA:
  88. obj_json.append({
  89. 'FIO': data.FIO,
  90. 'STARTDATE': self.startDate,
  91. 'ENDDATE': self.endDate,
  92. 'HEADERS': data.Headers,
  93. 'EmptyDatesAttendance': [{
  94. 'wd': x.wd,
  95. 'date': x.date,
  96. 'entrance': '',
  97. 'exit': '',
  98. 'diffTime': ''
  99. } for x in data.EmptyDatesAttendance ],
  100. 'DateAttendance': [{
  101. 'wd': x.wd,
  102. 'date': x.date,
  103. 'entrance': x.entrance,
  104. 'exit': x.exit,
  105. 'diffTime': str(x.diffTime)
  106. } for x in data.DatesAttendance]
  107. })
  108. return obj_json
  109. def create_links_api(self):
  110. list_api = []
  111. for fio in self.fios:
  112. list_api.append(self.API+fio+'/'+self.startDate.replace('-', '')+'/'+self.endDate.replace('-', ''))
  113. return list_api