feat: store VPC raw scrape data and add reset button

Species.vpc_raw (JSONField) stores the full scrape payload at import time.
vpc_species_reset view restores bloom_months, sunlight, max_height_cm etc.
from that snapshot. If vpc_raw is missing (species imported before this
feature), it re-scrapes from VPC on demand and saves the result.
Reset button appears on plant detail page for any plant with a VPC species.
A 'next' hidden field returns the user to the same plant after reset.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Stephan Kerkman 2026-05-28 18:49:15 +02:00
parent a57415983b
commit 7fef359ae0
5 changed files with 62 additions and 1 deletions

View file

@ -0,0 +1,16 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('plants', '0005_species_vpc_slug'),
]
operations = [
migrations.AddField(
model_name='species',
name='vpc_raw',
field=models.JSONField(blank=True, null=True),
),
]

View file

@ -26,6 +26,7 @@ class Species(models.Model):
api_image_url = models.URLField(blank=True)
perenual_id = models.IntegerField(unique=True, null=True, blank=True)
vpc_slug = models.CharField(max_length=200, blank=True, db_index=True)
vpc_raw = models.JSONField(null=True, blank=True)
class Meta:
ordering = ['common_name']

View file

@ -43,6 +43,18 @@
<span class="care-chip" style="background:#e8f5e9; color:#1b5e20;">🌱 {{ plant.species.growth_rate }}</span>
{% endif %}
</div>
{% if plant.species.vpc_slug %}
<div class="mb-3">
<form method="post" action="{% url 'vpc_species_reset' plant.species.pk %}">
{% csrf_token %}
<input type="hidden" name="next" value="{% url 'plant_detail' plant.pk %}">
<button type="submit" class="btn btn-sm btn-outline-secondary"
onclick="return confirm('Reset species data to original VPC values?')">
↩ Reset to original VPC data
</button>
</form>
</div>
{% endif %}
{% endif %}
{% load plant_extras %}

View file

@ -16,4 +16,5 @@ urlpatterns = [
path('species/search/', species.species_search, name='species_search'),
path('species/select/', species.species_select, name='species_select'),
path('species/select/vpc/', species.vpc_select, name='vpc_select'),
path('species/<int:species_pk>/reset-vpc/', species.vpc_species_reset, name='vpc_species_reset'),
]

View file

@ -1,6 +1,7 @@
from concurrent.futures import ThreadPoolExecutor
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from django.views.decorators.http import require_POST
from plants.models import Species
from plants.services.perenual import search_species as perenual_search, PerenualError
from plants.services import vpc as vpc_service
@ -93,5 +94,35 @@ def vpc_select(request):
description=data['description'],
api_image_url=data['api_image_url'],
vpc_slug=slug,
vpc_raw=data,
)
return redirect(f"{reverse('plant_add')}?species_id={species.pk}")
@require_POST
def vpc_species_reset(request, species_pk):
"""Restore species fields to the original VPC scrape values."""
species = get_object_or_404(Species, pk=species_pk, vpc_slug__gt='')
raw = species.vpc_raw
if not raw:
# vpc_raw missing (imported before this feature) — re-scrape
try:
raw = vpc_service.scrape_plant(species.vpc_slug)
species.vpc_raw = raw
except VPCError:
pass
if raw:
species.common_name = raw.get('common_name', species.common_name)
species.scientific_name = raw.get('scientific_name', species.scientific_name)
species.bloom_months = raw.get('bloom_months', species.bloom_months)
species.max_height_cm = raw.get('max_height_cm', species.max_height_cm)
species.sunlight = raw.get('sunlight', species.sunlight)
species.growth_rate = raw.get('growth_rate', species.growth_rate)
species.description = raw.get('description', species.description)
species.api_image_url = raw.get('api_image_url', species.api_image_url)
species.save()
# Redirect back to the plant that triggered the reset, if provided
next_url = request.POST.get('next') or reverse('plant_list')
return redirect(next_url)