示例#1
0
def get_dataset(request):
    """returns an array of import files for a data set
        The data set/import record id comes in as a GET param
        returns:
        importfiles = [
        {
            name: "DC_CoveredBuildings_50k.csv",
            number_of_buildings: 511,
            number_of_mappings: 511,
            number_of_cleanings: 1349,
            source_type: "AssessorRaw",
            number_of_matchings: 403,
            id: 1
        },
        {
            name: "DC_ESPM_Report.csv",
            number_of_buildings: 511,
            number_of_matchings: 403,
            source_type: "PMRaw",
            id: 2
        }
    ];
    """
    from seed.models import obj_to_dict
    dataset_id = request.GET.get('dataset_id', '')
    orgs = request.user.orgs.all()
    # check if user has access to the dataset
    d = ImportRecord.objects.filter(
        super_organization__in=orgs, pk=dataset_id
    )
    if d.exists():
        d = d[0]
    else:
        return {
            'status': 'success',
            'dataset': {},
        }

    dataset = obj_to_dict(d)
    importfiles = []
    for f in d.files:
        importfile = obj_to_dict(f)
        importfile['name'] = f.filename_only
        importfiles.append(importfile)

    dataset['importfiles'] = importfiles
    if d.last_modified_by:
        dataset['last_modified_by'] = d.last_modified_by.email
    dataset['number_of_buildings'] = BuildingSnapshot.objects.filter(
        import_file__in=d.files
    ).count()
    dataset['updated_at'] = convert_to_js_timestamp(d.updated_at)

    return {
        'status': 'success',
        'dataset': dataset,
    }
示例#2
0
def get_datasets(request):
    """returns an array of datasets for a user's organization
        importfiles = [
        {
            name: "DC_CoveredBuildings_50k.csv",
            number_of_buildings: 511,
            number_of_mappings: 511,
            number_of_cleanings: 1349,
            source_type: "AssessorRaw",
            number_of_matchings: 403,
            id: 1
        },
        {
            name: "DC_ESPM_Report.csv",
            number_of_buildings: 511,
            number_of_matchings: 403,
            source_type: "PMRaw",
            id: 2
        }
    ];
    datasets = [
        {
            name: "DC 2013 data",
            last_modified: (new Date()).getTime(),
            last_modified_by: "*****@*****.**",
            number_of_buildings: 89,
            id: 1,
            importfiles: mock_importfiles
        },
        ...
    ];
    """
    from seed.models import obj_to_dict
    org = Organization.objects.get(pk=request.GET.get('organization_id'))
    datasets = []
    for d in ImportRecord.objects.filter(super_organization=org):
        importfiles = [obj_to_dict(f) for f in d.files]
        dataset = obj_to_dict(d)
        dataset['importfiles'] = importfiles
        if d.last_modified_by:
            dataset['last_modified_by'] = d.last_modified_by.email
        dataset['number_of_buildings'] = BuildingSnapshot.objects.filter(
            import_file__in=d.files,
            canonicalbuilding__active=True,
        ).count()
        dataset['updated_at'] = convert_to_js_timestamp(d.updated_at)
        datasets.append(dataset)

    return {
        'status': 'success',
        'datasets': datasets,
    }
示例#3
0
 def retrieve(self, request, pk=None):
     """
     Returns a single meter based on its id
     ---
     type:
         status:
             required: true
             type: string
             description: Either success or error
         meters:
             required: true
             type: dict
             description: meter object
     parameters:
         - name: pk
           description: Meter primary key
           required: true
           paramType: path
     """
     meter = Meter.objects.get(pk=pk)
     if meter:
         res = {}
         res['status'] = 'success'
         res['meter'] = obj_to_dict(meter)
         res['meter']['timeseries_count'] = meter.timeseries_set.count()
         return JsonResponse(res)
     else:
         return JsonResponse({
             'status': 'error',
             'message': 'No meter object found',
         })
