Add plan variety change tracking
This commit is contained in:
67
backend/apps/plans/services.py
Normal file
67
backend/apps/plans/services.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from django.db import transaction
|
||||
|
||||
from .models import Plan, PlanVarietyChange
|
||||
|
||||
|
||||
class _NoChange:
|
||||
pass
|
||||
|
||||
|
||||
NO_CHANGE = _NoChange()
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def update_plan_with_variety_tracking(
|
||||
plan: Plan,
|
||||
*,
|
||||
crop=NO_CHANGE,
|
||||
variety=NO_CHANGE,
|
||||
notes=NO_CHANGE,
|
||||
reason: str = '',
|
||||
):
|
||||
old_variety = plan.variety
|
||||
updated_fields = []
|
||||
|
||||
if crop is not NO_CHANGE:
|
||||
plan.crop = crop
|
||||
updated_fields.append('crop')
|
||||
if variety is not NO_CHANGE:
|
||||
plan.variety = variety
|
||||
updated_fields.append('variety')
|
||||
if notes is not NO_CHANGE:
|
||||
plan.notes = notes
|
||||
updated_fields.append('notes')
|
||||
|
||||
if updated_fields:
|
||||
plan.save(update_fields=updated_fields)
|
||||
|
||||
if variety is not NO_CHANGE and _get_variety_id(old_variety) != _get_variety_id(plan.variety):
|
||||
handle_plan_variety_change(plan, old_variety=old_variety, new_variety=plan.variety, reason=reason)
|
||||
|
||||
return plan
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def handle_plan_variety_change(plan: Plan, *, old_variety, new_variety, reason: str = ''):
|
||||
if _get_variety_id(old_variety) == _get_variety_id(new_variety):
|
||||
return None
|
||||
|
||||
change = PlanVarietyChange.objects.create(
|
||||
field=plan.field,
|
||||
year=plan.year,
|
||||
plan=plan,
|
||||
old_variety=old_variety,
|
||||
new_variety=new_variety,
|
||||
reason=reason,
|
||||
)
|
||||
process_plan_variety_change(change)
|
||||
return change
|
||||
|
||||
|
||||
def process_plan_variety_change(change: PlanVarietyChange):
|
||||
"""後続 issue で施肥計画・田植え計画への移動処理を追加する入口。"""
|
||||
return change
|
||||
|
||||
|
||||
def _get_variety_id(variety):
|
||||
return getattr(variety, 'id', None)
|
||||
Reference in New Issue
Block a user