def demand_supply(request): if(request.GET.__contains__('playerId')): results = {} demand = [] supply = [] args = request.GET playerId = int(args.__getitem__('playerId')) plantQuery = Plant.gql("WHERE playerId = :1",playerId) plants = plantQuery.fetch(1000) for plant in plants: plantId = plant.key().id() shoeQuery = Shoe.gql("WHERE plantId = :1", plantId) shoes = shoeQuery.fetch(1000) for shoe in shoes: shoeId = shoe.key().id() supplyQuery = SupplyTransaction.gql("WHERE shoeId = :1",shoeId) supplys = supplyQuery.fetch(20) for _supply in supplys: _sDict = _supply.__dict__['_entity'] _sDict['trxnDate'] = str(_sDict['trxnDate']) _sDict['totalCost'] = float("%.2f" % round(_sDict['totalCost'],2)) _sDict['costPerUnit'] = float("%.2f" % round(_sDict['costPerUnit'],2)) _sDict['salePrice'] = float("%.2f" % round(shoe.price,2)) supply.append(_sDict) demandQuery = DemandTransaction.gql("WHERE shoeId = :1",shoeId) demands = demandQuery.fetch(20) for _demand in demands: _dDict = _demand.__dict__['_entity'] demand.append(_dDict) results['demand'] = demand results['supply'] = supply n = {"count": len(results)} return HttpResponse(simplejson.dumps(results),mimetype="application/json") else: dd = {} dd['error'] = "Error: No game selected." return HttpResponse(simplejson.dumps(dd),mimetype="application/json") #old method #def _retrieve_shoe_templates(request): # ''' # Return a list of shoes templates of a particular player(a particular plant) # ''' # ret = [] # if request.session.__contains__('account'): # getQuery = request.GET # playerId = int(getQuery.__getitem__('playerId')) # # shoes = Shoe.all() # for i in shoes: # plant = Plant.get_by_id(i.plantId) # if plant.playerId == playerId: # d = i.__dict__['_entity'] # d['shoeId'] = i.key().id() # ret.append(d) # else: # error = {} # error['error':"Unable to retreive shoe templates."] # return HttpResponse(simplejson.dumps(error),mimetype="application/json") # return HttpResponse(simplejson.dumps(ret),mimetype="application/json") #old method #def _create_shoe(request): # session = request.session # try: # account = session['account'] # if account != None: # q = request.GET # splantId = int(q.__getitem__('plantId')) # ssole = int(q.__getitem__('sole')) # sbody = int(q.__getitem__('body')) # scolor = int(q.__getitem__('color')) # sprice = float(q.__getitem__('price')) # _costPrice = shoeMatCost['sole'] * ssole + shoeMatCost['body'] * sbody + shoeMatCost['color'] * scolor # shoe = Shoe(plantId=splantId,sole=ssole,body = sbody,color = scolor,price = sprice,qty=0,costPrice=_costPrice) # shoe.put() # except KeyError: # return redirect("/") # return redirect("/app/main.html") #old method #def _manufacture_shoe(request): # if request.session.__contains__('account'): # args = request.GET # if args.__contains__('shoeId') and args.__contains__('playerId'): # try: # playerId = int(args.__getitem__('playerId')) # plantId = int(args.__getitem__('plantId')) # sshoeId = int(args.__getitem__('shoeId')) # # plant = Plant.get_by_id(plantId) # shoe = Shoe.get_by_id(sshoeId) # if plant != None and plant.playerId == playerId: # player = Player.get_by_id(playerId) # number = int(args.__getitem__('qty')) # stotalCost = shoe.costPrice * number # toBeCapacity = plant.currentCapacity + number # # if toBeCapacity <= plant.productionLimit and stotalCost <= player.money: # supplyTransaction = SupplyTransaction(shoeId=sshoeId,costPerUnit = shoe.costPrice,qty = number,totalCost=stotalCost,trxnDate = datetime.datetime.now().date()) # supplyTransaction.put() # shoe.qty = shoe.qty + number # shoe.put() # player.money = player.money - stotalCost # player.money_spent = player.money_spent + stotalCost # player.networth = player.money + player.money_spent # player.put() # plant.currentCapacity += number # plant.put() # else: # return HttpResponse("not enough cap") # else: # return HttpResponse("plant is none or plant does not belongs to playerId") # except ValueError: # return redirect("/error?error=ValueError") # else: # return HttpResponse("no values") # else: # #return redirect("/index") # return HttpResponse("error no account") # return redirect("/app/main.html") #Converting Python Objects to JSON format #use simplejson.dumps(obj.__dict__) #the resulting JSON string for the Account class is as follows: #{"_email": "test", "_money": 1000000.0, "_parent_key": null, "_parent": null, "_entity": {"money": 1000000.0, "email": "test", "networth": 0.0}, "_app": null, "_Model__namespace": "", "_networth": 0.0} #in angular, use > response._entity to get the account JSON object