From ed6fd6d48ec015880a30665c0bd2c2a1f68abf03 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 27 Mar 2020 16:36:52 -0700 Subject: [PATCH] Rename local key and suggest fedireads connectors --- fedireads/books_manager.py | 10 ++++---- fedireads/connectors/abstract_connector.py | 9 ++----- fedireads/connectors/openlibrary.py | 5 ++-- fedireads/connectors/settings.py | 8 ++---- ...327_2236.py => 0020_auto_20200327_2335.py} | 21 +++++++++++----- fedireads/models/book.py | 25 ++++++++++++++++--- fedireads/outgoing.py | 2 +- fedireads/templates/book.html | 4 +-- fedireads/templates/snippets/authors.html | 2 +- fedireads/templates/snippets/book.html | 2 +- .../templates/snippets/create_status.html | 6 ++--- fedireads/templates/snippets/shelf.html | 2 +- fedireads/templates/snippets/tag.html | 4 +-- init_db.py | 16 ++++++++++-- 14 files changed, 74 insertions(+), 42 deletions(-) rename fedireads/migrations/{0020_auto_20200327_2236.py => 0020_auto_20200327_2335.py} (66%) diff --git a/fedireads/books_manager.py b/fedireads/books_manager.py index 915d95cc6..c476f5796 100644 --- a/fedireads/books_manager.py +++ b/fedireads/books_manager.py @@ -2,14 +2,13 @@ import importlib from fedireads import models -from fedireads.connectors.settings import CONNECTORS def get_or_create_book(key): ''' pull up a book record by whatever means possible ''' try: book = models.Book.objects.select_subclasses().get( - local_key=key + fedireads_key=key ) return book except models.Book.DoesNotExist: @@ -32,6 +31,7 @@ def get_connector(book=None): else: connector_info = models.Connector.objects.first() - classname = CONNECTORS[connector_info.name]['classname'] - connector = importlib.import_module(classname) - return connector.Connector() + connector = importlib.import_module( + 'fedireads.connectors.%s' % connector_info.connector_file + ) + return connector.Connector(connector_info.identifier) diff --git a/fedireads/connectors/abstract_connector.py b/fedireads/connectors/abstract_connector.py index 7e83b7348..a0f8aa225 100644 --- a/fedireads/connectors/abstract_connector.py +++ b/fedireads/connectors/abstract_connector.py @@ -2,19 +2,14 @@ from abc import ABC, abstractmethod from fedireads import models -from fedireads.connectors import CONNECTORS class AbstractConnector(ABC): ''' generic book data connector ''' - def __init__(self, connector_name): + def __init__(self, identifier): # load connector settings - settings = CONNECTORS.get(connector_name) - if not settings: - raise ValueError('No connector with name "%s"' % connector_name) - - info = models.Connector.objects.get(name=settings['db_name']) + info = models.Connector.objects.get(identifier=identifier) self.model = info self.url = info.base_url diff --git a/fedireads/connectors/openlibrary.py b/fedireads/connectors/openlibrary.py index a83a7d630..0f69a79d5 100644 --- a/fedireads/connectors/openlibrary.py +++ b/fedireads/connectors/openlibrary.py @@ -10,8 +10,8 @@ from .abstract_connector import AbstractConnector, SearchResult class Connector(AbstractConnector): ''' instantiate a connector for OL ''' - def __init__(self): - super().__init__('OpenLibrary') + def __init__(self, identifier): + super().__init__(identifier) def search(self, query): @@ -140,3 +140,4 @@ class Connector(AbstractConnector): def update_book(self, book_obj): pass + diff --git a/fedireads/connectors/settings.py b/fedireads/connectors/settings.py index 9a0103f00..a56b2cf23 100644 --- a/fedireads/connectors/settings.py +++ b/fedireads/connectors/settings.py @@ -1,7 +1,3 @@ ''' settings book data connectors ''' -CONNECTORS = { - 'OpenLibrary': { - 'db_name': 'OpenLibrary', - 'classname': 'fedireads.connectors.openlibrary', - }, -} + +CONNECTORS = ['openlibrary', 'fedireads'] diff --git a/fedireads/migrations/0020_auto_20200327_2236.py b/fedireads/migrations/0020_auto_20200327_2335.py similarity index 66% rename from fedireads/migrations/0020_auto_20200327_2236.py rename to fedireads/migrations/0020_auto_20200327_2335.py index 7292e34cf..511b1bed7 100644 --- a/fedireads/migrations/0020_auto_20200327_2236.py +++ b/fedireads/migrations/0020_auto_20200327_2335.py @@ -1,7 +1,8 @@ -# Generated by Django 3.0.3 on 2020-03-27 22:36 +# Generated by Django 3.0.3 on 2020-03-27 23:35 from django.db import migrations, models import django.db.models.deletion +import fedireads.models.book class Migration(migrations.Migration): @@ -17,20 +18,24 @@ class Migration(migrations.Migration): ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('created_date', models.DateTimeField(auto_now_add=True)), ('updated_date', models.DateTimeField(auto_now=True)), - ('name', models.CharField(max_length=255, unique=True)), + ('identifier', models.CharField(max_length=255, unique=True)), + ('connector_file', models.CharField(choices=[('openlibrary', 'Openlibrary'), ('fedireads', 'Fedireads')], default='openlibrary', max_length=255)), + ('is_self', models.BooleanField(default=False)), ('api_key', models.CharField(max_length=255, null=True)), ('base_url', models.CharField(max_length=255)), ('covers_url', models.CharField(max_length=255)), ('search_url', models.CharField(max_length=255, null=True)), - ('key_name', models.CharField(max_length=255, unique=True)), + ('key_name', models.CharField(max_length=255)), ('politeness_delay', models.IntegerField(null=True)), ('max_query_count', models.IntegerField(null=True)), ('query_count', models.IntegerField(default=0)), ('query_count_expiry', models.DateTimeField(auto_now_add=True)), ], - options={ - 'abstract': False, - }, + ), + migrations.RenameField( + model_name='book', + old_name='local_key', + new_name='fedireads_key', ), migrations.RenameField( model_name='book', @@ -41,6 +46,10 @@ class Migration(migrations.Migration): model_name='book', name='local_edits', ), + migrations.AddConstraint( + model_name='connector', + constraint=models.CheckConstraint(check=models.Q(connector_file__in=fedireads.models.book.ConnectorFiles), name='connector_file_valid'), + ), migrations.AddField( model_name='book', name='connector', diff --git a/fedireads/models/book.py b/fedireads/models/book.py index 9edf59336..9f52a1f1f 100644 --- a/fedireads/models/book.py +++ b/fedireads/models/book.py @@ -8,17 +8,28 @@ from fedireads.settings import DOMAIN from fedireads.utils.fields import JSONField, ArrayField from fedireads.utils.models import FedireadsModel +from fedireads.connectors.settings import CONNECTORS + +ConnectorFiles = models.TextChoices('ConnectorFiles', CONNECTORS) class Connector(FedireadsModel): ''' book data source connectors ''' - name = models.CharField(max_length=255, unique=True) + identifier = models.CharField(max_length=255, unique=True) + connector_file = models.CharField( + max_length=255, + default='openlibrary', + choices=ConnectorFiles.choices + ) + # is this a connector to your own database, should only be true if + # the connector_file is `fedireads` + is_self = models.BooleanField(default=False) api_key = models.CharField(max_length=255, null=True) base_url = models.CharField(max_length=255) covers_url = models.CharField(max_length=255) search_url = models.CharField(max_length=255, null=True) - key_name = models.CharField(max_length=255, unique=True) + key_name = models.CharField(max_length=255) politeness_delay = models.IntegerField(null=True) #seconds max_query_count = models.IntegerField(null=True) @@ -27,13 +38,21 @@ class Connector(FedireadsModel): # when to reset the query count back to 0 (ie, after 1 day) query_count_expiry = models.DateTimeField(auto_now_add=True) + class Meta: + constraints = [ + models.CheckConstraint( + check=models.Q(connector_file__in=ConnectorFiles), + name='connector_file_valid' + ) + ] + class Book(FedireadsModel): ''' a generic book, which can mean either an edition or a work ''' # these identifiers apply to both works and editions openlibrary_key = models.CharField(max_length=255, unique=True, null=True) librarything_key = models.CharField(max_length=255, unique=True, null=True) - local_key = models.CharField(max_length=255, unique=True, default=uuid4) + fedireads_key = models.CharField(max_length=255, unique=True, default=uuid4) misc_identifiers = JSONField(null=True) # info about where the data comes from and where/if to sync diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index f204888b7..e2d99563e 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -232,7 +232,7 @@ def handle_tag(user, book, name): def handle_untag(user, book, name): ''' tag a book ''' - book = models.Book.objects.get(local_key=book) + book = models.Book.objects.get(fedireads_key=book) tag = models.Tag.objects.get(name=name, book=book, user=user) tag_activity = activitypub.get_remove_tag(tag) tag.delete() diff --git a/fedireads/templates/book.html b/fedireads/templates/book.html index 74777a19b..b337f5526 100644 --- a/fedireads/templates/book.html +++ b/fedireads/templates/book.html @@ -5,7 +5,7 @@

