def setinstance(self, pk): if pk is None: return try: # Check if id is valid UUID check_uuid(pk) self.instance = self.model.objects.get(id=pk) except (ObjectDoesNotExist, ParseError): self.instance = None
def validate_duplicate(self): protocol = self.data.get("protocol") sample_event = self.data.get("sample_event") or {} fishbelt_transect = self.data.get("fishbelt_transect") or {} number = fishbelt_transect.get("number") or None try: int(number) except (ValueError, TypeError): return self.error(self.identifier, self.NUMBER_MSG) label = fishbelt_transect.get("label") or "" width = fishbelt_transect.get("width") or None try: _ = check_uuid(width) except ParseError: return self.error(self.identifier, self.WIDTH_MSG) relative_depth = sample_event.get("relative_depth", None) or None if relative_depth is not None: try: _ = check_uuid(relative_depth) except ParseError: return self.error(self.identifier, self.RELATIVE_DEPTH_MSG) site = sample_event.get("site", None) or None management = sample_event.get("management", None) or None sample_date = sample_event.get("sample_date", None) or None depth = sample_event.get("depth", None) or None try: _ = check_uuid(site) _ = check_uuid(management) except ParseError: return self.error(self.identifier, self.INVALID_MSG) qry = { "sample_event__site": site, "sample_event__management": management, "sample_event__sample_date": sample_date, "number": number, "label": label, "sample_event__depth": depth, "sample_event__relative_depth": relative_depth, "width_id": width, } results = FishBeltTransect.objects.select_related().filter(**qry) for result in results: transect_methods = get_related_transect_methods(result) for transect_method in transect_methods: if transect_method.protocol == protocol: return self.warning(self.identifier, self.DUPLICATE_MSG) return self.ok(self.identifier)
def validate_duplicate(self): protocol = self.data.get("protocol") sample_event = self.data.get("sample_event") or {} quadrat_collection = self.data.get("quadrat_collection") or {} label = quadrat_collection.get("label") or "" relative_depth = sample_event.get("relative_depth", None) or None if relative_depth is not None: try: _ = check_uuid(relative_depth) except ParseError: return self.error(self.identifier, self.RELATIVE_DEPTH_MSG) site = sample_event.get("site", None) or None management = sample_event.get("management", None) or None sample_date = sample_event.get("sample_date", None) or None depth = sample_event.get("depth", None) or None profiles = [o.get("profile") for o in self.data.get("observers") or []] try: for profile in profiles: _ = check_uuid(profile) except ParseError: return self.error(self.identifier, self.INVALID_MSG) try: _ = check_uuid(site) _ = check_uuid(management) except ParseError: return self.error(self.identifier, self.INVALID_MSG) qry = { "sample_event__site": site, "sample_event__management": management, "sample_event__sample_date": sample_date, "label": label, "sample_event__depth": depth, } queryset = QuadratCollection.objects.filter(**qry) for profile in profiles: queryset = queryset.filter( bleachingquadratcollection_method__observers__profile_id=profile ) for result in queryset: transect_methods = get_related_transect_methods(result) for transect_method in transect_methods: if transect_method.protocol == protocol: return self.error(self.identifier, self.DUPLICATE_MSG) return self.ok(self.identifier)
def validate_observation_density(self): observations = self.data.get("obs_belt_fishes") or [] transect = self.data.get("fishbelt_transect") or {} len_surveyed = transect.get("len_surveyed") width_id = transect.get("width") try: _ = check_uuid(width_id) width = BeltTransectWidth.objects.get(id=width_id) except (BeltTransectWidth.DoesNotExist, ParseError): width = None # Create a fish attribute constants lookup fishattribute_ids = [] for o in observations: fish_attribute = o.get("fish_attribute") if not fish_attribute: continue fishattribute_ids.append(fish_attribute) fish_attr_lookup = { str(fa.id): fa.get_biomass_constants() for fa in FishAttribute.objects.filter(id__in=fishattribute_ids) } densities = [] for obs in observations: count = obs.get("count") size = obs.get("size") try: width_val = width.get_condition(size).val except AttributeError: width_val = None fish_attribute = obs.get("fish_attribute") constants = fish_attr_lookup.get(fish_attribute) or [None, None, None] density = calc_biomass_density( count, size, len_surveyed, width_val, *constants ) densities.append(density) total_density = sum([d for d in densities if d is not None]) if total_density > self.OBS_GT_DENSITY: return self.warning(self.identifier, self.DENSITY_GT_MSG) elif total_density < self.OBS_LT_DENSITY: return self.warning(self.identifier, self.DENSITY_LT_MSG) return self.ok(self.identifier)