示例#4
0
文件: meters.py 项目: Maalka/seed
def get_timeseries(request):
    """Return all time series data for a building, grouped by meter.

    Expected GET params:

    meter_id: int, unique identifier for the meter.
    offset: int, the offset from the most recent meter data to begin showing.
    num: int, the number of results to show.
    """
    meter_id = request.GET.get('meter_id', '')
    offset = int(request.GET.get('offset', 0))
    num = int(request.GET.get('num', 12))  # 12 because monthly data.

    if not meter_id:
        return {'status': 'error', 'message': 'No meter id specified'}

    result = {'status': 'success', 'meter_id': meter_id, 'timeseries': []}

    paginated_ts = TimeSeries.objects.filter(
        meter_id=meter_id
    )[offset:offset + num]

    for ts in paginated_ts:
        t = obj_to_dict(ts)
        result['timeseries'].append(t)

    return result
示例#5
0
def get_timeseries(request):
    """Return all time series data for a building, grouped by meter.

    Expected GET params:

    meter_id: int, unique identifier for the meter.
    offset: int, the offset from the most recent meter data to begin showing.
    num: int, the number of results to show.
    """
    meter_id = request.GET.get('meter_id', '')
    offset = int(request.GET.get('offset', 0))
    num = int(request.GET.get('num', 12))  # 12 because monthly data.

    if not meter_id:
        return {'status': 'error', 'message': 'No meter id specified'}

    result = {'status': 'success', 'meter_id': meter_id, 'timeseries': []}

    paginated_ts = TimeSeries.objects.filter(meter_id=meter_id)[offset:offset +
                                                                num]

    for ts in paginated_ts:
        t = obj_to_dict(ts)
        result['timeseries'].append(t)

    return result
def build_survey_payload(building_snapshot):
    """Return survey payload for a building."""
    survey = {}
    s = Survey.objects.first()
    if not s:
        return survey
    sb = SurveyBuilding.objects.filter(
        canonical_building=building_snapshot.canonical_building,
        survey=s
    ).first()
    if sb:
        # important to dictify SurveyBuilding first to get the correct id in
        # the response Survey payload
        survey.update(obj_to_dict(sb))
    survey.update(obj_to_dict(s))
    question_payload = {}
    question_responses_payload = {}
    for q in s.questions.all():
        if q.question_type == ENUM:
            sa_qs = SurveyAnswer.objects.filter(
                canonical_building=building_snapshot.canonical_building,
                question=q
            )
        else:
            sa = SurveyAnswer.objects.filter(
                canonical_building=building_snapshot.canonical_building,
                question=q
            ).first()
        question_payload[q.pk] = obj_to_dict(q)
        question_payload[q.pk]['question_type'] = q.get_question_type_display()
        question_payload[q.pk]['options'] = [
            obj_to_dict(o) for o in q.question_options.all()
        ]
        if q.question_type == ENUM:
            question_responses_payload[q.pk] = {}
            for sa in sa_qs:
                # only storing the checked options, False should be deleted
                question_responses_payload[q.pk][sa.answer] = True
        else:
            question_responses_payload[q.pk] = obj_to_dict(sa) if sa else {}
            question_responses_payload[q.pk]['question_id'] = q.pk

    survey['questions'] = question_payload
    survey['question_responses'] = question_responses_payload

    return survey
示例#7
0
    def list(self, request):
        """
        Returns all of the meters for a property view
        ---
        type:
            status:
                required: true
                type: string
                description: Either success or error
            property_view_id:
                required: true
                type: integer
                description: property view id of the request
            meters:
                required: true
                type: array[meters]
                description: list of meters for property_view_id
        parameters:
            - name: organization_id
              description: The organization_id for this user's organization
              required: true
              paramType: query
            - name: property_view_id
              description: The property_view_id of the building holding the meter data
              required: true
              paramType: query
        """
        pv_id = request.GET.get('property_view_id', None)
        org_id = request.GET.get('organization_id')

        if pv_id is None:
            return JsonResponse({
                'status': 'error',
                'message': 'No property_view_id specified',
                'meters': []
            })

        # verify that the user has access to view property
        pvs = PropertyView.objects.filter(id=pv_id, state__organization=org_id)
        if pvs.count() == 0:
            return JsonResponse({
                'status': 'success',
                'message': 'No property_ids found for organization',
                'meters': []
            })
        else:
            return JsonResponse({
                'status':
                'success',
                'property_view_id':
                pv_id,
                'meters': [
                    obj_to_dict(m)
                    for m in Meter.objects.filter(property_view=pv_id)
                ]
            })
