def row_parser(self, row): super().row_parser(row) row_datetime = utils.get_row_date(row) row_date = row_datetime.date() if utils.nan_to_none(row.get(self.box_key)): self.row_entered += utils.enter_indvd(self.anix_indv.pk, self.cleaned_data, row_date, row[self.box_key], self.box_anidc_id.pk, None) if utils.nan_to_none(row.get(self.location_key)): self.row_entered += utils.enter_indvd(self.anix_indv.pk, self.cleaned_data, row_date, row[self.location_key], self.boxl_anidc_id.pk, None) if utils.nan_to_none(row.get(self.indt_key)) and utils.nan_to_none( row.get(self.indt_amt_key)): indvtc_id = models.IndTreatCode.objects.filter( name__icontains=row[self.indt_key]).get() unit_id = models.UnitCode.objects.filter( name__icontains="gram").order_by(Length('name').asc()).first() self.row_entered += utils.enter_indvt(self.anix_indv.pk, self.cleaned_data, row_datetime, row[self.indt_amt_key], indvtc_id.pk, unit_id=unit_id)
def clean(self): cleaned_data = super().clean() cleaned_data["mort_date"] = utils.naive_to_aware( cleaned_data["mort_date"]) if not self.is_valid(): return cleaned_data # grab an event for facility info, etc. if cleaned_data["indv_mort"]: cleaned_data["evnt_id"] = models.AniDetailXref.objects.filter( indv_id_id=cleaned_data["indv_mort"]).last().evnt_id mort_evntc = models.EventCode.objects.filter( name="Mortality").get() else: cleaned_data["evnt_id"] = models.AniDetailXref.objects.filter( grp_id_id=cleaned_data["grp_mort"]).last().evnt_id mort_evntc = models.EventCode.objects.filter( name="Mortality").get() mortality_evnt = models.Event( evntc_id=mort_evntc, facic_id=cleaned_data["evnt_id"].facic_id, prog_id=cleaned_data["evnt_id"].prog_id, perc_id=cleaned_data["evnt_id"].perc_id, start_datetime=cleaned_data["mort_date"], end_datetime=cleaned_data["mort_date"], created_by=cleaned_data["created_by"], created_date=cleaned_data["created_date"], ) try: mortality_evnt.clean() mortality_evnt.save() except (ValidationError, IntegrityError): mortality_evnt = models.Event.objects.filter( evntc_id=mortality_evnt.evntc_id, facic_id=mortality_evnt.facic_id, prog_id=mortality_evnt.prog_id, start_datetime=mortality_evnt.start_datetime, end_datetime=mortality_evnt.end_datetime, ).get() cleaned_data["evnt_id"] = mortality_evnt cleaned_data["facic_id"] = mortality_evnt.facic_id if cleaned_data["indv_mort"]: indv = models.Individual.objects.filter( pk=cleaned_data["indv_mort"]).get() indv.indv_valid = False indv.save() anix, mort_entered = utils.enter_mortality( indv, cleaned_data, cleaned_data["mort_date"]) utils.enter_bulk_indvd( anix.pk, cleaned_data, cleaned_data["mort_date"], len_val=cleaned_data["indv_length"], weight=cleaned_data["indv_mass"], vial=cleaned_data["indv_vial"], scale_envelope=cleaned_data["scale_envelope"], gender=cleaned_data["indv_gender"], ) if cleaned_data["observations"].count() != 0: for adsc in cleaned_data["observations"]: utils.enter_indvd(anix.pk, cleaned_data, cleaned_data["mort_date"], None, adsc.anidc_id.pk, adsc.name, None) if cleaned_data["grp_mort"]: grp = models.Group.objects.filter( pk=cleaned_data["grp_mort"]).get() salmon_pk = models.SpeciesCode.objects.filter( name__icontains="Salmon").get().pk mort_sampc = models.SampleCode.objects.filter( name="Mortality Sample").get().pk cont = grp.current_cont(at_date=cleaned_data["mort_date"])[0] # create contx, link to grp and samp: contx, contx_entered = utils.enter_contx(cont, cleaned_data, None, return_contx=True) samp_anix, anix_entered = utils.enter_anix(cleaned_data, grp_pk=grp.pk, contx_pk=contx.pk) samp_id, samp_entered = utils.enter_samp(cleaned_data, cleaned_data["samp_num"], salmon_pk, mort_sampc, anix_pk=samp_anix.pk) mort_entered = utils.enter_samp_mortality( samp_id, cleaned_data, cleaned_data["mort_date"]) utils.enter_bulk_sampd( samp_id.pk, cleaned_data, cleaned_data["mort_date"], len_val=cleaned_data["indv_length"], weight=cleaned_data["indv_mass"], vial=cleaned_data["indv_vial"], scale_envelope=cleaned_data["scale_envelope"], gender=cleaned_data["indv_gender"], )
def row_parser(self, row): cleaned_data = self.cleaned_data row_datetime = utils.get_row_date(row) relc_id = self.site_dict[row[self.site_key]] year, coll = utils.year_coll_splitter(row[self.coll_key]) coll_id = utils.coll_getter(coll) stok_id = models.StockCode.objects.filter(name__iexact=relc_id.rive_id.name).get() indv_id = None if utils.nan_to_none(row[self.pit_key]): indv_id = models.Individual.objects.filter(pit_tag=row[self.pit_key]).first() if not indv_id: indv_id = models.Individual(spec_id=self.salmon_id, stok_id=stok_id, coll_id=coll_id, indv_year=year, pit_tag=row[self.pit_key], indv_valid=True, comments=utils.nan_to_none(row.get(self.comment_key)), created_by=cleaned_data["created_by"], created_date=cleaned_data["created_date"], ) try: indv_id.clean() indv_id.save() self.row_entered = True except (ValidationError, IntegrityError): indv_id = models.Individual.objects.filter(pit_tag=indv_id.pit_tag).get() indv_anix, data_entered = utils.enter_anix(cleaned_data, indv_pk=indv_id.pk) self.row_entered += data_entered # add program group to individual if needed: loc = models.Location(evnt_id_id=cleaned_data["evnt_id"].pk, locc_id=self.locc_id, rive_id=relc_id.rive_id, relc_id=relc_id, loc_date=row_datetime, created_by=cleaned_data["created_by"], created_date=cleaned_data["created_date"], ) try: loc.clean() loc.save() self.row_entered = True except ValidationError: loc = models.Location.objects.filter(evnt_id=loc.evnt_id, locc_id=loc.locc_id, rive_id=loc.rive_id, subr_id=loc.subr_id, relc_id=loc.relc_id, loc_lat=loc.loc_lat, loc_lon=loc.loc_lon, loc_date=loc.loc_date).get() self.loc = loc self.team_parser(row[self.crew_key], row, loc_id=loc) if indv_id: anix_loc_indv, anix_entered = utils.enter_anix(cleaned_data, loc_pk=loc.pk, indv_pk=indv_id.pk) self.row_entered += anix_entered self.row_entered += utils.enter_bulk_indvd(anix_loc_indv.pk, self.cleaned_data, row_datetime, gender=row.get(self.sex_key), len_mm=row.get(self.len_key_mm), len_val=row.get(self.len_key), weight=row.get(self.weight_key), weight_kg=row.get(self.weight_key_kg), vial=row.get(self.vial_key), scale_envelope=row.get(self.scale_key), prog_grp=row.get(self.grp_key), comments=row.get(self.comment_key) ) if utils.nan_to_none(row.get(self.mort_key)): if utils.y_n_to_bool(row[self.mort_key]): mort_anix, mort_entered = utils.enter_mortality(indv_id, self.cleaned_data, row_datetime) self.row_entered += mort_entered if utils.nan_to_none(row.get(self.wr_key)): if utils.y_n_to_bool(row[self.wr_key]): self.row_entered += utils.enter_indvd(anix_loc_indv.pk, cleaned_data, row_datetime, None, self.ani_health_anidc_id.pk, adsc_str=self.wr_adsc_id.name) if utils.nan_to_none(row.get(self.aquaculture_key)): if utils.y_n_to_bool(row[self.aquaculture_key]): self.row_entered += utils.enter_indvd(anix_loc_indv.pk, cleaned_data, row_datetime, None, self.ani_health_anidc_id.pk, adsc_str="Aquaculture") if utils.nan_to_none(row[self.tank_key]): self.row_entered += utils.enter_contx(self.tank_dict[row[self.tank_key]], cleaned_data, True, indv_id.pk) if self.loc.pk not in self.loc_caught_dict: self.loc_caught_dict[self.loc.pk] = 1 else: self.loc_caught_dict[self.loc.pk] += 1 else: if self.loc.pk not in self.loc_obs_dict: self.loc_obs_dict[self.loc.pk] = 1 else: self.loc_obs_dict[self.loc.pk] += 1 elif utils.nan_to_none(row.get(self.samp_key)): samp = models.Sample.objects.filter(anix_id__evnt_id=cleaned_data["evnt_id"], loc_id=loc, spec_id=self.salmon_id, samp_num=row[self.samp_key], sampc_id=self.sampc_id, ).get() if not samp: # create group for sample: grp_id = models.Group(spec_id=self.salmon_id, stok_id=stok_id, coll_id=coll_id, grp_year=year, grp_valid=False, created_by=cleaned_data["created_by"], created_date=cleaned_data["created_date"], ) grp_id.clean() grp_id.save() self.row_entered = True grp_anix, data_entered = utils.enter_anix(cleaned_data, grp_pk=grp_id.pk) self.row_entered += data_entered samp, samp_entered = utils.enter_samp(cleaned_data, row[self.samp_key], self.salmon_id.pk, self.sampc_id.pk, anix_pk=grp_anix.pk, loc_pk=loc.pk, comments=utils.nan_to_none(row.get(self.comment_key))) self.row_entered += samp_entered self.row_entered += utils.enter_bulk_sampd(samp.pk, self.cleaned_data, row_datetime, gender=row.get(self.sex_key), len_mm=row.get(self.len_key_mm), len_val=row.get(self.len_key), weight=row.get(self.weight_key), weight_kg=row.get(self.weight_key_kg), vial=row.get(self.vial_key), scale_envelope=row.get(self.scale_key), prog_grp=row.get(self.grp_key), comments=row.get(self.comment_key) ) if utils.nan_to_none(row.get(self.mort_key)): if utils.y_n_to_bool(row[self.mort_key]): self.row_entered += utils.enter_samp_mortality(samp, self.cleaned_data, row_datetime) if utils.nan_to_none(row.get(self.wr_key)): if utils.y_n_to_bool(row[self.wr_key]): self.row_entered += utils.enter_sampd(samp.pk, cleaned_data, row_datetime, None, self.ani_health_anidc_id.pk, adsc_str=self.wr_adsc_id.name) if utils.nan_to_none(row.get(self.aquaculture_key)): if utils.y_n_to_bool(row[self.aquaculture_key]): self.row_entered += utils.enter_sampd(samp.pk, cleaned_data, row_datetime, None, self.ani_health_anidc_id.pk, adsc_str="Aquaculture") else: raise Exception("Fish must either be assigned a sample number or a pit tag.")
def row_parser(self, row): cleaned_data = self.cleaned_data year, coll = utils.year_coll_splitter(row[self.coll_key]) row_datetime = utils.get_row_date(row) row_date = row_datetime.date() indv_ufid = utils.nan_to_none(row.get(self.ufid_key)) indv = models.Individual( grp_id=self.grp_id, spec_id=self.salmon_id, stok_id=self.stok_id, coll_id=self.coll_id, indv_year=year, pit_tag=row[self.pit_key], ufid=indv_ufid, indv_valid=True, comments=utils.nan_to_none(row.get(self.comment_key)), created_by=cleaned_data["created_by"], created_date=cleaned_data["created_date"], ) try: indv.clean() indv.save() self.row_entered = True except (ValidationError, IntegrityError): indv = models.Individual.objects.filter(pit_tag=indv.pit_tag).get() if utils.nan_to_none(row[self.from_tank_id_key]) or utils.nan_to_none( row[self.to_tank_id_key]): in_tank = row[self.from_tank_id_key] out_tank = row[self.to_tank_id_key] self.row_entered += utils.create_movement_evnt(in_tank, out_tank, cleaned_data, row_datetime, indv_pk=indv.pk) # if tagged fish goes back into same tank, still link fish to tank: if in_tank == out_tank: utils.enter_contx(in_tank, cleaned_data, True, indv_pk=indv.pk) anix_indv, anix_entered = utils.enter_anix(cleaned_data, indv_pk=indv.pk) self.row_entered += anix_entered self.anix_indv = anix_indv utils.enter_bulk_indvd( anix_indv.pk, self.cleaned_data, row_date, len_mm=row.get(self.len_key_mm), len_val=row.get(self.len_key), weight=row.get(self.weight_key), weight_kg=row.get(self.weight_key_kg), vial=row.get(self.vial_key), mark=row.get(self.mark_key), prog_grp=row.get(self.group_key), lifestage=row.get(self.lifestage_key), comments=row.get(self.comment_key), ) if utils.nan_to_none(row.get(self.precocity_key)): self.row_entered += utils.enter_indvd(anix_indv.pk, cleaned_data, row_date, None, self.ani_health_anidc_id.pk, "Precocity") if utils.nan_to_none(row.get(self.crew_key)): perc_list, inits_not_found = utils.team_list_splitter( row[self.crew_key]) for perc_id in perc_list: team_id, team_entered = utils.add_team_member( perc_id, cleaned_data["evnt_id"], role_id=self.tagger_code, return_team=True) self.row_entered += team_entered if team_id: self.row_entered += utils.enter_anix(cleaned_data, indv_pk=indv.pk, team_pk=team_id.pk, return_sucess=True) for inits in inits_not_found: self.log_data += "No valid personnel with initials ({}) for row with pit tag" \ " {}\n".format(inits, row[self.pit_key])
def row_parser(self, row): row_datetime = utils.get_row_date(row) row_date = row_datetime.date() indv_qs = models.Individual.objects.filter(pit_tag=row[self.pit_key]) if len(indv_qs) == 1: indv = indv_qs.get() else: self.log_data += "Error parsing row: \n" self.log_data += str(row) self.log_data += "\nFish with PIT {} not found in db\n".format( row[self.pit_key]) self.success = False return anix, anix_entered = utils.enter_anix(self.cleaned_data, indv_pk=indv.pk) self.row_entered += anix_entered self.row_entered += utils.enter_bulk_indvd( anix.pk, self.cleaned_data, row_date, gender=row.get(self.sex_key), len_mm=row.get(self.len_key_mm), len_val=row.get(self.len_key), weight=row.get(self.weight_key), weight_kg=row.get(self.weight_key_kg), vial=row.get(self.vial_key), scale_envelope=row.get(self.envelope_key), tissue_yn=row.get(self.tissue_key), mark=row.get(self.mark_key), vaccinated=row.get(self.vax_key), status=row.get(self.status_key), lifestage=row.get(self.lifestage_key), comments=row.get(self.comment_key)) if utils.nan_to_none(row.get(self.precocity_key)): if utils.y_n_to_bool(row[self.precocity_key]): self.row_entered += utils.enter_indvd( anix.pk, self.cleaned_data, row_date, None, self.ani_health_anidc_id.pk, "Precocity") if utils.nan_to_none(row.get(self.mort_key)): if utils.y_n_to_bool(row[self.mort_key]): mort_anix, mort_entered = utils.enter_mortality( indv, self.cleaned_data, row_datetime) self.row_entered += mort_entered in_tank = None out_tank = None if utils.nan_to_none(row[self.start_tank_key]): in_tank = models.Tank.objects.filter( name=row[self.start_tank_key]).get() if utils.nan_to_none(row[self.end_tank_key]): out_tank = models.Tank.objects.filter( name=row[self.end_tank_key]).get() if in_tank or out_tank: self.row_entered += utils.create_movement_evnt(in_tank, out_tank, self.cleaned_data, row_datetime, indv_pk=indv.pk) self.row_entered += utils.parse_extra_cols(row, self.cleaned_data, anix, indv=True)