added flag parsing, exported month view to a different file
This commit is contained in:
		
							
								
								
									
										106
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										106
									
								
								main.py
									
									
									
									
									
								
							| @@ -1,19 +1,32 @@ | ||||
| import pip._vendor.requests as requests | ||||
| import requests | ||||
| import json | ||||
| import datetime | ||||
| from dotenv import load_dotenv | ||||
| from os import getenv | ||||
| from month_view import * | ||||
| import sys | ||||
|  | ||||
| load_dotenv() | ||||
| ACCESS_TOKEN = getenv("TOKEN") | ||||
| USERNAMES = ["litneri"]  # Modify | ||||
| URL = "https://globaljira.roche.com/rest/api/2" | ||||
| CERT = getenv("CERT_LOCATION") | ||||
| WEEKS_BACK = 1 | ||||
| VERBOSE = True | ||||
|  | ||||
| MONTH_VIEW = True | ||||
| ACCESS_TOKEN = getenv("TOKEN") | ||||
| URL = getenv("URL") | ||||
| USERNAMES = ["kalinom6"]  # Modify | ||||
| CERT = getenv("CERT_LOCATION") | ||||
| WEEKS_BACK = -1 | ||||
| CHECK_MONTH_BACK = False | ||||
| MONTH_VIEW = False | ||||
| VERBOSE = False | ||||
| if "--monthview" in sys.argv: | ||||
|     MONTH_VIEW = True | ||||
|     if "--monthback" in sys.argv: | ||||
|         CHECK_MONTH_BACK = True | ||||
| else: | ||||
|     for arg in sys.argv: | ||||
|         if "--weeksback" in arg: | ||||
|             WEEKS_BACK = int(arg.split("=")[1]) | ||||
| if "--verbose" in sys.argv: | ||||
|     VERBOSE = True | ||||
|  | ||||
|  | ||||
| headers = { | ||||
|     "Authorization": f"Bearer {ACCESS_TOKEN}", | ||||
| @@ -23,11 +36,9 @@ headers = { | ||||
|  | ||||
| def send_request(url, method, payload=None): | ||||
|     if method == "GET": | ||||
|         response = requests.get(url, headers=headers, | ||||
|                                 data=json.dumps(payload), verify=CERT) | ||||
|         response = requests.get(url, headers=headers, data=json.dumps(payload), verify=CERT) | ||||
|     if method == "POST": | ||||
|         response = requests.post(url, headers=headers, | ||||
|                                  data=json.dumps(payload), verify=CERT) | ||||
|         response = requests.post(url, headers=headers, data=json.dumps(payload), verify=CERT) | ||||
|     if response.status_code == 200: | ||||
|         return response.json() | ||||
|     print(f"Error: {response.status_code}, {response.text}") | ||||
| @@ -117,76 +128,6 @@ def get_week_tickets(worklogs, days): | ||||
|             this_week_tickets.append(worklog) | ||||
|     return this_week_tickets | ||||
|  | ||||
| #code of month branch | ||||
| def get_days_of_month(previous_month=False): | ||||
|     today = datetime.date.today() | ||||
|     start_of_month = today.replace(day=1) | ||||
|     end_of_month = today.replace(day=28)+datetime.timedelta(days=4) | ||||
|     end_of_month -= datetime.timedelta(days=end_of_month.day) | ||||
|     days = [] | ||||
|  | ||||
|     # 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 | ||||
|  | ||||
| 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_omnimat_string(worklogs): | ||||
|  | ||||
|     week = datetime.datetime.strptime( | ||||
|         worklogs[0]["date"][0:10], '%Y-%m-%d').date() | ||||
|  | ||||
|     week_number_start = week | ||||
|     to_monday = week_number_start.weekday() | ||||
|     week_number_start -= datetime.timedelta(days=to_monday) | ||||
|     week_number_end = week_number_start+datetime.timedelta(days=6) | ||||
|  | ||||
|     week_iso = week.isocalendar().week | ||||
|     omnimat_string = worklogs[0]["author"] | ||||
|  | ||||
|     for worklog in worklogs: | ||||
|         ticket = worklog["ticket_id"] | ||||
|         date = worklog["date"][0:10] | ||||
|         timespent = worklog["time_spent"] | ||||
|  | ||||
|         week_day = datetime.datetime.strptime(date, '%Y-%m-%d').date() | ||||
|         if (week_day.isocalendar().week == week_iso): | ||||
|             omnimat_string += "\nWeek " + str(week_iso)+"\tfrom "+week_number_start.__str__() + \ | ||||
|                 "\tto "+week_number_end.__str__()+"\n\n" | ||||
|             week_iso += 1 | ||||
|             week_number_start += datetime.timedelta(days=7) | ||||
|             week_number_end += datetime.timedelta(days=7) | ||||
|  | ||||
|         omnimat_string += ticket+'\tdate '+date+'\ttime '+timespent + '\n' | ||||
|  | ||||
|     return omnimat_string | ||||
| #end of code month branch | ||||
|  | ||||
| def main(): | ||||
|     print('The script starts its work...') | ||||
|  | ||||
| @@ -211,6 +152,7 @@ def main(): | ||||
|                 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) | ||||
|   | ||||
							
								
								
									
										71
									
								
								month_view.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										71
									
								
								month_view.py
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,71 @@ | ||||
| import datetime | ||||
|  | ||||
| #code of month branch | ||||
| def get_days_of_month(previous_month=False): | ||||
|     today = datetime.date.today() | ||||
|     start_of_month = today.replace(day=1) | ||||
|     end_of_month = today.replace(day=28)+datetime.timedelta(days=4) | ||||
|     end_of_month -= datetime.timedelta(days=end_of_month.day) | ||||
|     days = [] | ||||
|  | ||||
|     # 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 | ||||
|  | ||||
| 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): | ||||
|  | ||||
|     week = datetime.datetime.strptime( | ||||
|         worklogs[0]["date"][0:10], '%Y-%m-%d').date() | ||||
|  | ||||
|     week_number_start = week | ||||
|     to_monday = week_number_start.weekday() | ||||
|     week_number_start -= datetime.timedelta(days=to_monday) | ||||
|     week_number_end = week_number_start+datetime.timedelta(days=6) | ||||
|  | ||||
|     week_iso = week.isocalendar().week | ||||
|     omnimat_string = worklogs[0]["author"] | ||||
|  | ||||
|     for worklog in worklogs: | ||||
|         ticket = worklog["ticket_id"] | ||||
|         date = worklog["date"][0:10] | ||||
|         timespent = worklog["time_spent"] | ||||
|  | ||||
|         week_day = datetime.datetime.strptime(date, '%Y-%m-%d').date() | ||||
|         if (week_day.isocalendar().week == week_iso): | ||||
|             omnimat_string += "\nWeek " + str(week_iso)+"\tfrom "+week_number_start.__str__() + \ | ||||
|                 "\tto "+week_number_end.__str__()+"\n\n" | ||||
|             week_iso += 1 | ||||
|             week_number_start += datetime.timedelta(days=7) | ||||
|             week_number_end += datetime.timedelta(days=7) | ||||
|  | ||||
|         omnimat_string += ticket+'\tdate '+date+'\ttime '+timespent + '\n' | ||||
|  | ||||
|     return omnimat_string | ||||
| #end of code month branch | ||||
		Reference in New Issue
	
	Block a user
	 Michał Kalinowski
					Michał Kalinowski