def list(self, request): try: page_number = int(request.GET.get('page_number')) items_per_page = int(request.GET.get('per_page')) offset = (page_number - 1) * items_per_page user_id = request.user.id results = ExportFile.objects(owner_id=user_id).exclude("file").skip(offset).limit(items_per_page).order_by('-updated_date') count = ExportFile.objects(owner_id=user_id).count() serializedList = ExportFilesSerializer(results, many=True) return JsonResponse({'results' : serializedList.data, 'count': count}) except Exception as e: return Response(str(e))
def list(self, request, file_id): try: user_id = request.user.id result = ExportFile.objects(Q(owner_id=user_id) & Q(id=ObjectId(file_id))).first() if result is None: return JsonResponse({'Error' : 'File not found to download'}) export_file = result.file chunk_size = 8192 response = StreamingHttpResponse(FileWrapper(export_file, chunk_size), content_type=result['type']) response['Content-Disposition'] = "attachment; filename=%s" % os.path.basename(result['file_name']) return response except Exception as e: return Response(str(e))
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)))