class PartnerDeploymentTableauSerializer(serializers.ModelSerializer): parent_society = MiniCountrySerializer() country_deployed_to = MiniCountrySerializer() district_deployed_to = serializers.SerializerMethodField() activity = PartnerDeploymentActivitySerializer() def get_district_deployed_to(self, obj): district_fields = {'name': ''} district_deployed_to = obj.district_deployed_to.all() if len(district_deployed_to) > 0: district_fields['name'] = ', '.join( [str(district.name) for district in district_deployed_to]) return district_fields class Meta: model = PartnerSocietyDeployment fields = ( 'start_date', 'end_date', 'name', 'role', 'parent_society', 'country_deployed_to', 'district_deployed_to', 'activity', 'id', )
class PartnerDeploymentSerializer(serializers.ModelSerializer): parent_society = MiniCountrySerializer() country_deployed_to = MiniCountrySerializer() district_deployed_to = MiniDistrictSerializer(many=True) activity = PartnerDeploymentActivitySerializer() class Meta: model = PartnerSocietyDeployment fields = ( 'start_date', 'end_date', 'name', 'role', 'parent_society', 'country_deployed_to', 'district_deployed_to', 'activity', 'id', )
class PersonnelSerializer(serializers.ModelSerializer): country_from = MiniCountrySerializer() deployment = PersonnelDeploymentSerializer() class Meta: model = Personnel fields = ('start_date', 'end_date', 'name', 'role', 'type', 'country_from', 'deployment', 'id',)
class ERUOwnerSerializer(serializers.ModelSerializer): eru_set = ERUSetSerializer(many=True) national_society_country = MiniCountrySerializer() class Meta: model = ERUOwner fields = ('created_at', 'updated_at', 'national_society_country', 'eru_set', 'id',)
class PersonnelDeploymentSerializer(serializers.ModelSerializer): country_deployed_to = MiniCountrySerializer() event_deployed_to = ListEventSerializer() class Meta: model = PersonnelDeployment fields = ('country_deployed_to', 'region_deployed_to', 'event_deployed_to', 'comments', 'id',)
class ERUSerializer(serializers.ModelSerializer): deployed_to = MiniCountrySerializer() event = ListEventSerializer() eru_owner = ERUOwnerSerializer() class Meta: model = ERU fields = ('type', 'units', 'equipment_units', 'deployed_to', 'event', 'eru_owner', 'available', 'id',)
class PersonnelSerializerAnon(ModelSerializer): country_from = MiniCountrySerializer() country_to = MiniCountrySerializer() deployment = PersonnelDeploymentSerializer() molnix_tags = MolnixTagSerializer(many=True, read_only=True) class Meta: model = Personnel fields = ( 'start_date', 'end_date', 'role', 'type', 'country_from', 'country_to', 'deployment', 'molnix_id', 'molnix_tags', 'is_active', 'id', )
class SubscriptionSerializer(serializers.ModelSerializer): country = MiniCountrySerializer() class Meta: model = Subscription fields = ( 'user', 'stype', 'rtype', 'country', 'region', 'dtype', 'lookup_id', )
class FactSerializer(serializers.ModelSerializer): event = MiniEventSerializer() country = MiniCountrySerializer() class Meta: model = Fact fields = ( 'start_date', 'country', 'region', 'event', 'dtype', 'comments', 'id', )
class SurgeAlertSerializer(EnumSupportSerializerMixin, ModelSerializer): event = ListEventSerializer() country = MiniCountrySerializer() atype_display = serializers.CharField(source='get_atype_display', read_only=True) category_display = serializers.CharField(source='get_category_display', read_only=True) molnix_tags = MolnixTagSerializer(many=True, read_only=True) class Meta: model = SurgeAlert fields = ('operation', 'country', 'message', 'deployment_needed', 'is_private', 'event', 'created_at', 'id', 'atype', 'atype_display', 'category', 'category_display', 'molnix_id', 'molnix_tags', 'molnix_status', 'opens', 'closes', 'start', 'end', 'is_active', 'is_stood_down')
class ERUSetSerializer(EnumSupportSerializerMixin, ModelSerializer): deployed_to = MiniCountrySerializer() type_display = serializers.CharField(source='get_type_display', read_only=True) class Meta: model = ERU fields = ( 'type', 'type_display', 'units', 'equipment_units', 'deployed_to', 'event', 'eru_owner', 'available', 'id', )
class PersonnelCsvSerializerAnon(ModelSerializer): country_from = MicroCountrySerializer() country_to = MiniCountrySerializer() deployment = PersonnelDeploymentCsvSerializer() class Meta: model = Personnel fields = ( 'start_date', 'end_date', 'role', 'type', 'country_from', 'country_to', 'deployment', 'id', 'is_active', )
class HeopSerializer(serializers.ModelSerializer): event = MiniEventSerializer() country = MiniCountrySerializer() class Meta: model = Heop fields = ( 'start_date', 'end_date', 'country', 'region', 'event', 'dtype', 'person', 'role', 'comments', 'id', )
class SubscriptionSerializer(EnumSupportSerializerMixin, ModelSerializer): country = MiniCountrySerializer() event = MiniEventSerializer() stype_display = serializers.CharField(source='get_stype_display', read_only=True) rtype_display = serializers.CharField(source='get_rtype_display', read_only=True) class Meta: model = Subscription fields = ( 'user', 'country', 'region', 'event', 'dtype', 'lookup_id', 'stype', 'stype_display', 'rtype', 'rtype_display', )
class ProjectSerializer(serializers.ModelSerializer): project_district_detail = MiniDistrictSerializer(source='project_district', read_only=True) reporting_ns_detail = MiniCountrySerializer(source='reporting_ns', read_only=True) regional_project_detail = RegionalProjectSerializer(source='regional_project', read_only=True) event_detail = MiniEventSerializer(source='event', read_only=True) class Meta: model = Project fields = '__all__' read_only_fields = ('user',) extra_kwargs = { field: { 'allow_null': False, 'required': True, } for field in ( 'reporting_ns', 'project_district', 'name', 'programme_type', 'primary_sector', 'target_total', ) } def validate(self, data): if data['status'] == Statuses.COMPLETED and data.get('reached_total') is None: raise serializers.ValidationError('Reached total should be provided if status is completed') elif ( data['operation_type'] == OperationTypes.EMERGENCY_OPERATION and data['programme_type'] == ProgrammeTypes.MULTILATERAL and data.get('event') is None ): raise serializers.ValidationError( 'Event should be provided if operation type is Emergency Operation and programme type is Multilateral' ) return data def create(self, validated_data): project = super().create(validated_data) project.user = self.context['request'].user project.save() return project
class ProjectSerializer(EnumSupportSerializerMixin, ModelSerializer): project_country_detail = MiniCountrySerializer(source='project_country', read_only=True) project_districts_detail = MiniDistrictSerializer( source='project_districts', read_only=True, many=True) reporting_ns_detail = MiniCountrySerializer(source='reporting_ns', read_only=True) dtype_detail = DisasterTypeSerializer(source='dtype', read_only=True) regional_project_detail = RegionalProjectSerializer( source='regional_project', read_only=True) event_detail = MiniEventSerializer(source='event', read_only=True) primary_sector_display = serializers.CharField( source='get_primary_sector_display', read_only=True) programme_type_display = serializers.CharField( source='get_programme_type_display', read_only=True) operation_type_display = serializers.CharField( source='get_operation_type_display', read_only=True) status_display = serializers.CharField(source='get_status_display', read_only=True) visibility_display = serializers.CharField(source='get_visibility_display', read_only=True) secondary_sectors_display = serializers.ListField( source='get_secondary_sectors_display', read_only=True) modified_by_detail = MiniUserSerializer(source='modified_by', read_only=True) class Meta: model = Project fields = '__all__' read_only_fields = ('user', 'modified_by') extra_kwargs = { field: { 'allow_null': False, 'required': True, } for field in ( 'reporting_ns', 'name', 'project_country', 'programme_type', 'primary_sector', 'project_districts', ) } def validate(self, data): d_project_districts = data['project_districts'] # Override country with district's country if isinstance(d_project_districts, list) and len(d_project_districts): data['project_country'] = data['project_districts'][0].country for project in data['project_districts'][1:]: if project.country != data['project_country']: raise serializers.ValidationError( ugettext( 'Different country found for given districts')) if (data['operation_type'] == OperationTypes.EMERGENCY_OPERATION and data['programme_type'] == ProgrammeTypes.MULTILATERAL and data.get('event') is None): raise serializers.ValidationError( ugettext( 'Event should be provided if operation type is Emergency Operation and programme type is Multilateral' )) return data def create(self, validated_data): project = super().create(validated_data) project.user = self.context['request'].user project.save() return project def update(self, instance, validated_data): validated_data['modified_by'] = self.context['request'].user return super().update(instance, validated_data)