parameters: ()
docstring: File uploading handler
docstring_summary: File uploading handler
docstring_tokens: File uploading handler
function: def upload_file():
      """ File uploading handler """
  
      try:
          filename = None
          app = get_app(name=request.vars.location.split('/')[0])
          path = apath(request.vars.location, r=request)
  
          if request.vars.filename:
              filename = re.sub('[^\w\./]+', '_', request.vars.filename)
          else:
              filename = os.path.split(request.vars.file.filename)[-1]
  
          if path[-8:] == '/models/' and not filename[-3:] == '.py':
              filename += '.py'
  
          if path[-9:] == '/modules/' and not filename[-3:] == '.py':
              filename += '.py'
  
          if path[-13:] == '/controllers/' and not filename[-3:] == '.py':
              filename += '.py'
  
          if path[-7:] == '/views/' and not filename[-5:] == '.html':
              filename += '.html'
  
          if path[-11:] == '/languages/' and not filename[-3:] == '.py':
              filename += '.py'
  
          filename = os.path.join(path, filename)
          dirpath = os.path.dirname(filename)
  
          if not os.path.exists(dirpath):
              os.makedirs(dirpath)
  
          safe_write(filename, request.vars.file.file.read(), 'wb')
          session.flash = T('file "%(filename)s" uploaded',
                            dict(filename=filename[len(path):]))
      except Exception:
          if filename:
              d = dict(filename = filename[len(path):])
          else:
              d = dict(filename = 'unkown')
          session.flash = T('cannot upload file "%(filename)s"', d)
  
      redirect(request.vars.sender)
function_tokens: def upload_file ( ) : try : filename = None app = get_app ( name = request . vars . location . split ( '/' ) [ 0 ] ) path = apath ( request . vars . location , r = request ) if request . vars . filename : filename = re . sub ( '[^\w\./]+' , '_' , request . vars . filename ) else : filename = os . path . split ( request . vars . file . filename ) [ - 1 ] if path [ - 8 : ] == '/models/' and not filename [ - 3 : ] == '.py' : filename += '.py' if path [ - 9 : ] == '/modules/' and not filename [ - 3 : ] == '.py' : filename += '.py' if path [ - 13 : ] == '/controllers/' and not filename [ - 3 : ] == '.py' : filename += '.py' if path [ - 7 : ] == '/views/' and not filename [ - 5 : ] == '.html' : filename += '.html' if path [ - 11 : ] == '/languages/' and not filename [ - 3 : ] == '.py' : filename += '.py' filename = os . path . join ( path , filename ) dirpath = os . path . dirname ( filename ) if not os . path . exists ( dirpath ) : os . makedirs ( dirpath ) safe_write ( filename , request . vars . file . file . read ( ) , 'wb' ) session . flash = T ( 'file "%(filename)s" uploaded' , dict ( filename = filename [ len ( path ) : ] ) ) except Exception : if filename : d = dict ( filename = filename [ len ( path ) : ] ) else : d = dict ( filename = 'unkown' ) session . flash = T ( 'cannot upload file "%(filename)s"' , d ) redirect ( request . vars . sender )