|
13 | 13 | FoodPurchaseForm, |
14 | 14 | LivelihoodActivityForm, |
15 | 15 | LivelihoodStrategyForm, |
| 16 | + MeatProductionForm, |
16 | 17 | MilkProductionForm, |
| 18 | + OtherCashIncomeForm, |
17 | 19 | OtherPurchaseForm, |
| 20 | + PaymentInKindForm, |
18 | 21 | ReliefGiftOtherForm, |
19 | 22 | WealthGroupCharacteristicValueForm, |
20 | 23 | WealthGroupForm, |
@@ -333,6 +336,134 @@ def get_extra(self, request, obj=None, **kwargs): |
333 | 336 |
|
334 | 337 | class LivelihoodActivityAdmin(admin.ModelAdmin): |
335 | 338 | form = LivelihoodActivityForm |
| 339 | + |
| 340 | + # Maps strategy_type to the subclass accessor name, its form, extra fieldsets, |
| 341 | + # and any extra fields to append to the base "Quantity" fieldset. |
| 342 | + _SUBCLASS_CONFIG = { |
| 343 | + LivelihoodStrategyType.MILK_PRODUCTION: { |
| 344 | + "accessor": "milkproduction", |
| 345 | + "form": MilkProductionForm, |
| 346 | + "quantity_extra": ["quantity_butter_production"], |
| 347 | + "extra_fieldsets": [ |
| 348 | + ( |
| 349 | + "Milk source", |
| 350 | + { |
| 351 | + "fields": [ |
| 352 | + "milking_animals", |
| 353 | + "lactation_days", |
| 354 | + "daily_production", |
| 355 | + "type_of_milk_consumed", |
| 356 | + "type_of_milk_sold_or_other_uses", |
| 357 | + ] |
| 358 | + }, |
| 359 | + ), |
| 360 | + ], |
| 361 | + }, |
| 362 | + LivelihoodStrategyType.MEAT_PRODUCTION: { |
| 363 | + "accessor": "meatproduction", |
| 364 | + "form": MeatProductionForm, |
| 365 | + "extra_fieldsets": [ |
| 366 | + ( |
| 367 | + "Meat source", |
| 368 | + { |
| 369 | + "fields": [ |
| 370 | + "animals_slaughtered", |
| 371 | + "carcass_weight", |
| 372 | + ] |
| 373 | + }, |
| 374 | + ), |
| 375 | + ], |
| 376 | + }, |
| 377 | + LivelihoodStrategyType.FOOD_PURCHASE: { |
| 378 | + "accessor": "foodpurchase", |
| 379 | + "form": FoodPurchaseForm, |
| 380 | + "extra_fieldsets": [ |
| 381 | + ( |
| 382 | + "Purchases", |
| 383 | + { |
| 384 | + "fields": [ |
| 385 | + "unit_multiple", |
| 386 | + "times_per_month", |
| 387 | + "months_per_year", |
| 388 | + "times_per_year", |
| 389 | + ] |
| 390 | + }, |
| 391 | + ), |
| 392 | + ], |
| 393 | + }, |
| 394 | + LivelihoodStrategyType.PAYMENT_IN_KIND: { |
| 395 | + "accessor": "paymentinkind", |
| 396 | + "form": PaymentInKindForm, |
| 397 | + "extra_fieldsets": [ |
| 398 | + ( |
| 399 | + "Payment", |
| 400 | + { |
| 401 | + "fields": [ |
| 402 | + "payment_product", |
| 403 | + "payment_per_time", |
| 404 | + "people_per_household", |
| 405 | + "times_per_month", |
| 406 | + "months_per_year", |
| 407 | + "times_per_year", |
| 408 | + ] |
| 409 | + }, |
| 410 | + ), |
| 411 | + ], |
| 412 | + }, |
| 413 | + LivelihoodStrategyType.RELIEF_GIFT_OTHER: { |
| 414 | + "accessor": "reliefgiftother", |
| 415 | + "form": ReliefGiftOtherForm, |
| 416 | + "extra_fieldsets": [ |
| 417 | + ( |
| 418 | + "Relief", |
| 419 | + { |
| 420 | + "fields": [ |
| 421 | + "unit_multiple", |
| 422 | + "times_per_month", |
| 423 | + "months_per_year", |
| 424 | + "times_per_year", |
| 425 | + ] |
| 426 | + }, |
| 427 | + ), |
| 428 | + ], |
| 429 | + }, |
| 430 | + LivelihoodStrategyType.OTHER_CASH_INCOME: { |
| 431 | + "accessor": "othercashincome", |
| 432 | + "form": OtherCashIncomeForm, |
| 433 | + "extra_fieldsets": [ |
| 434 | + ( |
| 435 | + "Income", |
| 436 | + { |
| 437 | + "fields": [ |
| 438 | + "payment_per_time", |
| 439 | + "people_per_household", |
| 440 | + "times_per_month", |
| 441 | + "months_per_year", |
| 442 | + "times_per_year", |
| 443 | + ] |
| 444 | + }, |
| 445 | + ), |
| 446 | + ], |
| 447 | + }, |
| 448 | + LivelihoodStrategyType.OTHER_PURCHASE: { |
| 449 | + "accessor": "otherpurchase", |
| 450 | + "form": OtherPurchaseForm, |
| 451 | + "extra_fieldsets": [ |
| 452 | + ( |
| 453 | + "Purchases", |
| 454 | + { |
| 455 | + "fields": [ |
| 456 | + "unit_multiple", |
| 457 | + "times_per_month", |
| 458 | + "months_per_year", |
| 459 | + "times_per_year", |
| 460 | + ] |
| 461 | + }, |
| 462 | + ), |
| 463 | + ], |
| 464 | + }, |
| 465 | + } |
| 466 | + |
336 | 467 | list_display = ( |
337 | 468 | "wealth_group", |
338 | 469 | "strategy_type", |
@@ -365,6 +496,41 @@ class LivelihoodActivityAdmin(admin.ModelAdmin): |
365 | 496 | *translation_fields("livelihood_strategy__season__name__icontains"), |
366 | 497 | ) |
367 | 498 |
|
| 499 | + def get_object(self, request, object_id, from_field=None): |
| 500 | + obj = super().get_object(request, object_id, from_field) |
| 501 | + if obj is None: |
| 502 | + return None |
| 503 | + config = self._SUBCLASS_CONFIG.get(obj.strategy_type) |
| 504 | + if config: |
| 505 | + try: |
| 506 | + return getattr(obj, config["accessor"]) |
| 507 | + except AttributeError: |
| 508 | + pass |
| 509 | + return obj |
| 510 | + |
| 511 | + def get_form(self, request, obj=None, change=False, **kwargs): |
| 512 | + if obj is not None: |
| 513 | + config = self._SUBCLASS_CONFIG.get(obj.strategy_type) |
| 514 | + if config and config.get("form"): |
| 515 | + return config["form"] |
| 516 | + return super().get_form(request, obj, change, **kwargs) |
| 517 | + |
| 518 | + def get_fieldsets(self, request, obj=None): |
| 519 | + fieldsets = deepcopy(self.fieldsets) |
| 520 | + if obj is None: |
| 521 | + return fieldsets |
| 522 | + config = self._SUBCLASS_CONFIG.get(obj.strategy_type) |
| 523 | + if not config: |
| 524 | + return fieldsets |
| 525 | + for extra_field in config.get("quantity_extra", []): |
| 526 | + for fs in fieldsets: |
| 527 | + if fs[0] == "Quantity": |
| 528 | + fs[1]["fields"].append(extra_field) |
| 529 | + break |
| 530 | + for i, extra_fs in enumerate(config.get("extra_fieldsets", []), start=1): |
| 531 | + fieldsets.insert(i, extra_fs) |
| 532 | + return fieldsets |
| 533 | + |
368 | 534 | def get_queryset(self, request): |
369 | 535 | qs = super().get_queryset(request) |
370 | 536 | return qs.select_related( |
|
0 commit comments