merging with upstream
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import io
|
||||
import os
|
||||
import uuid
|
||||
|
||||
import requests
|
||||
@@ -16,14 +17,12 @@ from helix.Services.s3_helper import s3_upload
|
||||
from helix.session_manager import SessionManager
|
||||
|
||||
|
||||
def get_site_characterization_from_sales_force(session, base_url):
|
||||
'''
|
||||
@base_url: Avoid URL_NOT_RESET errors
|
||||
'''
|
||||
def get_site_characterization_from_sales_force(session):
|
||||
access_token = session['sales_force_token']
|
||||
sfid = session['SFID']
|
||||
helix_id = session['id']
|
||||
url = base_url + '/services/apexrest/v1/HelixRoofDetails'
|
||||
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})
|
||||
if result.status_code == 200:
|
||||
@@ -50,12 +49,12 @@ def convert_sales_force_data_format_to_helix(data):
|
||||
# data['spectral_response_acceleration']
|
||||
|
||||
|
||||
def export_to_sfdc(session_id):
|
||||
def export_to_sfdc(helix_session_id, access_token, sfid):
|
||||
step = 'Exporting to SFDC'
|
||||
try:
|
||||
# 1. Load User Values
|
||||
step = 'Loading User Values'
|
||||
session = {'id': session_id}
|
||||
session = {'id': helix_session_id}
|
||||
db_session = sql_constant.sql_session_maker()
|
||||
session_manager = SessionManager(session, redis_constant.redis_store, db_session)
|
||||
user_values = session_manager.user_values()
|
||||
@@ -67,7 +66,9 @@ def export_to_sfdc(session_id):
|
||||
csv_file = CsvBuilder().build_bom_output(bom)
|
||||
# 2.1 Generate BOM CSV file
|
||||
step = 'Generating BOM as JSON'
|
||||
json_str = JsonBuilder().build_bom_output(bom)
|
||||
json_builder = JsonBuilder()
|
||||
bom_list = json_builder.build_bom(bom)
|
||||
bom_list_json = json_builder.bom_to_json(bom_list)
|
||||
|
||||
# 3. Generate DOCUMENTATION PDF file
|
||||
step = 'Generating Documentation'
|
||||
@@ -84,8 +85,8 @@ def export_to_sfdc(session_id):
|
||||
step = 'Uploading to S3'
|
||||
filename = uuid.uuid4().hex
|
||||
bom_csv_url = s3_upload(io.StringIO(csv_file), filename=filename + '.csv')
|
||||
bom_json_url = s3_upload(io.StringIO(json_str), filename=filename + '.json')
|
||||
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')
|
||||
else:
|
||||
@@ -93,26 +94,34 @@ def export_to_sfdc(session_id):
|
||||
|
||||
# 6. Notify SFDC system with an API request
|
||||
step = 'Notifying SFDC'
|
||||
SFDC_API_URL = 'https://localhost:8443/' # FIXME
|
||||
data = {
|
||||
'dxf_url': dxf_url,
|
||||
'bom_csv_url': bom_csv_url,
|
||||
'bom_json_url': bom_json_url,
|
||||
'documentation_url': doc_url,
|
||||
'dxfUrl': dxf_url,
|
||||
'bomCsvUrl': bom_csv_url,
|
||||
'documentationUrl': doc_url,
|
||||
'bom': bom_list,
|
||||
'helixSessionId': helix_session_id,
|
||||
'SFID': sfid,
|
||||
}
|
||||
print(data)
|
||||
# result = requests.post(SFDC_API_URL, data=data, timeout=30)
|
||||
|
||||
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)
|
||||
# 7. Internal logs
|
||||
# if result.status_code != 200: # FIXME
|
||||
# print('')
|
||||
# else:
|
||||
# print('')
|
||||
if result.status_code <= 299:
|
||||
print('Sales Force 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)
|
||||
print(error)
|
||||
print(result.content)
|
||||
|
||||
# Only Helix need this information for audit
|
||||
data['bomJsonUrl'] = bom_json_url
|
||||
|
||||
db_session.close()
|
||||
return data
|
||||
# return result.status_code
|
||||
return error, data
|
||||
except Exception as e:
|
||||
msg = 'Error while {} for session {}'.format(step, session_id)
|
||||
print(msg)
|
||||
raise e
|
||||
print('Error while {} for session {}'.format(step, helix_session_id))
|
||||
error = 'Error while {}'.format(step)
|
||||
return error, {}
|
||||
|
||||
Reference in New Issue
Block a user