Skip to content

Commit

Permalink
Add generate_report_for_specified_group
Browse files Browse the repository at this point in the history
Change category comparison to non case sensitive

Add --entrypoint bash to go.sh
  • Loading branch information
PhongT16 committed Aug 16, 2023
1 parent 28e0fcb commit 1b79b56
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
16 changes: 12 additions & 4 deletions GenerateReport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
import collections

import utils
import IndividualCalendar
def filter_simple_events(simple_events):
filtered_events = {}
for event in simple_events:
Expand All @@ -11,7 +12,8 @@ def filter_simple_events(simple_events):

return collections.OrderedDict(sorted(filtered_events.items()))

def print_table(sorted_simple_events):
def print_table(simple_events):
sorted_simple_events = filter_simple_events(simple_events)
for key in sorted_simple_events:
line = f"{key.date()},"
for count, event in enumerate(sorted_simple_events[key]):
Expand All @@ -25,6 +27,12 @@ def print_table(sorted_simple_events):
line = line + ","
print(line)


def generate_report_for_specified_group(group_name, start_date, end_date, access_token):
emails = utils.get_email_list_from_ldap(group_name)
calendars = IndividualCalendar.get_individual_calendars(start_date, end_date, emails, access_token)
events = IndividualCalendar.process_individual_calendars(calendars, start_date, end_date)
print_table(events)





29 changes: 21 additions & 8 deletions OutlookCalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def process_args():
''')

parser.add_argument('-s', '--update_shared_calendar', action='store_true', help='Update shared calendar')
parser.add_argument('-g', '--generate_report', action='store_true', help='Generate a report of the shared calendar')
#parser.add_argument('-g', '--generate_report', action='store_true', help='Generate a report of the shared calendar')
parser.add_argument('-d', '--dump_json', action='store_true', help='Dump table data to console as json')
parser.add_argument('-g', '--generate_report', action='store', nargs=3, help="Generate a report to console of members OUT events: "+
"<group_name> <start_date> <end_date> with format YYYY-MM-DD")
parser.add_argument('-m', '--manual_update', action='store', nargs=2, help="Manually update the shared calendar with start and end time "+
"with format YYYY-MM-DD")

Expand Down Expand Up @@ -62,9 +64,20 @@ def main(configs):
# Define the msal public client
app = PublicClientApplication(client_id=configs['client_id'], authority=f"https://login.microsoftonline.com/{configs['tenant_id']}")

if args.generate_report:
group_name = args.generate_report[0]
dates = sanitize_input(args.generate_report[1], args.generate_report[2])
start_date = dates[0]
end_date = dates[1]
access_token = utils.acquire_access_token(app, configs['scopes'])
GenerateReport.generate_report_for_specified_group(group_name, start_date, end_date, access_token)
return


count = 0
while True:
if args.update_shared_calendar or args.generate_report:
#if args.update_shared_calendar or args.generate_report:
if args.update_shared_calendar:
logger.info(f"Updating shared calendar -> count: {count}")
today = datetime.today()
start_date = datetime(year=today.year, month=today.month, day=today.day, hour=0,minute=0)
Expand Down Expand Up @@ -98,12 +111,12 @@ def main(configs):
SharedCalendar.update_shared_calendar(individual_calendars_events, shared_calendar_events, event_ids, shared_calendar_id, configs['category_name'], configs['category_color'], access_token)

if args.manual_update: break
if args.generate_report:
shared_calendar_id = SharedCalendar.get_shared_calendar_id(configs['shared_calendar_name'], access_token)
shared_calendar = SharedCalendar.get_shared_calendar(shared_calendar_id, start_date, end_date, access_token)
shared_calendar_events, event_ids = SharedCalendar.process_shared_calendar(shared_calendar, group_members)
GenerateReport.print_table(GenerateReport.filter_simple_events(shared_calendar_events))
break
# if args.generate_report:
# shared_calendar_id = SharedCalendar.get_shared_calendar_id(configs['shared_calendar_name'], access_token)
# shared_calendar = SharedCalendar.get_shared_calendar(shared_calendar_id, start_date, end_date, access_token)
# shared_calendar_events, event_ids = SharedCalendar.process_shared_calendar(shared_calendar, group_members)
# GenerateReport.print_table(shared_calendar_events)
# break

count = count + 1
time.sleep(configs['update_interval'])
Expand Down
2 changes: 1 addition & 1 deletion SharedCalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def get_category(access_token, category_name, category_color):
response = response.json()['value']

for category in response:
if category['displayName'] == category_name:
if category['displayName'].lower() == category_name.lower():
return category_name

return create_category(access_token, category_name, category_color)
Expand Down
4 changes: 3 additions & 1 deletion go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ tag=main

#tag=$(latest_tag)

docker run -it --pull always \
docker run -it
--entrypoint bash \
--pull always \
--mount type=bind,src=$HOME,dst=/home \
$REGISTRY/$REPO:$tag

Expand Down
6 changes: 3 additions & 3 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,22 @@ def get_email_list_from_ldap(group_name):

with ldap3.Connection(ldap_server, ldap_user, ldap_password) as conn:
if not conn.bind():
logger.error("Error: Could not bind to LDAP server")
raise Exception("Error: Could not bind to LDAP server")
else:
for group_name in group_list:
search_filter = f"(cn={group_name})"
#print("search_filter: " + search_filter)
result = conn.search(search_base, search_filter, search_scope, attributes=attributes)
if not result:
logger.error(f"Error: Could not find group {group_name}")
raise KeyError(f"Error: Could not find group {group_name}")
else:
members = [ m.split(',')[0].split('=')[1] for m in conn.entries[0].uniqueMember ]

emails = []
for member in members:
result = conn.search(search_base, f"(uid={member})", search_scope, attributes=attributes)
if not result:
logger.error(f"Error: Could not find member with uid {member}")
raise KeyError(f"Error: Could not find member with uid {member}")
else:
emails.append(str(conn.entries[0].mail))

Expand Down

0 comments on commit 1b79b56

Please sign in to comment.