Skip to content

Instantly share code, notes, and snippets.

@symbioquine
Created September 14, 2022 17:38
Show Gist options
  • Save symbioquine/a81d28f63f8b4d50ba1a49a647beaaf1 to your computer and use it in GitHub Desktop.
Save symbioquine/a81d28f63f8b4d50ba1a49a647beaaf1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
},
"kernelspec": {
"name": "python",
"display_name": "Pyolite",
"language": "python"
}
},
"nbformat_minor": 4,
"nbformat": 4,
"cells": [
{
"cell_type": "code",
"source": "from pyodide.http import pyfetch\nfrom IPython.display import JSON\n\nasync def get_all_entities(base_url):\n next_page = base_url\n while next_page is not None:\n resp = await pyfetch(next_page, method='GET')\n\n data = await resp.json()\n\n for entity in data.get('data', []):\n yield entity\n\n next_page = data.get('links', {}).get('next', {}).get('href', None)\n",
"metadata": {
"trusted": true
},
"execution_count": 15,
"outputs": []
},
{
"cell_type": "code",
"source": "from js import location\n\nresp = await pyfetch(location.origin + '/session/token', method='POST')\n\nprint(resp.ok, resp.status, resp.status_text)\n\nantiCsrfToken = await resp.string()\n\nheaders = {\n 'Content-type': \"application/vnd.api+json\",\n 'X-CSRF-Token': antiCsrfToken,\n}",
"metadata": {
"trusted": true
},
"execution_count": 16,
"outputs": [
{
"name": "stdout",
"text": "True 200 OK\n",
"output_type": "stream"
}
]
},
{
"cell_type": "code",
"source": "import json\n\nrabbit_food_material_asset_id = '06d06098-ff1e-4017-8471-1ad08ac04c6d'\nbags_unit_term_id = \"8e9dabf7-8b8b-4745-9a17-5a2a352ccb6d\"\n\nall_quantities = [q async for q in get_all_entities(location.origin + '/api/quantity/standard?' +\n 'filter[inventory_asset.id]=' + rabbit_food_material_asset_id +\n '&filter[A][condition][path]=units.id' +\n '&filter[A][condition][operator]=IS%20NULL')]\n\nprint(\"Found {} quantities to update...\".format(len(all_quantities)))\n\nfor quantity in all_quantities:\n resp = await pyfetch(quantity['links']['self']['href'], method='PATCH', body=json.dumps({\n \"data\": {\n \"type\": quantity['type'],\n \"id\": quantity['id'],\n \"relationships\": {\n \"units\": {\n \"data\": {\n \"type\": \"taxonomy_term--unit\",\n \"id\": bags_unit_term_id\n }\n }\n }\n },\n }), headers=headers)\n\n print(quantity['id'], resp.ok, resp.status, resp.status_text)",
"metadata": {
"trusted": true
},
"execution_count": 19,
"outputs": [
{
"name": "stdout",
"text": "Found 0 quantities to update...\n",
"output_type": "stream"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment