Merge pull request #508 from mouse-reeve/create-readthrough

Create readthrough
This commit is contained in:
Mouse Reeve 2021-01-10 18:55:58 -08:00 committed by GitHub
commit 5b7d9d1973
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 124 additions and 18 deletions

View file

@ -151,6 +151,26 @@
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}
<div class="block">
<div>
<input type="radio" class="toggle-control" name="add-readthrough-form" id="hide-create-readthrough" checked>
<div class="toggle-content hidden">
<label class="button" for="add-readthrough" class="button" role="button" tabindex="0">Add read dates</label>
</div>
</div>
<div>
<input type="radio" class="toggle-control" id="add-readthrough" name="add-readthrough-form">
<div class="toggle-content hidden box">
<form name="add-readthrough" action="/create-readthrough" method="post">
{% include 'snippets/readthrough_form.html' with readthrough=None %}
<div class="field is-grouped">
<button class="button is-primary" type="submit">Create</button>
<label class="button" for="hide-create-readthrough" role="button" tabindex="0">Cancel</label>
</div>
</form>
</div>
</div>
</div>
{% if request.user.is_authenticated %} {% if request.user.is_authenticated %}
<div class="box"> <div class="box">

View file

@ -11,9 +11,9 @@ Follow request already sent.
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}"> <input type="hidden" name="user" value="{{ user.username }}">
{% if user.manually_approves_followers %} {% if user.manually_approves_followers %}
<button class="button is-small is-primary" type="submit">Send follow request</button> <button class="button is-small is-link" type="submit">Send follow request</button>
{% else %} {% else %}
<button class="button is-small is-primary" type="submit">Follow</button> <button class="button is-small is-link" type="submit">Follow</button>
{% endif %} {% endif %}
</form> </form>
<form action="/unfollow/" method="POST" onsubmit="interact(event)" class="follow-{{ user.id }} {% if not request.user in user.followers.all %}hidden{%endif %}" data-id="follow-{{ user.id }}"> <form action="/unfollow/" method="POST" onsubmit="interact(event)" class="follow-{{ user.id }} {% if not request.user in user.followers.all %}hidden{%endif %}" data-id="follow-{{ user.id }}">

View file

@ -36,20 +36,7 @@
<div class="toggle-content hidden"> <div class="toggle-content hidden">
<div class="box"> <div class="box">
<form name="edit-readthrough" action="/edit-readthrough" method="post"> <form name="edit-readthrough" action="/edit-readthrough" method="post">
{% csrf_token %} {% include 'snippets/readthrough_form.html' with readthrough=readthrough %}
<input type="hidden" name="id" value="{{ readthrough.id }}">
<div class="field">
<label class="label">
Started reading
<input type="date" name="start_date" class="input" id="id_start_date-{{ readthrough.id }}" value="{{ readthrough.start_date | date:"Y-m-d" }}">
</label>
</div>
<div class="field">
<label class="label">
Finished reading
<input type="date" name="finish_date" class="input" id="id_finish_date-{{ readthrough.id }}" value="{{ readthrough.finish_date | date:"Y-m-d" }}">
</label>
</div>
<div class="field is-grouped"> <div class="field is-grouped">
<button class="button is-primary" type="submit">Save</button> <button class="button is-primary" type="submit">Save</button>
<label class="button" for="show-readthrough-{{ readthrough.id }}" role="button" tabindex="0">Cancel</label> <label class="button" for="show-readthrough-{{ readthrough.id }}" role="button" tabindex="0">Cancel</label>

View file

@ -0,0 +1,15 @@
{% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}">
<input type="hidden" name="book" value="{{ book.id }}">
<div class="field">
<label class="label">
Started reading
<input type="date" name="start_date" class="input" id="id_start_date-{{ readthrough.id }}" value="{{ readthrough.start_date | date:"Y-m-d" }}">
</label>
</div>
<div class="field">
<label class="label">
Finished reading
<input type="date" name="finish_date" class="input" id="id_finish_date-{{ readthrough.id }}" value="{{ readthrough.finish_date | date:"Y-m-d" }}">
</label>
</div>

View file