def build_survey_payload(building_snapshot):
    """Return survey payload for a building."""
    survey = {}
    s = Survey.objects.first()
    if not s:
        return survey
    sb = SurveyBuilding.objects.filter(
        canonical_building=building_snapshot.canonical_building,
        survey=s).first()
    if sb:
        # important to dictify SurveyBuilding first to get the correct id in
        # the response Survey payload
        survey.update(obj_to_dict(sb))
    survey.update(obj_to_dict(s))
    question_payload = {}
    question_responses_payload = {}
    for q in s.questions.all():
        if q.question_type == ENUM:
            sa_qs = SurveyAnswer.objects.filter(
                canonical_building=building_snapshot.canonical_building,
                question=q)
        else:
            sa = SurveyAnswer.objects.filter(
                canonical_building=building_snapshot.canonical_building,
                question=q).first()
        question_payload[q.pk] = obj_to_dict(q)
        question_payload[q.pk]['question_type'] = q.get_question_type_display()
        question_payload[q.pk]['options'] = [
            obj_to_dict(o) for o in q.question_options.all()
        ]
        if q.question_type == ENUM:
            question_responses_payload[q.pk] = {}
            for sa in sa_qs:
                # only storing the checked options, False should be deleted
                question_responses_payload[q.pk][sa.answer] = True
        else:
            question_responses_payload[q.pk] = obj_to_dict(sa) if sa else {}
            question_responses_payload[q.pk]['question_id'] = q.pk

    survey['questions'] = question_payload
    survey['question_responses'] = question_responses_payload

    return survey
示例#9
0
文件: datasets.py 项目: mmclark/seed
    def list(self, request):
        """
        Retrieves all datasets for the user's organization.
        ---
        type:
            status:
                required: true
                type: string
                description: Either success or error
            datasets:
                required: true
                type: array[dataset]
                description: Returns an array where each item is a full dataset structure, including
                             keys ''name'', ''number_of_buildings'', ''id'', ''updated_at'',
                             ''last_modified_by'', ''importfiles'', ...
        parameters:
            - name: organization_id
              description: The organization_id for this user's organization
              required: true
              paramType: query
        """

        org_id = request.query_params.get('organization_id', None)
        org = Organization.objects.get(pk=org_id)
        datasets = []
        for d in ImportRecord.objects.filter(super_organization=org):
            importfiles = [obj_to_dict(f) for f in d.files]
            dataset = obj_to_dict(d)
            dataset['importfiles'] = importfiles
            if d.last_modified_by:
                dataset['last_modified_by'] = d.last_modified_by.email
            dataset['number_of_buildings'] = BuildingSnapshot.objects.filter(
                import_file__in=d.files,
                canonicalbuilding__active=True,
            ).count()
            dataset['updated_at'] = convert_to_js_timestamp(d.updated_at)
            datasets.append(dataset)

        return JsonResponse({
            'status': 'success',
            'datasets': datasets,
        })
def build_business_payload(building_snapshot):
    """Return business related data."""
    businesses = []
    for b in building_snapshot.businesses.all():
        business_dict = obj_to_dict(b)
        business_dict['meters'] = build_meter_payload(business=b)
        business_dict['contact'] = b.owner.to_dict()
        business_dict.update(
            build_utility_payload(building_snapshot, business=b))
        businesses.append(business_dict)

    return businesses
示例#11
0
    def list(self, request):
        """
        Retrieves all datasets for the user's organization.
        """

        org_id = request.query_params.get('organization_id', None)
        org = Organization.objects.get(pk=org_id)
        datasets = []
        for d in ImportRecord.objects.filter(super_organization=org):
            importfiles = [obj_to_dict(f) for f in d.files]
            dataset = obj_to_dict(d)
            dataset['importfiles'] = importfiles
            if d.last_modified_by:
                dataset['last_modified_by'] = d.last_modified_by.email
            dataset['updated_at'] = convert_to_js_timestamp(d.updated_at)
            datasets.append(dataset)

        return JsonResponse({
            'status': 'success',
            'datasets': datasets,
        })
