Skip to content
Snippets Groups Projects
Commit fbebd0e1 authored by Martin Mareš's avatar Martin Mareš
Browse files

Export škol a jejich garantů

parent e90c0c5e
No related branches found
No related tags found
No related merge requests found
#!/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('')
if s.is_ss:
typy.append('')
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)
...@@ -14,6 +14,7 @@ setuptools.setup( ...@@ -14,6 +14,7 @@ setuptools.setup(
'bin/create-tasks', 'bin/create-tasks',
'bin/create-user', 'bin/create-user',
'bin/export-orgs', 'bin/export-orgs',
'bin/export-schools',
'bin/fix-submits', 'bin/fix-submits',
'bin/import-points', 'bin/import-points',
'bin/merge-users', 'bin/merge-users',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment