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)
def get(self, request, *args, **kwargs): 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) fiscal_sn = request.GET.get('fiscal_sn', None) verification_code = request.GET.get('verification_code', None) if fiscal_sn is None or verification_code is None: return HttpResponse(JsonResponse({ 'message': 'invalid parameters', 'success': False }), status=400) try: #l'utente corrente = l'applicazione consumer sm = Smartgrid(consumer.gestore.remote_db) user_id = sm.getUserId( fiscal_sn, verification_code ) #codice univoco solo entro il db del gestore if user_id is None: return HttpResponse(JsonResponse({ 'message': 'parameter mismatch', 'success': False }), status=404) else: already_registered = services.is_already_registered( user_id, consumer.gestore.id) if already_registered: return HttpResponse(JsonResponse({ 'message': 'already registered', 'success': False }), status=405) else: return HttpResponse( JsonResponse({ 'check': True, '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)
def get(self, request, *args, **kwargs): """returns user instant consumption""" 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) nome_tabella = request.GET.get('nome_tabella', None) sender_id = request.GET.get('sender_id', None) if nome_tabella is None: return HttpResponse(JsonResponse({ 'message': 'parameter nome_tabella not specified', 'success': False }), status=400) try: print "ApiErpGetLastTransmission remote db = " + consumer.gestore.remote_db sm = Smartgrid(consumer.gestore.remote_db) data = sm.getLastTransmission(sender_id, nome_tabella) if data is None: print "Not data found for last transmission of smartmeter: ", sender_id return HttpResponse(JsonResponse({ 'message': 'No smartmeters found', 'success': False }), status=404) else: print "Found data for last transmission of smartmeter: ", sender_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)) print "Error message for getLastTransmission :" + error_message if logger is None: print "Il logger risulta None" else: logger.error(error_message) return HttpResponse(JsonResponse({ 'message': error_message, 'success': False }), status=500)
def get(self, request, *args, **kwargs): """returns user specific invoice list""" 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) contract_id = request.GET.get('contract_id', None) if contract_id is None: return HttpResponse(JsonResponse({ 'message': 'invalid parameter', 'success': False }), status=400) try: sm = Smartgrid(consumer.gestore.remote_db) if not sm.checkContract(contract_id, consumer.smartgrid_client_id): return HttpResponse(JsonResponse({ 'message': 'no grants for this operation', 'success': False }), status=403) data = sm.getInvoiceList(contract_id) if data is None: return HttpResponse(JsonResponse({ 'message': 'contract %s not found' % str(contract_id), 'success': False }), status=404) else: 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)
def get(self, request, *args, **kwargs): """returns user's contracts info""" 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) try: sm = Smartgrid(consumer.gestore.remote_db) user_id = consumer.smartgrid_client_id data = sm.getContracts(user_id) if data is None: return HttpResponse(JsonResponse({ 'message': 'no contracts for user: %d' % int(user_id), 'success': False }), status=404) else: 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)
def get(self, request, *args, **kwargs): """returns data if allarm are present on sm""" consumer = services.bearer_from_access_token( request.META.get('HTTP_AUTHORIZATION', None).replace('Bearer ', '')) print "consumer: ", consumer if consumer.tipo != SCOPE: return HttpResponse(JsonResponse({ 'message': 'no grants for this operation', 'success': False }), status=403) contract_id = request.GET.get('contract_id', None) if contract_id is None: return HttpResponse(JsonResponse({ 'message': 'invalid parameter', 'success': False }), status=400) try: sm = Smartgrid(consumer.gestore.remote_db) data = sm.getAlarmSmartmeter(contract_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)
def get(self, request, *args, **kwargs): """get smart grid total alarms 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.getAlarms(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)
def get(self, request, *args, **kwargs): """returns an user invoice file""" 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) contract_id = request.GET.get('contract_id', None) invoice_id = request.GET.get('invoice_id', None) if contract_id is None or invoice_id is None: return HttpResponse(JsonResponse({ 'message': 'invalid parameters', 'success': False }), status=400) try: sm = Smartgrid(consumer.gestore.remote_db) if not sm.checkContract(contract_id, consumer.smartgrid_client_id): return HttpResponse(JsonResponse({ 'message': 'no grants for this operation', 'success': False }), status=403) fileName = sm.getInvoiceFileName(contract_id, invoice_id) if fileName is None: return HttpResponse(JsonResponse({ 'message': 'invoice %s for contract %s not found' % ((str(invoice_id)), str(contract_id)), 'success': False }), status=404) else: from protected_api.utils.s3Bucket import S3Bucket s3_file_name = 'prod/{file_name}'.format(file_name=fileName) bucket = S3Bucket(consumer.gestore.remote_db) response = FileResponse(bucket.getFileAsString(s3_file_name), content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename="fattura.pdf"' return HttpResponse(response) 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)
def get(self, request, *args, **kwargs): 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) fiscal_sn = request.GET.get('fiscal_sn', None) verification_code = request.GET.get('verification_code', None) if fiscal_sn is None or verification_code is None: return HttpResponse(JsonResponse({ 'message': 'invalid parameters', 'success': False }), status=400) try: # l'utente corrente = l'applicazione consumer sm = Smartgrid(consumer.gestore.remote_db) user_id = sm.getUserId( fiscal_sn, verification_code ) # codice univoco solo entro il db del gestore if user_id is None: return HttpResponse(JsonResponse({ 'message': 'parameter mismatch', 'success': False }), status=404) else: # crea l'applicazione id_cliente = '' + str(consumer.gestore.name) + '-' + str( user_id) + '-' + str(fiscal_sn) application = services.create_application( fiscal_sn, user_id, consumer.gestore.name, id_cliente) if application is None: return HttpResponse(JsonResponse({ 'message': 'cannot register user', 'success': False }), status=405) else: data = { 'user_id': user_id, 'client_id': application.client_id, 'client_secret': application.client_secret } 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)
def get(self, request, *args, **kwargs): """returns user instant consumption""" 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) nome_campo_select = request.GET.get('nome_campo_select', None) nome_tabella = request.GET.get('nome_tabella', None) nome_campo_where = request.GET.get('nome_campo_where', None) valore_campo_where = request.GET.get('valore_campo_where', None) if nome_campo_select is None: return HttpResponse(JsonResponse({ 'message': 'parameter nome_campo_select not specified', 'success': False }), status=400) if nome_tabella is None: return HttpResponse(JsonResponse({ 'message': 'parameter nome_tabella not specified', 'success': False }), status=400) if nome_campo_where is None: return HttpResponse(JsonResponse({ 'message': 'parameter nome_campo_where not specified', 'success': False }), status=400) if valore_campo_where is None: return HttpResponse(JsonResponse({ 'message': 'parameter valore_campo_where not specified', 'success': False }), status=400) try: print "Remote db = " + consumer.gestore.remote_db sm = Smartgrid(consumer.gestore.remote_db) data = sm.getData(nome_campo_select, nome_tabella, nome_campo_where, valore_campo_where) if data is None: print "ApiErpGetData : data is None :(" return HttpResponse(JsonResponse({ 'message': 'No values found', 'success': False }), status=404) else: print "ApiErpGetData : data is not None ! :)" return HttpResponse( JsonResponse({ 'data': data, 'success': True })) except Exception as e: error_message = "internal error {0} : {1}".format( str(self.__class__.__name__), str(e)) print "Error message:" + error_message if logger is None: print "Il logger risulta None" else: logger.error(error_message) return HttpResponse(JsonResponse({ 'message': error_message, 'success': False }), status=500)