def build_business_payload(building_snapshot):
    """Return business related data."""
    businesses = []
    for b in building_snapshot.businesses.all():
        business_dict = obj_to_dict(b)
        business_dict['meters'] = build_meter_payload(business=b)
        business_dict['contact'] = b.owner.to_dict()
        business_dict.update(
            build_utility_payload(building_snapshot, business=b)
        )
        businesses.append(business_dict)

    return businesses
示例#13
0
    def list(self, request):
        """
        Retrieves all datasets for the user's organization.
        ---
        type:
            status:
                required: true
                type: string
                description: Either success or error
            datasets:
                required: true
                type: array[dataset]
                description: Returns an array where each item is a full dataset structure, including
                             keys ''name'', ''number_of_buildings'', ''id'', ''updated_at'',
                             ''last_modified_by'', ''importfiles'', ...
        parameters:
            - name: organization_id
              description: The organization_id for this user's organization
              required: true
              paramType: query
        """

        org_id = request.query_params.get('organization_id', None)
        org = Organization.objects.get(pk=org_id)
        datasets = []
        for d in ImportRecord.objects.filter(super_organization=org):
            importfiles = [obj_to_dict(f) for f in d.files]
            dataset = obj_to_dict(d)
            dataset['importfiles'] = importfiles
            if d.last_modified_by:
                dataset['last_modified_by'] = d.last_modified_by.email
            dataset['updated_at'] = convert_to_js_timestamp(d.updated_at)
            datasets.append(dataset)

        return JsonResponse({
            'status': 'success',
            'datasets': datasets,
        })
示例#14
0
    def retrieve(self, request, pk=None):
        """
        Retrieves details about an ImportFile.
        """
        import_file_id = pk
        orgs = request.user.orgs.all()
        try:
            import_file = ImportFile.objects.get(pk=import_file_id)
            d = ImportRecord.objects.filter(super_organization__in=orgs,
                                            pk=import_file.import_record_id)
        except ObjectDoesNotExist:
            return JsonResponse(
                {
                    'status': 'error',
                    'message': 'Could not access an import file with this ID'
                },
                status=status.HTTP_403_FORBIDDEN)
        # check if user has access to the import file
        if not d.exists():
            return JsonResponse(
                {
                    'status': 'error',
                    'message': 'Could not locate import file with this ID',
                    'import_file': {},
                },
                status=status.HTTP_400_BAD_REQUEST)

        f = obj_to_dict(import_file)
        f['name'] = import_file.filename_only
        if not import_file.uploaded_filename:
            f['uploaded_filename'] = import_file.filename
        f['dataset'] = obj_to_dict(import_file.import_record)

        return JsonResponse({
            'status': 'success',
            'import_file': f,
        })
示例#15
0
文件: meters.py 项目: Maalka/seed
def get_meters(request):
    """Returns all of the meters for a building.

    Expected GET params:

    building_id: int, unique identifier for a (canonical) building.
    """
    building_id = request.GET.get('building_id', '')
    if not building_id:
        return {'status': 'error', 'message': 'No building id specified'}

    return {
        'status': 'success', 'building_id': building_id, 'meters': [
            obj_to_dict(m) for m in Meter.objects.filter(
                building_snapshot=building_id
            )
        ]
    }
示例#16
0
文件: meters.py 项目: teeterc/seed
def get_meters(request):
    """Returns all of the meters for a building.

    Expected GET params:

    building_id: int, unique identifier for a (canonical) building.
    """
    building_id = request.GET.get('building_id', '')
    if not building_id:
        return {'status': 'error', 'message': 'No building id specified'}

    return {
        'status': 'success', 'building_id': building_id, 'meters': [
            obj_to_dict(m) for m in Meter.objects.filter(
                building_snapshot=building_id
            )
        ]
    }
