Skip to content

Commit

Permalink
rename invenio_id to rid
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Nov 5, 2024
1 parent f8f3b9b commit d97f1d7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ async def delete_all_records():

@app.route("/records/<slug>", methods=["DELETE"])
async def delete_record(slug: str):
"""Delete InvenioRDM draft record using the invenio_id."""
"""Delete InvenioRDM draft record using the rid."""
if (
request.headers.get("Authorization", None) is None
or request.headers.get("Authorization").split(" ")[1]
Expand Down
44 changes: 22 additions & 22 deletions api/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,16 +1356,16 @@ def upsert_single_post(post):
)
guid = record.data.get("guid", None)
doi = record.data.get("doi", None)
invenio_id = record.data.get("invenio_id", None)
rid = record.data.get("rid", None)
community_id = py_.get(record.data, "blog.community_id")

# if DOI doen't exist (yet), ignore InvenioRDM
if doi is None:
return post_to_update.data[0]

# if InvenioRDM record exists, update it, otherwise create it
if invenio_id:
update_record(record.data, invenio_id, community_id)
if rid:
update_record(record.data, rid, community_id)
else:
print(f"creating record for guid {guid}")
return create_record(record.data, guid, community_id)
Expand Down Expand Up @@ -1403,42 +1403,42 @@ def create_record(record, guid: str, community_id: str):
print(response.status_code, "create_draft_record", guid)
return response.json()

invenio_id = response.json()["id"]
rid = response.json()["id"]

# publish draft record
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{invenio_id}/draft/actions/publish"
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{rid}/draft/actions/publish"
headers = {"Authorization": f"Bearer {environ['QUART_INVENIORDM_TOKEN']}"}
response = httpx.post(url, headers=headers, timeout=10.0)
if response.status_code >= 400:
print(response.status_code, "publish_draft_record", guid)
return response.json()

# add draft record to blog community
add_record_to_community(invenio_id, community_id)
add_record_to_community(rid, community_id)

# update rogue scholar database with InvenioRDM record id (invenio_id) if record was created
# update rogue scholar database with InvenioRDM record id (rid) if record was created
post_to_update = (
supabase_admin.table("posts")
.update(
{
"invenio_id": invenio_id,
"rid": rid,
}
)
.eq("guid", guid)
.execute()
)
if len(post_to_update.data) == 0:
print(f"error creating record invenio_id {invenio_id} for guid {guid}")
print(f"error creating record rid {rid} for guid {guid}")
return response

print(f"created record invenio_id {invenio_id} for guid {guid}")
print(f"created record rid {rid} for guid {guid}")
return response.json()
except Exception as error:
print(error)
return None


def update_record(record, invenio_id: str, community_id: str):
def update_record(record, rid: str, community_id: str):
"""Update InvenioRDM record."""
try:
subject = Metadata(record, via="json_feed_item")
Expand All @@ -1456,22 +1456,22 @@ def update_record(record, invenio_id: str, community_id: str):
)

# create draft record from published record
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{invenio_id}/draft"
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{rid}/draft"
headers = {"Authorization": f"Bearer {environ['QUART_INVENIORDM_TOKEN']}"}
response = httpx.post(url, headers=headers, timeout=10.0)
if response.status_code != 201:
return response.json()

# update draft record
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{invenio_id}/draft"
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{rid}/draft"
headers = {"Authorization": f"Bearer {environ['QUART_INVENIORDM_TOKEN']}"}
response = httpx.put(url, headers=headers, json=record, timeout=10.0)
if response.status_code != 200:
print(response.status_code, "u update_draft_record")
return response.json()

# publish draft record
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{invenio_id}/draft/actions/publish"
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{rid}/draft/actions/publish"
headers = {"Authorization": f"Bearer {environ['QUART_INVENIORDM_TOKEN']}"}
try:
response = httpx.post(url, headers=headers, timeout=10.0)
Expand All @@ -1487,24 +1487,24 @@ def update_record(record, invenio_id: str, community_id: str):
# add draft record to blog community if not already added
communities = py_.get(response.json(), "parent.communities.entries", [])
if len(communities) == 0:
add_record_to_community(invenio_id, community_id)
add_record_to_community(rid, community_id)

print(f"Updated record invenio_id {invenio_id} for guid {guid}")
print(f"Updated record rid {rid} for guid {guid}")
return response
except Exception as error:
print(error)
return None


