14658

How to stop execution after self.redirect

Question:

How to stop execution after self.redirect in request initilization?

The method redirect does not stop/halt script execution. My initilizer method is called but the process continues into method AnswerHandler.get() but self.user is None because user is not logged.

<pre class="lang-py prettyprint-override">class BaseRequestHandler(webapp.RequestHandler): def __init__(self): self.user = None def initialize(self, request=None, response=None): super(BaseRequestHandler,self).initialize(request, response) user = users.get_current_user() if not user: return self.redirect(users.create_login_url(request.uri)) self.user = user class AnswerHandler(BaseRequestHandler): def get(self): answer = Answer.next_answer_by_user(self.user) self.write_template({"answer" : answer})

Tanks!

Answer1:

I would go with this solution:

class BaseRequestHandler(webapp.RequestHandler): def __init__(self): self.user = None def setAuthUser(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(request.uri)) return False self.user = user return True class AnswerHandler(BaseRequestHandler): def get(self): if not self.setAuthUser(): return answer = Answer.next_answer_by_user(self.user) self.write_template({"answer" : answer})

I understand that you would like to work around having to have 2 lines of code in every handler.

If you really want that, your other option would be:

class BaseRequestHandler(webapp.RequestHandler): def __init__(self): self.user = None def get(self): self.user = users.get_current_user() if not self.user: self.redirect(users.create_login_url(request.uri)) return self.doGet() def doGet(self): raise Exception("doGet() needs to be implemented by a subclass.") class AnswerHandler(BaseRequestHandler): def doGet(self): answer = Answer.next_answer_by_user(self.user) self.write_template({"answer" : answer})

Answer2:

With webapp2, there is an optional abort parameter:

<blockquote>

redirect(uri, permanent=False, abort=False, code=None, body=None)

</blockquote>

Taken from the api <a href="https://webapp-improved.appspot.com/api/webapp2.html#webapp2.RequestHandler.redirect" rel="nofollow">here</a>

Answer3:

I accomplished this by using the abort() method and using a custom exception handler, thus trapping the exception

Recommend

  • Tornado iframe src
  • How to get the client port in RequestHandler?
  • TypeError on freebusy call to Google Calendar API v3 using Python apiclient
  • Jquery-Bootgrid, Row is null on click
  • AWS Elastic Beanstalk - changing owner of webapp folder
  • Why cygnus crashes when using ngsiv2 in orion?
  • redirect user after update in class based view in django
  • nhibernate queries SubQueryExpression
  • call a java program from a webapp in tomcat server - the java program is out side of tomcat server
  • Using an STL Iterator without initialising it
  • Python Tkinter after event OOPS implementation
  • MRO with multiple inheritance in python
  • Unix Network Programming Clarification
  • Google map in top corner left
  • Google OAuth: can't get refresh token with authorization code
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • Django foreign key drop down
  • Can't delete or rename original file after resizing
  • Execute scripts AJAX returns
  • error importing numpy
  • Grails calculated field in SQL
  • Android activity accessing service's static reference before the service is ready
  • Seeking advice on Jetty HttpClient Hang
  • Using variable in a value field in jMeter
  • Why is an OPTIONS request sent to the server?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • Return words with double consecutive letters
  • Is there a mandatory requirement to switch app.yaml?
  • Cannot Parse HTML Data Using Android / JSOUP
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app