def create_points(data): for id, values in data.iteritems(): print values new_point = TrapLocation() exp = Expedition.objects.get(id=values['exp_id']) if exp.grid_square_id is None: square_coords = values['square_number'] exp.grid_square_id = [ g.id for g in GridSquare.objects.all() if g.battleship_coords() == square_coords ][0] new_point.expedition = exp new_point.save() new_point.expedition.save() new_point.whether_a_trap_was_set_here = True new_point.transect_distance = values['distance'] # geography: # TODO some duplicates inside these model methods here.. can be # removed. new_point.set_transect_bearing_wrt_magnetic_north(values['bearing']) new_point.set_actual_lat_long([values['lat'], values['lon']]) new_point.set_suggested_lat_lon_from_mag_north(values['bearing'], values['distance']) new_point.understory = values['understory'] new_point.student_names = values['student_name'] new_point.team_letter = values['team_name'] new_point.team_number = values['trap_number'] new_point.bait_still_there = (values['bait_left'] == 't') if values['animal']: species = Species.objects.get( common_name__icontains=values['animal']) animal = Animal() animal.species = species animal.save() new_point.animal = animal animal.save() if values['habitat'] is not None: habitat = guess_habitat(values) new_point.habitat_id = habitat.id if values['bait'] is not None: bait = guess_bait(values) new_point.bait_id = bait.id new_point.save() test_points.append(new_point)
def setUp(self): self.grade_level = GradeLevel(label="grade_level_here") self.grade_level.save() self.bait = Bait(bait_name="very good nibbles") self.bait.save() self.species = Species( latin_name="official_mouse_name", common_name="blackrock_mouse", about_this_species="too smart for traps", ) self.species.save() self.label_menu = LabelMenu(label="label_here") self.label_menu.save() # left off sex of animal, age of animal, scale used # - foreign keys? other details which were booleans # with default left off self.animal = Animal(species=self.species, description="this is a special mouse", tag_number="6789", health="excellent", weight_in_grams=35) self.animal.save() self.trap = Trap(trap_string="ineffective mouse trap", notes="trap has not gotten any mice yet") self.trap.save() self.habitat = Habitat(label="habitat label", blurb="this is a habitat", image_path_for_legend="/path/here", color_for_map="111") self.habitat.save() self.school = School(name="school", address="school address", contact_1_name="contact 1 for school", contact_1_phone="000-000-0000", contact_1_email="*****@*****.**", contact_2_name="contact 2 for school", contact_2_phone="111-111-1111", contact_2_email="*****@*****.**", notes="this student has appropriate information") self.school.save()
def deal_with_animals(point, rp): # Deal with animals: animal_key = 'animal_%d' % (point.id) if animal_key in rp and rp[animal_key] != 'None': species_id = int(rp[animal_key]) species = Species.objects.get(id=species_id) # TODO (icing ) here we assume that the animal has never been # trapped before. animal = Animal() animal.species = species animal.save() # Note, would be nice to have a foreign key to another Animal # denoting that this Animal is the same actual organism as the # other Animal, but recaptured at a later date and # aidentified by the same tag. # animal = find_already_tagged_animal_in_the_database_somehow() point.animal = animal point.save() animal.save()
def setUp(self): ''' trying to create sighting leaving out attibtutes whic are key or objects - location = objects = species = date = observers = observation_type = how_many_observed = notes =''' self.sighting = Sighting() self.sighting.save() self.expedition = Expedition() self.expedition.save() # objects = models.GeoManager() self.trap_location = TrapLocation( expedition=self.expedition, notes_about_location="Notes about the location", team_letter='C', whether_a_trap_was_set_here=True, bait_still_there=False, notes_about_outcome='This is a good place for a trap.', student_names='Student 1, Student 2, Student 3') self.trap_location.save() self.species = Species( latin_name="official_mouse_name", common_name="blackrock_mouse", about_this_species="too smart for traps", ) self.species.save() # left off sex of animal, age of animal, scale used - foreign keys? # other details which were booleans with default left off self.animal_1 = Animal(species=self.species, description="this is a special mouse", tag_number="6789", health="excellent", weight_in_grams=35) self.animal_1.save() # left off sex of animal, age of animal, scale used - foreign # keys? other details which were booleans with default left off self.animal_2 = Animal(species=self.species, description="this is a naughty mouse", tag_number="8355", health="excellent", weight_in_grams=45) self.animal_2.save() # left off sex of animal, age of animal, scale used - foreign keys? # other details which were booleans with default left off self.animal_3 = Animal(species=self.species, description="this is a super mouse", tag_number="8888", health="excellent", weight_in_grams=15) self.animal_3.save() self.another_expedition = Expedition( start_date_of_expedition=datetime.now(), end_date_of_expedition=datetime.now()) self.another_expedition.save() self.habitat = Habitat(label="habitat label", blurb="this is a habitat", image_path_for_legend="/path/here", color_for_map="111") self.habitat.save() self.trap_1 = TrapLocation(expedition=self.another_expedition, team_letter="A", team_number=1, whether_a_trap_was_set_here=True, bait_still_there=False, habitat=self.habitat, animal=self.animal_1) self.trap_1.save() self.trap_2 = TrapLocation(expedition=self.another_expedition, team_letter="B", team_number=2, whether_a_trap_was_set_here=True, bait_still_there=False, habitat=self.habitat, animal=self.animal_2) self.trap_2.save() self.trap_3 = TrapLocation(expedition=self.another_expedition, team_letter="C", team_number=3, whether_a_trap_was_set_here=True, bait_still_there=False, habitat=self.habitat, animal=self.animal_3) self.trap_3.save() self.trap_4 = TrapLocation(expedition=self.another_expedition, team_letter="D", team_number=4, whether_a_trap_was_set_here=True, bait_still_there=False, habitat=self.habitat) self.trap_4.save() self.another_expedition.save() self.timed_expedition = Expedition( end_date_of_expedition=datetime.now()) self.timed_expedition.save() self.time_trap = TrapLocation(expedition=self.timed_expedition, team_letter="team name here", team_number=6, habitat=self.habitat, whether_a_trap_was_set_here=True, bait_still_there=False, animal=self.animal_1) self.time_trap.save() self.new_sighting = Sighting(species=self.species, date=datetime.now()) self.new_sighting.save() self.grid_point_nw = GridPoint() self.grid_point_nw.save() self.grid_point_ne = GridPoint() self.grid_point_ne.save() self.grid_point_sw = GridPoint() self.grid_point_sw.save() self.grid_point_se = GridPoint() self.grid_point_se.save() self.grid_point_center = GridPoint() self.grid_point_center.save() self.grid_square = GridSquare(NW_corner=self.grid_point_nw, NE_corner=self.grid_point_ne, SW_corner=self.grid_point_sw, SE_corner=self.grid_point_se, center=self.grid_point_center, display_this_square=False, row=1, column=0, access_difficulty=3, terrain_difficulty=4) self.grid_square.save()
def process_save_team_form(request): if request.method != 'POST': return HttpResponseRedirect('/mammals/all_expeditions/') rp = request.POST expedition_id = rp['expedition_id'] exp = Expedition.objects.get(id=expedition_id) team_letter = rp['team_letter'] the_team_points = exp.team_points(team_letter) form_map = { 'habitat': 'habitat_id', 'bait': 'bait_id', 'trap_type': 'trap_type_id' } form_map_booleans = { 'whether_a_trap_was_set_here': 'whether_a_trap_was_set_here', 'bait_still_there': 'bait_still_there' } for point in the_team_points: if 'student_names' in rp: point.student_names = rp['student_names'] point.save() for the_key, thing_to_update in form_map.iteritems(): rp_key = '%s_%d' % (the_key, point.id) if rp_key in rp and rp[rp_key] != 'None': setattr(point, '%s' % thing_to_update, rp[rp_key]) point.save() for the_key, thing_to_update in form_map_booleans.iteritems(): rp_key = '%s_%d' % (the_key, point.id) if rp_key in rp and rp[rp_key] != 'None': setattr(point, '%s' % thing_to_update, (rp[rp_key] == 'True')) point.save() rp_key = '%s_%d' % ('understory', point.id) if rp_key in rp and rp[rp_key] != 'None': point.understory = rp[rp_key] point.save() rp_key = '%s_%d' % ('notes_about_location', point.id) if rp_key in rp and rp[rp_key] != 'None': point.notes_about_location = rp[rp_key] point.save() # Deal with animals: animal_key = 'animal_%d' % (point.id) if animal_key in rp and rp[animal_key] != 'None': species_id = int(rp[animal_key]) species = Species.objects.get(id=species_id) # TODO (icing ) here we assume that the animal has never been # trapped before. animal_never_trapped_before = True if animal_never_trapped_before: animal = Animal() animal.species = species animal.save() else: # Note, would be nice to have a foreign key to another Animal # denoting that this Animal is the same actual organism as the # other Animal, but recaptured at a later date and # aidentified by the same tag. # animal = find_already_tagged_animal_in_the_database_somehow() pass point.animal = animal point.save() animal.save() # Deal with actual latitude and longitude: # The burden of providing good data here rests on the front end. # If the actual lat and lon don't meet our data standards, we're simply # not going to use them. # 1) Do they both exist? # 2) Are they both accurate to 5 decimals? # 3) Are they within a certain distance of the original lat and lon (no # interest in points in another country. correcting_lat_lon = False match_string = '(\-)?(\d){2}\.(\d){5}' # if your coordinate string doesn't match the above, you have a nice # day. lat_key = 'actual_lat_%d' % point.id lon_key = 'actual_lon_%d' % point.id max_diff = 250.0 # meters min_diff = 1.0 # meters correcting_lat_lon = True if correcting_lat_lon and lat_key not in rp: correcting_lat_lon = False if correcting_lat_lon and lon_key not in rp: correcting_lat_lon = False if correcting_lat_lon and match(match_string, rp[lat_key]) is None: correcting_lat_lon = False if correcting_lat_lon and match(match_string, rp[lon_key]) is None: correcting_lat_lon = False if correcting_lat_lon: diff_lat = point.actual_lat() - float(rp[lat_key]) diff_lon = point.actual_lon() - float(rp[lon_key]) distance_to_corrected_point_in_meters = hypotenuse( *to_meters(diff_lat, diff_lon)) if distance_to_corrected_point_in_meters > max_diff: correcting_lat_lon = False # distance_to_corrected_point_in_meters if (correcting_lat_lon and distance_to_corrected_point_in_meters < min_diff): correcting_lat_lon = False # distance_to_corrected_point_in_meters if correcting_lat_lon: point.set_actual_lat_long( [float(rp[lat_key]), float(rp[lon_key])]) point.save()