def beforeMain(self): from cherrypy import config, request from cherrypy.lib import cptools if not config.get('staticFilter.on', False): return regex = config.get('staticFilter.match', '') if regex: import re if not re.search(regex, request.path): return filename = config.get('staticFilter.file') if not filename: staticDir = config.get('staticFilter.dir') section = config.get('staticFilter.dir', returnSection=True) if section == 'global': section = "/" section = section.rstrip(r"\/") extraPath = request.path[len(section) + 1:] extraPath = extraPath.lstrip(r"\/") extraPath = urllib.unquote(extraPath) filename = os.path.join(staticDir, extraPath) # If filename is relative, make absolute using "root". if not os.path.isabs(filename): root = config.get('staticFilter.root', '').rstrip(r"\/") if root: filename = os.path.join(root, filename) cptools.serveFile(filename)
def index(self): """ In case the apache rewrite rule isn't paying attention, serve up the index file from here. """ loc = os.path.join(os.path.dirname(__file__), "static/index.html") return serveFile(loc)
def exportBatch(kw,exportType=None): template_config = {"_export":"report_download/TEMPLATE/Kohls_TEMPLATE.xls", "product_export":"report_download/TEMPLATE/Kohls_PRODUCT_TEMPLATE.xls", } header_ids = kw["header_ids"] if type(header_ids) != list: header_ids = [header_ids] get = turbogears.config.get current = datetime.now() dateStr = current.today().strftime("%Y%m%d%H%M%S") fileDir = os.path.join(get("ecrm.downloads"),"kohlsPO_download",identity.current.user_name,dateStr) if not os.path.exists(fileDir): os.makedirs(fileDir) dlzipFile = os.path.join(fileDir,"report_%s.zip" % current.strftime("%Y%m%d%H%M%S")) try: templatePath = os.path.join(os.path.abspath(os.path.curdir),template_config[exportType]) rm = random.randint(1,1000) copyTemplatePath = os.path.join(fileDir,"Kohls_TEMPLATE_%d.xls" %rm) #copy the template to the dest folder to invoid the confict. shutil.copyfile(templatePath,copyTemplatePath) isAdmin = "Admin" in identity.current.groups #add by ray on 29/5 fileList = [] for header_id in header_ids: #print header_id,fileDir,copyTemplatePath,"==========================" rs = KsPoHeader.get(id=header_id) if rs.latestFlag < 1 and not isAdmin:# and turbogears.identity.user.user_name <> 'admin': continue if exportType == "_export": (flag,fileName) = _createExcel(header_id,fileDir,copyTemplatePath) else: (flag,fileName) = product_createExcel(header_id,fileDir,copyTemplatePath) if flag: fileList.append( fileName ) dlzip = zipfile.ZipFile(dlzipFile,"w",zlib.DEFLATED) for fl in fileList: logging.info(os.path.abspath(fl)) dlzip.write(os.path.abspath(str(fl)),os.path.basename(str(fl))) dlzip.close() try: for fl in fileList: os.remove(fl) os.remove(copyTemplatePath) except: pass return serveFile(dlzipFile, "application/x-download", "attachment") except: traceback.print_exc() flash("Error occur in the Excel Exporting !") raise redirect("index")
def lang_file(self, code): # serve static file, the code can be pot or a lang code locales = self.locales_directory() if code == 'pot': path = os.path.join(locales, 'messages.pot') else: path = os.path.join(locales, code) path = os.path.join(path, 'LC_MESSAGES') path = os.path.join(path, 'messages.po') if 'If-Modified-Since' in cherrypy.request.headers: del cherrypy.request.headers['If-Modified-Since'] # see ticket #879 return serveFile(path, "application/x-download", "attachment")
def reportDetail(self,**kw): #prepare the sql if kw["report_type"] == "q": templatePath = os.path.join(os.path.abspath(os.path.curdir),"report_download/TEMPLATE/KOHLS_REPORT_QUARTERLY_TEMPLATE.xls") elif kw["report_type"] == "m": templatePath = os.path.join(os.path.abspath(os.path.curdir),"report_download/TEMPLATE/KOHLS_REPORT_MONTHLY_TEMPLATE.xls") sql = createKOHLSSQL(kw) print "_*"*30 print sql print "_*"*30 data = [] #execute the sql ,return the resultset try: db_connection = createConnection() cursor = db_connection.cursor() cursor.execute(str(sql)) resultSet = cursor.fetchall() def decodeCol(col): if not col: return "" if type(col) == datetime: return col.strftime("%Y-%m-%d") return str(col).decode("utf8") for row in resultSet: data.append( tuple([decodeCol(col)[:900] for col in row]) ) except: traceback.print_exc() flash("Error occur on the server side.") raise redirect("/kohlspo/report") finally: cursor.close() #write the resultset to the excel try: dateStr = datetime.today().strftime("%Y%m%d") fileDir = os.path.join(os.path.abspath(os.path.curdir),"report_download/kohls_report","%s" %dateStr) if not os.path.exists(fileDir): os.makedirs(fileDir) timeStr = datetime.now().time().strftime("%H%M%S") filename = os.path.join(fileDir,"%s%s.xls" %(dateStr,timeStr)) ke = KohlsReportExcel(templatePath = templatePath,destinationPath = filename) ke.inputData(data=data) ke.outputData() return serveFile(filename, "application/x-download", "attachment") except: traceback.print_exc() flash("Error occur whening generating the excel!") raise redirect("/kohlspo/report") finally: pass
def download(self, name): tables = {'members': session.query(model.Members).all(), 'books': session.query(model.Books).all(), 'issues': session.query(model.Issues).all(), 'overdue': self.report_overdue()} path = os.path.join(absDir, '%sReport.csv') % (name) fobj = open(path, 'w') report = tables[name] recordset = [] for eachrec in report: recordset.append (eachrec.dictify()) writer = csv.writer(fobj, delimiter=' ') writer.writerow(recordset) fobj.close() return cptools.serveFile(path, 'application/x-download', 'attachment', os.path.basename(path))
def before_main(self): config = cherrypy.config if not config.get('static_filter.on', False): return request = cherrypy.request path = request.object_path regex = config.get('static_filter.match', '') if regex: import re if not re.search(regex, path): return root = config.get('static_filter.root', '').rstrip(r"\/") filename = config.get('static_filter.file') if filename: static_dir = None else: static_dir = config.get('static_filter.dir') if not static_dir: msg = ("StaticFilter requires either static_filter.file " "or static_filter.dir (%s)" % request.path) raise cherrypy.WrongConfigValue(msg) section = config.get('static_filter.dir', return_section = True) if section == 'global': section = "/" section = section.rstrip(r"\/") extra_path = path[len(section) + 1:] extra_path = extra_path.lstrip(r"\/") extra_path = urllib.unquote(extra_path) # If extra_path is "", filename will end in a slash filename = os.path.join(static_dir, extra_path) # If filename is relative, make absolute using "root". # Note that, if "root" isn't defined, we still may send # a relative path to serveFile. if not os.path.isabs(filename): if not root: msg = ("StaticFilter requires an absolute final path. " "Make static_filter.dir, .file, or .root absolute.") raise cherrypy.WrongConfigValue(msg) filename = os.path.join(root, filename) # If we used static_filter.dir, then there's a chance that the # extra_path pulled from the URL might have ".." or similar uplevel # attacks in it. Check that the final file is a child of static_dir. # Note that we do not check static_filter.file--that can point # anywhere (since it does not use the request URL). if static_dir: if not os.path.isabs(static_dir): static_dir = os.path.join(root, static_dir) if not os.path.normpath(filename).startswith(os.path.normpath(static_dir)): raise cherrypy.HTTPError(403) # Forbidden try: # you can set the content types for a complete directory per extension content_types = config.get('static_filter.content_types', None) content_type = None if content_types: root, ext = os.path.splitext(filename) content_type = content_types.get(ext[1:], None) cptools.serveFile(filename, contentType=content_type) request.execute_main = False except cherrypy.NotFound: # If we didn't find the static file, continue handling the # request. We might find a dynamic handler instead. # But first check for an index file if a folder was requested. if filename[-1:] in ("/", "\\"): idx = config.get('static_filter.index', '') if idx: try: cptools.serveFile(os.path.join(filename, idx)) request.execute_main = False except cherrypy.NotFound: pass
def images(self, filename): return cptools.serveFile(f"{self.directory}/{filename}")
def productExport(kw): h = KsPoHeader.get(id=kw["header_id"]) ######### update hangtag view in excel conn = KsPoDetail._connection sql = '''select h.hangtag from (select header_id,hangtag from kohls_po_detail group by header_id,hangtag) h where h.header_id = %s ''' % (kw["header_id"]) rs = conn.queryAll(sql) h.hangtag = ",".join([item[0] for item in rs]) ######### podetail_ids = [id for id in kw["podetail_ids"].split("|") if id] sln_ids = [id for id in kw["sln_ids"].split("|") if id] po1Objs = [KsPoDetail.get(id=id) for id in podetail_ids ] slnObjs = [SLNDetail.get(id=id) for id in sln_ids] isAdmin = "Admin" in identity.current.groups if not isAdmin: any = lambda fun: lambda iterable: reduce(lambda a,b:a or b, imap(fun,iterable)) if any(lambda obj:obj.hasExported!=0)( po1Objs + slnObjs ): #if any of the item has been exported before, can't export again. flash("Some items in the list have been exported before ,please contact the admin if you want to generate the excel again.") raise redirect("/kohlspo/detail?id=%s" %kw["header_id"]) result = [] hangtag = None #use to fill in the item name in the excel header , not the real attr for the KsPoHeader poUOM = None #use to gen the excel file name,just use the first measurementCode in the detail or the SLN. eanCode = None upcCode = None total_qty = 0 ########### for d in po1Objs: if poUOM is None: poUOM = d.measurementCode if hangtag is None: hangtag = d.hangtag if eanCode is None: eanCode = d.eanCode if d.eanCode else None if upcCode is None: upcCode = d.upc if d.upc else None total_qty += int(d.poQty) tmp_list = re.split('^(\d*)\s*(\D*)$',(d.size)) _list = [n for n in tmp_list if n] upcORean = d.upc if d.upc else d.eanCode if len(_list) > 1: result.append( (d.styleNo,d.colorCode,d.colorDesc,d.deptNo,d.classNo,d.subclassNo, upcORean,d.retailPrice,_list[0],_list[1].upper(),d.productDesc.upper().split(":")[0],"","","","","",d.poQty) ) else: result.append( (d.styleNo,d.colorCode,d.colorDesc,d.deptNo,d.classNo,d.subclassNo, upcORean,d.retailPrice,d.size.upper(),"",d.productDesc.upper().split(":")[0],"","","","","",d.poQty) ) if not isAdmin: d.set(hasExported=1) for s in slnObjs: logging.info(str(s)) if poUOM is None: poUOM = s.poDetail.measurementCode if hangtag is None: hangtag = s.poDetail.hangtag if eanCode is None: eanCode = s.eanCode if s.eanCode else None if upcCode is None: upcCode = s.upc if s.upc else None po1Qty = s.poDetail.poQty total_qty += int(s.qty*po1Qty) tmp_list = re.split('^(\d*)\s*(\D*)$',(s.size)) _list = [n for n in tmp_list if n] upcORean = s.upc if s.upc else s.eanCode if len(_list) > 1: result.append( (s.styleNo,s.colorCode,s.colorDesc,s.deptNo,s.classNo,s.subclassNo, upcORean,s.retailPrice,_list[0],_list[1].upper(),s.productDesc.upper().split(":")[0],"","","","","",s.qty*po1Qty) ) else: result.append( (s.styleNo,s.colorCode,s.colorDesc,s.deptNo,s.classNo,s.subclassNo, upcORean,s.retailPrice,s.size.upper(),"",s.productDesc.upper().split(":")[0],"","","","","",s.qty*po1Qty) ) if not isAdmin: s.set(hasExported=1) #h.hangtag = hangtag h.upc = upcCode h.eanCode = eanCode get = turbogears.config.get current = datetime.now() dateStr = current.today().strftime("%Y%m%d") fileDir = os.path.join(get("ecrm.downloads"),"kohlsPO_download",identity.current.user_name,dateStr) #logging.info(fileDir) if not os.path.exists(fileDir): os.makedirs(fileDir) timeStr = current.time().strftime("%H%M%S") rn = random.randint(0,10000) username = identity.current.user_name templatePath = os.path.join(os.path.abspath(os.path.curdir),"report_download/TEMPLATE/Kohls_PRODUCT_TEMPLATE.xls") #The following is used to gen the excel file name in the special format. if poUOM == 'AS': _UOM = 'Assorted' elif poUOM == 'EA': _UOM = 'Each' if h.poType == 'BK' : _poType = '' else : _poType = h.poType + '_' if h.poPurposeCode == '07' : _purposeCode = '_Rev' else : _purposeCode = '' if isAdmin: xlsFileName = "%s%s%s_%s.xls" % (_poType, h.poNo, _purposeCode, _UOM) else: h.set(exportVersion=h.exportVersion+1) #update the export version xlsFileName = "%s%s%s_%s-%d.xls" % (_poType, h.poNo, _purposeCode, _UOM , h.exportVersion ) #******finish the excel file name gen process *************** filename = os.path.join(fileDir,xlsFileName) ke = KohlsPOExcel(templatePath = templatePath,destinationPath = filename) try: ke.inputData( POHeader=h,data=result,qty =total_qty) ke.outputData() if "Admin" not in identity.current.groups: _updateExportFlag(h) return serveFile(filename, "application/x-download", "attachment") except: traceback.print_exc() if ke: ke.clearData() flash("Error occur in the Excel Exporting !") raise redirect("index")
def export(self, type): def intValue(v): try: return int(v) except: return 0 def populate(row): unit_price=POLERTEC_PRICES[row[2]] if row[2] in POLERTEC_PRICES.keys() else None inventory_value=round((intValue(row[5])-intValue(row[6])+intValue(row[8]))/1000*unit_price) if unit_price else None return ( self._decodeCol(row[0]), #PO self._decodeCol(row[1]), #CUSTOMER PO self._decodeCol(row[2]), #ITEM_NO self._decodeCol(row[3]), #UNIT self._decodeCol(row[4]), #QTY ORDERED self._decodeCol(row[5]), #QTY RECV self._decodeCol(row[6]), #QTY SHIPPED "=RC[-3]-RC[-2]", "=RC[-3]-RC[-2]+RC[3]", intValue(row[7])-intValue(row[6]), #QTY REQUEST "=RC[-2]-RC[-1]", intValue(row[8]), #STOCK ADJUSTMENT unit_price, inventory_value, ) try: db_connection=createConnection() cursor=db_connection.cursor() def fetchNOUSData(rawRs): poData=[] soData=[] dnData=[] wovData=[] db_connection=createConnection() cursor=db_connection.cursor() for row in rawRs: poData.append(self._decodeCol(row[2])) cursor.execute(str(self._posearchSQL(poData))) poRs=cursor.fetchall() for row in poRs: soData.append(self._decodeCol(row[0])) cursor.execute(str(self._sosearchSQL(soData))) dnRs=cursor.fetchall() for row in dnRs: dnData.append(self._decodeCol(row[0])) cursor.execute(str(self._dnsearchSQL(dnData))) extraRs=cursor.fetchall() for init_row in rawRs: for extra_row in extraRs: init_row=list(init_row) if extra_row[0]==init_row[2] and init_row[6] is not None and extra_row[1] is not None: init_row[6]=int(init_row[6])-int(extra_row[1]) wovData.append(populate(init_row)) cursor.close() db_connection.close() return wovData wovData=[] hatData=[] hetData=[] if type=='US': cursor.execute(str(self._createSQL(type, "WOV"))) wovRs=cursor.fetchall() for row in wovRs: wovData.append(populate(row)) cursor.execute(str(self._createSQL(type, "HAT"))) hatRs=cursor.fetchall() for row in hatRs: hatData.append(populate(row)) elif type=='NOUS': cursor.execute(str(self._createSQL(type, "WOV"))) wovData=fetchNOUSData(cursor.fetchall()) cursor.execute(str(self._createSQL(type, "HAT"))) hatData=fetchNOUSData(cursor.fetchall()) cursor.execute(str(self._createSQL(type, "HET"))) hetData=fetchNOUSData(cursor.fetchall()) except: traceback.print_exc() flash("Error occur on the server side.") raise redirect("/polartec/report") finally: cursor.close() #write the resultset to the excel try: templatePath=os.path.join(os.path.abspath(os.path.curdir), "report_download/TEMPLATE/POLARTEC_TEMPLATE.xls") dateStr=datetime.today().strftime("%Y%m%d") fileDir=os.path.join(os.path.abspath(os.path.curdir), "report_download/kohls_report", "%s"%dateStr) if not os.path.exists(fileDir): os.makedirs(fileDir) timeStr=datetime.now().time().strftime("%H%M%S") filename=os.path.join(fileDir, "%s%s.xls"%(dateStr, timeStr)) ke=PolartecExcel(templatePath=templatePath, destinationPath=filename) ke.inputData(wovData=wovData, hatData=hatData, hetData=hetData, type=type) ke.outputData() return serveFile(filename, "application/x-download", "attachment") except: traceback.print_exc() flash("Error occur whening generating the excel!") raise redirect("/polartec/report")
def default(self, *args): return cptools.serveFile(os.path.abspath("/".join(args)))
path = rdw_helpers.decodeUrl(path) date = rdw_helpers.decodeUrl(date) try: rdw_helpers.ensurePathValid(repo) rdw_helpers.ensurePathValid(path) except rdw_helpers.accessDeniedError, error: return self.writeErrorPage(str(error)) if not repo: return self.writeErrorPage("Backup location not specified.") if not repo in self.userDB.getUserRepoPaths(self.getUsername()): return self.writeErrorPage("Access is denied.") try: restoreTime = rdw_helpers.rdwTime() restoreTime.loadFromString(date) (path, file) = os.path.split(path) if not file: file = path path = "/" filePath = librdiff.restoreFile(rdw_helpers.joinPaths(self.userDB.getUserRoot(self.getUsername()), repo), path, file, restoreTime) except librdiff.FileError, error: return self.writeErrorPage(error.getErrorString()) except ValueError: return self.writeErrorPage("Invalid date parameter.") (directory, filename) = os.path.split(filePath) file = autoDeleteDir(directory) filename = "\""+filename.replace("\"", "\\\"")+"\"" # quote file to handle files with spaces, while escaping quotes in filename return serveFile(filePath, None, disposition="attachment", name=filename) index.exposed = True
def mapPathToObject(path): """For path, return the corresponding exposed callable (or raise NotFound). path should be a "relative" URL path, like "/app/a/b/c". Leading and trailing slashes are ignored. Traverse path: for /a/b?arg=val, we'll try: root.a.b.index -> redirect to /a/b/?arg=val root.a.b.default(arg='val') -> redirect to /a/b/?arg=val root.a.b(arg='val') root.a.default('b', arg='val') root.default('a', 'b', arg='val') The target method must have an ".exposed = True" attribute. """ # Remove leading and trailing slash tpath = path.strip("/") if not tpath: objectPathList = [] else: objectPathList = tpath.split('/') if objectPathList == ['global']: objectPathList = ['_global'] objectPathList = ['root'] + objectPathList + ['index'] if getattr(cherrypy, "debug", None): cherrypy.log(" Attempting to map path: %s using %s" % (tpath, objectPathList), "DEBUG") # Try successive objects... (and also keep the remaining object list) isFirst = True isSecond = False foundIt = False virtualPathList = [] while objectPathList: if isFirst or isSecond: # Only try this for a.b.index() or a.b() candidate = getObjFromPath(objectPathList) if callable(candidate) and getattr(candidate, 'exposed', False): foundIt = True break # Couldn't find the object: pop one from the list and try "default" lastObj = objectPathList.pop() if (not isFirst) or (not tpath): virtualPathList.insert(0, lastObj) objectPathList.append('default') candidate = getObjFromPath(objectPathList) if callable(candidate) and getattr(candidate, 'exposed', False): foundIt = True break objectPathList.pop() # Remove "default" if isSecond: isSecond = False if isFirst: isFirst = False isSecond = True # Check results of traversal if not foundIt: if tpath.endswith("favicon.ico"): # Use CherryPy's default favicon.ico. If developers really, # really want no favicon, they can make a dummy method # that raises NotFound. icofile = os.path.join(os.path.dirname(__file__), "favicon.ico") cptools.serveFile(icofile) applyFilters('beforeFinalize') finalize() raise cherrypy.RequestHandled() else: # We didn't find anything if getattr(cherrypy, "debug", None): cherrypy.log(" NOT FOUND", "DEBUG") raise cherrypy.NotFound(path) if isFirst: # We found the extra ".index" # Check if the original path had a trailing slash (otherwise, do # a redirect) if path[-1] != '/': newUrl = path + '/' if cherrypy.request.queryString: newUrl += "?" + cherrypy.request.queryString if getattr(cherrypy, "debug", None): cherrypy.log(" Found: redirecting to %s" % newUrl, "DEBUG") raise cherrypy.HTTPRedirect(newUrl) if getattr(cherrypy, "debug", None): cherrypy.log(" Found: %s" % candidate, "DEBUG") return candidate, objectPathList, virtualPathList
def slice_file(self): path = os.path.join(os.getcwd(), os.path.dirname(__file__)) return cptools.serveFile(os.path.join(path, "static/index.html"))
def download(self): path = os.path.join(absDir, "pdf_file.pdf") return cptools.serveFile(path, "application/x-download", "attachment", os.path.basename(path))
def images(self, filename): return cptools.serveFile("{}/{}".format(self.directory, filename))