def add_record_to_community(invenio_id: str, community_id: str):
def add_record_to_community(rid: str, community_id: str):
"""Add record to community."""
try:
data = {
"communities": [
{"id": community_id},
]
}
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{invenio_id}/communities"
url = f"{environ['QUART_INVENIORDM_API']}/api/records/{rid}/communities"
headers = {"Authorization": f"Bearer {environ['QUART_INVENIORDM_TOKEN']}"}
response = httpx.post(url, headers=headers, json=data, timeout=10.0)
if response.status_code >= 400:
Expand Down Expand Up @@ -1930,13 +1930,13 @@ def get_award(id: Optional[str]) -> Optional[dict]:
return None
return award

async def delete_draft_record(invenio_id: str):
async def delete_draft_record(rid: str):
"""Delete an InvenioRDM draft record."""
if invenio_id is None:
if rid is None:
return None
try:
url = (
f"{environ['QUART_INVENIORDM_API']}/api/records/{invenio_id}/draft"
f"{environ['QUART_INVENIORDM_API']}/api/records/{rid}/draft"
)
headers = {
"Content-Type": "application/octet-stream",
Expand All @@ -1948,7 +1948,7 @@ async def delete_draft_record(invenio_id: str):
)
if response.status_code != 204:
print(response.json())
return {"message": f"Draft record {invenio_id} deleted"}
return {"message": f"Draft record {rid} deleted"}
except Exception as error:
print(error)
return None
Expand Down
10 changes: 5 additions & 5 deletions api/supabase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

blogsSelect = "slug, title, description, language, favicon, feed_url, feed_format, home_page_url, generator, category"
blogWithPostsSelect = "slug, title, description, language, favicon, feed_url, current_feed_url, archive_prefix, feed_format, home_page_url, mastodon, created_at, license, generator, category, prefix, status, funding, items: posts (id, guid, doi, url, archive_url, title, summary, abstract, published_at, updated_at, registered_at, indexed_at, authors, image, tags, language, reference)"
postsSelect = "guid, doi, url, title, summary, abstract, published_at, updated_at, registered_at, authors, image, tags, language, reference, relationships, blog_name, content_text, invenio_id"
postsWithConfigSelect = "id, guid, doi, url, archive_url, title, summary, abstract, published_at, updated_at, registered_at, indexed_at, indexed, authors, image, tags, language, reference, relationships, blog_name, blog_slug, invenio_id, blog: blogs!inner(slug, title, description, feed_url, home_page_url, language, category, status, generator, license, updated_at)"
postsWithBlogSelect = "id, guid, doi, url, archive_url, title, summary, abstract, published_at, updated_at, registered_at, indexed_at, indexed, authors, image, tags, language, reference, relationships, blog_name, blog_slug,invenio_id, blog: blogs!inner(*)"
postsWithContentSelect = "id, guid, doi, url, archive_url, title, summary, abstract, content_text, published_at, updated_at, registered_at, indexed_at, authors, image, tags, language, reference, relationships, blog_name, blog_slug, invenio_id, blog: blogs!inner(*)"
postsForUpsertSelect = "id, guid, doi, url, archive_url, title, summary, abstract, published_at, updated_at, registered_at, indexed_at, authors, image, tags, language, reference, relationships, blog_name, blog_slug, invenio_id, blog: blogs!inner(*)"
postsSelect = "guid, doi, url, title, summary, abstract, published_at, updated_at, registered_at, authors, image, tags, language, reference, relationships, blog_name, content_text, rid"
postsWithConfigSelect = "id, guid, doi, url, archive_url, title, summary, abstract, published_at, updated_at, registered_at, indexed_at, indexed, authors, image, tags, language, reference, relationships, blog_name, blog_slug, rid, blog: blogs!inner(slug, title, description, feed_url, home_page_url, language, category, status, generator, license, updated_at)"
postsWithBlogSelect = "id, guid, doi, url, archive_url, title, summary, abstract, published_at, updated_at, registered_at, indexed_at, indexed, authors, image, tags, language, reference, relationships, blog_name, blog_slug, rid, blog: blogs!inner(*)"
postsWithContentSelect = "id, guid, doi, url, archive_url, title, summary, abstract, content_text, published_at, updated_at, registered_at, indexed_at, authors, image, tags, language, reference, relationships, blog_name, blog_slug, rid, blog: blogs!inner(*)"
postsForUpsertSelect = "id, guid, doi, url, archive_url, title, summary, abstract, published_at, updated_at, registered_at, indexed_at, authors, image, tags, language, reference, relationships, blog_name, blog_slug, rid, blog: blogs!inner(*)"
worksSelect = "*"

0 comments on commit d97f1d7

Please sign in to comment.