Exemplo n.º 1
0
class PropertyStateWritableSerializer(serializers.ModelSerializer):
    """
    Used by PropertyViewAsState as a nested serializer

    This serializer is for use with the PropertyViewAsStateSerializer such that
    PropertyState can be created and updated through a single call to the
    associated PropertyViewViewSet.
    """
    extra_data = serializers.JSONField(required=False)
    measures = PropertyMeasureSerializer(source='propertymeasure_set',
                                         many=True,
                                         read_only=True)
    scenarios = ScenarioSerializer(many=True, read_only=True)
    files = BuildingFileSerializer(source='building_files',
                                   many=True,
                                   read_only=True)
    analysis_state = ChoiceField(choices=PropertyState.ANALYSIS_STATE_TYPES,
                                 required=False)

    # to support the old state serializer method with the PROPERTY_STATE_FIELDS variables
    import_file_id = serializers.IntegerField(allow_null=True, read_only=True)
    organization_id = serializers.IntegerField(read_only=True)

    # support naive datetime objects
    # support naive datetime objects
    generation_date = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                allow_null=True)
    recent_sale_date = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                 allow_null=True)
    release_date = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                             allow_null=True)
    analysis_start_time = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                    allow_null=True)
    analysis_end_time = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                  allow_null=True)

    # support the pint objects
    conditioned_floor_area = PintQuantitySerializerField(allow_null=True,
                                                         required=False)
    gross_floor_area = PintQuantitySerializerField(allow_null=True,
                                                   required=False)
    occupied_floor_area = PintQuantitySerializerField(allow_null=True,
                                                      required=False)
    site_eui = PintQuantitySerializerField(allow_null=True, required=False)
    site_eui_modeled = PintQuantitySerializerField(allow_null=True,
                                                   required=False)
    source_eui_weather_normalized = PintQuantitySerializerField(
        allow_null=True, required=False)
    source_eui = PintQuantitySerializerField(allow_null=True, required=False)
    source_eui_modeled = PintQuantitySerializerField(allow_null=True,
                                                     required=False)
    site_eui_weather_normalized = PintQuantitySerializerField(allow_null=True,
                                                              required=False)

    class Meta:
        fields = '__all__'
        model = PropertyState
Exemplo n.º 2
0
class PropertyStateSerializer(serializers.ModelSerializer):
    extra_data = serializers.JSONField(required=False)
    measures = PropertyMeasureSerializer(source='propertymeasure_set',
                                         many=True,
                                         read_only=True)
    scenarios = ScenarioSerializer(many=True, read_only=True)
    files = BuildingFileSerializer(source='building_files',
                                   many=True,
                                   read_only=True)
    analysis_state = ChoiceField(choices=PropertyState.ANALYSIS_STATE_TYPES)

    # support the pint objects
    conditioned_floor_area = PintQuantitySerializerField(allow_null=True)
    gross_floor_area = PintQuantitySerializerField(allow_null=True)
    occupied_floor_area = PintQuantitySerializerField(allow_null=True)
    site_eui = PintQuantitySerializerField(allow_null=True)
    site_eui_modeled = PintQuantitySerializerField(allow_null=True)
    source_eui_weather_normalized = PintQuantitySerializerField(
        allow_null=True)
    source_eui = PintQuantitySerializerField(allow_null=True)
    source_eui_modeled = PintQuantitySerializerField(allow_null=True)
    site_eui_weather_normalized = PintQuantitySerializerField(allow_null=True)

    # to support the old state serializer method with the PROPERTY_STATE_FIELDS variables
    import_file_id = serializers.IntegerField(allow_null=True, read_only=True)
    organization_id = serializers.IntegerField()

    class Meta:
        model = PropertyState
        fields = '__all__'
        extra_kwargs = {'organization': {'read_only': True}}

    def to_representation(self, data):
        """Overwritten to handle time conversion"""
        result = super(PropertyStateSerializer, self).to_representation(data)
        # for datetime to be isoformat and remove timezone data
        if data.generation_date:
            result['generation_date'] = make_naive(
                data.generation_date).isoformat()

        if data.recent_sale_date:
            result['recent_sale_date'] = make_naive(
                data.recent_sale_date).isoformat()

        if data.release_date:
            result['release_date'] = make_naive(data.release_date).isoformat()

        if data.analysis_start_time:
            result['analysis_start_time'] = make_naive(
                data.analysis_start_time).isoformat()

        if data.analysis_end_time:
            result['analysis_end_time'] = make_naive(
                data.analysis_end_time).isoformat()

        return result
