def update_step(self, argumentMap): # Decode JSON stepDescription = {} try: stepDescription = json.loads(self.request.body) except ValueError: return self.abort(400) # Check API key if not self.checkApiKeyIsAdmin(stepDescription.get('api_key')) and not self.checkApiKeyIsAdmin(argumentMap.get('api_key')): return self.abort(403) # Check parameters requiredKeys = ['name', 'number', 'type'] if not self.checkRequiredKeys(stepDescription, requiredKeys): return self.abort(400) # Fetch elements stepName = stepDescription.pop('name') stepIndex = stepDescription.pop('number') stepType = stepDescription.pop('type') stepId = stepDescription.pop('id', None) # Check type if not stepType in ['multi', 'warning', 'one']: return self.abort(400) # If 'id' is in query string, update, otherwise add the new one if stepId != None: entities.apiStep().update(stepName, long(stepIndex), stepType, long(stepId)) self.response.write(json.dumps({ 'success': True })) else: entities.apiStep().add(stepName, long(stepIndex), stepType) self.response.write(json.dumps({ 'success': True }))
def remove_step(self, queryStringArguments): # Check API key if not self.checkApiKeyIsAdmin(queryStringArguments.get('api_key')): return self.abort(403) # Check parameters requiredKeys = ['id'] if not self.checkRequiredKeys(queryStringArguments, requiredKeys): return self.abort(400) # Get Step id stepId = queryStringArguments.pop('id') # Delete step entities.apiStep().delete(long(stepId)) json.dump({'success': True}, self.response)
def update_step(self, argumentMap): # Decode JSON stepDescription = {} try: stepDescription = json.loads(self.request.body) except ValueError: return self.abort(400) # Check API key if not self.checkApiKeyIsAdmin(stepDescription.get( 'api_key')) and not self.checkApiKeyIsAdmin( argumentMap.get('api_key')): return self.abort(403) # Check parameters requiredKeys = ['name', 'number', 'type'] if not self.checkRequiredKeys(stepDescription, requiredKeys): return self.abort(400) # Fetch elements stepName = stepDescription.pop('name') stepIndex = stepDescription.pop('number') stepType = stepDescription.pop('type') stepId = stepDescription.pop('id', None) # Check type if not stepType in ['multi', 'warning', 'one']: return self.abort(400) # If 'id' is in query string, update, otherwise add the new one if stepId != None: entities.apiStep().update(stepName, long(stepIndex), stepType, long(stepId)) self.response.write(json.dumps({'success': True})) else: entities.apiStep().add(stepName, long(stepIndex), stepType) self.response.write(json.dumps({'success': True}))
def get_steps(self, argumentMap): steps = entities.apiStep().search(None) stepsData = [] for step in steps: stepData = self.serializableDataFromStep(step) components = entities.apiComponent().compByStep(step.key().id()) componentsData = [] for component in components: componentsData.append(self.serializableDataFromComponent(component)) stepData['components'] = componentsData stepsData.append(stepData) json.dump(stepsData, self.response)
def get_steps(self, argumentMap): steps = entities.apiStep().search(None) stepsData = [] for step in steps: stepData = self.serializableDataFromStep(step) components = entities.apiComponent().compByStep(step.key().id()) componentsData = [] for component in components: componentsData.append( self.serializableDataFromComponent(component)) stepData['components'] = componentsData stepsData.append(stepData) json.dump(stepsData, self.response)
def update_order(self, argumentMap): # Decode JSON orderData = {} try: orderData = json.loads(self.request.body) except ValueError: return self.abort(409) # Check parameters requiredKeys = ['components'] if not self.checkRequiredKeys(orderData, requiredKeys): return self.abort(408) orderComponents = orderData.pop('components') orderDateCreation = orderData.pop('dateCreation', time.time()) orderDateSelling = orderData.pop('dateSelling', None) orderId = orderData.pop('id', None) orderFId = orderData.pop('fid', None) # Check API key if argumentMap.get('api_key'): orderData['api_key'] = argumentMap.pop('api_key') apiUser = None if orderData.has_key('api_key'): apiUser = entities.apiUser().getApiKey(orderData['api_key']) if not apiUser: return self.abort(407) orderUser = apiUser.key().name() else: orderUser = None # Build components list componentsIds = [] steps = {} for compId in orderComponents: compQuantity = orderComponents[str(compId)] # Fetch component component = entities.apiComponent().get(long(compId)) if component == None: return self.abort(406) # Fetch step if component.Step.key().id() not in steps: step = entities.apiStep().get(component.Step.key().id()) if step == None: return self.abort(405) steps[component.Step.key().id()] = step else: step = steps[component.Step.key().id()] # Check number of components for type `one` if step.type == "one": return self.abort(402) # Decrease component stock component.stock -= compQuantity component.put() for i in range(compQuantity): componentsIds.append(long(compId)) # Favorite Hits if orderFId != None: favourite = entities.apifavoriteOrder().get(orderFId) if not favourite: return slef.abort(401) favourite.nbVote += 1 favourite.put() # Ajout/Update de l'order if orderId != None: if not apiUser or not apiUser.admin: return self.abort(403) order = entities.apiOrder().update(componentsIds, long(orderId), datetime.datetime.fromtimestamp(orderDateSelling), str(orderUser)) self.response.write(json.dumps({ 'orderId': order.key().id() })) else: order = entities.apiOrder().add(componentsIds, datetime.datetime.fromtimestamp(orderDateCreation), str(orderUser)) self.response.write(json.dumps({ 'orderId': order.key().id() }))
def update_order(self, argumentMap): # Decode JSON orderData = {} try: orderData = json.loads(self.request.body) except ValueError: return self.abort(409) # Check parameters requiredKeys = ['components'] if not self.checkRequiredKeys(orderData, requiredKeys): return self.abort(408) orderComponents = orderData.pop('components') orderDateCreation = orderData.pop('dateCreation', time.time()) orderDateSelling = orderData.pop('dateSelling', None) orderId = orderData.pop('id', None) orderFId = orderData.pop('fid', None) # Check API key if argumentMap.get('api_key'): orderData['api_key'] = argumentMap.pop('api_key') apiUser = None if orderData.has_key('api_key'): apiUser = entities.apiUser().getApiKey(orderData['api_key']) if not apiUser: return self.abort(407) orderUser = apiUser.key().name() else: orderUser = None # Build components list componentsIds = [] steps = {} for compId in orderComponents: compQuantity = orderComponents[str(compId)] # Fetch component component = entities.apiComponent().get(long(compId)) if component == None: return self.abort(406) # Fetch step if component.Step.key().id() not in steps: step = entities.apiStep().get(component.Step.key().id()) if step == None: return self.abort(405) steps[component.Step.key().id()] = step else: step = steps[component.Step.key().id()] # Check number of components for type `one` if step.type == "one": return self.abort(402) # Decrease component stock component.stock -= compQuantity component.put() for i in range(compQuantity): componentsIds.append(long(compId)) # Favorite Hits if orderFId != None: favourite = entities.apifavoriteOrder().get(orderFId) if not favourite: return slef.abort(401) favourite.nbVote += 1 favourite.put() # Ajout/Update de l'order if orderId != None: if not apiUser or not apiUser.admin: return self.abort(403) order = entities.apiOrder().update( componentsIds, long(orderId), datetime.datetime.fromtimestamp(orderDateSelling), str(orderUser)) self.response.write(json.dumps({'orderId': order.key().id()})) else: order = entities.apiOrder().add( componentsIds, datetime.datetime.fromtimestamp(orderDateCreation), str(orderUser)) self.response.write(json.dumps({'orderId': order.key().id()}))