class TPMActionPointSerializer(PermissionsBasedSerializerMixin, ActionPointBaseSerializer): reference_number = serializers.ReadOnlyField(label=_('Reference No.')) partner = MinimalPartnerOrganizationListSerializer( read_only=True, label=_('Related Partner')) intervention = SeparatedReadWriteField( read_field=BaseInterventionListSerializer(), required=False, label=_('Related PD/SSFA')) cp_output = SeparatedReadWriteField(read_field=ResultSerializer(), required=False, label=_('Related CP Output')) location = SeparatedReadWriteField(read_field=LocationLightSerializer(), required=False, label=_('Related Location')) section = SeparatedReadWriteField(read_field=SectionSerializer(), required=True, label=_('Section of Assignee')) office = SeparatedReadWriteField(read_field=OfficeSerializer(), required=True, label=_('Office of Assignee')) is_responsible = serializers.SerializerMethodField() history = HistorySerializer(many=True, label=_('History'), read_only=True, source='get_meaningful_history') url = serializers.ReadOnlyField(label=_('Link'), source='get_object_url') class Meta(ActionPointBaseSerializer.Meta): model = TPMActionPoint fields = ActionPointBaseSerializer.Meta.fields + [ 'partner', 'intervention', 'cp_output', 'location', 'tpm_activity', 'section', 'office', 'history', 'is_responsible', 'url' ] fields.remove('category') extra_kwargs = copy(ActionPointBaseSerializer.Meta.extra_kwargs) extra_kwargs.update({ 'tpm_activity': { 'label': _('Related Task') }, 'high_priority': { 'label': _('Priority') }, }) def get_is_responsible(self, obj): return self.get_user() == obj.assigned_to def create(self, validated_data): activity = validated_data['tpm_activity'] validated_data.update({ 'partner_id': activity.partner_id, }) return super(TPMActionPointSerializer, self).create(validated_data)
class InterventionLocationSectionNestedSerializer(serializers.ModelSerializer): locations = LocationLightSerializer(many=True) sector = SectionSerializer() class Meta: model = InterventionSectorLocationLink fields = ('id', 'sector', 'locations')
class EngagementActionPointSerializer(PermissionsBasedSerializerMixin, ActionPointBaseSerializer): reference_number = serializers.ReadOnlyField(label=_('Reference No.')) partner = MinimalPartnerOrganizationListSerializer( read_only=True, label=_('Related Partner')) intervention = SeparatedReadWriteField( label=_('Related PD/SSFA'), read_field=BaseInterventionListSerializer(), required=False, ) section = SeparatedReadWriteField( read_field=SectionSerializer(read_only=True), required=True, label=_('Section of Assignee')) office = SeparatedReadWriteField( read_field=OfficeSerializer(read_only=True), required=True, label=_('Office of Assignee')) category = CategoryModelChoiceField( label=_('Action Point Category'), required=True, queryset=Category.objects.filter(module=Category.MODULE_CHOICES.audit)) history = HistorySerializer(many=True, label=_('History'), read_only=True, source='get_meaningful_history') url = serializers.ReadOnlyField(label=_('Link'), source='get_object_url') class Meta(ActionPointBaseSerializer.Meta): model = EngagementActionPoint fields = ActionPointBaseSerializer.Meta.fields + [ 'partner', 'intervention', 'history', 'url', ] extra_kwargs = copy(ActionPointBaseSerializer.Meta.extra_kwargs) extra_kwargs.update({ 'high_priority': { 'label': _('Priority') }, }) def create(self, validated_data): engagement = validated_data['engagement'] validated_data.update({ 'partner_id': engagement.partner_id, }) return super().create(validated_data)
class ActionPointListSerializer(PermissionsBasedSerializerMixin, ActionPointBaseSerializer): related_module = serializers.ChoiceField( label=_('Related App'), choices=ActionPoint.MODULE_CHOICES, read_only=True) partner = SeparatedReadWriteField( read_field=MinimalPartnerOrganizationListSerializer( read_only=True, label=_('Partner')), ) intervention = SeparatedReadWriteField( read_field=BaseInterventionListSerializer(read_only=True, label=_('PD/SSFA')), required=False, ) cp_output = SeparatedReadWriteField( read_field=ResultSerializer(read_only=True, label=_('CP Output')), required=False, ) location = SeparatedReadWriteField(read_field=LocationLightSerializer( read_only=True, label=_('Location')), ) section = SeparatedReadWriteField( read_field=SectionSerializer(read_only=True, label=_('Section of Assignee')), required=True, ) office = SeparatedReadWriteField(read_field=OfficeSerializer( read_only=True, label=_('Office of Assignee')), required=True) class Meta(ActionPointBaseSerializer.Meta): fields = ActionPointBaseSerializer.Meta.fields + [ 'related_module', 'cp_output', 'partner', 'intervention', 'location', 'engagement', 'tpm_activity', 'travel_activity', ]
def get_sections(self, obj): return SectionSerializer(set( map(lambda a: a.section, obj.tpm_activities.all())), many=True).data
class TPMActivitySerializer(PermissionsBasedSerializerMixin, WritableNestedSerializerMixin, ActivitySerializer): partner = SeparatedReadWriteField( read_field=MinimalPartnerOrganizationListSerializer( read_only=True, label=_('Implementing Partner')), ) intervention = SeparatedReadWriteField( read_field=InterventionCreateUpdateSerializer(read_only=True, label=_('PD/SSFA')), required=False, ) cp_output = SeparatedReadWriteField( read_field=ResultSerializer(read_only=True, label=_('CP Output')), required=False, ) locations = SeparatedReadWriteField(read_field=LocationLightSerializer( many=True, read_only=True, label=_('Locations')), ) section = SeparatedReadWriteField( read_field=SectionSerializer(read_only=True, label=_('Section')), required=True, ) unicef_focal_points = SeparatedReadWriteField( read_field=MinimalUserSerializer(read_only=True, many=True, label=_('UNICEF Focal Points')), required=True, ) offices = SeparatedReadWriteField( read_field=OfficeSerializer( read_only=True, many=True, label=_('Office(s) of UNICEF Focal Point(s)')), required=True, ) pv_applicable = serializers.BooleanField(read_only=True) def _validate_partner_intervention(self, validated_data, instance=None): if 'partner' in validated_data and 'intervention' not in validated_data: validated_data['intervention'] = None partner = validated_data.get('partner', instance.partner if instance else None) intervention = validated_data.get( 'intervention', instance.intervention if instance else None) if partner and partner.partner_type not in [PartnerType.GOVERNMENT, PartnerType.BILATERAL_MULTILATERAL] \ and not intervention: raise ValidationError( {'intervention': _('This field is required.')}) return instance def create(self, validated_data): self._validate_partner_intervention(validated_data) return super(TPMActivitySerializer, self).create(validated_data) def update(self, instance, validated_data): self._validate_partner_intervention(validated_data, instance=instance) return super(TPMActivitySerializer, self).update(instance, validated_data) class Meta(WritableNestedSerializerMixin.Meta): model = TPMActivity fields = [ 'id', 'partner', 'intervention', 'cp_output', 'section', 'unicef_focal_points', 'date', 'locations', 'additional_information', 'pv_applicable', 'offices', ] extra_kwargs = { 'id': { 'label': _('Task ID') }, 'date': { 'required': True }, 'partner': { 'required': True }, }
class PRPInterventionListSerializer(serializers.ModelSerializer): # todo: do these need to be lowercased? amendments = InterventionAmendmentSerializer(read_only=True, many=True) offices = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name') business_area_code = serializers.SerializerMethodField() partner_org = PRPPartnerOrganizationListSerializer( read_only=True, source='agreement.partner') agreement = serializers.CharField(read_only=True, source='agreement.agreement_number') unicef_focal_points = UserFocalPointSerializer(many=True, read_only=True) agreement_auth_officers = AuthOfficerSerializer( many=True, read_only=True, source='agreement.authorized_officers') focal_points = PartnerFocalPointSerializer(many=True, read_only=True, source='partner_focal_points') start_date = serializers.DateField(source='start') end_date = serializers.DateField(source='end') cso_budget = serializers.DecimalField(source='total_partner_contribution', read_only=True, max_digits=20, decimal_places=2) cso_budget_currency = serializers.SerializerMethodField(read_only=True) unicef_budget = serializers.DecimalField(source='total_unicef_budget', read_only=True, max_digits=20, decimal_places=2) unicef_budget_supplies = serializers.DecimalField( source='total_in_kind_amount', read_only=True, max_digits=20, decimal_places=2) unicef_budget_cash = serializers.DecimalField(source='total_unicef_cash', read_only=True, max_digits=20, decimal_places=2) unicef_budget_currency = serializers.SerializerMethodField(read_only=True) # TODO: update this after FR Validation changes, pending new Insight API changes. expected_results = PRPResultSerializer(many=True, read_only=True, source='all_lower_results') update_date = serializers.DateTimeField(source='modified') reporting_requirements = ReportingRequirementsSerializer(many=True, read_only=True) special_reports = SpecialReportingRequirementsSerializer( source="special_reporting_requirements", many=True, read_only=True) sections = SectionSerializer(source="combined_sections", many=True, read_only=True) locations = PRPLocationSerializer(source="flat_locations", many=True, read_only=True) def get_unicef_budget_currency(self, obj): # Intervention.planned_budget isn't a real field, it's a related # name from an InterventionBudget, and there might not be one. try: return obj.planned_budget.currency except ObjectDoesNotExist: return '' get_cso_budget_currency = get_unicef_budget_currency def get_business_area_code(self, obj): return connection.tenant.business_area_code def get_funds_received(self, obj): return obj.total_frs['total_actual_amt'] class Meta: model = Intervention fields = ( 'id', 'title', 'business_area_code', 'offices', # todo: convert to names, not ids 'number', 'status', 'partner_org', 'special_reports', 'sections', 'agreement', 'unicef_focal_points', 'agreement_auth_officers', 'focal_points', 'start_date', 'end_date', 'cso_budget', 'cso_budget_currency', 'unicef_budget', 'unicef_budget_currency', 'reporting_requirements', 'expected_results', 'update_date', 'amendments', 'locations', 'unicef_budget_cash', 'unicef_budget_supplies')