diff --git a/fedireads/migrations/0001_initial.py b/fedireads/migrations/0001_initial.py index feeb008fa..5bbe22373 100644 --- a/fedireads/migrations/0001_initial.py +++ b/fedireads/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.2 on 2020-01-29 06:31 +# Generated by Django 3.0.2 on 2020-01-29 07:36 from django.conf import settings import django.contrib.auth.models @@ -43,7 +43,7 @@ class Migration(migrations.Migration): ('local', models.BooleanField(default=True)), ('localname', models.CharField(blank=True, max_length=255, null=True, unique=True)), ('name', models.CharField(blank=True, max_length=100, null=True)), - ('avatar', models.ImageField(blank=True, null=True, upload_to='uploads/')), + ('avatar', models.ImageField(blank=True, null=True, upload_to='avatars/')), ('created_date', models.DateTimeField(auto_now_add=True)), ('updated_date', models.DateTimeField(auto_now=True)), ('followers', models.ManyToManyField(to=settings.AUTH_USER_MODEL)), @@ -89,6 +89,7 @@ class Migration(migrations.Migration): ('activitypub_id', models.CharField(max_length=255)), ('openlibrary_key', models.CharField(max_length=255, unique=True)), ('data', django.contrib.postgres.fields.jsonb.JSONField()), + ('cover', models.ImageField(blank=True, null=True, upload_to='covers/')), ('added_date', models.DateTimeField(auto_now_add=True)), ('updated_date', models.DateTimeField(auto_now=True)), ('added_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), diff --git a/fedireads/models.py b/fedireads/models.py index eb9f4aecf..5720d886f 100644 --- a/fedireads/models.py +++ b/fedireads/models.py @@ -30,7 +30,7 @@ class User(AbstractUser): ) # name is your display name, which you can change at will name = models.CharField(max_length=100, blank=True, null=True) - avatar = models.ImageField(upload_to='uploads/', blank=True, null=True) + avatar = models.ImageField(upload_to='avatars/', blank=True, null=True) # TODO: a field for if non-local users are readers or others followers = models.ManyToManyField('self', symmetrical=False) created_date = models.DateTimeField(auto_now_add=True) @@ -205,6 +205,7 @@ class Book(models.Model): data = JSONField() works = models.ManyToManyField('Work') authors = models.ManyToManyField('Author') + cover = models.ImageField(upload_to='covers/', blank=True, null=True) shelves = models.ManyToManyField( 'Shelf', symmetrical=False, diff --git a/fedireads/openlibrary.py b/fedireads/openlibrary.py index 2b3ac0b3a..964792bd7 100644 --- a/fedireads/openlibrary.py +++ b/fedireads/openlibrary.py @@ -1,12 +1,13 @@ ''' activitystream api and books ''' from django.core.exceptions import ObjectDoesNotExist +from django.core.files.base import ContentFile import requests from fedireads.models import Author, Book, Work from fedireads.settings import OL_URL -def get_or_create_book(olkey, user=None, update=True): +def get_or_create_book(olkey, user=None, update=False): ''' add a book ''' # TODO: check if this is a valid open library key, and a book olkey = olkey @@ -44,9 +45,23 @@ def get_or_create_book(olkey, user=None, update=True): author_id = author_id['key'] book.authors.add(get_or_create_author(author_id)) + if len(data['covers']): + book.cover.save(*get_cover(data['covers'][0]), save=True) + return book +def get_cover(cover_id): + ''' ask openlibrary for the cover ''' + image_name = '%s-M.jpg' % cover_id + url = 'https://covers.openlibrary.org/b/id/%s' % image_name + response = requests.get(url) + if not response.ok: + response.raise_for_status() + image_content = ContentFile(requests.get(url).content) + return [image_name, image_content] + + def get_or_create_work(olkey): ''' load em up ''' # TODO: validate that this is a work key diff --git a/fedireads/static/format.css b/fedireads/static/format.css index c53691c0a..7c4f666d2 100644 --- a/fedireads/static/format.css +++ b/fedireads/static/format.css @@ -33,6 +33,10 @@ h2 { #branding { flex-grow: 1; } +#branding a { + text-decoration: none; + color: black; +} #actions { flex-grow: 0; text-align: right; @@ -44,11 +48,6 @@ h2 { padding: 0.5rem; } -#top-bar a { - color: black; - text-decoration: none; -} - #branding { font-size: 2em; } @@ -76,6 +75,11 @@ h2 { margin-right: 0.5em; } +.book-cover.small { + width: 50px; + height: auto; +} + #feed, #content { display: flex; flex-direction: column; diff --git a/fedireads/templates/book.html b/fedireads/templates/book.html index e6837d345..c3511a955 100644 --- a/fedireads/templates/book.html +++ b/fedireads/templates/book.html @@ -3,7 +3,7 @@
- +

{{ book.data.title }}

by {{ book.authors.first.data.name }} {{ rating }} stars diff --git a/fedireads/templates/edit_user.html b/fedireads/templates/edit_user.html index e59a3ea26..5ca6050a2 100644 --- a/fedireads/templates/edit_user.html +++ b/fedireads/templates/edit_user.html @@ -3,7 +3,6 @@