class InterventionAmendmentCUSerializer(AttachmentSerializerMixin, serializers.ModelSerializer): amendment_number = serializers.CharField(read_only=True) signed_amendment_file = serializers.FileField(source="signed_amendment", read_only=True) signed_amendment_attachment = AttachmentSingleFileField( override="signed_amendment") internal_prc_review = AttachmentSingleFileField(required=False) class Meta: model = InterventionAmendment fields = "__all__" def validate(self, data): data = super().validate(data) if 'signed_date' in data and data['signed_date'] > date.today(): raise ValidationError("Date cannot be in the future!") if 'intervention' in data: if data['intervention'].in_amendment is True: raise ValidationError( "Cannot add a new amendment while another amendment is in progress." ) if data['intervention'].agreement.partner.blocked is True: raise ValidationError( "Cannot add a new amendment while the partner is blocked in Vision." ) if InterventionAmendment.OTHER in data["types"]: if "other_description" not in data or not data["other_description"]: raise ValidationError( "Other description required, if type 'Other' selected.") return data
def test_attachment_single_file_field_attachment(file_type, upload_file): file_type.attachment = Attachment.objects.filter(code=file_type.code) field = AttachmentSingleFileField(source="attachment") attachment = AttachmentFactory( file_type=file_type, content_object=file_type, code=file_type.code, file=upload_file, ) assert field.get_attribute(file_type) == attachment.file
class InterventionAttachmentSerializer(AttachmentSerializerMixin, serializers.ModelSerializer): attachment_file = serializers.FileField(source="attachment", read_only=True) attachment_document = AttachmentSingleFileField( source="attachment_file", override="attachment", ) def update(self, instance, validated_data): intervention = validated_data.get('intervention', instance.intervention) if intervention and intervention.status in [ Intervention.ENDED, Intervention.CLOSED, Intervention.TERMINATED ]: raise ValidationError( 'An attachment cannot be changed in statuses "Ended, Closed or Terminated"' ) return super().update(instance, validated_data) class Meta: model = InterventionAttachment fields = ( 'id', 'intervention', 'created', 'type', 'active', 'attachment', 'attachment_file', 'attachment_document', )
class CoreValuesAssessmentSerializer(AttachmentSerializerMixin, serializers.ModelSerializer): attachment = AttachmentSingleFileField() # assessment = serializers.FileField(required=True) assessment_file = serializers.FileField(source='assessment', read_only=True) class Meta: model = CoreValuesAssessment fields = "__all__"
class AgreementAmendmentCreateUpdateSerializer(AttachmentSerializerMixin, serializers.ModelSerializer): number = serializers.CharField(read_only=True) created = serializers.DateTimeField(read_only=True) modified = serializers.DateTimeField(read_only=True) signed_amendment_file = serializers.FileField(source="signed_amendment", read_only=True) signed_amendment_attachment = AttachmentSingleFileField() class Meta: model = AgreementAmendment fields = "__all__"
class AssessmentDetailSerializer(AttachmentSerializerMixin, serializers.ModelSerializer): report_attachment = AttachmentSingleFileField() report_file = serializers.FileField(source='report', read_only=True) report = serializers.FileField(required=False) completed_date = serializers.DateField(required=True) class Meta: model = Assessment fields = "__all__" def validate_completed_date(self, completed_date): today = timezone.now().date() if completed_date > today: raise serializers.ValidationError( 'The Date of Report cannot be in the future') return completed_date
class AgreementDetailSerializer(serializers.ModelSerializer): partner_name = serializers.CharField(source='partner.name', read_only=True) authorized_officers = PartnerStaffMemberNestedSerializer(many=True, read_only=True) amendments = AgreementAmendmentCreateUpdateSerializer(many=True, read_only=True) unicef_signatory = SimpleUserSerializer(source='signed_by') partner_signatory = SimpleStaffMemberSerializer(source='partner_manager') attached_agreement_file = serializers.FileField(source="attached_agreement", read_only=True) attachment = AttachmentSingleFileField(read_only=True) permissions = serializers.SerializerMethodField(read_only=True) def get_permissions(self, obj): user = self.context['request'].user ps = Agreement.permission_structure() permissions = AgreementPermissions(user=user, instance=self.instance, permission_structure=ps) return permissions.get_permissions() class Meta: model = Agreement fields = "__all__"
class AgreementCreateUpdateSerializer(AttachmentSerializerMixin, SnapshotModelSerializer): partner_name = serializers.CharField(source='partner.name', read_only=True) agreement_type = serializers.CharField(required=True) amendments = AgreementAmendmentCreateUpdateSerializer(many=True, read_only=True) signed_by = serializers.CharField(read_only=True, allow_null=True) country_programme = serializers.PrimaryKeyRelatedField(queryset=CountryProgramme.objects.all(), required=False, allow_null=True) unicef_signatory = SimpleUserSerializer(source='signed_by', read_only=True) partner_signatory = SimpleStaffMemberSerializer(source='partner_manager', read_only=True) agreement_number = serializers.CharField(read_only=True) attached_agreement_file = serializers.FileField(source="attached_agreement", read_only=True) attachment = AttachmentSingleFileField() class Meta: model = Agreement fields = "__all__" def validate(self, data): data = super().validate(data) agreement_type = data.get('agreement_type', None) or self.instance.agreement_type if agreement_type == Agreement.PCA: # Look for country programme in data and on instance. country_programme = data.get('country_programme') if not country_programme and self.instance: country_programme = self.instance.country_programme if country_programme is None: raise ValidationError({'country_programme': 'Country Programme is required for PCAs!'}) # When running validations in the serializer.. keep in mind that the # related fields have not been updated and therefore not accessible on old_instance.relatedfield_old. # If you want to run validation only after related fields have been updated. please run it in the view if self.context.get('skip_global_validator', None): return data validator = AgreementValid(data, old=self.instance, user=self.context['request'].user) if not validator.is_valid: raise serializers.ValidationError({'errors': validator.errors}) return data
def test_attachment_single_file_field_no_attachment(file_type): file_type.attachment = Attachment.objects.filter(code=file_type.code) field = AttachmentSingleFileField(source="attachment") assert field.get_attribute(file_type) is None
def test_attachment_single_file_field_no_attribute(file_type): field = AttachmentSingleFileField(source="wrong") assert field.get_attribute(file_type) is None
class InterventionPDFileSerializer(serializers.ModelSerializer): signed_pd_document_file = AttachmentSingleFileField(source='signed_pd_attachment', read_only=True) class Meta: model = Intervention fields = ('signed_pd_document_file',)
class InterventionDetailSerializer(serializers.ModelSerializer): planned_budget = InterventionBudgetCUSerializer(read_only=True) partner = serializers.CharField(source='agreement.partner.name') partner_vendor = serializers.CharField( source='agreement.partner.vendor_number') partner_id = serializers.CharField(source='agreement.partner.id', read_only=True) prc_review_document_file = serializers.FileField( source='prc_review_document', read_only=True) prc_review_attachment = AttachmentSingleFileField(read_only=True) signed_pd_document_file = serializers.FileField( source='signed_pd_document', read_only=True) signed_pd_attachment = AttachmentSingleFileField(read_only=True) activation_letter_file = serializers.FileField(source='activation_letter', read_only=True) activation_letter_attachment = AttachmentSingleFileField(read_only=True) termination_doc_file = serializers.FileField(source='termination_doc', read_only=True) termination_doc_attachment = AttachmentSingleFileField(read_only=True) amendments = InterventionAmendmentCUSerializer(many=True, read_only=True, required=False) attachments = InterventionAttachmentSerializer(many=True, read_only=True, required=False) planned_visits = PlannedVisitsNestedSerializer(many=True, read_only=True, required=False) result_links = InterventionResultNestedSerializer(many=True, read_only=True, required=False) submitted_to_prc = serializers.ReadOnlyField() frs_details = FRsSerializer(source='frs', read_only=True) permissions = serializers.SerializerMethodField(read_only=True) flagged_sections = serializers.SerializerMethodField(read_only=True) section_names = serializers.SerializerMethodField(read_only=True) days_from_submission_to_signed = serializers.CharField(read_only=True) days_from_review_to_signed = serializers.CharField(read_only=True) locations = serializers.SerializerMethodField() location_names = serializers.SerializerMethodField() cluster_names = serializers.SerializerMethodField() donors = serializers.SerializerMethodField() donor_codes = serializers.SerializerMethodField() grants = serializers.SerializerMethodField() location_p_codes = serializers.SerializerMethodField() def get_location_p_codes(self, obj): return [location.p_code for location in obj.flat_locations.all()] def get_donors(self, obj): donors = set() for fr_item_qs in obj.frs.all(): for fr_li in fr_item_qs.fr_items.all(): donors.add(fr_li.donor) return donors def get_donor_codes(self, obj): donor_codes = set() for fr_item_qs in obj.frs.all(): for fr_li in fr_item_qs.fr_items.all(): donor_codes.add(fr_li.donor_code) return donor_codes def get_grants(self, obj): grants = set() for fr_item_qs in obj.frs.all(): for fr_li in fr_item_qs.fr_items.all(): grants.add(fr_li.grant_number) return grants def get_permissions(self, obj): user = self.context['request'].user ps = Intervention.permission_structure() permissions = InterventionPermissions(user=user, instance=self.instance, permission_structure=ps) return permissions.get_permissions() def get_locations(self, obj): return [l.id for l in obj.flat_locations.all()] def get_location_names(self, obj): return [ '{} [{} - {}]'.format(l.name, l.gateway.name, l.p_code) for l in obj.flat_locations.all() ] def get_section_names(self, obj): return [l.name for l in obj.sections.all()] def get_flagged_sections(self, obj): return [l.id for l in obj.sections.all()] def get_cluster_names(self, obj): return [c for c in obj.intervention_clusters()] class Meta: model = Intervention fields = ( "id", 'frs', "partner", "agreement", "document_type", "number", "prc_review_document_file", "frs_details", "signed_pd_document_file", "title", "status", "start", "end", "submission_date_prc", "review_date_prc", "submission_date", "prc_review_document", "submitted_to_prc", "signed_pd_document", "signed_by_unicef_date", "unicef_signatory", "unicef_focal_points", "partner_focal_points", "partner_authorized_officer_signatory", "offices", "population_focus", "signed_by_partner_date", "created", "modified", "planned_budget", "result_links", 'country_programme', 'metadata', 'contingency_pd', "amendments", "attachments", 'permissions', 'partner_id', "sections", "planned_visits", "locations", "location_names", "cluster_names", "flat_locations", "flagged_sections", "section_names", "in_amendment", "prc_review_attachment", "signed_pd_attachment", "donors", "donor_codes", "grants", "location_p_codes", "days_from_submission_to_signed", "days_from_review_to_signed", "partner_vendor", "reference_number_year", "activation_letter_file", "activation_letter_attachment", "termination_doc_file", "termination_doc_attachment")
class InterventionCreateUpdateSerializer(AttachmentSerializerMixin, SnapshotModelSerializer): planned_budget = InterventionBudgetCUSerializer(read_only=True) partner = serializers.CharField(source='agreement.partner.name', read_only=True) prc_review_document_file = serializers.FileField( source='prc_review_document', read_only=True) prc_review_attachment = AttachmentSingleFileField(required=False) signed_pd_document_file = serializers.FileField( source='signed_pd_document', read_only=True) signed_pd_attachment = AttachmentSingleFileField(required=False) activation_letter_file = serializers.FileField(source='activation_letter', read_only=True) activation_letter_attachment = AttachmentSingleFileField(required=False) termination_doc_file = serializers.FileField(source='termination_doc', read_only=True) termination_doc_attachment = AttachmentSingleFileField() planned_visits = PlannedVisitsNestedSerializer(many=True, read_only=True, required=False) attachments = InterventionAttachmentSerializer(many=True, read_only=True, required=False) result_links = InterventionResultCUSerializer(many=True, read_only=True, required=False) frs = serializers.PrimaryKeyRelatedField( many=True, queryset=FundsReservationHeader.objects.prefetch_related( 'intervention').all(), required=False) class Meta: model = Intervention fields = "__all__" def to_internal_value(self, data): if 'frs' in data: if data['frs'] is None: data['frs'] = [] return super().to_internal_value(data) def validate_frs(self, frs): for fr in frs: if fr.intervention: if (self.instance is None) or (not self.instance.id) or ( fr.intervention.id != self.instance.id): raise ValidationError([ 'One or more of the FRs selected is related to a different PD/SSFA,' ' {}'.format(fr.fr_number) ]) else: pass # unicef/etools-issues:779 # TODO: add this validation back after all legacy data has been handled. # make sure it's not expired # if fr.expired: # raise ValidationError({'error': 'One or more selected FRs is expired,' # ' {}'.format(fr.fr_number)}) return frs @transaction.atomic def update(self, instance, validated_data): updated = super().update(instance, validated_data) return updated
class AuthorOverrideSerializer(AttachmentSerializerMixin, AuthorBaseSerializer): profile_image = AttachmentSingleFileField(override="image")
class AuthorSerializer(AttachmentSerializerMixin, AuthorBaseSerializer): profile_image = AttachmentSingleFileField()