I have two classes with similar first checking codes, but different behaviors. I supose there is a way to refactor the code, so I dont need to retype it every time. It is possible to refactor this, or I must retype this code everytime?
# Get actual logged user
user, msg = checkLoggedUser()
if user:
self.renderPage("customMessage.htm", custom_msg=msg)
else:
The code is simple. Forget about my way to render the page, is a method over my own PageHandler class that works
# Check for actual logged user
def checkLoggedUser():
# Get actual logged user
user = users.get_current_user()
# Don't allow to register for a logged user
if user:
return True, "You are already logged in."
else:
return False, None
# Get and post for the login page
class Login(custom.PageHandler):
def get(self):
# Get actual logged user
user, msg = checkLoggedUser()
if user:
self.renderPage("customMessage.htm", custom_msg=msg)
else:
self.renderPage('login.htm')
def post(self):
# Get actual logged user
user, msg = checkLoggedUser()
if user:
self.renderPage("customMessage.htm", custom_msg=msg)
else:
# Make the login
# bla, bla bla... code for login the user.
# Get and post for the register page
class Register(custom.PageHandler):
def get(self):
# Get actual logged user
user, msg = checkLoggedUser()
if user:
self.renderPage("customMessage.htm", custom_msg=msg)
else:
self.renderPage("registerUser.htm")
def post(self):
# Get actual logged user
user, msg = checkLoggedUser()
if user:
self.renderPage("customMessage.htm", custom_msg=msg)
else:
# Store in vars the form values
# bla, bla bla... code for register the user.