@ -1,12 +1,14 @@
''' test for app action functionality ''' ''' test for app action functionality '''
from unittest.mock import patch from unittest.mock import patch
import dateutil
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.contrib.auth.models import Group, Permission from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.http.response import Http404 from django.http.response import Http404
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from django.utils import timezone
from bookwyrm import forms, models, view_actions as actions from bookwyrm import forms, models, view_actions as actions
from bookwyrm.settings import DOMAIN from bookwyrm.settings import DOMAIN
@ -358,6 +360,71 @@ class ViewActions(TestCase):
self.assertEqual(resp.template_name, 'edit_author.html') self.assertEqual(resp.template_name, 'edit_author.html')
def test_edit_readthrough(self):
''' adding dates to an ongoing readthrough '''
start = timezone.make_aware(dateutil.parser.parse('2021-01-03'))
readthrough = models.ReadThrough.objects.create(
book=self.book, user=self.local_user, start_date=start)
request = self.factory.post(
'', {
'start_date': '2017-01-01',
'finish_date': '2018-03-07',
'book': '',
'id': readthrough.id,
})
request.user = self.local_user
actions.edit_readthrough(request)
readthrough.refresh_from_db()
self.assertEqual(readthrough.start_date.year, 2017)
self.assertEqual(readthrough.start_date.month, 1)
self.assertEqual(readthrough.start_date.day, 1)
self.assertEqual(readthrough.finish_date.year, 2018)
self.assertEqual(readthrough.finish_date.month, 3)
self.assertEqual(readthrough.finish_date.day, 7)
self.assertEqual(readthrough.book, self.book)
def test_delete_readthrough(self):
''' remove a readthrough '''
readthrough = models.ReadThrough.objects.create(
book=self.book, user=self.local_user)
models.ReadThrough.objects.create(
book=self.book, user=self.local_user)
request = self.factory.post(
'', {
'id': readthrough.id,
})
request.user = self.local_user
actions.delete_readthrough(request)
self.assertFalse(
models.ReadThrough.objects.filter(id=readthrough.id).exists())
def test_create_readthrough(self):
''' adding new read dates '''
request = self.factory.post(
'', {
'start_date': '2017-01-01',
'finish_date': '2018-03-07',
'book': self.book.id,
'id': '',
})
request.user = self.local_user
actions.create_readthrough(request)
readthrough = models.ReadThrough.objects.get()
self.assertEqual(readthrough.start_date.year, 2017)
self.assertEqual(readthrough.start_date.month, 1)
self.assertEqual(readthrough.start_date.day, 1)
self.assertEqual(readthrough.finish_date.year, 2018)
self.assertEqual(readthrough.finish_date.month, 3)
self.assertEqual(readthrough.finish_date.day, 7)
self.assertEqual(readthrough.book, self.book)
self.assertEqual(readthrough.user, self.local_user)
def test_tag(self): def test_tag(self):
''' add a tag to a book ''' ''' add a tag to a book '''
request = self.factory.post( request = self.factory.post(

View file

@ -110,6 +110,7 @@ urlpatterns = [
re_path(r'^switch-edition/?$', actions.switch_edition), re_path(r'^switch-edition/?$', actions.switch_edition),
re_path(r'^edit-readthrough/?$', actions.edit_readthrough), re_path(r'^edit-readthrough/?$', actions.edit_readthrough),
re_path(r'^delete-readthrough/?$', actions.delete_readthrough), re_path(r'^delete-readthrough/?$', actions.delete_readthrough),
re_path(r'^create-readthrough/?$', actions.create_readthrough),
re_path(r'^rate/?$', actions.rate), re_path(r'^rate/?$', actions.rate),
re_path(r'^review/?$', actions.review), re_path(r'^review/?$', actions.review),

View file

@ -522,6 +522,18 @@ def delete_readthrough(request):
return redirect(request.headers.get('Referer', '/')) return redirect(request.headers.get('Referer', '/'))
@login_required
@require_POST
def create_readthrough(request):
''' can't use the form because the dates are too finnicky '''
book = get_object_or_404(models.Edition, id=request.POST.get('book'))
readthrough = update_readthrough(request, create=True, book=book)
if not readthrough:
return redirect(book.local_path)
readthrough.save()
return redirect(request.headers.get('Referer', '/'))
@login_required @login_required
@require_POST @require_POST
def rate(request): def rate(request):
@ -824,7 +836,7 @@ def update_readthrough(request, book=None, create=True):
start_date = request.POST.get('start_date') start_date = request.POST.get('start_date')
if start_date: if start_date:
try: try:
start_date = dateutil.parser.parse(start_date) start_date = timezone.make_aware(dateutil.parser.parse(start_date))
readthrough.start_date = start_date readthrough.start_date = start_date
except ParserError: except ParserError:
pass pass
@ -832,9 +844,13 @@ def update_readthrough(request, book=None, create=True):
finish_date = request.POST.get('finish_date') finish_date = request.POST.get('finish_date')
if finish_date: if finish_date:
try: try:
finish_date = dateutil.parser.parse(finish_date) finish_date = timezone.make_aware(
dateutil.parser.parse(finish_date))
readthrough.finish_date = finish_date readthrough.finish_date = finish_date
except ParserError: except ParserError:
pass pass
if not readthrough.start_date and not readthrough.finish_date:
return None
return readthrough return readthrough