2024-02-07 01:26:49 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
2020-05-14 13:16:10 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2021-08-02 16:18:53 +00:00
|
|
|
from argparse import ArgumentParser, FileType
|
2020-05-14 13:16:10 +00:00
|
|
|
|
|
|
|
from django.core.management import BaseCommand
|
2021-08-02 16:18:53 +00:00
|
|
|
from django.db import transaction
|
|
|
|
|
2021-09-02 11:44:18 +00:00
|
|
|
from ...forms import CurrentSurvey
|
2020-05-14 13:16:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Attribute to each first year member a bus for the WEI"
|
|
|
|
|
2021-08-02 16:18:53 +00:00
|
|
|
def add_arguments(self, parser: ArgumentParser):
|
|
|
|
parser.add_argument('--doit', '-d', action='store_true', help='Finally run the algorithm in non-dry mode.')
|
|
|
|
parser.add_argument('--output', '-o', nargs='?', type=FileType('w'), default=self.stdout,
|
|
|
|
help='Output file for the algorithm result. Default is standard output.')
|
|
|
|
|
|
|
|
@transaction.atomic
|
2020-05-14 13:16:10 +00:00
|
|
|
def handle(self, *args, **options):
|
|
|
|
"""
|
|
|
|
Run the WEI algorithm to attribute a bus to each first year member.
|
|
|
|
"""
|
2021-08-02 16:18:53 +00:00
|
|
|
sid = transaction.savepoint()
|
|
|
|
|
|
|
|
algorithm = CurrentSurvey.get_algorithm_class()()
|
2021-09-16 18:46:34 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from tqdm import tqdm
|
2021-09-27 11:59:43 +00:00
|
|
|
del tqdm
|
2021-09-16 18:46:34 +00:00
|
|
|
display_tqdm = True
|
|
|
|
except ImportError:
|
|
|
|
display_tqdm = False
|
|
|
|
|
|
|
|
algorithm.run_algorithm(display_tqdm=display_tqdm)
|
2021-08-02 16:18:53 +00:00
|
|
|
|
|
|
|
output = options['output']
|
|
|
|
registrations = algorithm.get_registrations()
|
2021-09-05 18:36:17 +00:00
|
|
|
per_bus = {bus: [r for r in registrations if 'selected_bus_pk' in r.information
|
|
|
|
and r.information['selected_bus_pk'] == bus.pk]
|
2021-08-02 16:18:53 +00:00
|
|
|
for bus in algorithm.get_buses()}
|
|
|
|
for bus, members in per_bus.items():
|
|
|
|
output.write(bus.name + "\n")
|
|
|
|
output.write("=" * len(bus.name) + "\n")
|
2021-09-27 11:59:43 +00:00
|
|
|
_order = -1
|
2021-08-02 16:18:53 +00:00
|
|
|
for r in members:
|
2021-09-16 18:46:34 +00:00
|
|
|
survey = CurrentSurvey(r)
|
2021-09-27 11:59:43 +00:00
|
|
|
for _order, (b, _score) in enumerate(survey.ordered_buses()):
|
2021-09-16 18:46:34 +00:00
|
|
|
if b == bus:
|
|
|
|
break
|
2021-09-27 11:59:43 +00:00
|
|
|
output.write(f"{r.user.username} ({_order + 1})\n")
|
2021-08-02 16:18:53 +00:00
|
|
|
output.write("\n")
|
|
|
|
|
|
|
|
if not options['doit']:
|
|
|
|
self.stderr.write(self.style.WARNING("Running in dry mode. "
|
|
|
|
"Use --doit option to really execute the algorithm."))
|
|
|
|
transaction.savepoint_rollback(sid)
|
|
|
|
return
|