示例#17
0
文件: models.py 项目: andremichi/seed
 def to_dict(self):
     """serializes an audit_log"""
     # avoid cyclical import
     from seed.models import obj_to_dict
     log_dict = obj_to_dict(self)
     log_dict['audit_type'] = self.get_audit_type_display()
     log_dict['user'] = {
         'first_name': self.user.first_name,
         'last_name': self.user.last_name,
         'email': self.user.email,
         'id': self.user.pk,
     }
     log_dict['organization'] = {
         'id': self.organization.pk,
         'name': self.organization.name,
     }
     log_dict['content_type'] = self.content_object._meta.model_name
     return log_dict
示例#18
0
 def to_dict(self):
     """serializes an audit_log"""
     # avoid cyclical import
     from seed.models import obj_to_dict
     log_dict = obj_to_dict(self)
     log_dict['audit_type'] = self.get_audit_type_display()
     log_dict['user'] = {
         'first_name': self.user.first_name,
         'last_name': self.user.last_name,
         'email': self.user.email,
         'id': self.user.pk,
     }
     log_dict['organization'] = {
         'id': self.organization.pk,
         'name': self.organization.name,
     }
     log_dict['content_type'] = self.content_object._meta.model_name
     return log_dict
示例#19
0
    def timeseries(self, request, pk=None):
        """
        Returns timeseries for meter
        ---
        type:
            status:
                required: true
                type: string
                description: Either success or error
            meter:
                required: true
                type: dict
                description: meter information
            data:
                required: true
                type: list
                description: timeseries information
        parameters:
            - name: pk
              description: Meter primary key
              required: true
              paramType: path
        """
        meter = Meter.objects.get(pk=pk)
        res = {
            'status': 'success',
            'meter': obj_to_dict(meter),
        }
        res['meter']['data'] = []

        ts = meter.timeseries_set.order_by('begin_time')
        for t in ts:
            res['meter']['data'].append({
                'begin': str(t.begin_time),
                'end': str(t.begin_time),
                'value': t.reading,
            })

        return JsonResponse(res)
示例#20
0
 def __unicode__(self):
     return json.dumps(obj_to_dict(self))
 def to_dict(self):
     # avoid circular import
     from seed.models import obj_to_dict
     return obj_to_dict(self)
示例#22
0
def get_import_file(request):
    """returns an import file if the user has permission
        The data set/ImportRecord id comes in as the GET param `import_file_id`
        returns:
        {
            "name": "DC_CoveredBuildings_50k.csv",
            "number_of_buildings": 511,
            "number_of_mappings": 511,
            "number_of_cleanings": 1349,
            "source_type": "AssessorRaw",
            "number_of_matchings": 403,
            "id": 1,
            "dataset": {
                "name": "DC dataset"
                "id": 1,
                "importfiles": [
                    {
                        "name": "DC_CoveredBuildings_50k.csv",
                        "id": 1
                    },
                    {
                        "name": "DC_PM_report.csv",
                        "id": 2
                    }
                ]
            }
        }
    """
    from seed.models import obj_to_dict
    import_file_id = request.GET.get('import_file_id', '')
    orgs = request.user.orgs.all()
    import_file = ImportFile.objects.get(
        pk=import_file_id
    )
    d = ImportRecord.objects.filter(
        super_organization__in=orgs, pk=import_file.import_record_id
    )
    # check if user has access to the import file
    if not d.exists():
        return {
            'status': 'success',
            'import_file': {},
        }

    f = obj_to_dict(import_file)
    f['name'] = import_file.filename_only
    f['dataset'] = obj_to_dict(import_file.import_record)
    # add the importfiles for the matching select
    f['dataset']['importfiles'] = []
    files = f['dataset']['importfiles']
    for i in import_file.import_record.files:
        files.append({
            'name': i.filename_only,
            'id': i.pk
        })
    # make the first element in the list the current import file
    i = files.index({
        'name': import_file.filename_only,
        'id': import_file.pk
    })
    files[0], files[i] = files[i], files[0]

    return {
        'status': 'success',
        'import_file': f,
    }
