Skip to content

Commit

Permalink
feb-r1 (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
mainlyIt authored Feb 9, 2025
1 parent 59a3968 commit 5045546
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
4 changes: 0 additions & 4 deletions vespadb/observations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,6 @@ def update(self, instance: Observation, validated_data: dict[Any, Any]) -> Obser
"""Update method to handle observation reservations."""
user = self.context["request"].user

# Check if someone is trying to update a nest reserved by another user
if instance.reserved_by and instance.reserved_by != user and not user.is_superuser:
raise serializers.ValidationError("You cannot edit an observation reserved by another user.")

# Only proceed if user has appropriate permissions
if not user.is_superuser:
user_municipality_ids = user.municipalities.values_list("id", flat=True)
Expand Down
60 changes: 45 additions & 15 deletions vespadb/observations/tasks/export_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,29 @@ class WriterProtocol(Protocol):
def writerow(self, row: List[str]) -> Any: ...

PUBLIC_FIELDS = [
"id", "created_datetime", "latitude", "longitude", "source",
"nest_height", "nest_size", "nest_location", "nest_type",
"observation_datetime", "province", "municipality", "nest_status",
"source_id", "anb_domain"
"id",
"observation_datetime",
"latitude",
"longitude",
"province",
"municipality",
"anb_domain",
"nest_status",
"eradication_date",
"eradication_result",
"images",
"nest_type",
"nest_location",
"nest_height",
"nest_size",
"notes",
"source",
"source_id",
"wn_id",
"wn_validation_status",
"wn_cluster_id",
"created_datetime",
"modified_datetime",
]

def get_status(observation: Observation) -> str:
Expand All @@ -30,19 +49,10 @@ def prepare_row_data(
is_admin: bool,
user_municipality_ids: Set[str]
) -> List[str]:
"""
Prepare a single row of data for the CSV export with error handling.
"""
try:
allowed_fields = PUBLIC_FIELDS

row_data: List[str] = []
for field in PUBLIC_FIELDS:
try:
if field not in allowed_fields:
row_data.append("")
continue

if field == "latitude":
row_data.append(str(observation.location.y) if observation.location else "")
elif field == "longitude":
Expand All @@ -60,18 +70,38 @@ def prepare_row_data(
row_data.append(observation.municipality.name if observation.municipality else "")
elif field == "nest_status":
row_data.append(get_status(observation))
elif field == "eradication_date":
date_val = getattr(observation, "eradication_date", None)
row_data.append(date_val.isoformat() if date_val else "")
elif field == "eradication_result":
value = getattr(observation, "eradication_result", "")
row_data.append(str(value) if value is not None else "")
elif field == "images":
value = getattr(observation, "images", "")
row_data.append(str(value) if value is not None else "")
elif field == "notes":
value = getattr(observation, "notes", "")
row_data.append(str(value) if value is not None else "")
elif field == "wn_id":
value = getattr(observation, "wn_id", "")
row_data.append(str(value) if value is not None else "")
elif field == "wn_validation_status":
value = getattr(observation, "wn_validation_status", "")
row_data.append(str(value) if value is not None else "")
elif field == "wn_cluster_id":
value = getattr(observation, "wn_cluster_id", "")
row_data.append(str(value) if value is not None else "")
else:
value = getattr(observation, field, "")
row_data.append(str(value) if value is not None else "")
except Exception as e:
logger.warning(f"Error processing field {field}: {str(e)}")
row_data.append("")

return row_data
except Exception as e:
logger.error(f"Error preparing row data: {str(e)}")
return [""] * len(PUBLIC_FIELDS)

def generate_rows(
queryset: QuerySet[Model],
writer: WriterProtocol,
Expand Down
6 changes: 5 additions & 1 deletion vespadb/observations/tasks/observation_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,14 @@ def map_external_data_to_observation_model(external_data: dict[str, Any]) -> dic
eradication_flagged = True
break

if eradication_flagged and not check_existing_eradication_date(external_data["id"]):
if eradication_flagged:
mapped_data["eradication_date"] = observation_datetime_utc.date()
mapped_data["eradicator_name"] = "Gemeld als bestreden"

# Ensure eradication_result is also set if it's missing
if "eradication_result" not in mapped_data or not mapped_data["eradication_result"]:
mapped_data["eradication_result"] = "eradicated"

return mapped_data

def check_existing_eradication_date(wn_id: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion vespadb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

# vespawatch specific settings
MAX_RESERVATIONS = 50
RESERVATION_DURATION_DAYS = 5
RESERVATION_DURATION_DAYS = 10
ERADICATION_KEYWORD_LIST = ["BESTREDEN"]


Expand Down

0 comments on commit 5045546

Please sign in to comment.