diff --git a/bookwyrm/tests/actions/test_readthrough.py b/bookwyrm/tests/actions/test_readthrough.py new file mode 100644 index 00000000..1c5cddb8 --- /dev/null +++ b/bookwyrm/tests/actions/test_readthrough.py @@ -0,0 +1,60 @@ +from django.test import TestCase, Client +from django.utils import timezone +from datetime import datetime + +from bookwyrm import view_actions as actions, models + +class ReadThrough(TestCase): + def setUp(self): + self.client = Client() + + self.work = models.Work.objects.create( + title='Example Work' + ) + + self.edition = models.Edition.objects.create( + title='Example Edition', + parent_work=self.work + ) + self.work.default_edition = self.edition + self.work.save() + + self.user = models.User.objects.create() + + self.client.force_login(self.user) + + def test_create_basic_readthrough(self): + """A basic readthrough doesn't create a progress update""" + self.assertEqual(self.edition.readthrough_set.count(), 0) + + self.client.post('/start-reading/{}'.format(self.edition.id), { + 'start_date': '2020-11-27', + }) + + readthroughs = self.edition.readthrough_set.all() + self.assertEqual(len(readthroughs), 1) + self.assertEqual(readthroughs[0].progressupdate_set.count(), 0) + self.assertEqual(readthroughs[0].start_date, + datetime(2020, 11, 27, tzinfo=timezone.utc)) + self.assertEqual(readthroughs[0].pages_read, None) + self.assertEqual(readthroughs[0].finish_date, None) + + def test_create_progress_readthrough(self): + self.assertEqual(self.edition.readthrough_set.count(), 0) + + self.client.post('/start-reading/{}'.format(self.edition.id), { + 'start_date': '2020-11-27', + 'pages_read': 50, + }) + + readthroughs = self.edition.readthrough_set.all() + self.assertEqual(len(readthroughs), 1) + self.assertEqual(readthroughs[0].start_date, + datetime(2020, 11, 27, tzinfo=timezone.utc)) + self.assertEqual(readthroughs[0].pages_read, 50) + self.assertEqual(readthroughs[0].finish_date, None) + + progress_updates = readthroughs[0].progressupdate_set.all() + self.assertEqual(len(progress_updates), 1) + self.assertEqual(progress_updates[0].mode, models.ProgressMode.PAGE) + self.assertEqual(progress_updates[0].progress, 50) diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index 130af8f6..f5aee7a4 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -363,6 +363,13 @@ def start_reading(request, book_id): if readthrough.start_date: readthrough.save() + # create a progress update if we have a page + if readthrough.pages_read: + readthrough.progressupdate_set.create( + user=request.user, + progress=readthrough.pages_read, + mode=models.ProgressMode.PAGE) + # shelve the book if request.POST.get('reshelve', True): try: @@ -728,7 +735,8 @@ def update_readthrough(request, book=None, create=True): start_date = request.POST.get('start_date') if start_date: try: - start_date = dateutil.parser.parse(start_date) + start_date = timezone.make_aware( + dateutil.parser.parse(start_date)) readthrough.start_date = start_date except ParserError: pass @@ -736,7 +744,8 @@ def update_readthrough(request, book=None, create=True): finish_date = request.POST.get('finish_date') if finish_date: try: - finish_date = dateutil.parser.parse(finish_date) + finish_date = timezone.make_aware( + dateutil.parser.parse(finish_date)) readthrough.finish_date = finish_date except ParserError: pass