def set_cull_level(request, stage: Stage): """set what level a cull should happen from :param request: expecting POST parameters:\n cull_number: value to set CullStage.number at :param stage: Stage of type cull to set :return: JSONResponse with either success or an error message """ cull_stage = stage.cullstage_set.first() if stage.state == Stage.NOT_STARTED: try: input_size = len(stage.input()) except Stage.NotCompleteError: return api_failure("previous stage incomplete") number = int(request.POST['cull_number']) if 2 < number < input_size: cull_stage.number = number stage.state = Stage.READY cull_stage.save() stage.save() return api_success() else: return api_failure( "invalid cull number", "cull number must be > 2 and < size of stages input ") else: return api_failure("incorrect state", "stage not in ready state")
def add_entries(request, stage: Stage): """add entries and set stage as ready entries will be ordered according to their seed :param request: expecting POST parameters:\n :param list of int ids: list of entry ids to add :param stage: Stage of type add :return: JSONResponse with either success or an error message """ add_stage = stage.addstage_set.first() if stage.state != Stage.NOT_STARTED: return api_failure("incorrect state", _("Stage not in NOT_STARTED state")) ids = request.POST.getlist('ids') entries = stage.competition.entry_set.filter(pk__in=ids) if len(entries) != len(ids) or not set(entries.all()).issubset( add_stage.possible_additions()): return api_failure( 'bad_entry', _('one of these entries has already been added or is not in this competition' )) additions = [] for y, equal_fencers in groupby(entries.order_by('seed'), lambda x: x.seed): additions.append(list(equal_fencers)) add_stage.add_entries(additions) stage.state = Stage.READY stage.save() return api_success()
def finish_stage(request, stage: Stage) -> JsonResponse: """mark a finished de_stage as finished is it is complete""" if stage.state != Stage.STARTED: return api_failure("invalid state", _("Stage not currently running")) de_stage = stage.destage_set.first() # type: DeStage if de_stage.detable_set.filter(complete=False).exists(): return api_failure('stage not finished', _('Stage not finished')) stage.state = Stage.FINISHED stage.save() return api_success()
def confirm_add(stage: Stage): """advances a stage from READY -> FINISHED :param request: expects type: "confirm_add" :param stage: Stage of type add :return: JSONResponse with either success or an error message """ if stage.state != Stage.READY: return api_failure("incorrect state", _("Stage not in READY state")) stage.state = Stage.FINISHED stage.save() return api_success()
def start_stage(request, stage: Stage) -> JsonResponse: """start a not running de stage and generate its head table""" if stage.state != Stage.NOT_STARTED: return api_failure("invalid state", "Stage has already started") de_stage = stage.destage_set.first() # type: DeStage try: de_stage.start() except Stage.NotCompleteError: return api_failure('previous stage incomplete', 'Previous stage not finished yet') stage.state = Stage.STARTED stage.save() return api_success()
def confirm_cull(request, stage: Stage): """confirm a cull :param request: no additional parameters :param stage: Stage of type cull to set :return: JSONResponse with either success or an error message """ cull_stage = stage.cullstage_set.first() if stage.state == Stage.NOT_STARTED: return api_failure('number not set') elif stage.state == Stage.READY: stage.state = Stage.FINISHED stage.save() return api_success() else: return api_failure("stage already complete")