2010-11-07 88 views
1

我不確定這是否有效。它有效,但有時我覺得......很奇怪。你能告訴我這是否是一種好方法嗎?這是在App Engine/Python中做/處理url重寫的正確方法嗎?

我扔在引擎收錄的代碼,因爲我覺得這是一個有點太多,放在這裏:http://pastebin.com/662TiQLq

編輯 我編輯了標題,使其更客觀。

+4

您應該包括這裏的代碼,所以這個問題是自給自足的。從大量的代碼中你不清楚你究竟在詢問什麼 - 雖然我沒有看到在這個代碼片段中有任何URL重寫。 – 2010-11-07 10:46:41

回答

2

我只是猜測提問者詢問在處理程序的__ init __函數中創建函數字典,然後在「get」函數中使用此字典來查找特定函數。如果這是個問題,那麼恕我直言,更清晰的方法是爲每個不同的功能設置不同的處理程序。例如,

class QuotesView(webapp.RequestHandler): 
    """Super class for quotes that can accommodate common functionality""" 
    pass 

class QuotesViewSingle(QuotesView): 
    def get(self): 
     ... 

class QuotesViewRandom(QuotesView): 
    def get(self): 
     ... 

class QuotesViewAll(QuotesView): 
    def get(self): 
     ... 

def main(): 
    application = webapp.WSGIApplication([('/quote/new',NewQuote), 
              (r'/quotes/single',QuotesViewSingle), 
              (r'/quotes/all',QuotesViewAll), 
              (r'/quotes/random',QuotesViewRandom), 
              ... 
              ('/', MainHandler)], 
             debug=True) 

順便說一句。很多人在WSGIApplication調用中使用正則表達式來解析get函數的參數。它沒有什麼特別的錯誤。我不是那個功能的忠實粉絲,而且更喜歡解析get函數中的參數。但那只是我。

爲了完整這裏是原代碼:

class Quote(db.Model): 
    author = db.StringProperty() 
    string = db.StringProperty() 


class MainHandler(webapp.RequestHandler): 
    def get(self): 
     user = users.get_current_user() 


     quotes = Quote.all() 
     path = os.path.join(os.path.dirname(__file__),'quotery.html') 
     template_values = {'quotes':quotes,'user':user,'login_url':users.create_login_url('/')} 
     self.response.out.write(template.render(path, template_values)) 


class QuoteHandler(webapp.RequestHandler): 

    def __init__(self): 
     self.actions = {'fetch':self.fetch, 'random':self.fetch_random} 

     #Memcache the number of quotes in the datastore, to minimize datastore calls 
     self.quote_count = memcache.get('quote_count') 
     if not self.quote_count: 
      self.quote_count = self.cache_quote_count() 

    def cache_quote_count(self): 
     count = Quote.all().count() 
     memcache.add(key='quote_count', value=count, time=3600) 
     return count 


    def get(self, key): 
     if key in self.actions: 
      action = self.actions[key] 
      action() 




    def fetch(self): 
     for quote in Quote.all(): 
      print 'Quote!' 
      print 'Author: ',quote.author 
      print 'String: ',quote.string 
      print 


    def fetch_random(self): 
     max_offset = self.quote_count-1 
     random_offset = random.randint(0,max_offset) 
     '''self.response.out.write(max_offset) 
     self.response.out.write('\n<br/>') 
     self.response.out.write(random_offset)''' 
     try: 
      query = db.GqlQuery("SELECT * FROM Quote") 
      quotes = query.fetch(1,random_offset) 
      return quotes 
      '''for quote in quotes: 
       self.response.out.write(quote.author) 
       self.response.out.write('\n') 
       self.response.out.write(quote.string)''' 
     except BaseException: 
      raise 


class NewQuote(webapp.RequestHandler): 

    def post(self): 
     author = self.request.get('quote_author') 
     string = self.request.get('quote_string') 

     if not author or not string: 
      return False   
     quote = Quote() 
     quote.author = author 
     quote.string = string 
     quote.put() 
     QuoteHandler().cache_quote_count() 
     self.redirect("/") 
     #return True 


class QuotesView(webapp.RequestHandler): 

    def __init__(self): 
     self.actions = {'all':self.view_all,'random':self.view_random,'get':self.view_single} 

    def get(self, key): 
     if not key or key not in self.actions: 
      self.view_all() 
     if key in self.actions: 
      action = self.actions[key] 
      action() 

    def view_all(self): 
     print 'view all' 

    def view_random(self): 
     quotes = QuoteHandler().fetch_random() 
     template_data = {} 

     for quote in quotes: 
      template_data['quote'] = quote 

     template_path = os.path.join(os.path.dirname(__file__),'base_view.html') 
     self.response.out.write(template.render(template_path, template_data)) 


    def view_single(self): 
     print 'view single' 


def main(): 
    application = webapp.WSGIApplication([('/quote/new',NewQuote),(r'/quotes/(.*)',QuotesView),(r'/quote/(.*)',QuoteHandler),('/', MainHandler)], 
             debug=True) 
    util.run_wsgi_app(application) 
+0

謝謝。我一直有點忙,所以我無法更新帖子,對此我很抱歉。你澄清了我不確定的事情。我不知道如果我應該使用正則表達式的路徑,或者如果我應該使用「顯式」模式。 – 2010-11-09 02:47:49

相關問題