{{ book.title }} by {% include 'snippets/authors.html' with book=book %}

- {% if book.parent_work %}

Edition of {{ book.parent_work.title }}

{% endif %} + {% if book.parent_work %}

Edition of {{ book.parent_work.title }}

{% endif %}
{% include 'snippets/book_cover.html' with book=book size=large %} @@ -32,7 +32,7 @@

Leave a review

{% csrf_token %} - + {{ review_form.as_p }}
diff --git a/fedireads/templates/snippets/authors.html b/fedireads/templates/snippets/authors.html index e45a19a5f..35fe6a03c 100644 --- a/fedireads/templates/snippets/authors.html +++ b/fedireads/templates/snippets/authors.html @@ -1 +1 @@ -{{ book.authors.first.name }} +{{ book.authors.first.name }} diff --git a/fedireads/templates/snippets/book.html b/fedireads/templates/snippets/book.html index dad89603a..3c5fd377c 100644 --- a/fedireads/templates/snippets/book.html +++ b/fedireads/templates/snippets/book.html @@ -1,7 +1,7 @@ {% load fr_display %} {% include 'snippets/book_cover.html' with book=book %}

- {{ book.title }} + {{ book.title }}

by {% include 'snippets/authors.html' with book=book %} diff --git a/fedireads/templates/snippets/create_status.html b/fedireads/templates/snippets/create_status.html index bd3efbcf7..70bc9fef4 100644 --- a/fedireads/templates/snippets/create_status.html +++ b/fedireads/templates/snippets/create_status.html @@ -4,7 +4,7 @@

