general refactoring
This commit is contained in:
		
							
								
								
									
										124
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										124
									
								
								main.py
									
									
									
									
									
								
							| @@ -10,21 +10,27 @@ load_dotenv() | |||||||
|  |  | ||||||
| ACCESS_TOKEN = getenv("TOKEN") | ACCESS_TOKEN = getenv("TOKEN") | ||||||
| URL = getenv("URL") | URL = getenv("URL") | ||||||
| USERNAMES = ["kalinom6"]  # Modify | USERNAMES = ["kalinom6"] | ||||||
| CERT = getenv("CERT_LOCATION") | CERT = getenv("CERT_LOCATION") | ||||||
| WEEKS_BACK = -1 | WEEKS_BACK = 0 | ||||||
| CHECK_MONTH_BACK = False |  | ||||||
| MONTH_VIEW = False | MONTH_VIEW = False | ||||||
| VERBOSE = False | MONTHS_BACK = 0 | ||||||
|  | VERBOSE = True | ||||||
|  |  | ||||||
|  | def get_flag_value(flag): | ||||||
|  |     return flag.split("=")[1] | ||||||
|  |  | ||||||
| if "--monthview" in sys.argv: | if "--monthview" in sys.argv: | ||||||
|     MONTH_VIEW = True |     MONTH_VIEW = True | ||||||
|     if "--monthback" in sys.argv: | for arg in sys.argv: | ||||||
|         CHECK_MONTH_BACK = True |     if "--weeksback" in arg and not MONTH_VIEW: | ||||||
| else: |         WEEKS_BACK = int(get_flag_value(arg)) | ||||||
|     for arg in sys.argv: |     if "--monthsback" in arg and MONTH_VIEW: | ||||||
|         if "--weeksback" in arg: |         MONTHS_BACK = int(get_flag_value(arg)) | ||||||
|             WEEKS_BACK = int(arg.split("=")[1]) |     if "--username" in arg: | ||||||
|  |         USERNAMES[0] = get_flag_value(arg) | ||||||
|  |         if ',' in USERNAMES[0]: | ||||||
|  |             USERNAMES = USERNAMES[0].split(',') | ||||||
| if "--verbose" in sys.argv: | if "--verbose" in sys.argv: | ||||||
|     VERBOSE = True |     VERBOSE = True | ||||||
|  |  | ||||||
| @@ -50,7 +56,7 @@ def get_issues(username): | |||||||
|     payload = { |     payload = { | ||||||
|         "jql": jql, |         "jql": jql, | ||||||
|         "startAt": 0, |         "startAt": 0, | ||||||
|         "maxResults": 15, |         "maxResults": 999, | ||||||
|         "fields": [ |         "fields": [ | ||||||
|             "key", "summary" |             "key", "summary" | ||||||
|         ] |         ] | ||||||
| @@ -68,7 +74,7 @@ def get_issues(username): | |||||||
|         return tasks |         return tasks | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_worklogs(issues): | def get_all_worklogs(issues): | ||||||
|     worklogs = [] |     worklogs = [] | ||||||
|     for issue in issues: |     for issue in issues: | ||||||
|         data = send_request(f'{URL}/issue/{issue["task_id"]}/worklog', "GET") |         data = send_request(f'{URL}/issue/{issue["task_id"]}/worklog', "GET") | ||||||
| @@ -84,11 +90,11 @@ def get_worklogs(issues): | |||||||
|                 if worklog["author"] in USERNAMES: |                 if worklog["author"] in USERNAMES: | ||||||
|                     worklogs.append(worklog) |                     worklogs.append(worklog) | ||||||
|  |  | ||||||
|     worklogs.sort(key=date_sort) |     worklogs.sort(key=date_getter) | ||||||
|     return worklogs |     return worklogs | ||||||
|  |  | ||||||
|  |  | ||||||
| def date_sort(worklog): | def date_getter(worklog): | ||||||
|     return worklog['date'] |     return worklog['date'] | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -120,65 +126,65 @@ def get_days(day): | |||||||
|     return days, days_second_part |     return days, days_second_part | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_week_tickets(worklogs, days): | def get_worklogs_for_days(worklogs, days): | ||||||
|     this_week_tickets = [] |     timeperiod_tickets = [] | ||||||
|     for worklog in worklogs: |     for worklog in worklogs: | ||||||
|         worklog_date = datetime.datetime.strptime( |         worklog_date = datetime.datetime.strptime( | ||||||
|             worklog["date"].split('T')[0], '%Y-%m-%d').date() |             worklog["date"].split('T')[0], '%Y-%m-%d').date() | ||||||
|         if str(worklog_date) in days: |         if str(worklog_date) in days: | ||||||
|             this_week_tickets.append(worklog) |             timeperiod_tickets.append(worklog) | ||||||
|     return this_week_tickets |     return timeperiod_tickets | ||||||
|  |  | ||||||
| def main(): | def main(): | ||||||
|     print('The script starts its work...') |     print('The script starts its work...') | ||||||
|  |     issues = [] | ||||||
|  |     worklogs = [] | ||||||
|     if MONTH_VIEW: |     if MONTH_VIEW: | ||||||
|         for username in USERNAMES: |         for username in USERNAMES: | ||||||
|             issues = get_issues(username) |             issues = get_issues(username) | ||||||
|             worklogs = get_worklogs(issues) |             all_worklogs = get_all_worklogs(issues) | ||||||
|             days = get_days_of_month(CHECK_MONTH_BACK) |             days = get_days_of_month(MONTHS_BACK) | ||||||
|             worklogs = get_month_tickets(worklogs, days, CHECK_MONTH_BACK) |             print(all_worklogs) | ||||||
|             omnimat_string = get_month_view(worklogs) |             worklogs = get_worklogs_for_days(all_worklogs, days) | ||||||
|             print(omnimat_string) |             print(worklogs) | ||||||
|     else: |             month_view = get_month_view(worklogs) | ||||||
|         for username in USERNAMES: |             print(month_view) | ||||||
|             issues = get_issues(username) |         return | ||||||
|             worklogs = get_worklogs(issues) |     for username in USERNAMES: | ||||||
|             today = datetime.date.today() |         issues = get_issues(username) | ||||||
|             days, days_second_part = get_days(today) |         all_worklogs = get_all_worklogs(issues) | ||||||
|             week_worklogs = get_week_tickets(worklogs, days) |         today = datetime.date.today() | ||||||
|             omnimat_string = "" |         days, days_second_part = get_days(today) | ||||||
|  |         worklogs = get_worklogs_for_days(all_worklogs, days) | ||||||
|             for worklog in week_worklogs: |         omnimat_string = "" | ||||||
|                 if worklog["ticket_id"] not in omnimat_string: |  | ||||||
|                     omnimat_string += worklog["ticket_id"] + '\n' |  | ||||||
|  |  | ||||||
|             omnimat_string = "" |  | ||||||
|             if days_second_part: |  | ||||||
|                 second_omnimat_string = "" |  | ||||||
|                 worklogs_second_part = get_week_tickets(worklogs, days_second_part) |  | ||||||
|                 for worklog in week_worklogs: |  | ||||||
|                     if worklog["ticket_id"] not in second_omnimat_string: |  | ||||||
|                         second_omnimat_string += worklog["ticket_id"] + '\n' |  | ||||||
|  |  | ||||||
|  |         print(worklogs) | ||||||
|  |         for worklog in worklogs: | ||||||
|  |             if worklog["ticket_id"] not in omnimat_string: | ||||||
|  |                 omnimat_string += worklog["ticket_id"] + '\n' | ||||||
|  |         if days_second_part: | ||||||
|  |             second_omnimat_string = "" | ||||||
|  |             worklogs_second_part = get_worklogs_for_days(all_worklogs, days_second_part) | ||||||
|  |             for worklog in worklogs: | ||||||
|  |                 if worklog["ticket_id"] not in second_omnimat_string: | ||||||
|  |                     second_omnimat_string += worklog["ticket_id"] + '\n' | ||||||
|  |         if VERBOSE: | ||||||
|  |             print('Issues of the user:') | ||||||
|  |             print(json.dumps(issues, indent=4)) | ||||||
|  |             print(f"Worklogs for the time period:") | ||||||
|  |             print(json.dumps(worklogs, indent=4)) | ||||||
|  |         print("Days:") | ||||||
|  |         print(days) | ||||||
|  |         print(f"Omnimat string:") | ||||||
|  |         print(omnimat_string) | ||||||
|  |         if days_second_part: | ||||||
|             if VERBOSE: |             if VERBOSE: | ||||||
|                 print('Issues of the user:') |                 print(f"Worklogs for the weeks 2nd part:") | ||||||
|                 print(json.dumps(issues, indent=4)) |                 print(json.dumps(worklogs_second_part, indent=4)) | ||||||
|                 print(f"Worklogs for the week:") |  | ||||||
|                 print(json.dumps(week_worklogs, indent=4)) |  | ||||||
|             print("Days:") |             print("Days:") | ||||||
|             print(days) |             print(days_second_part) | ||||||
|             print(f"Omnimat string:") |             print(f"Omnimat string:") | ||||||
|             print(omnimat_string) |             print(second_omnimat_string) | ||||||
|             if days_second_part: |  | ||||||
|                 if VERBOSE: |  | ||||||
|                     print(f"Worklogs for the weeks 2nd part:") |  | ||||||
|                     print(json.dumps(worklogs_second_part, indent=4)) |  | ||||||
|                 print("Days:") |  | ||||||
|                 print(days_second_part) |  | ||||||
|                 print(f"Omnimat string:") |  | ||||||
|                 print(omnimat_string) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     main() |     main() | ||||||
|   | |||||||
| @@ -1,43 +1,23 @@ | |||||||
| import datetime | import datetime | ||||||
|  |  | ||||||
| #code of month branch | def get_days_of_month(months_back=0): | ||||||
| def get_days_of_month(previous_month=False): |     # set the current day to the first day of the current month | ||||||
|     today = datetime.date.today() |     # and loop for the days until the month changes | ||||||
|     start_of_month = today.replace(day=1) |     current_day = datetime.datetime.today().replace(day=1) | ||||||
|     end_of_month = today.replace(day=28)+datetime.timedelta(days=4) |     current_month = current_day.month | ||||||
|     end_of_month -= datetime.timedelta(days=end_of_month.day) |  | ||||||
|     days = [] |     days = [] | ||||||
|  |     while months_back: | ||||||
|  |         current_day = datetime.datetime.today().replace(day=1) | ||||||
|  |         current_day -= datetime.timedelta(days=1) | ||||||
|  |         current_day = current_day.replace(day=1) | ||||||
|  |         current_month = current_day.month | ||||||
|  |         months_back -= 1 | ||||||
|  |     while current_day.month == current_month: | ||||||
|  |         days.append(str(current_day).split(' ')[0]) | ||||||
|  |         current_day += datetime.timedelta(days=1) | ||||||
|  |  | ||||||
|     # minus one month |  | ||||||
|     if previous_month == True: |  | ||||||
|         start_of_month -= datetime.timedelta(days=1) |  | ||||||
|         start_of_month = start_of_month.replace(day=1) |  | ||||||
|         end_of_month = start_of_month |  | ||||||
|         end_of_month = today.replace(day=28)+datetime.timedelta(days=4) |  | ||||||
|         end_of_month -= datetime.timedelta(days=end_of_month.day) |  | ||||||
|  |  | ||||||
|     days.append(str(start_of_month)) |  | ||||||
|     while start_of_month != end_of_month: |  | ||||||
|         start_of_month += datetime.timedelta(days=1) |  | ||||||
|         days.append(str(start_of_month)) |  | ||||||
|  |  | ||||||
|     # returns all days of current month |  | ||||||
|     return days |     return days | ||||||
|  |  | ||||||
| def get_month_tickets(worklogs, days, previous_month=False): |  | ||||||
|     this_month_tickets = [] |  | ||||||
|     for worklog in worklogs: |  | ||||||
|         worklog_date = datetime.datetime.strptime( |  | ||||||
|             worklog["date"].split('T')[0], '%Y-%m-%d').date() |  | ||||||
|         current_month = datetime.date.today().month |  | ||||||
|         # minus one month |  | ||||||
|         if previous_month == True: |  | ||||||
|             current_month -= 1 |  | ||||||
|         worklog_month = str(worklog_date).split('-')[1] |  | ||||||
|         if str(worklog_date) in days and int(worklog_month) == current_month: |  | ||||||
|             this_month_tickets.append(worklog) |  | ||||||
|     return this_month_tickets |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_month_view(worklogs): | def get_month_view(worklogs): | ||||||
|  |  | ||||||
| @@ -50,7 +30,7 @@ def get_month_view(worklogs): | |||||||
|     week_number_end = week_number_start+datetime.timedelta(days=6) |     week_number_end = week_number_start+datetime.timedelta(days=6) | ||||||
|  |  | ||||||
|     week_iso = week.isocalendar().week |     week_iso = week.isocalendar().week | ||||||
|     omnimat_string = worklogs[0]["author"] |     month_view = worklogs[0]["author"] | ||||||
|  |  | ||||||
|     for worklog in worklogs: |     for worklog in worklogs: | ||||||
|         ticket = worklog["ticket_id"] |         ticket = worklog["ticket_id"] | ||||||
| @@ -59,13 +39,12 @@ def get_month_view(worklogs): | |||||||
|  |  | ||||||
|         week_day = datetime.datetime.strptime(date, '%Y-%m-%d').date() |         week_day = datetime.datetime.strptime(date, '%Y-%m-%d').date() | ||||||
|         if (week_day.isocalendar().week == week_iso): |         if (week_day.isocalendar().week == week_iso): | ||||||
|             omnimat_string += "\nWeek " + str(week_iso)+"\tfrom "+week_number_start.__str__() + \ |             month_view += "\nWeek " + str(week_iso)+"\tfrom "+week_number_start.__str__() + \ | ||||||
|                 "\tto "+week_number_end.__str__()+"\n\n" |                 "\tto "+week_number_end.__str__()+"\n\n" | ||||||
|             week_iso += 1 |             week_iso += 1 | ||||||
|             week_number_start += datetime.timedelta(days=7) |             week_number_start += datetime.timedelta(days=7) | ||||||
|             week_number_end += datetime.timedelta(days=7) |             week_number_end += datetime.timedelta(days=7) | ||||||
|  |  | ||||||
|         omnimat_string += ticket+'\tdate '+date+'\ttime '+timespent + '\n' |         month_view += ticket+'\tdate '+date+'\ttime '+timespent + '\n' | ||||||
|  |  | ||||||
|     return omnimat_string |     return month_view | ||||||
| #end of code month branch |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Michał Kalinowski
					Michał Kalinowski