From 53ff28b5dc9ff56db80bc930fd05c9931f2caab3 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Wed, 15 Apr 2020 12:13:38 +0100 Subject: [PATCH] Save progress information from imports. --- fedireads/goodreads_import.py | 22 +++++++++++++++++++++- fedireads/models/__init__.py | 2 +- fedireads/outgoing.py | 5 +++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/fedireads/goodreads_import.py b/fedireads/goodreads_import.py index adcbad3c..884cd76a 100644 --- a/fedireads/goodreads_import.py +++ b/fedireads/goodreads_import.py @@ -2,9 +2,10 @@ import re import csv import itertools +import dateutil.parser from fedireads import books_manager -from fedireads.models import Edition +from fedireads.models import Edition, ReadThrough # Mapping goodreads -> fedireads shelf titles. @@ -99,6 +100,25 @@ class GoodreadsItem: def rating(self): return int(self.line['My Rating']) + @property + def date_added(self): + if self.line['Date Added']: + return dateutil.parser.parse(self.line['Date Added']) + + @property + def date_read(self): + if self.line['Date Read']: + return dateutil.parser.parse(self.line['Date Read']) + + @property + def reads(self): + return [ReadThrough( + # Date added isn't the start date, but it's (perhaps) better than nothing. + start_date=self.date_added, + finish_date=self.date_read, + pages_read=None, + )] + def __repr__(self): return "".format(self.line['Title']) diff --git a/fedireads/models/__init__.py b/fedireads/models/__init__.py index 26c7c6cc..87891e01 100644 --- a/fedireads/models/__init__.py +++ b/fedireads/models/__init__.py @@ -2,6 +2,6 @@ from .book import Connector, Book, Work, Edition, Author from .shelf import Shelf, ShelfBook from .status import Status, Review, Comment, Quotation -from .status import Favorite, Boost, Tag, Notification +from .status import Favorite, Boost, Tag, Notification, ReadThrough from .user import User, UserFollows, UserFollowRequest, UserBlocks from .user import FederatedServer diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index 53e1410c..109d8cbe 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -177,6 +177,11 @@ def handle_import_books(user, items): recipients = get_recipients(user, 'public') broadcast(user, activity, recipients) + for read in item.reads: + read.book = item.book + read.user = user + read.save() + if new_books: message = 'imported {} books'.format(len(new_books)) status = create_status(user, message, mention_books=new_books)