Skip to content

Commit

Permalink
Merge f3867ee into 06d63a6
Browse files Browse the repository at this point in the history
  • Loading branch information
aviveiros11 authored Jun 30, 2020
2 parents 06d63a6 + f3867ee commit e29857d
Show file tree
Hide file tree
Showing 4 changed files with 491 additions and 0 deletions.
4 changes: 4 additions & 0 deletions seed/api/v3/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from seed.views.v3.datasets import DatasetViewSet
from seed.views.v3.geocode import GeocodeViewSet
from seed.views.v3.import_files import ImportFileViewSet
from seed.views.v3.measures import MeasureViewSet
from seed.views.v3.labels import LabelViewSet
from seed.views.v3.label_inventories import LabelInventoryViewSet
from seed.views.v3.meters import MeterViewSet
Expand All @@ -25,6 +26,7 @@
from seed.views.v3.property_scenarios import PropertyScenarioViewSet
from seed.views.v3.taxlots import TaxlotViewSet
from seed.views.v3.ubid import UbidViewSet
from seed.views.v3.uploads import UploadViewSet
from seed.views.v3.users import UserViewSet

api_v3_router = routers.DefaultRouter()
Expand All @@ -37,11 +39,13 @@
api_v3_router.register(r'labels', LabelViewSet, base_name='labels')
api_v3_router.register(r'data_quality_checks', DataQualityCheckViewSet, base_name='data_quality_checks')
api_v3_router.register(r'import_files', ImportFileViewSet, base_name='import_files')
api_v3_router.register(r'measures', MeasureViewSet, base_name='import_files')
api_v3_router.register(r'meters', MeterViewSet, base_name='meters')
api_v3_router.register(r'organizations', OrganizationViewSet, base_name='organizations')
api_v3_router.register(r'properties', PropertyViewSet, base_name='properties')
api_v3_router.register(r'taxlots', TaxlotViewSet, base_name='taxlots')
api_v3_router.register(r'ubid', UbidViewSet, base_name='ubid')
api_v3_router.register(r'upload', UploadViewSet, base_name='upload')
api_v3_router.register(r'users', UserViewSet, base_name='user')

data_quality_checks_router = nested_routers.NestedSimpleRouter(api_v3_router, r'data_quality_checks', lookup="nested")
Expand Down
30 changes: 30 additions & 0 deletions seed/utils/api_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ def path_id_field(description):
type=openapi.TYPE_INTEGER
)

@staticmethod
def form_string_field(name, required, description):
return openapi.Parameter(
name,
openapi.IN_FORM,
description=description,
required=required,
type=openapi.TYPE_STRING
)

@staticmethod
def form_integer_field(name, required, description):
return openapi.Parameter(
name,
openapi.IN_FORM,
description=description,
required=required,
type=openapi.TYPE_INTEGER
)

@staticmethod
def upload_file_field(name, required, description):
return openapi.Parameter(
name,
openapi.IN_FORM,
description=description,
required=required,
type=openapi.TYPE_FILE
)

@classmethod
def body_field(cls, required, description, name='body', params_to_formats={}):
return openapi.Parameter(
Expand Down
79 changes: 79 additions & 0 deletions seed/views/v3/measures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# !/usr/bin/env python
# encoding: utf-8
"""
:copyright (c) 2014 - 2020, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Department of Energy) and contributors. All rights reserved. # NOQA
:author
"""
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.parsers import JSONParser, FormParser
from rest_framework.renderers import JSONRenderer

from seed.models import (
Measure,
)
from seed.utils.api_schema import AutoSchemaHelper, swagger_auto_schema_org_query_param


@method_decorator(
name='retrieve',
decorator=swagger_auto_schema(
manual_parameters=[AutoSchemaHelper.query_integer_field(
name="id",
required=True,
description="A unique integer value identifying this measure.")]
),
)
class MeasureViewSet(viewsets.ReadOnlyModelViewSet):
"""
list:
Return a list of all measures
retrieve:
Return a measure by a unique id
"""
parser_classes = (JSONParser, FormParser,)
renderer_classes = (JSONRenderer,)
queryset = Measure.objects.all()
pagination_class = None

@swagger_auto_schema_org_query_param
@action(detail=False, methods=['POST'])
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)
Loading

0 comments on commit e29857d

Please sign in to comment.