2013-07-03 65 views
3

我正在製作應用程序來上傳文字和圖片。我已經對blobstore和Google高性能映像服務有了很多瞭解,最後我得到了將它們一起實施的方法。GAE + NDB + Blobstore + Google高性能圖像服務

我想知道的是,如果一切都做得好,或者可以以更好的方式做,並且如果在模型中保存serving_url更好,或者每次我想要在每次打印圖像時計算這一頁。

只有用戶和圖片。

這是代碼(總結,忘了我的custom.PageHandler,只有具備的功能來呈現輕鬆的網頁,並檢查形式值的東西,等):

class User(ndb.Model): 
    """ A User """ 
    username = ndb.StringProperty(required=True) 
    password = ndb.StringProperty(required=True) 
    email = ndb.StringProperty(required=True) 

class Picture(ndb.Model): 
    """ All pictures that a User has uploaded """ 
    title = ndb.StringProperty(required=True) 
    description = ndb.StringProperty(required=True) 
    blobKey = ndb.BlobKeyProperty(required=True) 
    servingUrl = ndb.StringProperty() 
    created = ndb.DateTimeProperty(auto_now_add=True) 
    user = ndb.KeyProperty(kind=User) 

# This class shows the user pics 
class List(custom.PageHandler): 
    def get(self): 
     # Get the actual user pics 
     pics = Picture.by_user(self.user.key) 
     for pic in pics: 
      pic.servingUrl = images.get_serving_url(pic.blobKey, size=90, crop=True) 
     self.render_page("myPictures.htm", data=pics) 

# Get and post for the send page 
class Send(custom.PageHandler, blobstore_handlers.BlobstoreUploadHandler): 
    def get(self): 
     uploadUrl = blobstore.create_upload_url('/addPic') 
     self.render_page("addPicture.htm", form_action=uploadUrl) 

    def post(self): 
     # Create a dictionary with the values, we will need in case of error 
     templateValues = self.template_from_request() 
     # Test if all data form is valid 
     testErrors = check_fields(self) 

     if testErrors[0]: 
      # No errors, save the object 
      try: 
       # Get the file and upload it 
       uploadFiles = self.get_uploads('picture') 
       # Get the key returned from blobstore, for the first element 
       blobInfo = uploadFiles[0] 
       # Add the key to the template 
       templateValues['blobKey'] = blobInfo.key() 

       # Save all 
       pic = Picture.save(self.user.key, **templateValues) 
       if pic is None: 
        logging.error('Picture save error.') 

       self.redirect("/myPics") 

      except: 
       self.render_page("customMessage.htm", custom_msg=_("Problems while uploading the picture.")) 
     else: 
      # Errors, render the page again, with the values, and showing the errors 
      templateValues = custom.prepare_errors(templateValues, testErrors[1]) 
      # The session for upload a file must be new every reload page 
      templateValues['form_action'] = blobstore.create_upload_url('/addPic') 

      self.render_page("addPicture.htm", **templateValues) 

基本上,我列出所有的圖片,顯示在這條線的Jinja2模板圖像:

{% for line in data %} 
    <tr> 
    <td class="col-center-data"><img src="{{ line.servingUrl }}"></td> 

所以在List類我計算出每個服務的URL,並暫時將其添加到模型中。我不知道是否將它直接保存在模型中,因爲我不知道URL是否會隨時間而改變。將永久的圖像的網址?在這種情況下,我可以保存它而不是計算,是真的嗎?

Send類只顯示上傳圖像並將數據保存到模型的表單。在重新渲染頁面的情況下,我總是生成一個新的form_action鏈接,因爲文檔討論它。這樣對嗎?

該代碼正在工作,但我想知道哪種方法是更好的方法,在性能和資源節約方面。

回答

4

你說得對。您確實想要保存get_serving_url()而不是重複調用它。它保持不變。

請注意,當您完成網址時,會出現delete_serving_url(),就像刪除blob時一樣。

+0

完美,感謝您的確認,@dragonx – Eagle