Use status source as connector

This commit is contained in:
Mouse Reeve
2020-05-03 19:49:32 -07:00
parent b8568a76c8
commit 7594bed5d3
3 changed files with 50 additions and 13 deletions

View File

@ -1,8 +1,9 @@
''' Handle user activity '''
from urllib.parse import urlparse
from django.db import IntegrityError
from fedireads import models
from fedireads.books_manager import get_or_create_book
from fedireads import books_manager, models
from fedireads.sanitize_html import InputHtmlParser
@ -25,6 +26,41 @@ def create_review_from_activity(author, activity):
return review
def get_or_create_book(remote_id):
''' try the remote id and then figure out the right connector '''
book = get_by_absolute_id(remote_id, models.Book)
if book:
return book
connector = get_or_create_connector(remote_id)
return books_manager.get_or_create_book(
remote_id,
key=connector.key_name,
connector_id=connector.id
)
def get_or_create_connector(remote_id):
''' get the connector related to the author's server '''
url = urlparse(remote_id)
identifier = url.netloc
try:
connector_info = models.Connector.objects.get(identifier=identifier)
except models.Connector.DoesNotExist:
models.Connector.objects.create(
identifier=identifier,
connector_file='fedireads_connector',
base_url='https://%s' % identifier,
books_url='https://%s/book' % identifier,
covers_url='https://%s/images/covers' % identifier,
search_url='https://%s/search?q=' % identifier,
key_name='remote_id',
priority=3
)
return books_manager.load_connector(connector_info)
def create_rating(user, book, rating):
''' a review that's just a rating '''
if not rating or rating < 1 or rating > 5: