예제 #1
0
 def test_race(self):
     org = organization_create()
     race1 = mg.Race(org_id=org.get_nid())
     props = dict(name="16k", type="Hoofdwedstrijd")
     race1.add(**props)
     self.assertEqual(race1.get_racetype(), "Hoofdwedstrijd")
     # Create new race, set to 'Hoofdwedstrijd'. Race 1 needs to change to Nevenwedstrijd.
     race2 = mg.Race(org_id=org.get_nid())
     props = dict(name="14k", type="Hoofdwedstrijd")
     race2.add(**props)
     self.assertEqual(race2.get_racetype(), "Hoofdwedstrijd")
     self.assertEqual(race1.get_racetype(), "Nevenwedstrijd")
     mg.race_delete(race1.get_nid())
     mg.race_delete(race2.get_nid())
     organization_delete(org=org)
예제 #2
0
def race_add(org_id, race_id=None):
    """
    This method allows to add or edit a race. Race name is optional.

    :param org_id: nid of the organization to which the race is added.
    :param race_id:  nid of the race if edit is required.
    :return:
    """
    org = mg.Organization(org_id=org_id)
    org_type = org.get_type()
    form = RaceAdd()
    if org_type == "Deelname":
        del form.raceType
    if request.method == "GET":
        race_add_attribs = mg.get_race_list_attribs(org_id)
        if race_id:
            race = mg.Race(race_id=race_id)
            form.name.data = race.get_name()
            if org_type == "Wedstrijd":
                form.raceType.data = race.get_racetype()
        else:
            # New race: set type to Nevenwedstrijd if there is a hoofdwedstrijd (default) already.
            if org.get_race_main():
                form.raceType.data = def_nevenwedstrijd
        race_add_attribs['form'] = form
        return render_template('race_list.html', **race_add_attribs)
    else:
        # Method is Post
        params = {'name': form.name.data}
        try:
            params['type'] = form.raceType.data
        except AttributeError:
            # In case of Deelname form.raceType does not exist so cannot have an attribute .data
            pass
        if race_id:
            mg.Race(race_id=race_id).edit(**params)
        else:
            mg.Race(org_id).add(**params)
        # Form validated successfully, clear fields!
        return redirect(url_for('main.race_list', org_id=org_id))
예제 #3
0
def participant_list(race_id):
    """
    This method will show the participants in sequence of arrival for a race.
    :param race_id:
    :return:
    """
    race = mg.Race(race_id=race_id)
    param_dict = dict(race_label=race.get_label(),
                      org_id=race.get_org_id(),
                      race_id=race_id)
    finishers = race.part_person_seq_list()
    if finishers:
        param_dict['finishers'] = finishers
    return render_template('participant_list.html', **param_dict)
예제 #4
0
def participant_add(race_id):
    """
    This method will add a person to a race. The previous runner (earlier arrival) is selected from drop-down list.
    By default the participant is appended as the last position in the race, so the previous person was the last one in
    the race. First position is specified as previous runner equals -1.

    :param race_id: ID of the race.
    :return: The person added or modified as a participant to the race.
    """
    race = mg.Race(race_id=race_id)
    race_label = race.get_label()
    if request.method == "GET":
        # Get method, initialize page.
        org_id = race.get_org_id()
        part_last = race.part_person_last_id()
        # Initialize Form
        form = ParticipantAdd(prev_runner=part_last)
        next_part_nodes = race.get_next_part()
        form.name.choices = [(x["person"]["nid"], x["person"]["name"])
                             for x in next_part_nodes]
        form.prev_runner.choices = race.part_person_after_list()
        param_dict = dict(form=form,
                          race_id=race_id,
                          race_label=race_label,
                          org_id=org_id)
        finishers = race.part_person_seq_list()
        if finishers:
            param_dict['finishers'] = finishers
        return render_template('participant_add.html', **param_dict)
    else:
        # Call form to get input values
        form = ParticipantAdd()
        # Add collected info as participant to race.
        runner_id = form.name.data
        prev_runner_id = form.prev_runner.data
        # Create the participant node, connect to person and to race.
        part = mg.Participant(race_id=race_id, person_id=runner_id)
        part.add(prev_person_id=prev_runner_id)
        # Collect properties for this participant so that they can be added to the participant node.
        props = {}
        for prop in part_config_props:
            if form.data[prop]:
                props[prop] = form.data[prop]
        part.set_props(**props)
        # Get organization, recalculate points
        org = mg.Organization(org_id=race.get_org_id())
        org.calculate_points()
        return redirect(url_for('main.participant_add', race_id=race_id))
예제 #5
0
def participant_edit(part_id):
    """
    This method allows to edit participant properties.

    :param part_id: NID of the participant. This is sufficient to find Person and Race objects.
    :return:
    """
    part = mg.Participant(part_id=part_id)
    person_nid = part.get_person_nid()
    person = mg.Person(person_id=person_nid)
    person_dict = person.get_dict()
    race_id = part.get_race_nid()
    race = mg.Race(race_id=race_id)
    race_label = race.get_label()
    if request.method == "GET":
        # Get method, initialize page.
        org = mg.Organization(race_id=race_id)
        org_id = org.get_nid()
        # Initialize Form, populate with keyword arguments
        # (http://wtforms.readthedocs.io/en/latest/crash_course.html#how-forms-get-data)
        part_props = part.get_props()
        form = ParticipantEdit(**part_props)
        finishers = race.part_person_seq_list()
        # There must be finishers, since I can update one of them
        return render_template('participant_edit.html',
                               form=form,
                               race_id=race_id,
                               finishers=finishers,
                               race_label=race_label,
                               org_id=org_id,
                               person=person_dict)
    else:
        # Call form to get input values
        form = ParticipantEdit()
        # Collect properties for this participant
        props = {}
        for prop in part_config_props:
            try:
                props[prop] = form.data[prop]
            except KeyError:
                pass
        part.set_props(**props)
        return redirect(url_for('main.participant_add', race_id=race_id))
예제 #6
0
def race_delete(race_id):
    """
    This method will delete an existing race. This can be done only if there are no participants attached to the
    race.

    :param race_id: The Node ID of the race.
    :return: True if the race is removed, False otherwise.
    """
    current_app.logger.debug("Delete race {race_id}".format(race_id=race_id))
    race = mg.Race(race_id=race_id)
    org_id = race.get_org_id()
    if mg.race_delete(race_id=race_id):
        flash("Wedstrijd verwijderd.", "info")
        return redirect(url_for('main.race_list', org_id=org_id))
    else:
        flash(
            "Wedstrijd niet verwijderd, er zijn nog deelnemers mee verbonden.",
            "warning")
        return redirect(url_for('main.participant_add', race_id=race_id))