{% include 'snippets/avatar.html' with user=user %} Your thoughts on - {{ book.title }} + {{ book.title }} by {% include 'snippets/authors.html' with book=book %}

@@ -24,14 +24,14 @@ {% include 'snippets/book_cover.html' with book=book %}
{% csrf_token %} - + {{ review_form.as_p }}
diff --git a/fedireads/templates/snippets/shelf.html b/fedireads/templates/snippets/shelf.html index e0975a487..2171efb22 100644 --- a/fedireads/templates/snippets/shelf.html +++ b/fedireads/templates/snippets/shelf.html @@ -33,7 +33,7 @@ {% include 'snippets/book_cover.html' with book=book size="small" %} - {{ book.title }} + {{ book.title }} {{ book.authors.first.name }} diff --git a/fedireads/templates/snippets/tag.html b/fedireads/templates/snippets/tag.html index 14d6f1029..fdc686dc1 100644 --- a/fedireads/templates/snippets/tag.html +++ b/fedireads/templates/snippets/tag.html @@ -3,14 +3,14 @@ {% if tag.identifier in user_tags %}
{% csrf_token %} - +
{% else %}
{% csrf_token %} - +
diff --git a/init_db.py b/init_db.py index 2d6f2d94d..e121503b4 100644 --- a/init_db.py +++ b/init_db.py @@ -1,6 +1,7 @@ ''' starter data ''' -from fedireads.models import Connector, User from fedireads.books_manager import get_or_create_book +from fedireads.models import Connector, User +from fedireads.settings import DOMAIN User.objects.create_user('mouse', 'mouse.reeve@gmail.com', 'password123') User.objects.create_user( @@ -11,13 +12,24 @@ User.objects.create_user( User.objects.get(id=1).followers.add(User.objects.get(id=2)) Connector.objects.create( - name='OpenLibrary', + identifier='openlibrary.org', + connector_file='openlibrary', base_url='https://openlibrary.org', covers_url='https://covers.openlibrary.org', search_url='https://openlibrary.org/search?q=', key_name='openlibrary_key', ) +Connector.objects.create( + identifier=DOMAIN, + connector_file='fedireads', + base_url='https://%s/book' % DOMAIN, + covers_url='https://%s/images/covers' % DOMAIN, + search_url='https://%s/search?q=' % DOMAIN, + key_name='openlibrary_key', + is_self=True +) + get_or_create_book('OL1715344W') get_or_create_book('OL102749W')