mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
Adds book data source connector database table
This commit is contained in:
parent
fd077f482a
commit
8494719512
6 changed files with 102 additions and 8 deletions
|
@ -9,3 +9,7 @@ def get_or_create_book(key):
|
||||||
def search(query):
|
def search(query):
|
||||||
''' ya '''
|
''' ya '''
|
||||||
return openlibrary.search(query)
|
return openlibrary.search(query)
|
||||||
|
|
||||||
|
|
||||||
|
def update_book(key):
|
||||||
|
return openlibrary.update_book(key)
|
||||||
|
|
|
@ -61,4 +61,5 @@ class SearchResult(object):
|
||||||
self.raw_data = raw_data
|
self.raw_data = raw_data
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<SearchResult key={!r} title={!r} author={!r}>".format(self.key, self.title, self.author)
|
return "<SearchResult key={!r} title={!r} author={!r}>".format(
|
||||||
|
self.key, self.title, self.author)
|
||||||
|
|
49
fedireads/migrations/0020_auto_20200327_2114.py
Normal file
49
fedireads/migrations/0020_auto_20200327_2114.py
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
# Generated by Django 3.0.3 on 2020-03-27 21:14
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('fedireads', '0019_comment'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Connector',
|
||||||
|
fields=[
|
||||||
|
('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)),
|
||||||
|
('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)),
|
||||||
|
('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='origin',
|
||||||
|
new_name='source_url',
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='book',
|
||||||
|
name='local_edits',
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='book',
|
||||||
|
name='connector',
|
||||||
|
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='fedireads.Connector'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,5 +1,5 @@
|
||||||
''' bring all the models into the app namespace '''
|
''' bring all the models into the app namespace '''
|
||||||
from .book import Book, Work, Edition, Author
|
from .book import Connector, Book, Work, Edition, Author
|
||||||
from .shelf import Shelf, ShelfBook
|
from .shelf import Shelf, ShelfBook
|
||||||
from .status import Status, Review, Comment, Favorite, Tag, Notification
|
from .status import Status, Review, Comment, Favorite, Tag, Notification
|
||||||
from .user import User, FederatedServer, UserFollows, UserFollowRequest, \
|
from .user import User, FederatedServer, UserFollows, UserFollowRequest, \
|
||||||
|
|
|
@ -8,6 +8,25 @@ from fedireads.utils.fields import JSONField, ArrayField
|
||||||
from fedireads.utils.models import FedireadsModel
|
from fedireads.utils.models import FedireadsModel
|
||||||
|
|
||||||
|
|
||||||
|
class Connector(FedireadsModel):
|
||||||
|
''' book data source connectors '''
|
||||||
|
name = models.CharField(max_length=255)
|
||||||
|
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)
|
||||||
|
|
||||||
|
politeness_delay = models.IntegerField(null=True) #seconds
|
||||||
|
max_query_count = models.IntegerField(null=True)
|
||||||
|
# how many queries executed in a unit of time, like a day
|
||||||
|
query_count = models.IntegerField(default=0)
|
||||||
|
# when to reset the query count back to 0 (ie, after 1 day)
|
||||||
|
query_count_expiry = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
|
||||||
class Book(FedireadsModel):
|
class Book(FedireadsModel):
|
||||||
''' a generic book, which can mean either an edition or a work '''
|
''' a generic book, which can mean either an edition or a work '''
|
||||||
# these identifiers apply to both works and editions
|
# these identifiers apply to both works and editions
|
||||||
|
@ -17,10 +36,11 @@ class Book(FedireadsModel):
|
||||||
misc_identifiers = JSONField(null=True)
|
misc_identifiers = JSONField(null=True)
|
||||||
|
|
||||||
# info about where the data comes from and where/if to sync
|
# info about where the data comes from and where/if to sync
|
||||||
origin = models.CharField(max_length=255, unique=True, null=True)
|
source_url = models.CharField(max_length=255, unique=True, null=True)
|
||||||
local_edits = models.BooleanField(default=False)
|
|
||||||
sync = models.BooleanField(default=True)
|
sync = models.BooleanField(default=True)
|
||||||
last_sync_date = models.DateTimeField(default=datetime.now)
|
last_sync_date = models.DateTimeField(default=datetime.now)
|
||||||
|
connector = models.ForeignKey(
|
||||||
|
'Connector', on_delete=models.PROTECT, null=True)
|
||||||
|
|
||||||
# TODO: edit history
|
# TODO: edit history
|
||||||
|
|
||||||
|
@ -55,7 +75,12 @@ class Book(FedireadsModel):
|
||||||
return '%s/%s/%s' % (base_path, model_name, self.openlibrary_key)
|
return '%s/%s/%s' % (base_path, model_name, self.openlibrary_key)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<{} key={!r} title={!r} author={!r}>".format(self.__class__, self.openlibrary_key, self.title, self.author)
|
return "<{} key={!r} title={!r} author={!r}>".format(
|
||||||
|
self.__class__,
|
||||||
|
self.openlibrary_key,
|
||||||
|
self.title,
|
||||||
|
self.author
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Work(Book):
|
class Work(Book):
|
||||||
|
@ -82,6 +107,8 @@ class Author(FedireadsModel):
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
last_name = models.CharField(max_length=255, null=True)
|
last_name = models.CharField(max_length=255, null=True)
|
||||||
first_name = models.CharField(max_length=255, null=True)
|
first_name = models.CharField(max_length=255, null=True)
|
||||||
aliases = ArrayField(models.CharField(max_length=255), blank=True, default=list)
|
aliases = ArrayField(
|
||||||
|
models.CharField(max_length=255), blank=True, default=list
|
||||||
|
)
|
||||||
bio = models.TextField(null=True, blank=True)
|
bio = models.TextField(null=True, blank=True)
|
||||||
|
|
||||||
|
|
17
init_db.py
17
init_db.py
|
@ -1,10 +1,23 @@
|
||||||
from fedireads.models import User
|
''' starter data '''
|
||||||
|
from fedireads.models import Connector, User
|
||||||
from fedireads.books_manager import get_or_create_book
|
from fedireads.books_manager import get_or_create_book
|
||||||
|
|
||||||
User.objects.create_user('mouse', 'mouse.reeve@gmail.com', 'password123')
|
User.objects.create_user('mouse', 'mouse.reeve@gmail.com', 'password123')
|
||||||
User.objects.create_user('rat', 'rat@rat.com', 'ratword', manually_approves_followers=True)
|
User.objects.create_user(
|
||||||
|
'rat', 'rat@rat.com', 'ratword',
|
||||||
|
manually_approves_followers=True
|
||||||
|
)
|
||||||
|
|
||||||
User.objects.get(id=1).followers.add(User.objects.get(id=2))
|
User.objects.get(id=1).followers.add(User.objects.get(id=2))
|
||||||
|
|
||||||
|
Connector.objects.create(
|
||||||
|
name='OpenLibrary',
|
||||||
|
base_url='https://openlibrary.org',
|
||||||
|
covers_url='https://covers.openlibrary.org',
|
||||||
|
search_url='https://openlibrary.org/search?q=',
|
||||||
|
key_name='olkey',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
get_or_create_book('OL1715344W')
|
get_or_create_book('OL1715344W')
|
||||||
get_or_create_book('OL102749W')
|
get_or_create_book('OL102749W')
|
||||||
|
|
Loading…
Reference in a new issue