示例#23
0
    def retrieve(self, request, pk=None):
        """
            Retrieves a dataset (ImportRecord).
            ---
            type:
                status:
                    required: true
                    type: string
                    description: Either success or error
                dataset:
                    required: true
                    type: dictionary
                    description: A dictionary of a full dataset structure, including
                                 keys ''name'', ''id'', ''updated_at'',
                                 ''last_modified_by'', ''importfiles'', ...
            parameter_strategy: replace
            parameters:
                - name: pk
                  description: The ID of the dataset to retrieve
                  required: true
                  paramType: path
                - name: organization_id
                  description: The organization_id for this user's organization
                  required: true
                  paramType: query
        """

        organization_id = request.query_params.get('organization_id', None)
        if organization_id is None:
            return JsonResponse(
                {
                    'status': 'error',
                    'message': 'Missing organization_id query parameter'
                },
                status=status.HTTP_400_BAD_REQUEST)
        try:
            organization_id = int(organization_id)
        except ValueError:
            return JsonResponse(
                {
                    'status': 'error',
                    'message': 'Bad (non-numeric) organization_id'
                },
                status=status.HTTP_400_BAD_REQUEST)

        valid_orgs = OrganizationUser.objects.filter(
            user_id=request.user.id).values_list(
                'organization_id', flat=True).order_by('organization_id')
        if organization_id not in valid_orgs:
            return JsonResponse(
                {
                    'status': 'error',
                    'message':
                    'Cannot access datasets for this organization id',
                },
                status=status.HTTP_403_FORBIDDEN)

        # check if dataset exists
        try:
            d = ImportRecord.objects.get(pk=pk)
        except ImportRecord.DoesNotExist:
            return JsonResponse(
                {
                    'status': 'error',
                    'message': 'dataset with id {} does not exist'.format(pk)
                },
                status=status.HTTP_404_NOT_FOUND)

        if d.super_organization_id != organization_id:
            return JsonResponse(
                {
                    'status':
                    'error',
                    'message':
                    'Organization ID mismatch between dataset and organization'
                },
                status=status.HTTP_400_BAD_REQUEST)

        dataset = obj_to_dict(d)
        importfiles = []
        for f in d.files:
            importfile = obj_to_dict(f)
            if not f.uploaded_filename:
                importfile['name'] = f.filename_only
            else:
                importfile['name'] = f.uploaded_filename
            importfiles.append(importfile)

        dataset['importfiles'] = importfiles
        if d.last_modified_by:
            dataset['last_modified_by'] = d.last_modified_by.email
        dataset['updated_at'] = convert_to_js_timestamp(d.updated_at)

        return JsonResponse({
            'status': 'success',
            'dataset': dataset,
        })
示例#24
0
 def to_dict(self):
     # avoid circular import
     from seed.models import obj_to_dict
     return obj_to_dict(self)
示例#25
0
文件: datasets.py 项目: mmclark/seed
    def retrieve(self, request, pk=None):
        """
            Retrieves a dataset (ImportRecord).
            ---
            type:
                status:
                    required: true
                    type: string
                    description: Either success or error
                dataset:
                    required: true
                    type: dictionary
                    description: A dictionary of a full dataset structure, including
                                 keys ''name'', ''number_of_buildings'', ''id'', ''updated_at'',
                                 ''last_modified_by'', ''importfiles'', ...
            parameter_strategy: replace
            parameters:
                - name: pk
                  description: "Primary Key"
                  required: true
                  paramType: path
                - name: organization_id
                  description: The organization_id for this user's organization
                  required: true
                  paramType: query
        """

        organization_id = request.query_params.get('organization_id', None)
        if organization_id is None:
            return JsonResponse({'status': 'error', 'message': 'Missing organization_id query parameter'})
        try:
            organization_id = int(organization_id)
        except ValueError:
            return JsonResponse({'status': 'error', 'message': 'Bad (non-numeric) organization_id'})

        dataset_id = pk

        # check if user has access to the dataset
        d = ImportRecord.objects.filter(
            super_organization_id=organization_id, pk=dataset_id
        )
        if d.exists():
            d = d[0]
        else:
            return JsonResponse({
                'status': 'success',
                'dataset': {},
            })

        dataset = obj_to_dict(d)
        importfiles = []
        for f in d.files:
            importfile = obj_to_dict(f)
            if not f.uploaded_filename:
                importfile['name'] = f.filename_only
            else:
                importfile['name'] = f.uploaded_filename
            importfiles.append(importfile)

        dataset['importfiles'] = importfiles
        if d.last_modified_by:
            dataset['last_modified_by'] = d.last_modified_by.email
        dataset['number_of_buildings'] = BuildingSnapshot.objects.filter(
            import_file__in=d.files
        ).count()
        dataset['updated_at'] = convert_to_js_timestamp(d.updated_at)

        return JsonResponse({
            'status': 'success',
            'dataset': dataset,
        })
