Пример #1
0
    def update_component(self, argumentMap):
        # Decode JSON
        componentDescription = {}
        try:
            componentDescription = json.loads(self.request.body)
        except ValueError:
            return self.abort(400)

        # Check API key
        if not self.checkApiKeyIsAdmin(componentDescription.get('api_key')) and not self.checkApiKeyIsAdmin(argumentMap.get('api_key')):
            return self.abort(403)

        # Check parameters
        requiredKeys = ['name', 'stock', 'price', 'step']
        if not self.checkRequiredKeys(componentDescription, requiredKeys):
            return self.abort(400)

        compName  = componentDescription.pop('name')
        compStock = componentDescription.pop('stock')
        compPrice = componentDescription.pop('price')
        compStep  = componentDescription.pop('step')
        compId    = componentDescription.pop('id', None)

        # If 'id' is in query string, update, otherwise add the new one
        if compId != None:
            entities.apiComponent().update(long(compId), compName, long(compStock), long(compStep), float(compPrice))
            self.response.write(json.dumps({ 'success': True }))
        else:
            entities.apiComponent().add(compName, long(compStock), long(compStep), float(compPrice))
            self.response.write(json.dumps({ 'success': True }))
Пример #2
0
    def update_component(self, argumentMap):
        # Decode JSON
        componentDescription = {}
        try:
            componentDescription = json.loads(self.request.body)
        except ValueError:
            return self.abort(400)

        # Check API key
        if not self.checkApiKeyIsAdmin(componentDescription.get(
                'api_key')) and not self.checkApiKeyIsAdmin(
                    argumentMap.get('api_key')):
            return self.abort(403)

        # Check parameters
        requiredKeys = ['name', 'stock', 'price', 'step']
        if not self.checkRequiredKeys(componentDescription, requiredKeys):
            return self.abort(400)

        compName = componentDescription.pop('name')
        compStock = componentDescription.pop('stock')
        compPrice = componentDescription.pop('price')
        compStep = componentDescription.pop('step')
        compId = componentDescription.pop('id', None)

        # If 'id' is in query string, update, otherwise add the new one
        if compId != None:
            entities.apiComponent().update(long(compId), compName,
                                           long(compStock), long(compStep),
                                           float(compPrice))
            self.response.write(json.dumps({'success': True}))
        else:
            entities.apiComponent().add(compName, long(compStock),
                                        long(compStep), float(compPrice))
            self.response.write(json.dumps({'success': True}))
Пример #3
0
    def remove_component(self, queryStringArguments):
        # Check API key
        if not self.checkApiKeyIsAdmin(queryStringArguments.get('api_key')):
            return self.abort(403)

        # Check si l'id du component est present
        requiredKeys = ['id']
        if not self.checkRequiredKeys(queryStringArguments, requiredKeys):
            return self.abort(400)

        # Get component id
        compId = queryStringArguments.pop('id')

        # Delete component
        entities.apiComponent().delete(long(compId))

        json.dump({'success': True}, self.response)
Пример #4
0
    def get_components(self, argumentMap):
        components = entities.apiComponent().search(None)

        componentsData = []
        for component in components:
            componentsData.append(self.serializableDataFromComponent(component))

        json.dump(componentsData, self.response)
Пример #5
0
    def remove_component(self, queryStringArguments):
        # Check API key
        if not self.checkApiKeyIsAdmin(queryStringArguments.get('api_key')):
            return self.abort(403)

        # Check si l'id du component est present
        requiredKeys = ['id']
        if not self.checkRequiredKeys(queryStringArguments, requiredKeys):
            return self.abort(400)

        # Get component id
        compId = queryStringArguments.pop('id')

        # Delete component
        entities.apiComponent().delete(long(compId))

        json.dump({'success': True}, self.response)
Пример #6
0
    def get_components(self, argumentMap):
        components = entities.apiComponent().search(None)

        componentsData = []
        for component in components:
            componentsData.append(
                self.serializableDataFromComponent(component))

        json.dump(componentsData, self.response)
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
    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() }))
Пример #10
0
    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()}))