Ejemplo n.º 1
0
 def test_organization_add(self):
     nr_nodes = len(self.ns.get_nodes())
     # This function tests the organization.
     name = "Dwars door Hillesheim"
     city = "Hillesheim_X"
     ds_str = "1963-07-02"
     ds = datetime.datetime.strptime(ds_str, "%Y-%m-%d")
     org_dict = dict(name=name, location=city, datestamp=ds, org_type=False)
     org = mg.Organization()
     self.assertTrue(org.add(**org_dict))
     org_nid = org.get_nid()
     self.assertTrue(isinstance(org_nid, str))
     self.assertTrue(isinstance(org.get_node(), Node))
     # Test Location
     loc = org.get_location()
     self.assertTrue(isinstance(loc, Node))
     self.assertEqual(loc["city"], city)
     # Test Datestamp
     self.assertEqual(org.get_date()["key"], ds_str)
     # Test name
     self.assertEqual(org.get_name(), name)
     # Test label
     self.assertTrue(isinstance(org.get_label(), str))
     # Test Type organizatie
     self.assertEqual(org.get_type(), "Wedstrijd")
     mg.organization_delete(org_id=org_nid)
     self.assertFalse(
         mg.get_location(loc["nid"]),
         "Location is removed as part of Organization removal")
     self.assertEqual(nr_nodes, len(self.ns.get_nodes()))
Ejemplo n.º 2
0
 def test_organization_participants(self):
     """
     This method will test the participants method of the Organization object.
     """
     # Find organization for Lilse Bergen
     props = dict(name="Lilse Bergen")
     org_node = self.ns.get_node(lbl_organization, **props)
     org = mg.Organization(org_id=org_node["nid"])
     participants = org.get_participants()
     for part_node in participants:
         self.assertTrue(part_node, Node)
Ejemplo n.º 3
0
def participant_down(race_id, pers_id):
    """
    This method will move the participant down one position and return to the race.

    :param race_id: ID of the race. This can be calculated, but it is always available.
    :param pers_id: Node ID of the participant in the race.
    :return:
    """
    part = mg.Participant(race_id=race_id, person_id=pers_id)
    part.down()
    org = mg.Organization(race_id=race_id)
    org.calculate_points()
    return redirect(url_for('main.participant_add', race_id=race_id))
Ejemplo n.º 4
0
def organization_create():
    """
    This method will create an organization for the purpose of testing other methods.

    :return: organization object
    """
    name = "Dwars door Hillesheim"
    city = "Hillesheim_X"
    ds_str = "1963-07-02"
    ds = datetime.datetime.strptime(ds_str, "%Y-%m-%d")
    org_dict = dict(name=name, location=city, datestamp=ds, org_type=False)
    org = mg.Organization()
    org.add(**org_dict)
    return org
Ejemplo n.º 5
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))
Ejemplo n.º 6
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))
Ejemplo n.º 7
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))
Ejemplo n.º 8
0
 def test_organization_edit(self):
     nr_nodes = len(self.ns.get_nodes())
     # This function tests the organization Edit.
     name = "Dwars door Hillesheim"
     city = "Hillesheim_X"
     ds_str = "1963-07-02"
     ds = datetime.datetime.strptime(ds_str, "%Y-%m-%d")
     org_dict = dict(name=name, location=city, datestamp=ds, org_type=False)
     org = mg.Organization()
     # Test organization is created.
     self.assertTrue(org.add(**org_dict))
     org_nid = org.get_nid()
     self.assertTrue(isinstance(org_nid, str))
     # Now update organization
     # Update organization
     name = "Rondom Berndorf"
     city = "Berndorf-Y"
     ds_str = "1964-10-28"
     ds = datetime.datetime.strptime(ds_str, "%Y-%m-%d")
     org_dict = dict(name=name, location=city, datestamp=ds, org_type=True)
     org.edit(**org_dict)
     # Test Location
     loc = org.get_location()
     loc_nid = loc["nid"]
     self.assertEqual(loc["city"], city)
     self.assertTrue(mg.get_location(loc_nid), "Location is available")
     # Test Datestamp
     self.assertEqual(org.get_date()["key"], ds_str)
     # Test name
     self.assertEqual(org.get_name(), name)
     # Test label
     self.assertTrue(isinstance(org.get_label(), str))
     # Test Type organizatie
     self.assertEqual(org.get_type(), "Deelname")
     mg.organization_delete(org_id=org_nid)
     self.assertFalse(
         mg.get_location(loc_nid),
         "Location is removed as part of Organization removal")
     self.assertEqual(nr_nodes, len(self.ns.get_nodes()),
                      "Number of end nodes not equal to start nodes")
Ejemplo n.º 9
0
def organization_add(org_id=None):
    if request.method == "POST":
        form = OrganizationAdd()
        # if form.validate_on_submit(): Form Validate doesn't work on SelectField with choices not defined.
        city = mg.get_location(form.location.data)
        org_dict = dict(name=form.name.data,
                        location=city,
                        datestamp=form.datestamp.data,
                        org_type=form.org_type.data)
        if org_id:
            current_app.logger.debug(
                "Modify organisation with ID {id}".format(id=org_id))
            org = mg.Organization(org_id=org_id)
            if org.edit(**org_dict):
                flash(org_dict["name"] + ' aangepast.', "success")
            else:
                flash(org_dict["name"] + ' bestaat reeds.', "warning")
        else:
            current_app.logger.debug(
                "Ready to add organization with values {v}".format(v=org_dict))
            if mg.Organization().add(**org_dict):
                flash(org_dict["name"] + ' toegevoegd als organizatie',
                      "success")
            else:
                flash(org_dict["name"] + ' bestaat reeds, niet toegevoegd.',
                      "warning")
        # Form validated successfully, clear fields!
        return redirect(url_for('main.organization_list'))
    # Request Method is GET or Form did not validate
    # Note that in case Form did not validate, the fields will be reset.
    # But how can we fail on form_validate?
    organizations = mg.organization_list()
    if org_id:
        current_app.logger.debug(
            "Get Form to edit organization with ID: {id}".format(id=org_id))
        org = mg.Organization(org_id=org_id)
        name = org.get_name()
        location = org.get_location()["nid"]
        datestamp = org.get_date()["key"]
        org_type = org.get_type()
        if org_type == "Deelname":
            org_type_flag = True
        else:
            org_type_flag = False
        form = OrganizationAdd(org_type=org_type_flag)
        form.name.data = name
        form.location.data = location
        form.datestamp.data = my_env.datestr2date(datestamp)
    else:
        current_app.logger.debug("Get Form to add organization")
        name = None
        location = None
        datestamp = None
        form = OrganizationAdd()
    # Form did not validate successfully, keep fields.
    form.location.choices = mg.get_location_list()
    return render_template('organization_add.html',
                           form=form,
                           name=name,
                           location=location,
                           datestamp=datestamp,
                           organizations=organizations)