Skip to content

Commit

Permalink
add support for updating single posts
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Apr 11, 2024
1 parent 2eb4444 commit aff1362
Show file tree
Hide file tree
Showing 15 changed files with 8,278 additions and 599 deletions.
17 changes: 15 additions & 2 deletions api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
format_relationships,
translate_titles,
)
from api.posts import extract_all_posts, extract_all_posts_by_blog, update_all_posts, update_all_posts_by_blog
from api.posts import extract_all_posts, extract_all_posts_by_blog, update_all_posts, update_all_posts_by_blog, update_single_post
from api.blogs import extract_single_blog, extract_all_blogs
from api.works import SUPPORTED_ACCEPT_HEADERS, get_formatted_work
from api.schema import Blog, Post, Work, PostQuery
Expand Down Expand Up @@ -388,9 +388,22 @@ async def post_posts():
)
return jsonify(extracted_posts)
except Exception as e:
logger.warning(e) # .args[0])
logger.warning(e)
return {"error": "An error occured."}, 400

@validate_response(Post)
@app.route("/posts/<slug>", methods=["POST"])
@app.route("/posts/<slug>/<suffix>", methods=["POST"])
async def post_post(slug: str, suffix: Optional[str] = None):
"""Update post by either uuid or doi, using information from the blog's feed."""

try:
result = await update_single_post(slug, suffix=suffix)
return jsonify(result)
except Exception as e:
logger.warning(e.args[0])
return {"error": "An error occured."}, 400


@validate_response(Post)
@app.route("/posts/<slug>")
Expand Down
36 changes: 35 additions & 1 deletion api/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import time
import traceback
from urllib.parse import unquote
from commonmeta import validate_doi, normalize_id, validate_url
from commonmeta import validate_doi, normalize_id, validate_url, validate_prefix
from Levenshtein import ratio

from api.utils import (
Expand All @@ -30,6 +30,7 @@
fix_xml,
get_markdown,
write_html,
validate_uuid,
EXCLUDED_TAGS,
)
from api.works import get_single_work
Expand Down Expand Up @@ -338,6 +339,39 @@ async def update_all_posts_by_blog(slug: str, page: int = 1):
print(traceback.format_exc())
return []

async def update_single_post(slug: str, suffix: Optional[str] = None):
"""Update single post"""
try:
if validate_uuid(slug):
response = (
supabase.table("posts")
.select(postsWithContentSelect)
.eq("id", slug)
.maybe_single()
.execute()
)
elif validate_prefix(slug) and suffix:
doi = f"https://doi.org/{slug}/{suffix}"
response = (
supabase.table("posts")
.select(postsWithContentSelect)
.eq("doi", doi)
.maybe_single()
.execute()
)
else:
return {"error": "An error occured."}, 400
blog = response.data.get("blog", None)
if not blog:
return {"error": "Blog not found."}, 404
post = py_.omit(response.data, "blog")
updated_post = await update_rogue_scholar_post(post, blog)
response = upsert_single_post(updated_post)
return response
except Exception:
print(traceback.format_exc())
return {}


async def extract_wordpress_post(post, blog):
"""Extract WordPress post from REST API."""
Expand Down
500 changes: 250 additions & 250 deletions poetry.lock

Large diffs are not rendered by default.

1,010 changes: 1,010 additions & 0 deletions tests/cassettes/test-posts/test_extract_posts_by_blog_blogger.yaml

Large diffs are not rendered by default.

653 changes: 653 additions & 0 deletions tests/cassettes/test-posts/test_extract_posts_by_blog_ghost.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -52594,4 +52594,59 @@ interactions:
- 1; mode=block
http_version: HTTP/1.1
status_code: 400
- request:
body: '{"pid": "https://opencitations.net/download", "type": "WebPage", "url":
"https://opencitations.net/download", "contributors": [], "titles": [], "container":
{}, "publisher": null, "references": [], "relations": [], "date": {"accessed":
"2024-04-11T22:23:55"}, "descriptions": [], "license": null, "alternate_identifiers":
[], "funding_references": [], "files": [], "subjects": [], "provider": null,
"archive_locations": [], "geo_locations": [], "version": null, "language": "en",
"additional_type": null, "sizes": [], "formats": []}'
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate, br
connection:
- keep-alive
content-length:
- '532'
content-type:
- application/json
host:
- commonmeta.org
user-agent:
- python-httpx/0.24.1
method: POST
uri: https://commonmeta.org/api/collections/works/records
response:
content: '{"code":400,"message":"Failed to create record.","data":{"pid":{"code":"validation_not_unique","message":"Value
must be unique."}}}

'
headers:
content-encoding:
- br
content-type:
- application/json; charset=UTF-8
date:
- Thu, 11 Apr 2024 20:23:55 GMT
fly-request-id:
- 01HV7D9JDGZVW8WC2VH3TNNGGH-fra
server:
- Fly/e7839a863 (2024-04-09)
transfer-encoding:
- chunked
vary:
- Origin
via:
- 1.1 fly.io
x-content-type-options:
- nosniff
x-frame-options:
- SAMEORIGIN
x-xss-protection:
- 1; mode=block
http_version: HTTP/1.1
status_code: 400
version: 1
Loading

0 comments on commit aff1362

Please sign in to comment.