j4ponezu
8/25/2017 - 6:18 PM

Upload attachment

Upload attachment

    def core_attachment_upload(self, relation_data, file_path, filename=None, token=None, inline=False):
        relation_data.update({"entry_type": "upload", "action_type": "post"})
        params = {}

        if os.path.isfile(file_path) and os.path.getsize(file_path) > 0:
            if filename is None:
                filename = os.path.split(file_path)[1]
            url_entry = "/uploads.json?filename=%s" % filename
            if token:
                url_entry = url_entry + "&token=%s" % token
            f_data = open(file_path, "rb")
            if inline:
                params["inline"] = "true"
            # TODO: Try to use self._call_api here - you fucked up the caching
            res = requests.post("%s%s" % (self.base_url, url_entry), data=f_data,
                                auth=self._session.auth, params=params,
                                headers={'Content-Type': 'application/octet-stream'})
            if res.status_code == 201:
                response = json.loads(res.content)
                return self.migration_map.update_relations(response, relation_data)
            else:
                raise ValueError('There was a problem uploading attachment: %s' % filename)
        else:
            self.migration_map.log_error({"not_found": {"file_path": file_path, "file_name": filename}}, relation_data)
            log.error("File not found: {}".format(file_path))
        return False