Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Why can't I rename this variable?

$
0
0
Hi there. The following code is from a textbook that I'm learning.
'''
spreadsheet3.py is adapted to present the user with a logon screen first, before serving the spreadsheet.

it uses the default implementation of the Logon class, so the only user that will be authenticated will
be the user 'user' with the password 'secret'.

The few lines of code that had to be added are marked with the sting CHANGED
'''

import cherrypy

import os.path

current_dir = os.path.dirname(os.path.abspath(__file__))

# CHANGED, we need to import the logon module
import logon

class Root(object):
	
	spreadsheet = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" href="/jquerytheme.css" type="text/css" media="screen, projection" />
<script type="text/javascript" src="/jquery.js" ></script>
<script type="text/javascript" src="/jquery-ui.js" ></script>
<script type="text/javascript" src="/static/js/jeditable.js" ></script>
<script type="text/javascript" src="/spreadsheet.js" ></script>
</head>
<body id="spreadsheet_example">
<div id="example"></div>
<p id="logging">
</p>
<script type="text/javascript">
$("#example").sheet({cols:8,rows:10});
</script>
</body>
</html>
	'''

	# CHANGED, we mount an instance of the Logon class
	logon = logon.Logon(path='/logon')
	
	@cherrypy.expose
	def index(self):
		# CHANGED, before serving the sheet, we authenticate the user. No returnpage argument is needed
		# as the defautl one is / 
		username=logon.checkauth('/logon')
		return Root.spreadsheet
	
if __name__ == "__main__":
	
	cherrypy.quickstart(Root(),config={
		# CHANGED, enable sessions
		'/': 
		{ 'tools.sessions.on': True },
		'/static':
		{ 'tools.staticdir.on':True,
		  'tools.staticdir.dir':os.path.join(current_dir,"static")
		},
		'/jquery.js':
		{ 'tools.staticfile.on':True,
		'tools.staticfile.filename':os.path.join(current_dir,"static","jquery","jquery-1.4.2.js")
		},
		'/jquery-ui.js':
		{ 'tools.staticfile.on':True,
		'tools.staticfile.filename':os.path.join(current_dir,"static","jquery","jquery-ui-1.8.1.custom.min.js")
		},
		'/jquerytheme.css':
		{ 'tools.staticfile.on':True,
		'tools.staticfile.filename':os.path.join(current_dir,"static","jquery","css","redmond","jquery-ui-1.8.1.custom.css")
		},
		'/spreadsheet.js':
		{ 'tools.staticfile.on':True,
		  'tools.staticfile.filename':os.path.join(current_dir,"spreadsheet.js")
		}
	})



The logon.py module is like this:
import cherrypy
import urllib.parse

import logging

def checkauth(logonurl="/", returntopage=False):
	returnpage=''
	if returntopage:
		returnpage='?returnpage='+cherrypy.request.script_name+cherrypy.request.path_info
		#returnpage='?returnpage='+cherrypy.request.base+cherrypy.request.script_name+cherrypy.request.path_info
		#returnpage='?returnpage='+cherrypy.request.path_info
	
	auth = cherrypy.session.get('authenticated',None)
	if auth == None : raise cherrypy.HTTPRedirect(logonurl+returnpage)
	return auth

class Logon:
	base_page = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="/jquery.js" ></script>
<script type="text/javascript" src="/jquery-ui.js" ></script>
<style type="text/css" title="currentStyle">
	@import "/jquerytheme.css";
	@import "/static/css/logon.css";
</style>
</head>
<body id="logonscreen">
<div id="content">
%s
</div>
<script type="text/javascript">$("button").button({icons: {primary: 'ui-icon-power'}})</script>
</body>
</html>
'''

	# change the GET once in production: otherwise passwords may end up in the access log!
	
	# use <button> NOT <input> for submit and reet otherwise icons won't show!
	logon_screen = base_page % '''
<form class="login" action="%s/logon" method="GET">
<fieldset>
<label for="username">Username</label><input id="username" type="text" name="username" />
<script type="text/javascript">$("#username").focus()</script>
<label for="password">Password</label><input id="password" type="password" name="password" />
<input type="hidden" name="returnpage" value="%s" />
<button type="submit" class="login-button" value="Log in">Log in</button>
</fieldset>
</form>
'''

	not_authenticated = base_page % '''<h1>Login or password not correct</h1>'''

	def __init__(self,path="/logon",authenticated="/",not_authenticated="/"):
		self.path=path
		self.authenticated=authenticated
		self.not_authenticated=not_authenticated
		
	# change this to a proper check in a production environment
	@staticmethod
	def checkpass(username,password):
		if username=='user' and password=='secret': return True
		return False

	@cherrypy.expose
	def index(self,returnpage=''):
		if returnpage is None : returnpage = '' 
		return Logon.logon_screen % (self.path,urllib.parse.quote(returnpage))
	index._cp_config = {'tools.expires.on':True,'tools.expires.secs':0,'tools.expires.force':True}
	
	@cherrypy.expose
	def logon(self,username,password,returnpage=''):
		returnpage = urllib.parse.unquote(returnpage)
		#logging.error("####"+returnpage+"####")
		if Logon.checkpass(username,password):
			cherrypy.session['authenticated']=username
			if returnpage != '':
				raise cherrypy.InternalRedirect(returnpage)
			else:
				raise cherrypy.InternalRedirect(self.authenticated)
		raise cherrypy.InternalRedirect(self.not_authenticated)
	
	@cherrypy.expose
	def logoff(self,logoffurl=None):
		cherrypy.session.delete()
		cherrypy.lib.sessions.expire()
		#cherrypy.session['authenticated']=None
		if logoffurl is None :
			raise cherrypy.InternalRedirect(self.not_authenticated)
		raise cherrypy.InternalRedirect(logoffurl)
	


However, when I changed the variable name in the main module from
logon = logon.Logon(path='/logon')
into
logonnew = logon.Logon(path='/logon')
, the code breaks down saying:"NameError: global name 'logonnew' is not defined"

Why can't I change a variable name? I checked and checked just didn't find any reason. :cry2:/>

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>