From f3e820809184fcb1314df14b93e6f6395c263389 Mon Sep 17 00:00:00 2001 From: Martin Mares <mj@ucw.cz> Date: Tue, 23 Nov 2021 12:30:07 +0100 Subject: [PATCH] =?UTF-8?q?Export=20=C5=A1kol=20a=20jejich=20garant=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/export-schools | 78 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 79 insertions(+) create mode 100755 bin/export-schools diff --git a/bin/export-schools b/bin/export-schools new file mode 100755 index 00000000..cc0dc339 --- /dev/null +++ b/bin/export-schools @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +# Přehled všech škol v regionu a jejich garantů + +import argparse +from dataclasses import dataclass +import locale +from sqlalchemy import and_ +from sqlalchemy.orm import joinedload +import sys + +import mo.csv +import mo.db as db +from mo.util import die, init_standalone + +parser = argparse.ArgumentParser(description='Exportuje školy a jejich garanty') +parser.add_argument('--region', type=str, help='omezí export na danou oblast') + +args = parser.parse_args() +init_standalone() +sess = db.get_session() + +if args.region: + reg = db.get_place_by_code(args.region) + if reg is None: + die(f"Oblast {args.region} neexistuje") +else: + reg = db.get_root_place() + +schools = (sess.query(db.School, db.UserRole) + .join(db.RegionDescendant, and_(db.RegionDescendant.region == reg.place_id, db.RegionDescendant.descendant == db.School.place_id)) + .outerjoin(db.UserRole, and_(db.UserRole.role == db.RoleType.garant_skola, db.UserRole.place_id == db.School.place_id)) + .options(joinedload(db.School.place, db.Place.parent_place), joinedload(db.UserRole.user)) + .all()) + +@dataclass +class Row: + obec: str + skola: str + kod_skoly: str + red_izo: str + typ: str + garant: str + garant_email: str + + +def key(item): + s, ur = item + town = locale.strxfrm(s.place.parent_place.name) + school = locale.strxfrm(s.place.name) + garant = ur.user.sort_key() if ur else ("", "") + return town, school, garant + + +output = [] +for s, ur in sorted(schools, key=key): + place = s.place + town = s.place.parent_place + user = ur.user if ur else None + + typy = [] + if s.is_zs: + typy.append('ZŠ') + if s.is_ss: + typy.append('SŠ') + + r = Row( + obec=town.name, + skola=place.name, + kod_skoly=place.get_code(), + red_izo=s.red_izo, + typ='+'.join(typy), + garant=user.full_name() if user else "", + garant_email=user.email if user else "", + ) + + output.append(r) + +mo.csv.write(sys.stdout, mo.csv.FileFormat.en_csv, Row, output) diff --git a/setup.py b/setup.py index 8398fe3e..fc1cecce 100644 --- a/setup.py +++ b/setup.py @@ -14,6 +14,7 @@ setuptools.setup( 'bin/create-tasks', 'bin/create-user', 'bin/export-orgs', + 'bin/export-schools', 'bin/fix-submits', 'bin/import-points', 'bin/merge-users', -- GitLab