def exportToPdf(company_id, user_id, template_name, source_type, content_type, phantomjs_script, url, token, sessionid, authenticatedAccount, file_name): #, output, error try: user_id = ObjectId(user_id) output = NamedTemporaryFile(delete=False) error = NamedTemporaryFile(delete=False) external_process = Popen(["phantomjs", phantomjs_script, url, token, sessionid, authenticatedAccount, file_name], stdout=output, stderr=error) # external_process.communicate(30) export_file = open(file_name, 'rb') exportFile = ExportFile(company_id=company_id, owner_id=user_id, source=template_name, source_type=source_type, type=content_type, file_name=os.path.basename(file_name)) exportFile.file.put(export_file, content_type=content_type) exportFile.save() try: message = 'PDF file successfully exported' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your export of data from ' + template_name + ' is ready. It is available in My Exports with the file name ' + os.path.basename(file_name) + '.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Your PDF export is baked and ready', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) except Exception as e: print 'exception was ' + str(e) return str(e)
def exportToCsv(object_type, system_code, data, source_type, chart_name, user_id, company_id): print 'in export to csv' if object_type is None or system_code is None or data is None: print 'returning due to none' return try: if object_type == 'lead': result = exportLeadsToCsv(system_code, data, chart_name, user_id, company_id) print 'got result ' + str(result) file_name = result['file_name'] if file_name != '': content_type = result['content_type'] export_file = open(file_name, 'rb') exportFile = ExportFile(company_id=company_id, owner_id=user_id, source=chart_name, source_type=source_type, type=content_type, file_name=os.path.basename(file_name)) exportFile.file.put(export_file, content_type=content_type) exportFile.save() try: message = 'CSV file successfully exported' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your download of data from ' + chart_name + ' is ready. It is available in My Exports with the file name ' + os.path.basename(file_name) + '.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Your CSV export is baked and ready', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) else: try: message = 'CSV download failed' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your download of data from ' + chart_name + ' failed. Please contact the Claritix team so that we can look into this.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Oh no! Your CSV download failed', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) else: return except Exception as e: print 'exception while trying to save CSV file: ' + str(e) send_notification(dict(type='error', success=False, message=str(e)))