Example #1
0
    def sync_models(self):

        if self.counters_to_download is None or self.counters_to_upload is None:
            self.sync_device_records()

        # Download (but prepare for errors--both thrown and unthrown!)
        download_results = {
            "saved_model_count": 0,
            "unsaved_model_count": 0,
        }
        try:
            response = json.loads(
                self.post("models/download", {
                    "device_counters": self.counters_to_download
                }).content)
            # As usual, we're deserializing from the central server, so we assume that what we're getting
            #   is "smartly" dumbed down for us.  We don't need to specify the src_version, as it's
            #   pre-cleanaed for us.
            download_results = model_sync.save_serialized_models(
                response.get("models", "[]"))
            self.session.models_downloaded += download_results[
                "saved_model_count"]
            self.session.errors += download_results.has_key("error")
            self.session.errors += download_results.has_key("exceptions")
        except Exception as e:
            download_results["error"] = e
            self.session.errors += 1

        # Upload (but prepare for errors--both thrown and unthrown!)
        upload_results = {
            "saved_model_count": 0,
            "unsaved_model_count": 0,
        }
        try:
            # By not specifying a dest_version, we're sending everything.
            #   Again, this is OK because we're sending to the central server.
            response = self.post(
                "models/upload", {
                    "models":
                    model_sync.get_serialized_models(self.counters_to_upload)
                })
            upload_results = json.loads(response.content)
            self.session.models_uploaded += upload_results["saved_model_count"]
            self.session.errors += upload_results.has_key("error")
            self.session.errors += upload_results.has_key("exceptions")
        except Exception as e:
            upload_results["error"] = e
            self.session.errors += 1

        self.counters_to_download = None
        self.counters_to_upload = None

        return {
            "download_results": download_results,
            "upload_results": upload_results
        }
Example #2
0
def model_download(data, session):
    """This device is having its own data downloaded"""
    if "device_counters" not in data:
        return JsonResponse({"error": "Must provide device counters.", "count": 0}, status=500)
    try:
        # Return the objects serialized to the version of the other device.
        result = model_sync.get_serialized_models(data["device_counters"], zone=session.client_device.get_zone(), include_count=True, dest_version=session.client_version)
    except Exception as e:
        result = { "error": e.message, "count": 0 }

    session.models_downloaded += result["count"]
    session.errors += result.has_key("error")
    return JsonResponse(result)
Example #3
0
    def sync_models(self):

        if self.counters_to_download is None or self.counters_to_upload is None:
            self.sync_device_records()

        # Download (but prepare for errors--both thrown and unthrown!)
        download_results = {
            "saved_model_count" : 0,
            "unsaved_model_count" : 0,
        }
        try:
            response = json.loads(self.post("models/download", {"device_counters": self.counters_to_download}).content)
            # As usual, we're deserializing from the central server, so we assume that what we're getting
            #   is "smartly" dumbed down for us.  We don't need to specify the src_version, as it's
            #   pre-cleanaed for us.
            download_results = model_sync.save_serialized_models(response.get("models", "[]"))
            self.session.models_downloaded += download_results["saved_model_count"]
            self.session.errors += download_results.has_key("error")
            self.session.errors += download_results.has_key("exceptions")
        except Exception as e:
            download_results["error"] = e
            self.session.errors += 1

        # Upload (but prepare for errors--both thrown and unthrown!)
        upload_results = {
            "saved_model_count" : 0,
            "unsaved_model_count" : 0,
        }
        try:
            # By not specifying a dest_version, we're sending everything.
            #   Again, this is OK because we're sending to the central server.
            response = self.post("models/upload", {"models": model_sync.get_serialized_models(self.counters_to_upload)})
            upload_results = json.loads(response.content)
            self.session.models_uploaded += upload_results["saved_model_count"]
            self.session.errors += upload_results.has_key("error")
            self.session.errors += upload_results.has_key("exceptions")
        except Exception as e:
            upload_results["error"] = e
            self.session.errors += 1

        self.counters_to_download = None
        self.counters_to_upload = None

        return {"download_results": download_results, "upload_results": upload_results}