Ejemplo n.º 1
0
    def get_payload(self):
        """
        This function currently returns either a full string payload or a string name of a file
        that contains the contents of the payload. If FILE_RESTORE toggle is enabled, then this will return
        the filename, otherwise it will return the full string payload
        """
        user = self.user
        last_synclog = self.sync_log

        self.validate()

        cached_response = self.get_cached_payload()
        if cached_response.exists():
            return cached_response

        start_time = datetime.utcnow()
        last_seq = str(get_db().info()["update_seq"])

        # create a sync log for this
        previous_log_id = last_synclog.get_id if last_synclog else None
        new_synclog = SyncLog(
            user_id=user.user_id,
            last_seq=last_seq,
            owner_ids_on_phone=user.get_owner_ids(),
            date=datetime.utcnow(),
            previous_log_id=previous_log_id
        )
        new_synclog.save(**get_safe_write_kwargs())

        # start with standard response
        with get_restore_class(user)(user.username, items=self.items) as response:
            # add sync token info
            response.append(xml.get_sync_element(new_synclog.get_id))
            # registration block
            response.append(xml.get_registration_element(user))

            # fixture block
            for fixture in generator.get_fixtures(user, self.version, last_synclog):
                response.append(fixture)

            case_response, self.num_batches = get_case_payload_batched(
                self.domain, self.stock_settings, self.version, user, last_synclog, new_synclog
            )
            combined_response = response + case_response
            case_response.close()
            combined_response.finalize()

        duration = datetime.utcnow() - start_time
        new_synclog.duration = duration.seconds
        new_synclog.save()
        self.set_cached_payload_if_necessary(combined_response, duration)
        return combined_response
Ejemplo n.º 2
0
    def get_payload(self):
        user = self.user
        last_sync = self.sync_log

        self.validate()

        cached_payload = self.get_cached_payload()
        if cached_payload:
            return cached_payload

        start_time = datetime.utcnow()
        last_seq = str(get_db().info()["update_seq"])

        # create a sync log for this
        previous_log_id = last_sync.get_id if last_sync else None
        synclog = SyncLog(
            user_id=user.user_id,
            last_seq=last_seq,
            owner_ids_on_phone=user.get_owner_ids(),
            date=datetime.utcnow(),
            previous_log_id=previous_log_id
        )
        synclog.save(**get_safe_write_kwargs())

        # start with standard response
        batch_enabled = BATCHED_RESTORE.enabled(self.user.domain) or BATCHED_RESTORE.enabled(self.user.username)
        logger.debug('Batch restore enabled: %s', batch_enabled)
        if batch_enabled:
            response = StringRestoreResponse(user.username, items=self.items)
        else:
            response = EtreeRestoreResponse(user.username, items=self.items)

        # add sync token info
        response.append(xml.get_sync_element(synclog.get_id))
        # registration block
        response.append(xml.get_registration_element(user))

        # fixture block
        for fixture in generator.get_fixtures(user, self.version, last_sync):
            response.append(fixture)

        payload_fn = self._get_case_payload_batched if batch_enabled else self._get_case_payload
        response = payload_fn(response, user, last_sync, synclog)

        resp = str(response)
        duration = datetime.utcnow() - start_time
        synclog.duration = duration.seconds
        synclog.save()
        add_custom_parameter('restore_response_size', response.num_items)
        self.set_cached_payload_if_necessary(resp, duration)
        return resp