def get(self, request, *args, **kwargs):
        """get smart grid total consumption/production stats"""

        consumer = services.bearer_from_access_token(
            request.META.get('HTTP_AUTHORIZATION',
                             None).replace('Bearer ', ''))
        if consumer.tipo != SCOPE:
            return HttpResponse(JsonResponse({
                'message': 'no grants for this operation',
                'success': False
            }),
                                status=403)

        zone_id = request.GET.get('zone_id', default=None)
        if zone_id is None:
            return HttpResponse(JsonResponse({
                'message': 'invalid parameters',
                'success': False
            }),
                                status=400)

        try:
            sm = Smartgrid(consumer.gestore.remote_db)
            data = sm.getStats(zone_id)
            return HttpResponse(JsonResponse({'data': data, 'success': True}))
        except Exception as e:
            error_message = "internal error {0} : {1}".format(
                str(self.__class__.__name__), str(e))
            logger.error(error_message)
            return HttpResponse(JsonResponse({
                'message': error_message,
                'success': False
            }),
                                status=500)
예제 #2
0
    def get(self, request, *args, **kwargs):
        """get smart grid total consumption/production stats"""
        token = request.GET.get('token', default=None)
        zone_id = request.GET.get('zone_id', default=None)
        remote_db = request.GET.get('remote_db', default='')
        if token is None or zone_id is None:
            return HttpResponse(JsonResponse({'message':
                                              'invalid parameters'}),
                                status=400)
        elif not old_auth.check_token(token):
            return HttpResponse(JsonResponse(
                {'message': 'invalid or expired token'}),
                                status=401)

        try:
            sm = Smartgrid(remote_db)
            data = sm.getStats(zone_id)
            return HttpResponse(JsonResponse(data))
        except Exception as e:
            logger.error("{0} : {1}".format(str(self.__class__.__name__),
                                            str(e)))
            return HttpResponse(JsonResponse(
                {'message': 'internal error %s' % str(e)}),
                                status=500)