In order to provide the customer with information on their last purchase, in the domain service class for purchases, we need to add the following method. The technique for finding the last purchase involves including a sort parameter in the pymongo.collection.Collection.find_one() method, which tells the driver to sort by dateOfPurchase in descending order:
def fetchLastPurchaseForCust(self, custKey) :
query = dict({"customerKey" : custKey})
projection = None
return self.db.purchases.find_one(query, projection, 0, 0, \
False, CursorType.NON_TAILABLE, [('dateOfPurchase', -1)])
Returning to the HTML responder class, we add another custom method that pulls information out of the Purchase object that's presented as an argument. Note that productsPurchased is itself a dictionary of ProductPurchase objects. Accordingly, a for loop is needed for extraction:
def buildLastPurchase...