Ejemplo n.º 1
0
    def test_property_view_list_serializer(self):
        """Test to_representation method."""
        property_view_1 = self.property_view_factory.get_property_view()
        property_view_2 = self.property_view_factory.get_property_view()
        gap1_data = self.gap_data.copy()
        gap2_data = self.gap_data.copy()
        gap1_data['view'] = property_view_1
        gap2_data['view'] = property_view_2
        gap2_data['metric'] = 4
        self.gap_factory.get_green_assessment_property(**gap1_data)
        self.gap_factory.get_green_assessment_property(**gap2_data)
        serializer = PropertyViewListSerializer(
            child=PropertyViewSerializer()
        )
        result = serializer.to_representation(
            [property_view_1, property_view_2]
        )
        self.assertEqual(result[0]['cycle']['id'], property_view_1.cycle_id)
        self.assertEqual(result[1]['cycle']['id'], property_view_2.cycle_id)
        self.assertEqual(result[0]['state']['id'], property_view_1.state_id)
        self.assertEqual(result[1]['state']['id'], property_view_2.state_id)
        self.assertEqual(result[0]['certifications'][0]['score'], 5)
        self.assertEqual(result[1]['certifications'][0]['score'], 4)
        self.assertEqual(
            result[0]['certifications'][0]['assessment']['name'],
            self.assessment.name
        )
        self.assertEqual(
            result[1]['certifications'][0]['assessment']['name'],
            self.assessment.name
        )

        # with queryset
        serializer = PropertyViewListSerializer(
            child=PropertyViewSerializer()
        )
        queryset = PropertyView.objects.filter(
            id__in=[property_view_1.id, property_view_2.id]
        )
        result = serializer.to_representation(queryset)
        self.assertEqual(result[0]['cycle']['id'], property_view_1.cycle_id)
        self.assertEqual(result[1]['cycle']['id'], property_view_2.cycle_id)
        self.assertEqual(result[0]['state']['id'], property_view_1.state_id)
        self.assertEqual(result[1]['state']['id'], property_view_2.state_id)
        self.assertEqual(result[0]['certifications'][0]['score'], 5)
        self.assertEqual(result[1]['certifications'][0]['score'], 4)
        self.assertEqual(
            result[0]['certifications'][0]['assessment']['name'],
            self.assessment.name
        )
        self.assertEqual(
            result[1]['certifications'][0]['assessment']['name'],
            self.assessment.name
        )
Ejemplo n.º 2
0
 def retrieve(self, request, pk=None):
     """
     Get property details
     ---
     parameters:
         - name: pk
           description: The primary key of the PropertyView
           required: true
           paramType: path
         - name: organization_id
           description: The organization_id for this user's organization
           required: true
           paramType: query
     """
     result = self._get_property_view(pk)
     if result.get('status', None) != 'error':
         property_view = result.pop('property_view')
         result = {'status': 'success'}
         result.update(PropertyViewSerializer(property_view).data)
         # remove PropertyView id from result
         result.pop('id')
         result['state'] = PropertyStateSerializer(property_view.state).data
         result['taxlots'] = self._get_taxlots(property_view.pk)
         result['history'], master = self.get_history(property_view)
         result = update_result_with_master(result, master)
         return JsonResponse(result,
                             encoder=PintJSONEncoder,
                             status=status.HTTP_200_OK)
     else:
         return JsonResponse(result)
Ejemplo n.º 3
0
 def retrieve(self, request, pk=None):
     """
     Get property details
     ---
     parameters:
         - name: cycle_id
           description: The cycle id for filtering the property view
           required: true
           paramType: query
         - name: organization_id
           description: The organization_id for this user's organization
           required: true
           paramType: query
     """
     cycle_pk = request.query_params.get('cycle_id', None)
     if not cycle_pk:
         return JsonResponse(
             {'status': 'error', 'message': 'Must pass in cycle_id as query parameter'})
     result = self._get_property_view(pk, cycle_pk)
     if result.get('status', None) != 'error':
         property_view = result.pop('property_view')
         result.update(PropertyViewSerializer(property_view).data)
         # remove PropertyView id from result
         result.pop('id')
         result['state'] = PropertyStateSerializer(property_view.state).data
         result['taxlots'] = self._get_taxlots(property_view.pk)
         result['history'], master = self.get_history(property_view)
         result = update_result_with_master(result, master)
         status_code = status.HTTP_200_OK
     else:
         status_code = status.HTTP_404_NOT_FOUND
     return Response(result, status=status_code)