Exemplo n.º 3
0
class PropertyStateSerializer(serializers.ModelSerializer):
    extra_data = serializers.JSONField(required=False)
    measures = PropertyMeasureSerializer(source='propertymeasure_set',
                                         many=True,
                                         read_only=True)
    scenarios = ScenarioSerializer(many=True, read_only=True)
    files = BuildingFileSerializer(source='building_files',
                                   many=True,
                                   read_only=True)
    analysis_state = ChoiceField(choices=PropertyState.ANALYSIS_STATE_TYPES)

    # support the pint objects
    conditioned_floor_area = PintQuantitySerializerField(allow_null=True)
    gross_floor_area = PintQuantitySerializerField(allow_null=True)
    occupied_floor_area = PintQuantitySerializerField(allow_null=True)
    site_eui = PintQuantitySerializerField(allow_null=True)
    site_eui_modeled = PintQuantitySerializerField(allow_null=True)
    source_eui_weather_normalized = PintQuantitySerializerField(
        allow_null=True)
    source_eui = PintQuantitySerializerField(allow_null=True)
    source_eui_modeled = PintQuantitySerializerField(allow_null=True)
    site_eui_weather_normalized = PintQuantitySerializerField(allow_null=True)

    # support naive datetime objects
    generation_date = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                allow_null=True)
    recent_sale_date = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                 allow_null=True)
    release_date = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                             allow_null=True)
    analysis_start_time = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                    allow_null=True)
    analysis_end_time = serializers.DateTimeField('%Y-%m-%dT%H:%M:%S',
                                                  allow_null=True)

    # to support the old state serializer method with the PROPERTY_STATE_FIELDS variables
    import_file_id = serializers.IntegerField(allow_null=True, read_only=True)
    organization_id = serializers.IntegerField()

    class Meta:
        model = PropertyState
        fields = '__all__'
        extra_kwargs = {'organization': {'read_only': True}}

    def __init__(self,
                 instance=None,
                 data=empty,
                 all_extra_data_columns=None,
                 show_columns=None,
                 **kwargs):
        """
        If show_columns is passed, then all_extra_data_columns is not needed since the extra_data columns are embedded
        in the show_columns.

        TODO: remove the use of all_extra_data_columns.

        :param instance: instance to serialize
        :param data: initial data
        :param all_extra_data_columns:
        :param show_columns: dict of list, Which columns to show in the form of c['fields']=[] and c['extra_data'].
        """
        if show_columns is not None:
            self.all_extra_data_columns = show_columns['extra_data']
        else:
            self.all_extra_data_columns = all_extra_data_columns

        super().__init__(instance=instance, data=data, **kwargs)

        # remove the fields to display based on the show_columns list.
        if show_columns is not None:
            for field_name in set(self.fields) - set(show_columns['fields']):
                self.fields.pop(field_name)

    def to_representation(self, data):
        """Overwritten to handle time conversion and extra_data null fields"""
        result = super().to_representation(data)

        # Prepopulate the extra_data columns with a default of None so that they will appear in the result
        # This will also handle the passing of the show_columns extra data list. If the show_columns isn't
        # requiring a column, then it won't show up here.
        if self.all_extra_data_columns and data.extra_data:
            prepopulated_extra_data = {
                col_name: data.extra_data.get(col_name, None)
                for col_name in self.all_extra_data_columns
            }

            result['extra_data'] = prepopulated_extra_data

        return result
Exemplo n.º 4
0
class PropertyStateSerializer(serializers.ModelSerializer):
    extra_data = serializers.JSONField(required=False)
    measures = PropertyMeasureSerializer(source='propertymeasure_set',
                                         many=True,
                                         read_only=True)
    scenarios = ScenarioSerializer(many=True, read_only=True)
    files = BuildingFileSerializer(source='building_files',
                                   many=True,
                                   read_only=True)
    analysis_state = ChoiceField(choices=PropertyState.ANALYSIS_STATE_TYPES)

    # support the pint objects
    conditioned_floor_area = PintQuantitySerializerField(allow_null=True)
    gross_floor_area = PintQuantitySerializerField(allow_null=True)
    occupied_floor_area = PintQuantitySerializerField(allow_null=True)
    site_eui = PintQuantitySerializerField(allow_null=True)
    site_eui_modeled = PintQuantitySerializerField(allow_null=True)
    source_eui_weather_normalized = PintQuantitySerializerField(
        allow_null=True)
    source_eui = PintQuantitySerializerField(allow_null=True)
    source_eui_modeled = PintQuantitySerializerField(allow_null=True)
    site_eui_weather_normalized = PintQuantitySerializerField(allow_null=True)

    # to support the old state serializer method with the PROPERTY_STATE_FIELDS variables
    import_file_id = serializers.IntegerField(allow_null=True, read_only=True)
    organization_id = serializers.IntegerField()

    class Meta:
        model = PropertyState
        fields = '__all__'
        extra_kwargs = {'organization': {'read_only': True}}

    def __init__(self,
                 instance=None,
                 data=empty,
                 all_extra_data_columns=None,
                 **kwargs):
        """Override __init__ for the optional all_extra_data_columns argument"""
        self.all_extra_data_columns = all_extra_data_columns
        super(PropertyStateSerializer, self).__init__(instance=instance,
                                                      data=data,
                                                      **kwargs)

    def to_representation(self, data):
        """Overwritten to handle time conversion and extra_data null fields"""
        result = super(PropertyStateSerializer, self).to_representation(data)

        # Prepopulate the extra_data columns with a default of None so that they will appear in the result
        if self.all_extra_data_columns and data.extra_data:
            prepopulated_extra_data = {
                col_name: data.extra_data.get(col_name, None)
                for col_name in self.all_extra_data_columns
            }

            result['extra_data'] = prepopulated_extra_data

        # for datetime to be isoformat and remove timezone data
        if data.generation_date:
            result['generation_date'] = make_naive(
                data.generation_date).isoformat()

        if data.recent_sale_date:
            result['recent_sale_date'] = make_naive(
                data.recent_sale_date).isoformat()

        if data.release_date:
            result['release_date'] = make_naive(data.release_date).isoformat()

        if data.analysis_start_time:
            result['analysis_start_time'] = make_naive(
                data.analysis_start_time).isoformat()

        if data.analysis_end_time:
            result['analysis_end_time'] = make_naive(
                data.analysis_end_time).isoformat()

        return result