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