Ejemplo n.º 4
0
 def _get_properties(self, taxlot_view_pk):
     property_view_pks = TaxLotProperty.objects.filter(
         taxlot_view_id=taxlot_view_pk).values_list('property_view_id',
                                                    flat=True)
     property_views = PropertyView.objects.filter(
         pk__in=property_view_pks).select_related('cycle', 'state')
     properties = []
     for property_view in property_views:
         properties.append(PropertyViewSerializer(property_view).data)
     return properties
Ejemplo n.º 5
0
    def retrieve(self, request, pk=None):
        """
        Get property details
        ---
        parameters:
            - name: pk
              description: The primary key of the PropertyView
              required: true
              paramType: path
            - name: organization_id
              description: The organization_id for this user's organization
              required: true
              paramType: query
        """
        result = self._get_property_view(pk)
        if result.get('status', None) != 'error':
            property_view = result.pop('property_view')
            result = {'status': 'success'}
            result.update(PropertyViewSerializer(property_view).data)
            # remove PropertyView id from result
            result.pop('id')

            # Grab extra_data columns to be shown in the result
            organization_id = request.query_params['organization_id']
            all_extra_data_columns = Column.objects.filter(
                organization_id=organization_id,
                is_extra_data=True,
                table_name='PropertyState').values_list('column_name',
                                                        flat=True)

            result['state'] = PropertyStateSerializer(
                property_view.state,
                all_extra_data_columns=all_extra_data_columns).data
            result['taxlots'] = self._get_taxlots(property_view.pk)
            result['history'], master = self.get_history(property_view)
            result = update_result_with_master(result, master)
            return JsonResponse(result,
                                encoder=PintJSONEncoder,
                                status=status.HTTP_200_OK)
        else:
            return JsonResponse(result)
Ejemplo n.º 6
0
    def retrieve(self, request, pk):
        """
        Retrieves details about a project.

        :GET: Expects organization_id in query string.
        ---
        parameter_strategy: replace
        parameters:
            - name: organization_id
              description: The organization_id for this user's organization
              required: true
              paramType: query
            - name: project slug or pk
              description: The project slug identifier or primary key for this project
              required: true
              paramType: path

        Returns::

            {
             'id': project's primary key,
             'name': project's name,
             'slug': project's identifier,
             'status': 'active',
             'number_of_buildings': Count of buildings associated with project
             'last_modified': Timestamp when project last changed
             'last_modified_by': {
                'first_name': first name of user that made last change,
                'last_name': last name,
                'email': email address,
                },
             'is_compliance': True if project is a compliance project,
             'compliance_type': Description of compliance type,
             'deadline_date': Timestamp of when compliance is due,
             'end_date': Timestamp of end of project
             'property_count': number of property views associated with project,
             'taxlot_count':  number of taxlot views associated with project,
             'property_views': [list of serialized property views associated with the project...],
             'taxlot_views': [list of serialized taxlot views associated with the project...],
            }

        """

        error = None
        status_code = status.HTTP_200_OK
        key = self.get_key(pk)
        project = self.get_project(key, pk)
        cycle = request.query_params.get('cycle', None)
        if not project:
            error, status_code = self.get_error(
                'not found', key=key, val=pk
            )
            result = {'status': 'error', 'message': error}
        else:
            project = project[0]
            property_views = project.property_views.all()
            taxlot_views = project.taxlot_views.all()
            if cycle:
                property_views = property_views.filter(
                    cycle_id=cycle
                )
                taxlot_views = taxlot_views.filter(
                    cycle_id=cycle
                )
            project_data = ProjectSerializer(project).data
            project_data['property_views'] = [
                PropertyViewSerializer(property_view).data
                for property_view in property_views
            ]
            project_data['taxlot_views'] = [
                TaxLotViewSerializer(taxlot_view).data
                for taxlot_view in taxlot_views
            ]
            result = {
                'status': 'success',
                'project': project_data,
            }
        return Response(result, status=status_code)