Skip to content

Commit

Permalink
rewrite something
Browse files Browse the repository at this point in the history
  • Loading branch information
Kakadus committed Oct 9, 2023
1 parent 1fa6d46 commit ff58fe8
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions tools/enrollment_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,62 @@ class User:
email: str


def fix_users(users: dict[str, User], title: Cell | None, last_name: Cell, first_name: Cell, email: Cell):
imported = User(last_name=last_name.value, first_name=first_name.value, email=email.value, title=title.value or "" if title else "") # type: ignore[arg-type] # if schema is correct, all values are strings
@dataclass
class UserCells:
title: Cell | None
last_name: Cell
first_name: Cell
email: Cell

def value(self):
return User(
self.title.value if self.title else "", self.last_name.value, self.first_name.value, self.email.value
)


def user_decision(field: str, existing: str, imported: str) -> str:
if existing == imported:
return existing
decision = input(f"Do you want to keep the existing user {field}? (y/n) ")
if decision and decision[0].lower() == "n":
return imported
return existing


def fix_users(users: dict[str, User], imported_cells: UserCells):
imported = imported_cells.value()
existing = users.setdefault(imported.email, imported)
if existing != imported:
print("There is a conflict in the user data.")
print(f"existing: {existing}.")
print(f"imported: {imported}.")
if input("Do you want to keep the existing user? (y/n)")[0].lower() == "n":
return
if imported == existing:
return
print("There is a conflict in the user data.")
print(f"existing: {existing}.")
print(f"imported: {imported}.")

email.value = existing.email
last_name.value = existing.last_name
first_name.value = existing.first_name
if title:
title.value = existing.title
if imported_cells.title:
imported_cells.title.value = user_decision("title", existing.title, imported.title)
imported_cells.last_name.value = user_decision("last name", existing.last_name, imported.last_name)
imported_cells.first_name.value = user_decision("first name", existing.first_name, imported.first_name)
imported_cells.email.value = user_decision("email", existing.email, imported.email)


if __name__ == "__main__":
parser = ArgumentParser(description="Commandline tool to preprocess enrollment xlsx files.")
parser.add_argument(
"-ud", "--user-data", help="Path to a csv file containing an export of all existing users.", required=True
"-u", "--user-data", help="Path to a csv file containing an export of all existing users.", required=True
)
parser.add_argument(
"-ed", "--enrollment-data", help="Path to the enrollment data in xlsx format for import.", required=True
"-e", "--enrollment-data", help="Path to the enrollment data in xlsx format for import.", required=True
)
ns = parser.parse_args(sys.argv[1:])

workbook = load_workbook(ns.enrollment_data)
user_dict = {}
users = {}
with open(ns.user_data, encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(*row)
user_dict[row[0]] = User(*row)
users[row[-1]] = User(*row)
for sheet_name in ["MA Belegungen", "BA Belegungen"]:
for row in workbook[sheet_name].iter_rows(min_row=2, min_col=2):
fix_users(user_dict, None, *row[:3])
fix_users(user_dict, *row[7:])
fix_users(users, UserCells(None, *row[:3]))
fix_users(users, UserCells(*row[7:]))
workbook.save(ns.enrollment_data)

0 comments on commit ff58fe8

Please sign in to comment.