def get(self, phoneID = None): 
            phoneKey = None
            if phoneID is not None:

                query = Phone.all()
                query.filter("phoneid =", phoneID)
                results = query.fetch(1)
                
                if query.count(1) > 0:
                    phoneKey = results[0].key()
            else:
                self.error(404)

            if phoneKey is not None:
                list = {}
                benchmark = {}

                query = Benchmark.all()
                query.order("-timestamp") # can cause extra CPU load
                query.filter("phonekey =", phoneKey)

                results = query.fetch(1000)

                benchmark["Benchmark"] = results
                list["list"] = benchmark;

                printJson(self, list)
            else:
                self.error(404)
    def post(self):
        post = simplejson.loads(self.request.body)

        phoneKey = None
        deviceKey = None
        brandKey = None
        benchmarkKey = None

        phoneID = None
        device = None
        brand = None
        totalScore = None
        testResults = []
        
        # get post data
        try:
            phoneID = post["phoneid"]
            device = post["device"]    
            brand = post["brand"]
            totalScore = int(post["total_score"])
            testResults = post["results"]
        except KeyError:
            self.error(404)
        
        if phoneID is not None and device is not None and brand is not None and totalScore is not None and testResults: 
            query = Phone.all()
            query.filter("phoneid =", phoneID)
            results = query.fetch(1)
            
            # get phone key
            if query.count(1) > 0:
                phoneKey = results[0].key() 
            else:
                phoneKey = Phone(
                    phoneid = phoneID
                ).put()

            query = Device.all()
            query.filter("name =", device)
            results = query.fetch(1)

            # get device key
            if query.count(1) > 0:
                deviceKey = results[0].key()
            else:       
                query = Brand.all()
                query.filter("brandname =", brand)
                results = query.fetch(1)
                
                # get brand key
                if query.count(1) > 0:
                     brandKey = results[0].key()
                else:
                    # add brand
                    brandKey = Brand(
                        brandname = brand
                    ).put()

                # add device
                deviceKey = Device(
                    brandkey = Key(brandKey),
                    name = device
                ).put()
                
            # new benchmark           
            if phoneKey is not None and deviceKey is not None:
                benchmarkKey = Benchmark(
                    phonekey = phoneKey,
                    devicekey = deviceKey,
                    total_score = totalScore
                ).put()

                if benchmarkKey is not None:                    
                    for result in testResults:
                        test = None
                        testKey = None
                        
                        try:
                            test = result["test"]
                        except KeyError:
                            self.error(404)


                        # get test key
                        query = Test.all()
                        query.filter("name =", test)
                        results = query.fetch(1)
                        
                        # get phone key
                        if query.count(1) > 0:
                            testKey = results[0].key() 

                        score = None
                        successful = None

                        try:                        
                            score = int(result["score"])
                            successful = bool(result["successful"])
                        except KeyError:
                            self.error(404)                          

                        if testKey is not None and score is not None and successful is not None:
                            # put test result
                            testResultKey = TestResult(
                                benchmarkkey = benchmarkKey,
                                testkey = testKey,
                                score = score,
                                successful = successful
                            ).put()
                            if not testResultKey:
                                self.error(405)
                                break;
                        else:
                            self.error(404)
                else:
                    self.error(404)
            else:
                self.error(404)
        else:
            self.error(404)