From 372c32d02eaf443c4540ea65a57fa9a8595f1f56 Mon Sep 17 00:00:00 2001 From: "amyu@google.com" Date: Thu, 3 Jan 2013 04:01:24 +0000 Subject: [PATCH] update to reflect API changes git-svn-id: http://google-app-engine-samples.googlecode.com/svn/trunk@167 99225164-8649-0410-878b-2ba91e509939 --- search/product_search_python/docs.py | 30 ++++++++++++------------------ search/product_search_python/handlers.py | 6 +++--- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/search/product_search_python/docs.py b/search/product_search_python/docs.py index d231320..7e41a21 100644 --- a/search/product_search_python/docs.py +++ b/search/product_search_python/docs.py @@ -49,19 +49,13 @@ class BaseDocumentManager(object): """ self.doc = doc fields = doc.fields - self.doc_map = {} - for field in fields: - fieldlist = self.doc_map.get(field.name,[]) - fieldlist.append(field) - self.doc_map[field.name] = fieldlist - - def getFirstFieldVal(self, fname): - """Get the value of the (first) document field with the given name.""" + + def getFieldVal(self, fname): + """Get the value of the document field with the given name. If there is + more than one such field, the method returns None.""" try: - return self.doc_map.get(fname)[0].value - except IndexError: - return None - except TypeError: + return self.doc.field(fname).value + except ValueError: return None def setFirstField(self, new_field): @@ -267,19 +261,19 @@ class Product(BaseDocumentManager): def getPID(self): """Get the value of the 'pid' field of a Product doc.""" - return self.getFirstFieldVal(self.PID) + return self.getFieldVal(self.PID) def getName(self): """Get the value of the 'name' field of a Product doc.""" - return self.getFirstFieldVal(self.PRODUCT_NAME) + return self.getFieldVal(self.PRODUCT_NAME) def getDescription(self): """Get the value of the 'description' field of a Product doc.""" - return self.getFirstFieldVal(self.DESCRIPTION) + return self.getFieldVal(self.DESCRIPTION) def getCategory(self): """Get the value of the 'cat' field of a Product doc.""" - return self.getFirstFieldVal(self.CATEGORY) + return self.getFieldVal(self.CATEGORY) def setCategory(self, cat): """Set the value of the 'cat' (category) field of a Product doc.""" @@ -287,7 +281,7 @@ class Product(BaseDocumentManager): def getAvgRating(self): """Get the value of the 'ar' (average rating) field of a Product doc.""" - return self.getFirstFieldVal(self.AVG_RATING) + return self.getFieldVal(self.AVG_RATING) def setAvgRating(self, ar): """Set the value of the 'ar' field of a Product doc.""" @@ -295,7 +289,7 @@ class Product(BaseDocumentManager): def getPrice(self): """Get the value of the 'price' field of a Product doc.""" - return self.getFirstFieldVal(self.PRICE) + return self.getFieldVal(self.PRICE) @classmethod def generateRatingsBuckets(cls, query_string): diff --git a/search/product_search_python/handlers.py b/search/product_search_python/handlers.py index e808310..ffb9951 100644 --- a/search/product_search_python/handlers.py +++ b/search/product_search_python/handlers.py @@ -530,9 +530,9 @@ class StoreLocationHandler(BaseHandler): response_obj2 = [] for res in results: gdoc = docs.Store(res) - geopoint = gdoc.getFirstFieldVal(gdoc.STORE_LOCATION) - resp = {'addr': gdoc.getFirstFieldVal(gdoc.STORE_ADDRESS), - 'storename': gdoc.getFirstFieldVal(gdoc.STORE_NAME), + geopoint = gdoc.getFieldVal(gdoc.STORE_LOCATION) + resp = {'addr': gdoc.getFieldVal(gdoc.STORE_ADDRESS), + 'storename': gdoc.getFieldVal(gdoc.STORE_NAME), 'lat': geopoint.latitude, 'lon': geopoint.longitude} response_obj2.append(resp) logging.info("resp: %s", response_obj2) -- 2.11.4.GIT