|
| 1 | +"""This module gets called by cron every day""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | + |
| 6 | +import github |
| 7 | +import google_api |
| 8 | +import utils |
| 9 | +from database import _db_session |
| 10 | +from officers.crud import all_officer_terms, get_user_by_username, officer_terms |
| 11 | + |
| 12 | +_logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +async def update_google_permissions(db_session): |
| 15 | + # TODO: implement this function |
| 16 | + # google_permissions = google_api.all_permissions() |
| 17 | + # one_year_ago = datetime.today() - timedelta(days=365) |
| 18 | + |
| 19 | + # TODO: for performance, only include officers with recent end-date (1 yr) |
| 20 | + # but measure performance first |
| 21 | + for term in await all_officer_terms(db_session): |
| 22 | + if utils.is_active(term): |
| 23 | + # TODO: if google drive permission is not active, update them |
| 24 | + pass |
| 25 | + else: |
| 26 | + # TODO: if google drive permissions are active, remove them |
| 27 | + pass |
| 28 | + |
| 29 | + _logger.info("updated google permissions") |
| 30 | + |
| 31 | +async def update_github_permissions(db_session): |
| 32 | + github_permissions, team_id_map = github.all_permissions() |
| 33 | + |
| 34 | + for term in await all_officer_terms(db_session): |
| 35 | + new_teams = ( |
| 36 | + # move all active officers to their respective teams |
| 37 | + github.officer_teams(term.position) |
| 38 | + if utils.is_active(term) |
| 39 | + # move all inactive officers to the past_officers github organization |
| 40 | + else ["past_officers"] |
| 41 | + ) |
| 42 | + if term.username not in github_permissions: |
| 43 | + user = get_user_by_username(term.username) |
| 44 | + github.invite_user( |
| 45 | + user.id, |
| 46 | + [team_id_map[team] for team in new_teams], |
| 47 | + ) |
| 48 | + else: |
| 49 | + github.set_user_teams( |
| 50 | + term.username, |
| 51 | + github_permissions[term.username].teams, |
| 52 | + new_teams |
| 53 | + ) |
| 54 | + |
| 55 | + _logger.info("updated github permissions") |
| 56 | + |
| 57 | +async def update_permissions(): |
| 58 | + db_session = _db_session() |
| 59 | + |
| 60 | + update_google_permissions(db_session) |
| 61 | + db_session.commit() |
| 62 | + update_github_permissions(db_session) |
| 63 | + db_session.commit() |
| 64 | + |
| 65 | + _logger.info("all permissions updated") |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + asyncio.run(update_permissions()) |
| 69 | + |
0 commit comments