diff --git a/bookwyrm/goodreads_import.py b/bookwyrm/goodreads_import.py deleted file mode 100644 index fb4e8e0f1..000000000 --- a/bookwyrm/goodreads_import.py +++ /dev/null @@ -1,14 +0,0 @@ -""" handle reading a csv from goodreads """ -from bookwyrm.importer import Importer - -# GoodReads is the default importer, thus Importer follows its structure. For a more complete example of overriding see librarything_import.py - - -class GoodreadsImporter(Importer): - service = "GoodReads" - - def parse_fields(self, data): - data.update({"import_source": self.service}) - # add missing 'Date Started' field - data.update({"Date Started": None}) - return data diff --git a/bookwyrm/importers/__init__.py b/bookwyrm/importers/__init__.py new file mode 100644 index 000000000..f13672e06 --- /dev/null +++ b/bookwyrm/importers/__init__.py @@ -0,0 +1,5 @@ +""" import classes """ + +from .importer import Importer +from .goodreads_import import GoodreadsImporter +from .librarything_import import LibrarythingImporter diff --git a/bookwyrm/importers/goodreads_import.py b/bookwyrm/importers/goodreads_import.py new file mode 100644 index 000000000..0b126c14c --- /dev/null +++ b/bookwyrm/importers/goodreads_import.py @@ -0,0 +1,16 @@ +""" handle reading a csv from goodreads """ +from . import Importer + + +class GoodreadsImporter(Importer): + """GoodReads is the default importer, thus Importer follows its structure. + For a more complete example of overriding see librarything_import.py""" + + service = "GoodReads" + + def parse_fields(self, entry): + """ handle the specific fields in goodreads csvs """ + entry.update({"import_source": self.service}) + # add missing 'Date Started' field + entry.update({"Date Started": None}) + return entry diff --git a/bookwyrm/importer.py b/bookwyrm/importers/importer.py similarity index 94% rename from bookwyrm/importer.py rename to bookwyrm/importers/importer.py index 2fbb3430f..ddbfa3048 100644 --- a/bookwyrm/importer.py +++ b/bookwyrm/importers/importer.py @@ -10,6 +10,8 @@ logger = logging.getLogger(__name__) class Importer: + """ Generic class for csv data import from an outside service """ + service = "Unknown" delimiter = "," encoding = "UTF-8" @@ -29,10 +31,12 @@ class Importer: self.save_item(job, index, entry) return job - def save_item(self, job, index, data): + def save_item(self, job, index, data): # pylint: disable=no-self-use + """ creates and saves an import item """ ImportItem(job=job, index=index, data=data).save() def parse_fields(self, entry): + """ updates csv data with additional info """ entry.update({"import_source": self.service}) return entry diff --git a/bookwyrm/librarything_import.py b/bookwyrm/importers/librarything_import.py similarity index 50% rename from bookwyrm/librarything_import.py rename to bookwyrm/importers/librarything_import.py index b3dd9d56b..3755cb1ad 100644 --- a/bookwyrm/librarything_import.py +++ b/bookwyrm/importers/librarything_import.py @@ -1,35 +1,35 @@ """ handle reading a csv from librarything """ -import csv import re import math -from bookwyrm import models -from bookwyrm.models import ImportItem -from bookwyrm.importer import Importer +from . import Importer class LibrarythingImporter(Importer): + """ csv downloads from librarything """ + service = "LibraryThing" delimiter = "\t" encoding = "ISO-8859-1" # mandatory_fields : fields matching the book title and author mandatory_fields = ["Title", "Primary Author"] - def parse_fields(self, initial): + def parse_fields(self, entry): + """ custom parsing for librarything """ data = {} data["import_source"] = self.service - data["Book Id"] = initial["Book Id"] - data["Title"] = initial["Title"] - data["Author"] = initial["Primary Author"] - data["ISBN13"] = initial["ISBN"] - data["My Review"] = initial["Review"] - if initial["Rating"]: - data["My Rating"] = math.ceil(float(initial["Rating"])) + data["Book Id"] = entry["Book Id"] + data["Title"] = entry["Title"] + data["Author"] = entry["Primary Author"] + data["ISBN13"] = entry["ISBN"] + data["My Review"] = entry["Review"] + if entry["Rating"]: + data["My Rating"] = math.ceil(float(entry["Rating"])) else: data["My Rating"] = "" - data["Date Added"] = re.sub("\[|\]", "", initial["Entry Date"]) - data["Date Started"] = re.sub("\[|\]", "", initial["Date Started"]) - data["Date Read"] = re.sub("\[|\]", "", initial["Date Read"]) + data["Date Added"] = re.sub(r"\[|\]", "", entry["Entry Date"]) + data["Date Started"] = re.sub(r"\[|\]", "", entry["Date Started"]) + data["Date Read"] = re.sub(r"\[|\]", "", entry["Date Read"]) data["Exclusive Shelf"] = None if data["Date Read"]: diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index a05325f3f..880c41229 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -1,6 +1,7 @@ """ make a list of books!! """ from django.apps import apps from django.db import models +from django.utils import timezone from bookwyrm import activitypub from bookwyrm.settings import DOMAIN @@ -79,6 +80,10 @@ class ListItem(CollectionItemMixin, BookWyrmModel): """ create a notification too """ created = not bool(self.id) super().save(*args, **kwargs) + # tick the updated date on the parent list + self.book_list.updated_date = timezone.now() + self.book_list.save(broadcast=False) + list_owner = self.book_list.user # create a notification if somoene ELSE added to a local user's list if created and list_owner.local and list_owner != self.user: diff --git a/bookwyrm/templates/import.html b/bookwyrm/templates/import.html index 99ff5c424..a54051310 100644 --- a/bookwyrm/templates/import.html +++ b/bookwyrm/templates/import.html @@ -7,36 +7,43 @@ {% block content %}

{% trans "Import Books" %}

-
+ {% csrf_token %} -