from rest_framework import serializers
from .models import FinanceEntry
class FinanceSerializer(serializers.ModelSerializer):
 campus_name=serializers.CharField(source='campus.name',read_only=True)
 submitted_by_name=serializers.SerializerMethodField()
 class Meta: model=FinanceEntry; fields='__all__'; read_only_fields=['submitted_by']
 def get_submitted_by_name(self,obj):
  member=getattr(obj.submitted_by,'member_profile',None)
  if member:
   return ' '.join(part for part in (member.first_name,member.other_names,member.last_name) if part)
  return obj.submitted_by.get_full_name() or obj.submitted_by.phone_number
 def validate(self,data):
  if data.get('scope')=='CAMPUS' and not data.get('campus'): raise serializers.ValidationError({'campus':'Required for campus finance.'})
  if data.get('scope')=='NATIONAL': data['campus']=None
  return data
