def render_pdf(fileName=None, viewName=None, startTime=None, stopTime=None, interval=None, account=None, mail=None, owner=None, orientation='Portrait', pagesize='A4'): if not stopTime: stopTime = int(time.time()) if not startTime: if interval: startTime = stopTime - interval else: startTime = -1 if viewName is None: raise ValueError("task_render_pdf: you must at least provide a viewName") #check if the account is just a name or a real caccount if isinstance(account ,str) or isinstance(account ,unicode): root_account = caccount(user='******',group='root',mail='*****@*****.**') root_storage = cstorage(account=root_account, namespace='object') bd_account = root_storage.find_one(mfilter={'_id':'account.%s' % str(account)}) if isinstance(bd_account, crecord): account = caccount(bd_account) logger.info('Successfuly retrieve right user from db') else: account = caccount(mail='*****@*****.**') logger.info('Anonymous account created') #get view options storage = cstorage(account=account, namespace='object') try: view_record = storage.get(viewName,account=account) except: raise Exception("Impossible to find view '%s' with account '%s'" % (viewName, account._id)) #set fileName if fileName is None: toDate = date.fromtimestamp(int(stopTime)) if startTime and startTime != -1: fromDate = date.fromtimestamp(int(startTime)) fileName = '%s_From_%s_To_%s.pdf' % (view_record.name, fromDate, toDate) else: fileName = '%s_%s.pdf' % (view_record.name,toDate) logger.info('fileName: %s' % fileName) ascii_fileName = hashlib.md5(fileName.encode('ascii', 'ignore')).hexdigest() #get orientation and pagesize view_options = view_record.data.get('view_options', {}) if isinstance(view_options, dict): orientation = view_options.get('orientation', 'Portrait') pagesize = view_options.get('pageSize', 'A4') logger.info('Orientation: %s' % orientation) logger.info('Pagesize: %s' % pagesize) wrapper_conf_file = os.path.expanduser("~/etc/wkhtmltopdf_wrapper.json") file_path = open(wrapper_conf_file, "r").read() file_path = '%s/%s' % (json.loads(file_path)['report_dir'],ascii_fileName) #create wrapper object wkhtml_wrapper = Wrapper( ascii_fileName, viewName, startTime, stopTime, account, wrapper_conf_file, orientation=orientation, pagesize=pagesize) # Run rendering logger.debug('Run pdf rendering') wkhtml_wrapper.run_report() logger.info('Put it in grid fs: %s' % file_path) doc_id = put_in_grid_fs(file_path, fileName, account,owner) logger.debug('Remove tmp report file with docId: %s' % doc_id) os.remove(file_path) #Subtask mail (if needed) if isinstance(mail, dict): #get cfile try: reportStorage = cstorage(account=account, namespace='files') meta = reportStorage.get(doc_id) meta.__class__ = cfile except Exception, err: logger.error('Error while fetching cfile : %s' % err) try: mail['account'] = account mail['attachments'] = meta result = task_mail.send.subtask(kwargs=mail).delay() result.get() result = result.result #if subtask fail, raise exception if(result['success'] == False): raise Exception('Subtask mail have failed : %s' % result['celery_output'][0]) except Exception, err: logger.error(err) raise Exception('Impossible to send mail')
def render_pdf( fileName=None, viewName=None, startTime=None, stopTime=None, interval=None, account=None, mail=None, owner=None, orientation="Portrait", pagesize="A4", ): if not stopTime: stopTime = int(time.time()) if not startTime: if interval: startTime = stopTime - interval else: startTime = -1 if viewName is None: raise ValueError("task_render_pdf: you must at least provide a viewName") # check if the account is just a name or a real caccount if isinstance(account, str) or isinstance(account, unicode): root_account = caccount(user="******", group="root", mail="*****@*****.**") root_storage = cstorage(account=root_account, namespace="object") bd_account = root_storage.find_one(mfilter={"_id": "account.%s" % str(account)}) if isinstance(bd_account, crecord): account = caccount(bd_account) logger.info("Successfuly retrieve right user from db") else: account = caccount(mail="*****@*****.**") logger.info("Anonymous account created") # get view options storage = cstorage(account=account, namespace="object") try: view_record = storage.get(viewName, account=account) except: raise Exception("Impossible to find view '%s' with account '%s'" % (viewName, account._id)) # set fileName if fileName is None: toDate = date.fromtimestamp(int(stopTime)) if startTime and startTime != -1: fromDate = date.fromtimestamp(int(startTime)) fileName = "%s_From_%s_To_%s.pdf" % (view_record.name, fromDate, toDate) else: fileName = "%s_%s.pdf" % (view_record.name, toDate) logger.info("fileName: %s" % fileName) ascii_fileName = hashlib.md5(fileName.encode("ascii", "ignore")).hexdigest() # get orientation and pagesize view_options = view_record.data.get("view_options", {}) if isinstance(view_options, dict): orientation = view_options.get("orientation", "Portrait") pagesize = view_options.get("pageSize", "A4") logger.info("Orientation: %s" % orientation) logger.info("Pagesize: %s" % pagesize) wrapper_conf_file = os.path.expanduser("~/etc/wkhtmltopdf_wrapper.json") file_path = open(wrapper_conf_file, "r").read() file_path = "%s/%s" % (json.loads(file_path)["report_dir"], ascii_fileName) # create wrapper object wkhtml_wrapper = Wrapper( ascii_fileName, viewName, startTime, stopTime, account, wrapper_conf_file, orientation=orientation, pagesize=pagesize, ) # Run rendering logger.debug("Run pdf rendering") wkhtml_wrapper.run_report() logger.info("Put it in grid fs: %s" % file_path) doc_id = put_in_grid_fs(file_path, fileName, account, owner) logger.debug("Remove tmp report file with docId: %s" % doc_id) os.remove(file_path) # Subtask mail (if needed) if isinstance(mail, dict): # get cfile try: reportStorage = cstorage(account=account, namespace="files") meta = reportStorage.get(doc_id) meta.__class__ = cfile except Exception, err: logger.error("Error while fetching cfile : %s" % err) try: mail["account"] = account mail["attachments"] = meta result = task_mail.send.subtask(kwargs=mail).delay() result.get() result = result.result # if subtask fail, raise exception if result["success"] == False: raise Exception("Subtask mail have failed : %s" % result["celery_output"][0]) except Exception, err: logger.error(err) raise Exception("Impossible to send mail")
def render_pdf(fileName=None, viewName=None, startTime=None, stopTime=None, interval=None, account=None, mail=None, owner=None, orientation='Portrait', pagesize='A4'): if not stopTime: stopTime = int(time.time()) if not startTime: if interval: startTime = stopTime - interval else: startTime = -1 if viewName is None: raise ValueError("task_render_pdf: you must at least provide a viewName") #check if the account is just a name or a real caccount if isinstance(account ,str) or isinstance(account ,unicode): root_account = caccount(user='******',group='root',mail='*****@*****.**') root_storage = cstorage(account=root_account, namespace='object') bd_account = root_storage.find_one(mfilter={'_id':'account.%s' % str(account)}) if isinstance(bd_account, crecord): account = caccount(bd_account) logger.info('Successfuly retrieve right user from db') else: account = caccount(mail='*****@*****.**') logger.info('Anonymous account created') #get view options storage = cstorage(account=account, namespace='object') try: view_record = storage.get(viewName,account=account) except: raise Exception("Impossible to find view '%s' with account '%s'" % (viewName, account._id)) logger.info("Account '%s' ask a rendering of view '%s' (%s)" % (account.name, view_record.name, viewName,)) #set fileName if fileName is None: toDate = date.fromtimestamp(int(stopTime)) if startTime and startTime != -1: fromDate = date.fromtimestamp(int(startTime)) fileName = '%s_From_%s_To_%s.pdf' % (view_record.name, fromDate, toDate) else: fileName = '%s_%s.pdf' % (view_record.name,toDate) logger.info('fileName: %s' % fileName) ascii_fileName = hashlib.md5(fileName.encode('ascii', 'ignore')).hexdigest() #get orientation and pagesize view_options = view_record.data.get('view_options', {}) if isinstance(view_options, dict): orientation = view_options.get('orientation', 'Portrait') pagesize = view_options.get('pageSize', 'A4') logger.info('Orientation: %s' % orientation) logger.info('Pagesize: %s' % pagesize) wrapper_conf_file = os.path.expanduser("~/etc/wkhtmltopdf_wrapper.json") file_path = open(wrapper_conf_file, "r").read() file_path = '%s/%s' % (json.loads(file_path)['report_dir'],ascii_fileName) #create wrapper object wkhtml_wrapper = Wrapper( ascii_fileName, viewName, startTime, stopTime, account, wrapper_conf_file, orientation=orientation, pagesize=pagesize) # Run rendering logger.debug('Run pdf rendering') wkhtml_wrapper.run_report() logger.info('Put it in grid fs: %s' % file_path) doc_id = put_in_grid_fs(file_path, fileName, account,owner) logger.debug('Remove tmp report file with docId: %s' % doc_id) os.remove(file_path) #Subtask mail (if needed) if isinstance(mail, dict): #get cfile try: reportStorage = cstorage(account=account, namespace='files') meta = reportStorage.get(doc_id) meta.__class__ = cfile except Exception, err: logger.error('Error while fetching cfile : %s' % err) try: mail['account'] = account mail['attachments'] = meta result = task_mail.send.subtask(kwargs=mail).delay() result.get() result = result.result #if subtask fail, raise exception if(result['success'] == False): raise Exception('Subtask mail have failed : %s' % result['celery_output'][0]) except Exception, err: logger.error(err) raise Exception('Impossible to send mail')
def render_pdf(fileName=None, viewName=None, exporting=None, account=None, mail=None, owner=None, orientation='Portrait', pagesize='A4'): logger.info('start render') logger.debug("fileName: %s " % fileName) logger.debug("viewName: %s " % viewName) logger.debug("exporting: %s " % exporting) logger.debug("account: %s " % account) logger.debug("mail: %s " % mail) if exporting is None: exporting = {"enable": False} now = time.time() startTime = None stopTime = None logger.debug("now: %s " % now) timezone = exporting.get( 'timezone', {"type": "local", "value": 0}) if timezone.get('type') == 'utc': timezone['value'] = 0 elif timezone.get('type') == 'server': timezone['value'] = time.timezone elif 'value' not in timezone: timezone['value'] = 0 logger.error('timezone value does not exist') if not exporting.get('enable', False): stopTime = now elif exporting.get('type', 'duration') == 'duration': _datetime = datetime.fromtimestamp(now) kwargs = { exporting.get('unit', 'days'): int(exporting.get('length', 1)) } rd = relativedelta(**kwargs) _datetime -= rd startTime = time.mktime(_datetime.timetuple()) stopTime = now elif exporting['type'] == 'fixed': logger.debug('now: %s' % now) def get_timestamp(_time): """ Get a timestamp from an input _time struct. """ result = 0 _datetime = datetime.utcfromtimestamp(now) before = _time.get('before') if before is not None: for key in before: before[key] = int(before[key]) rd = relativedelta(**before) _datetime -= rd kwargs = dict() day = _time.get('day') if day is not None: logger.debug("day: %s" % day) kwargs['day'] = int(day) month = _time.get('month') if month is not None: logger.debug("month: %s" % month) kwargs['month'] = int(month) hour = _time.get('hour') if hour is not None: logger.debug("hour: %s" % hour) kwargs['hour'] = int(hour) minute = _time.get('minute') if minute is not None: logger.debug("minute: %s" % minute) kwargs['minute'] = int(minute) logger.debug("_datetime: %s" % _datetime) _datetime = _datetime.replace(**kwargs) logger.debug("_datetime: %s" % _datetime) day_of_week = _time.get('day_of_week') if day_of_week is not None: logger.debug("day_of_week: %s" % day_of_week) day_of_week = int(day_of_week) weekday = calendar.weekday(_datetime.year, _datetime.month, _datetime.day) days = weekday - day_of_week logger.debug("days %s" % days) td = timedelta(days=days) _datetime -= td result = time.mktime(_datetime.utctimetuple()) result -= timezone['value'] logger.debug('result: %s' % result) return result startTime = None _from = exporting.get('from') if _from is not None and _from: startTime = int(_from.get('timestamp', get_timestamp(_from))) logger.info('from : {0} and startTime : {1} ({2})'.format(_from, startTime, datetime.utcfromtimestamp(startTime))) stopTime = now _to = exporting.get('to') if startTime and _to is not None and _to.get('enable', False): stopTime = int(_to.get('timestamp', get_timestamp(_to))) logger.info('to : {0} and stopTime : {1} ({2})'.format(_to, stopTime, datetime.utcfromtimestamp(stopTime))) else: logger.error('Wrong exporting type %s' % exporting['type']) if viewName is None: raise ValueError("task_render_pdf: you must at least provide a viewName") #check if the account is just a name or a real caccount if isinstance(account ,str) or isinstance(account ,unicode): root_account = caccount(user='******',group='root',mail='*****@*****.**') root_storage = cstorage(account=root_account, namespace='object') bd_account = root_storage.find_one(mfilter={'_id':'account.%s' % str(account)}) if isinstance(bd_account, crecord): account = caccount(bd_account) logger.info('Successfuly retrieve right user from db') else: account = caccount(mail='*****@*****.**') logger.info('Anonymous account created') #get view options storage = cstorage(account=account, namespace='object') try: view_record = storage.get(viewName,account=account) except: raise Exception("Impossible to find view '%s' with account '%s'" % (viewName, account._id)) logger.info("Account '%s' ask a rendering of view '%s' (%s)" % (account.name, view_record.name, viewName,)) timezone_td = timedelta( seconds=int(timezone.get('value', 0))) def get_datetime_with_timezone(timestamp): result = datetime.utcfromtimestamp(timestamp) result -= timezone_td return result #set fileName if fileName is None: toDate = get_datetime_with_timezone(int(stopTime)) if startTime and startTime != -1: fromDate = get_datetime_with_timezone(int(startTime)) fileName = '%s_From_%s_To_%s' % (view_record.name, fromDate, toDate) else: fileName = '%s_%s' % (view_record.name,toDate) fileName += '_Tz_%s_%s' % (timezone.get('type', 'server'), (timezone.get('value', 0) if timezone.get('value', 0) >= 0 \ else 'minus_%s' % -timezone['value'])) fileName += '.pdf' logger.info('fileName: %s' % fileName) ascii_fileName = hashlib.md5(fileName.encode('ascii', 'ignore')).hexdigest() #get orientation and pagesize view_options = view_record.data.get('view_options', {}) if isinstance(view_options, dict): orientation = view_options.get('orientation', 'Portrait') pagesize = view_options.get('pageSize', 'A4') logger.info('Orientation: %s' % orientation) logger.info('Pagesize: %s' % pagesize) wrapper_conf_file = os.path.expanduser("~/etc/wkhtmltopdf_wrapper.json") file_path = open(wrapper_conf_file, "r").read() file_path = '%s/%s' % (json.loads(file_path)['report_dir'],ascii_fileName) #create wrapper object wkhtml_wrapper = Wrapper( ascii_fileName, viewName, startTime, stopTime, account, wrapper_conf_file, orientation=orientation, pagesize=pagesize) wkhtml_wrapper.logger = logger # Run rendering logger.info('Run pdf rendering') wkhtml_wrapper.run_report() logger.info('Put it in grid fs: %s' % file_path) try: doc_id = put_in_grid_fs(file_path, fileName, account,owner) except Exception as e: import inspect inspect.trace() logger.info('Error in generating file (try to not use slink): %s, %s ' % (e, inspect.trace())) logger.info('Remove tmp report file with docId: %s' % doc_id) os.remove(file_path) #Subtask mail (if needed) if isinstance(mail, dict): #get cfile try: reportStorage = cstorage(account=account, namespace='files') meta = reportStorage.get(doc_id) meta.__class__ = cfile except Exception, err: logger.error('Error while fetching cfile : %s' % err) try: mail['account'] = account mail['attachments'] = meta result = task_mail.send.subtask(kwargs=mail).delay() result.get() result = result.result #if subtask fail, raise exception if(result['success'] == False): raise Exception('Subtask mail have failed : %s' % result['celery_output'][0]) except Exception, err: logger.error(err) raise Exception('Impossible to send mail')