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 EngagementActionPointSerializer(PermissionsBasedSerializerMixin, ActionPointBaseSerializer): section = SeparatedReadWriteField( read_field=SectorSerializer(read_only=True, label=_('Section')), required=True, ) office = SeparatedReadWriteField(read_field=OfficeSerializer( read_only=True, label=_('Office')), required=True) 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 + [ 'section', 'office', 'history', 'url', ] extra_kwargs = copy(ActionPointBaseSerializer.Meta.extra_kwargs) extra_kwargs.update( {'assigned_to': { 'label': _('Person Responsible') }})
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 Child1Serializer(serializers.ModelSerializer): parent = SeparatedReadWriteField( read_field=ParentSerializer(read_only=True), ) class Meta: model = Child1 fields = ['id', 'field', 'parent']
class TPMActionPointSerializer(PermissionsBasedSerializerMixin, ActionPointBaseSerializer): section = SeparatedReadWriteField(read_field=SectorSerializer( read_only=True, label=_('Section')), read_only=True) office = SeparatedReadWriteField(read_field=OfficeSerializer( read_only=True, label=_('Office')), required=True) 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 + [ 'tpm_activity', 'section', 'office', 'history', 'is_responsible', 'url' ] extra_kwargs = copy(ActionPointBaseSerializer.Meta.extra_kwargs) extra_kwargs.update({ 'tpm_activity': { 'label': _('Site Visit Schedule') }, 'assigned_to': { 'label': _('Person Responsible') } }) 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, 'intervention_id': activity.intervention_id, 'cp_output_id': activity.cp_output_id, 'section': activity.section, }) return super(TPMActionPointSerializer, self).create(validated_data)
class EngagementLightSerializer(serializers.ModelSerializer): agreement = SeparatedReadWriteField( read_field=PurchaseOrderSerializer(read_only=True), label=_('Purchase Order')) po_item = SeparatedReadWriteField( read_field=PurchaseOrderItemSerializer(read_only=True), label=_('PO Item')) related_agreement = PurchaseOrderSerializer(write_only=True, required=False) partner = SeparatedReadWriteField( read_field=PartnerOrganizationLightSerializer(read_only=True), ) status = serializers.ChoiceField(choices=Engagement.DISPLAY_STATUSES, source='displayed_status', read_only=True) status_date = serializers.ReadOnlyField(source='displayed_status_date', label=_('Date of Status')) unique_id = serializers.ReadOnlyField(label=_('Unique ID')) class Meta: model = Engagement fields = [ 'id', 'unique_id', 'agreement', 'po_item', 'related_agreement', 'partner', 'engagement_type', 'status', 'status_date', ] def validate(self, attrs): attrs = super(EngagementLightSerializer, self).validate(attrs) po_item = attrs.get('po_item') agreement = attrs.get('agreement') if po_item and agreement and po_item.purchase_order != agreement: msg = self.fields['po_item'].error_messages['does_not_exist'] raise serializers.ValidationError( {'po_item': [msg.format(pk_value=po_item.pk)]}) return attrs
class PurchaseOrderSerializer(PermissionsBasedSerializerMixin, WritableNestedSerializerMixin, serializers.ModelSerializer): auditor_firm = SeparatedReadWriteField( read_field=AuditorFirmLightSerializer(read_only=True), ) items = PurchaseOrderItemSerializer(many=True) class Meta(WritableNestedSerializerMixin.Meta): model = PurchaseOrder fields = [ 'id', 'order_number', 'auditor_firm', 'items', 'contract_start_date', 'contract_end_date' ]
class BaseAttachmentSerializer(UserContextSerializerMixin, serializers.ModelSerializer): uploaded_by = SeparatedReadWriteField( write_field=serializers.HiddenField( default=serializers.CurrentUserDefault()), read_field=SimpleUserSerializer(label=_('Uploaded By')), ) def _validate_attachment(self, validated_data, instance=None): file_attachment = validated_data.get( 'file', None) or (instance.file if instance else None) hyperlink = validated_data.get( 'hyperlink', None) or (instance.hyperlink if instance else None) if bool(file_attachment) == bool(hyperlink): raise ValidationError(_('Please provide file or hyperlink.')) def create(self, validated_data): self._validate_attachment(validated_data) return super(BaseAttachmentSerializer, self).create(validated_data) def update(self, instance, validated_data): self._validate_attachment(validated_data, instance=instance) return super(BaseAttachmentSerializer, self).update(instance, validated_data) class Meta: model = Attachment fields = [ 'id', 'file_type', 'file', 'hyperlink', 'created', 'modified', 'uploaded_by', 'filename', ] extra_kwargs = { 'created': { 'label': _('Date Uploaded'), }, 'filename': { 'read_only': True }, }
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', ]
class EngagementSerializer(EngagementDatesValidation, WritableNestedParentSerializerMixin, EngagementListSerializer): staff_members = SeparatedReadWriteField( read_field=AuditorStaffMemberSerializer(many=True, required=False), label=_('Audit Staff Team Members')) active_pd = SeparatedReadWriteField( read_field=BaseInterventionListSerializer(many=True, required=False), label=_('Programme Document(s) or SSFA(s) - (optional)'), required=False) authorized_officers = SeparatedReadWriteField( read_field=PartnerStaffMemberNestedSerializer(many=True, read_only=True), label=_('Authorized Officers')) specific_procedures = SpecificProcedureSerializer( many=True, label=_('Specific Procedure To Be Performed')) class Meta(EngagementListSerializer.Meta): fields = EngagementListSerializer.Meta.fields + [ 'total_value', 'staff_members', 'active_pd', 'authorized_officers', 'joint_audit', 'shared_ip_with', 'exchange_rate', 'start_date', 'end_date', 'partner_contacted_at', 'date_of_field_visit', 'date_of_draft_report_to_ip', 'date_of_comments_by_ip', 'date_of_draft_report_to_unicef', 'date_of_comments_by_unicef', 'date_of_report_submit', 'date_of_final_report', 'date_of_cancel', 'cancel_comment', 'specific_procedures', ] extra_kwargs = { field: { 'required': True } for field in [ 'start_date', 'end_date', 'total_value', 'partner_contacted_at', 'date_of_field_visit', 'date_of_draft_report_to_ip', 'date_of_comments_by_ip', 'date_of_draft_report_to_unicef', 'date_of_comments_by_unicef', ] } extra_kwargs['engagement_type'] = {'label': _('Engagement Type')} def validate(self, data): validated_data = super(EngagementSerializer, self).validate(data) staff_members = validated_data.get('staff_members', []) validated_data.pop('related_agreement', None) agreement = validated_data.get( 'agreement', None) or self.instance.agreement if self.instance else None if staff_members and agreement and agreement.auditor_firm: existed_staff_members = agreement.auditor_firm.staff_members.all() unexisted = set(staff_members) - set(existed_staff_members) if unexisted: msg = self.fields[ 'staff_members'].write_field.child_relation.error_messages[ 'does_not_exist'] raise serializers.ValidationError({ 'staff_members': [ msg.format(pk_value=staff_member.pk) for staff_member in unexisted ], }) return validated_data
class TPMVisitLightSerializer(PermissionsBasedSerializerMixin, serializers.ModelSerializer): tpm_partner = SeparatedReadWriteField(read_field=TPMPartnerLightSerializer( label=_('TPM Partner'), read_only=True), ) tpm_partner_focal_points = SeparatedReadWriteField( read_field=TPMPartnerStaffMemberSerializer( read_only=True, many=True, label=_('TPM Focal Points')), ) status_date = serializers.ReadOnlyField(label=_('Status Date')) implementing_partners = serializers.SerializerMethodField( label=_('Implementing Partners')) locations = serializers.SerializerMethodField(label=_('Locations')) sections = serializers.SerializerMethodField(label=_('Sections')) unicef_focal_points = MinimalUserSerializer(label=_('UNICEF Focal Points'), many=True) def get_implementing_partners(self, obj): return MinimalPartnerOrganizationListSerializer(set( map(lambda a: a.partner, obj.tpm_activities.all())), many=True).data def get_locations(self, obj): return LocationLightSerializer(set( itertools.chain( *map(lambda a: a.locations.all(), obj.tpm_activities.all()))), many=True).data def get_sections(self, obj): return SectionSerializer(set( map(lambda a: a.section, obj.tpm_activities.all())), many=True).data class Meta: model = TPMVisit fields = [ 'id', 'start_date', 'end_date', 'tpm_partner', 'implementing_partners', 'locations', 'sections', 'status', 'status_date', 'reference_number', 'unicef_focal_points', 'tpm_partner_focal_points', 'date_created', 'date_of_assigned', 'date_of_tpm_accepted', 'date_of_tpm_rejected', 'date_of_tpm_reported', 'date_of_unicef_approved', 'date_of_tpm_report_rejected', 'date_of_cancelled', ] extra_kwargs = { 'reference_number': { 'label': _('Reference Number'), }, }
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 ActionPointBaseSerializer(UserContextSerializerMixin, SnapshotModelSerializer, serializers.ModelSerializer): reference_number = serializers.ReadOnlyField(label=_('Reference Number')) author = MinimalUserSerializer(read_only=True, label=_('Author')) assigned_by = MinimalUserSerializer(read_only=True, label=_('Assigned By')) assigned_to = SeparatedReadWriteField(required=True, label=_('Assignee'), read_field=MinimalUserSerializer()) category = SeparatedReadWriteField(label=_('Category'), required=False, read_field=CategorySerializer()) status_date = serializers.DateTimeField(read_only=True, label=_('Status Date')) class Meta: model = ActionPoint fields = [ 'id', 'reference_number', 'category', 'author', 'assigned_by', 'assigned_to', 'high_priority', 'due_date', 'description', 'office', 'section', 'created', 'date_of_completion', 'status', 'status_date', ] extra_kwargs = { 'status': { 'read_only': True }, 'date_of_completion': { 'read_only': True }, 'due_date': { 'required': True, 'label': _('Due On') }, } def create(self, validated_data): validated_data.update({ 'author': self.get_user(), 'assigned_by': self.get_user() }) return super(ActionPointBaseSerializer, self).create(validated_data) def update(self, instance, validated_data): if 'assigned_to' in validated_data: validated_data['assigned_by'] = self.get_user() return super(ActionPointBaseSerializer, self).update(instance, validated_data)