Пример #1
0
    def reset(self, request):
        """
        Reset all the measures back to the defaults (as provided by BuildingSync)
        ---
        parameters: {}
        type:
            organization_id:
                required: true
                type: integer
                paramType: query
            status:
                required: true
                type: string
                description: Either success or error
            measures:
                required: true
                type: list
                description: list of measures
        """
        organization_id = request.query_params.get('organization_id', None)
        if not organization_id:
            return JsonResponse(
                {
                    'status': 'error',
                    'message': 'organization_id not provided'
                },
                status=status.HTTP_400_BAD_REQUEST)

        Measure.populate_measures(organization_id)
        data = dict(measures=list(
            Measure.objects.filter(
                organization_id=organization_id).order_by('id').values()))

        data['status'] = 'success'
        return JsonResponse(data)
Пример #2
0
    def save(self, *args, **kwargs):
        """Perform checks before saving."""
        # There can only be one.
        if self.parent_org is not None and self.parent_org.parent_org is not None:
            raise TooManyNestedOrgs

        super(Organization, self).save(*args, **kwargs)

        # Create a default cycle for the organization if there isn't one already
        from seed.models import Cycle
        Cycle.get_or_create_default(self)
        from seed.models import Measure
        Measure.populate_measures(self.id)
Пример #3
0
    def reset(self, request):
        """
        Reset all the measures back to the defaults (as provided by BuildingSync)
        """
        organization_id = request.query_params.get('organization_id', None)
        if not organization_id:
            return JsonResponse(
                {
                    'status': 'error',
                    'message': 'organization_id not provided'
                },
                status=status.HTTP_400_BAD_REQUEST)

        Measure.populate_measures(organization_id)
        data = dict(measures=list(
            Measure.objects.filter(
                organization_id=organization_id).order_by('id').values()))

        data['status'] = 'success'
        return JsonResponse(data)
Пример #4
0
    def add_measures(self, request, pk=None):
        """
        Update the measures applied to the building. There are two options, one for adding
        measures and one for removing measures.

        ---
        type:
            status:
                required: true
                type: string
            message:
                required: true
                type: object
            added_measure_ids:
                required: true
                type: array
                description: list of measure ids that were added to the property
            removed_measure_ids:
                required: true
                type: array
                description: list of measure ids that were removed from the property
            existing_measure_ids:
                required: true
                type: array
                description: list of measure ids that already existed for the property
        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
            - name: add_measures
              description: list of measure_ids or measure long names to add to property
              type: array
              required: false
              paramType: form
            - name: remove_measures
              description: list of measure_ids or measure long names to remove from property
              type: array
              required: false
              paramType: form
            - name: implementation_status
              description: Enum on type of measures. Recommended, Proposed, Implemented
              required: true
              paramType: form
              type: string
              enum: ["Recommended", "Proposed", "Implemented"]
        """
        cycle_pk = request.query_params.get('cycle_id', None)
        if not cycle_pk:
            return JsonResponse({
                'status':
                'error',
                'message':
                'Must pass cycle_id as query parameter'
            })

        implementation_status = PropertyMeasure.str_to_impl_status(
            request.data.get('implementation_status', None))
        if not implementation_status:
            return JsonResponse({
                'status':
                'error',
                'message':
                'None or invalid implementation_status type'
            })

        result = self._get_property_view(pk)
        pv = None
        if result.get('status', None) != 'error':
            pv = result.pop('property_view')
        else:
            return JsonResponse(result)

        # get the list of measures to add/remove and return the ids
        add_measure_ids = Measure.validate_measures(
            request.data.get('add_measures', []).split(','))
        remove_measure_ids = Measure.validate_measures(
            request.data.get('remove_measures', []).split(','))

        # add_measures = request.data
        message_add = []
        message_remove = []
        message_existed = []

        property_state_id = pv.state.pk

        for m in add_measure_ids:
            join, created = PropertyMeasure.objects.get_or_create(
                property_state_id=property_state_id,
                measure_id=m,
                implementation_status=implementation_status)
            if created:
                message_add.append(m)
            else:
                message_existed.append(m)

        for m in remove_measure_ids:
            qs = PropertyMeasure.objects.filter(
                property_state_id=property_state_id,
                measure_id=m,
                implementation_status=implementation_status)
            if qs.exists():
                qs.delete()
                message_remove.append(m)

        return JsonResponse({
            "status": "success",
            "message": "Updated measures for property state",
            "added_measure_ids": message_add,
            "removed_measure_ids": message_remove,
            "existing_measure_ids": message_existed,
        })