syncing with upstream
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import io
|
||||
import os
|
||||
import uuid
|
||||
|
||||
import requests
|
||||
|
||||
@@ -12,45 +11,50 @@ from helix.doc_gen_params_builder import DocGenParamsBuilder
|
||||
from helix.helpers.camel_case import convert_dict_keys_to_snake_case
|
||||
from helix.json_builder import JsonBuilder
|
||||
from helix.presenters.image_presenter import ImagePresenter
|
||||
from helix.qa_helper import QAScenario, QAException
|
||||
from helix.Services.doc_gen_service import DocGenService
|
||||
from helix.Services.s3_helper import s3_upload
|
||||
from helix.session_manager import SessionManager
|
||||
|
||||
|
||||
def get_site_characterization_from_sales_force(session):
|
||||
access_token = session['sales_force_token']
|
||||
sfid = session['SFID']
|
||||
helix_id = session['id']
|
||||
def get_site_characterization_from_sales_force(helix_session_id, access_token, sfid):
|
||||
SFDC_API_URL = os.getenv('SFDC_API_URL', 'https://sunpower--qa.cs8.my.salesforce.com')
|
||||
url = SFDC_API_URL + '/services/apexrest/v1/HelixRoofDetails'
|
||||
headers = {'Authorization': 'Bearer {}'.format(access_token)}
|
||||
result = requests.get(url, headers=headers, params={'SFID': sfid, 'helix_session_id': helix_id})
|
||||
params = {'sfid': sfid, 'helix_session_id': helix_session_id}
|
||||
result = requests.get(url, headers=headers, params=params, timeout=30)
|
||||
if result.status_code == 200:
|
||||
data = result.json()
|
||||
if data:
|
||||
data = convert_sales_force_data_format_to_helix(data)
|
||||
return data
|
||||
return data, 200
|
||||
else:
|
||||
print('Error while getting data from Sales Force: {}'.format(result.status_code))
|
||||
print('Error while getting data from Salesforce ({})'.format(result.status_code))
|
||||
print(url, params)
|
||||
print(result.content)
|
||||
# print(result.request.headers)
|
||||
return None, result.status_code
|
||||
|
||||
|
||||
def convert_sales_force_data_format_to_helix(data):
|
||||
data = convert_dict_keys_to_snake_case(data)
|
||||
|
||||
if data['system_type'] == 'Single-Tilt':
|
||||
if data['system_type'] in ('Single-Tilt', 'Single Tilt'):
|
||||
data['system_type'] = SystemType.singleTilt.value
|
||||
elif data['system_type'] == 'Dual-Tilt':
|
||||
elif data['system_type'] == ('Dual-Tilt', 'Dual Tilt'):
|
||||
data['system_type'] = SystemType.dualTilt.value
|
||||
|
||||
if data['anchor_type'] == 'OMG Power Grip Plus':
|
||||
data['anchor_type'] = 'OMG PowerGrip Plus'
|
||||
|
||||
data['ballast_block_weight'] = data['ballast_weight']
|
||||
data['max_system_pressure'] = data['max_psf'] = data['system_pressure']
|
||||
return data
|
||||
# data['spectral_response_acceleration']
|
||||
|
||||
|
||||
def export_to_sfdc(helix_session_id, access_token, sfid):
|
||||
step = 'Exporting to SFDC'
|
||||
def export_to_sfdc(helix_session_id, access_token, sfid, new_dxf_file=False, qa_test=None):
|
||||
step = 'Exporting to Salesforce'
|
||||
try:
|
||||
# 1. Load User Values
|
||||
step = 'Loading User Values'
|
||||
@@ -83,36 +87,43 @@ def export_to_sfdc(helix_session_id, access_token, sfid):
|
||||
|
||||
# 5. Save CSV/PDF/DXF files into AWS-S3
|
||||
step = 'Uploading to S3'
|
||||
filename = uuid.uuid4().hex
|
||||
bom_csv_url = s3_upload(io.StringIO(csv_file), filename=filename + '.csv')
|
||||
doc_url = s3_upload(io.BytesIO(document), filename=filename + '.pdf')
|
||||
bom_json_url = s3_upload(io.StringIO(bom_list_json), filename=filename + '.json')
|
||||
if dxf_contents: # Optional
|
||||
dxf_url = s3_upload(io.StringIO(dxf_contents), filename=filename + '.dxf')
|
||||
if qa_test == QAScenario.SF_ERROR_UPLOAD_S3.value:
|
||||
raise QAException(qa_test)
|
||||
|
||||
bom_csv_url = s3_upload(io.StringIO(csv_file), filename=sfid + '-csv.csv')
|
||||
doc_url = s3_upload(io.BytesIO(document), filename=sfid + '-pdf.pdf')
|
||||
bom_json_url = s3_upload(io.StringIO(bom_list_json), filename=sfid + '-json.json')
|
||||
# Send only if the file DXF file was uploaded in Helix
|
||||
if new_dxf_file and dxf_contents: # Optional
|
||||
dxf_url = s3_upload(io.StringIO(dxf_contents), filename=sfid + '-dxf.dxf')
|
||||
else:
|
||||
dxf_url = None
|
||||
|
||||
# 6. Notify SFDC system with an API request
|
||||
step = 'Notifying SFDC'
|
||||
step = 'Notifying Salesforce'
|
||||
if qa_test == QAScenario.SF_ERROR_NOTIFY_SF.value:
|
||||
raise QAException(qa_test)
|
||||
|
||||
data = {
|
||||
'dxfUrl': dxf_url,
|
||||
'bomCsvUrl': bom_csv_url,
|
||||
'documentationUrl': doc_url,
|
||||
'bom': bom_list,
|
||||
'helixSessionId': helix_session_id,
|
||||
'SFID': sfid,
|
||||
'sfid': sfid,
|
||||
}
|
||||
headers = {'Authorization': 'Bearer {}'.format(access_token)}
|
||||
SFDC_API_URL = os.getenv('SFDC_API_URL', 'https://sunpower--qa.cs8.my.salesforce.com')
|
||||
url = SFDC_API_URL + '/services/apexrest/v1/HelixRoofDetails'
|
||||
result = requests.post(url, headers=headers, data=data, timeout=30)
|
||||
result = requests.post(url, headers=headers, json=data, timeout=30)
|
||||
# 7. Internal logs
|
||||
if result.status_code <= 299:
|
||||
print('Sales Force notified successfully for session {}.'.format(helix_session_id))
|
||||
print('Salesforce notified successfully for session {}.'.format(helix_session_id))
|
||||
# print(result.content)
|
||||
error = None
|
||||
else:
|
||||
error = 'Helix wasn\'t able to notify the Sales Force ({})'.format(result.status_code)
|
||||
error = 'Helix Calculator was not able to notify Salesforce ({})'.format(result.status_code)
|
||||
|
||||
print(error)
|
||||
print(result.content)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user