123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import requests
- import json
- from datetime import datetime, timedelta
- class DataEmployee:
-
-
- DATA = []
- API = "http://http-service:http-service@172.16.10.231/acms/hs/api/getbyfio/"
-
- def __init__(self, fios, startDate, endDate):
- self.fios = fios
- self.startDate = startDate
- self.endDate = endDate
- self.get_correct_data_to_table()
- class EmployeesTable():
- Headers = [
- {
- 'text': 'День недели',
- 'value': 'wd'
- },{
- 'text': 'Дата',
- 'value': 'date'
- },{
- 'text': 'Вход',
- 'value': 'entrance'
- },{
- 'text': 'Выход',
- 'value': 'exit'
- },{
- 'text': 'Время пребывания',
- 'value': 'diffTime'
- }
- ]
- class DateAttendance:
- WEEKDAYS = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс']
- def __init__(self, date, startDate = None, endDate = None):
- dateD = datetime.fromisoformat(date)
- self.wd = self.WEEKDAYS[dateD.weekday()],
- self.date = dateD.strftime('%d.%m.%Y'),
- if startDate is None and endDate is None:
- self.entrance = None,
- self.exit = None
- self.diffTime = None
- else:
- self.entrance = startDate.split('T')[1],
- self.exit = endDate.split('T')[1]
- self.diffTime = str(self.get_diff_time(startDate, endDate))
-
- def get_diff_time(self, startDate, endDate):
- start = datetime.fromisoformat(startDate)
- end = datetime.fromisoformat(endDate)
- return abs(end-start)
- def get_range_date(self):
- start = datetime.fromisoformat(self.startDate)
- end = datetime.fromisoformat(self.endDate)
- range_date = []
- while start <= end:
- range_date.append(start)
- start += timedelta(days=1)
-
- return range_date
- def get_correct_data_to_table(self):
- self.DATA = []
- date_range = self.get_range_date()
- for data in self.get_data_from_api():
-
- employee = self.EmployeesTable()
- employee.FIO = data['fio']
- employee.DatesAttendance = []
- employee.EmptyDatesAttendance = []
-
- dates = [x['d'] for x in data['data']]
- print(dates)
- for date in self.get_range_date():
- if date.strftime('%Y-%m-%d') not in dates:
- employee.EmptyDatesAttendance.append(self.DateAttendance(str(date)))
-
- for d_attandence in data['data']:
- employee.DatesAttendance.append(self.DateAttendance(d_attandence['d'],
- d_attandence['d_first'],
- d_attandence['d_last']))
- self.DATA.append(employee)
- def get_data_from_api(self):
- data = []
- for link_api in self.create_links_api():
- response = requests.get(link_api)
- try:
- data.append(json.loads(response.text))
- except json.decoder.JSONDecodeError:
- response.encoding = 'utf-8-sig'
- data.append(json.loads(response.text))
- return data
- def get_json(self):
- obj_json = []
- for data in self.DATA:
- obj_json.append({
- 'FIO': data.FIO,
- 'STARTDATE': self.startDate,
- 'ENDDATE': self.endDate,
- 'HEADERS': data.Headers,
- 'EmptyDatesAttendance': [{
- 'wd': x.wd,
- 'date': x.date,
- 'entrance': '',
- 'exit': '',
- 'diffTime': ''
- } for x in data.EmptyDatesAttendance ],
- 'DateAttendance': [{
- 'wd': x.wd,
- 'date': x.date,
- 'entrance': x.entrance,
- 'exit': x.exit,
- 'diffTime': str(x.diffTime)
- } for x in data.DatesAttendance]
- })
- return obj_json
- def create_links_api(self):
- list_api = []
- for fio in self.fios:
- list_api.append(self.API+fio+'/'+self.startDate.replace('-', '')+'/'+self.endDate.replace('-', ''))
-
- return list_api
|