2012-01-03 63 views
0

我有一個可以正常工作的應用程序。我試圖讓其中一個腳本只在用戶是管理員(我)時才運行。管理員登錄工作正常,但後來我重定向到頁面/地理編碼,什麼也沒有發生。我看到200的HTTP響應。以管理員身份登錄後腳本無法運行,但如果不需要管理員,則工作正常

有什麼建議嗎?

謝謝

編輯2:要求添加完整的代碼。

的app.yaml:

application: theapp 
version: 1 
runtime: python 
api_version: 1 

handlers: 

- url: /geocode 
    script: geocode.py 
    login: admin 

- url: /admin/.* 
    script: $PYTHON_LIB/google/appengine/ext/admin 
    login: admin 

- url: /favicon\.ico 
    static_files: static/images/favicon.ico 
    upload: static/images/favicon\.ico 

- url: /static 
    static_dir: static 

- url: /.* 
    script: first.py 
    secure: never 

builtins: 
- appstats: on 

inbound_services: 
- warmup 

first.py:

import os 

from beer import Beer 
from desktop import Desktop 
from geocode import Geocode 


from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 

application = webapp.WSGIApplication(
     [('/', Desktop), 
     ('/beer', Beer), 
     ('/geocode', Geocode)], 
     debug=True) 

def main(): 
    run_wsgi_app(application) 

if __name__ == "__main__": 
    main() 

geocode.py:

import pubs 
import os 
import time 


from google.appengine.ext import webapp 
from google.appengine.ext.webapp import template 
from google.appengine.ext.webapp import util 
from google.appengine.api import urlfetch 

import urllib 
import urllib2 
import simplejson 

class Geocode(webapp.RequestHandler): 
    '''Class for ./geocode 
    A couple of methods to take the address' from the Pubs.pub_address array 
    and turn them into usefull Latitudes & Longitudes. 
    ''' 
    def get_lat_long(self, location): 
     place = urllib.quote_plus(location) 
     print location 
     url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=true&region=no" % (place) 
     print url 
     out = urllib.urlopen(url) 
     jsonResponse = simplejson.load(out) 
     pub_location = jsonResponse.get('results')[0].get('geometry').get('location') 
     lat = pub_location.get('lat') 
     lng = pub_location.get('lng') 
     return lat, lng 

    def get(self): 
     pub_address = [] 
     pub_bounce = [] 
     self.response.headers['Content-Type'] = 'text/html; charset=utf-8' 
     path = os.path.join(os.path.dirname(__file__), 'templates') 
     path = os.path.join(path, 'geocode.html') 

     for i, element in enumerate(pubs.pub_list): 
      pub_bounce.append(pubs.pub_list[i].pub_bounce) 

     for i, element in enumerate(pubs.pub_list): 
      pub_address.append(pubs.pub_list[i].pub_address.encode('utf-8')) 

     for i, element in enumerate(pub_address): 
      pubs.pub_list[i].pub_lat, pubs.pub_list[i].pub_lng = self.get_lat_long(pub_address[i]) 
      print pubs.pub_list[i].pub_lat, pubs.pub_list[i].pub_lng 
      time.sleep(.2) 


     template_values = {"pub_address": pub_address, "pub_bounce": pub_bounce} 
     self.response.out.write(template.render(path, template_values)) 
+0

不應該登錄/ geocode登錄:需要而不是管理員? – 2012-01-03 16:46:10

+0

您可以提供關於/ geocode工作的任何東西嗎? – 2012-01-03 16:47:50

+0

/geocode從數據模型中獲取一些地址並將其轉換爲經緯度。正如我所提到的,如果/ geocode的處理程序被移除,它可以正常工作。 「login:admin」適用於已登錄和管理員的用戶。 「login:required」似乎僅適用於已登錄的任何用戶。 – Gareth 2012-01-03 18:12:34

回答

1

我認爲你需要這樣的:

- url: /geocode 
    script: first.py 
    login: admin 

從app.yaml中需要參考的腳本是應用處理器(例如,具有run_wsgi_app等在其中)。 geocode.py不是應用程序處理程序 - 它是由first.py導入的一些代碼。

+0

謝謝。這解決了它。 – Gareth 2012-01-04 02:42:29

1

貴geocode.py通過POST方法傳遞變量?

根據我的經驗,在重定向到「登錄」頁面時,通過POST方法傳遞的變量將會丟失。 (通過GET傳遞的變量將保持) 然後它會導致200響應的情況,但沒有真正發生。

# app.yaml 
application: xxx 
version: 1 
runtime: python 
api_version: 1 


handlers: 
- url: /test\.html 
    static_files: test.html 
    upload: test.html 

- url:/
    login: admin 
    script: urls.py 
# urls.py 
from google.appengine.ext import db, webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
import logging 

class Test(webapp.RequestHandler): 
    def get(self): 
     print "get" 
     print self.request.get("var") 

     logging.info("GET %s"%self.request.get("var")) 

    def post(self): 
     print "post" 
     print self.request.get("var") 

     logging.info("POST %s"%self.request.get("var")) 

application = webapp.WSGIApplication([ 
    (r'/', Test),]) 

def main(): 
    run_wsgi_app(application) 

#=============================================================================== 
# # it is necessary; otherwise, it will cause the server won't response 
# # at the first request which create a instance 
#=============================================================================== 
if __name__ == "__main__": 
    main() 


<!-- test.html --> 
<html> 
    <body> 
     <form method="POST" action="/"> 
      <input type="text" name="var" value="123"> 
      <input type="submit"> 
     </form> 
    </body> 
</html> 
+0

我編輯了我的問題以顯示Geocode腳本創建了兩個傳遞給視圖的列表。沒有郵政那裏,但感謝您花時間幫助:) – Gareth 2012-01-03 23:50:10

相關問題