示例#26
0
 def __str__(self):
     return json.dumps(obj_to_dict(self))
示例#27
0
    def create(self, request):
        """
        Creates a new project

        :POST: Expects organization_id in query string.
        ---
        parameters:
            - name: organization_id
              description: ID of organization to associate new project with
              type: integer
              required: true
              paramType: query
            - name: property_view_id
              description: Property view id to which to add the meter
              required: true
              paramType: form
            - name: name
              description: name of the new meter
              type: string
              required: true
              paramType: form
            - name: energy_type
              description: type of metered energy
              type: integer
              required: true
              paramType: form
            - name: energy_units
              description: units of energy being metered
              type: integer
              required: true
              paramType: form
        type:
            status:
                required: true
                type: string
                description: Either success or error

        """
        org_id = request.GET.get('organization_id', '')

        # verify that the user has access to view property
        pv_id = request.data['property_view_id']
        pvs = PropertyView.objects.filter(id=pv_id, state__organization=org_id)
        if pvs.count() == 0 or pvs.count() > 1:
            return JsonResponse({
                'status': 'success',
                'message': 'No property id {} found for organization {}'.format(pv_id, org_id),
            })
        else:
            #     energy_type = _convert_energy_data(energy_type_name, ENERGY_TYPES)
            #     energy_units = _convert_energy_data(energy_unit_name, ENERGY_UNITS)
            data = {
                "name": request.data['name'],
                "energy_type": request.data['energy_type'],
                "energy_units": request.data['energy_units'],
                "property_view": pvs.first(),
            }
            m = Meter.objects.create(**data)

            return JsonResponse({
                'status': 'success',
                'meter': obj_to_dict(m),
            })
示例#28
0
    def retrieve(self, request, pk=None):
        """
        Retrieves details about an ImportFile.
        ---
        type:
            status:
                required: true
                type: string
                description: either success or error
            import_file:
                type: ImportFile structure
                description: full detail of import file
        parameter_strategy: replace
        parameters:
            - name: pk
              description: "Primary Key"
              required: true
              paramType: path
        """

        import_file_id = pk
        orgs = request.user.orgs.all()
        try:
            import_file = ImportFile.objects.get(
                pk=import_file_id
            )
            d = ImportRecord.objects.filter(
                super_organization__in=orgs, pk=import_file.import_record_id
            )
        except ObjectDoesNotExist:
            return JsonResponse({
                'status': 'error',
                'message': 'Could not access an import file with this ID'
            }, status=status.HTTP_403_FORBIDDEN)
        # check if user has access to the import file
        if not d.exists():
            return JsonResponse({
                'status': 'error',
                'message': 'Could not locate import file with this ID',
                'import_file': {},
            }, status=status.HTTP_400_BAD_REQUEST)

        f = obj_to_dict(import_file)
        f['name'] = import_file.filename_only
        if not import_file.uploaded_filename:
            f['uploaded_filename'] = import_file.filename
        f['dataset'] = obj_to_dict(import_file.import_record)
        # add the importfiles for the matching select
        f['dataset']['importfiles'] = []
        files = f['dataset']['importfiles']
        for i in import_file.import_record.files:
            tmp_uploaded_filename = i.filename_only
            if i.uploaded_filename:
                tmp_uploaded_filename = i.uploaded_filename

            files.append({
                'name': i.filename_only,
                'uploaded_filename': tmp_uploaded_filename,
                'id': i.pk
            })

        # make the first element in the list the current import file
        i = next(index for (index, d) in enumerate(files) if d["id"] == import_file.pk)
        files[0], files[i] = files[i], files[0]

        return JsonResponse({
            'status': 'success',
            'import_file': f,
        })