Merge pull request #250 from mouse-reeve/readthrough

UI for editable readthroughs
This commit is contained in:
Mouse Reeve 2020-10-30 11:23:27 -07:00 committed by GitHub
commit 905c99b075
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 104 additions and 6 deletions

View file

@ -29,6 +29,7 @@ class CustomForm(ModelForm):
visible.field.widget.attrs['rows'] = None visible.field.widget.attrs['rows'] = None
visible.field.widget.attrs['class'] = css_classes[input_type] visible.field.widget.attrs['class'] = css_classes[input_type]
class LoginForm(CustomForm): class LoginForm(CustomForm):
class Meta: class Meta:
model = models.User model = models.User

View file

@ -56,10 +56,58 @@
</div> </div>
{% for readthrough in readthroughs %} {% for readthrough in readthroughs %}
<div class="block"> <div class="content block">
{{ readthrough.start_date }} <input class="toggle-control" type="radio" name="show-edit-readthrough" id="show-readthrough-{{ readthrough.id }}" checked>
{{ readthrough.finish_date }} <div class="toggle-content hidden">
{{ readthrough.pages_read }} <dl>
<dt>Started reading:</dt>
<dd>{{ readthrough.start_date | naturalday }}</dd>
<dt>Finished reading:</dt>
<dd>{{ readthrough.finish_date | naturalday }}</dd>
</dl>
<div class="field is-grouped">
<label class="button is-small" for="edit-readthrough-{{ readthrough.id }}">
<span class="icon icon-pencil">
<span class="is-sr-only">Edit readthrough dates</span>
</span>
</label>
<form name="delete-readthrough-{{ readthrough.id }}" action="/delete-readthrough" method="POST">
{% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}">
<button class="button is-small" type="submit">
<span class="icon icon-x">
<span class="is-sr-only">Delete this readthrough</span>
</span>
</button>
</form>
</div>
</div>
</div>
<div class="content block">
<input class="toggle-control" type="radio" name="show-edit-readthrough" id="edit-readthrough-{{ readthrough.id }}">
<div class="toggle-content hidden">
<form name="edit-readthrough" action="/edit-readthrough" method="post">
{% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}">
<div class="field">
<label class="label" for="start_date">
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" for="finish_date">
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">
<button class="button is-small" type="submit">Save</button>
<label class="button is-small" for="show-readthrough-{{ readthrough.id }}">Cancel</label>
</div>
</form>
</div>
</div> </div>
{% endfor %} {% endfor %}

View file

@ -102,6 +102,9 @@ urlpatterns = [
re_path(r'^edit_book/(?P<book_id>\d+)/?', actions.edit_book), re_path(r'^edit_book/(?P<book_id>\d+)/?', actions.edit_book),
re_path(r'^upload_cover/(?P<book_id>\d+)/?', actions.upload_cover), re_path(r'^upload_cover/(?P<book_id>\d+)/?', actions.upload_cover),
re_path(r'^edit-readthrough/?', actions.edit_readthrough),
re_path(r'^delete-readthrough/?', actions.delete_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),
re_path(r'^quotate/?$', actions.quotate), re_path(r'^quotate/?$', actions.quotate),

View file

@ -2,6 +2,8 @@
from io import BytesIO, TextIOWrapper from io import BytesIO, TextIOWrapper
from PIL import Image from PIL import Image
import dateutil.parser
from dateutil.parser import ParserError
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
@ -261,6 +263,51 @@ def upload_cover(request, book_id):
return redirect('/book/%s' % book.id) return redirect('/book/%s' % book.id)
@login_required
def edit_readthrough(request):
''' can't use the form because the dates are too finnicky '''
try:
readthrough = models.ReadThrough.objects.get(id=request.POST.get('id'))
except models.ReadThrough.DoesNotExist:
return HttpResponseNotFound()
# don't let people edit other people's data
if request.user != readthrough.user:
return HttpResponseBadRequest()
# convert dates into a legible format
start_date = request.POST.get('start_date')
try:
start_date = dateutil.parser.parse(start_date)
except ParserError:
start_date = None
readthrough.start_date = start_date
finish_date = request.POST.get('finish_date')
try:
finish_date = dateutil.parser.parse(finish_date)
except ParserError:
finish_date = None
readthrough.finish_date = finish_date
readthrough.save()
return redirect(request.headers.get('Referer', '/'))
@login_required
def delete_readthrough(request):
''' remove a readthrough '''
try:
readthrough = models.ReadThrough.objects.get(id=request.POST.get('id'))
except models.ReadThrough.DoesNotExist:
return HttpResponseNotFound()
# don't let people edit other people's data
if request.user != readthrough.user:
return HttpResponseBadRequest()
readthrough.delete()
return redirect(request.headers.get('Referer', '/'))
@login_required @login_required
def shelve(request): def shelve(request):
''' put a on a user's shelf ''' ''' put a on a user's shelf '''

View file

@ -493,7 +493,6 @@ def book_page(request, book_id):
book=book, book=book,
).order_by('start_date') ).order_by('start_date')
rating = reviews.aggregate(Avg('rating')) rating = reviews.aggregate(Avg('rating'))
tags = models.Tag.objects.filter( tags = models.Tag.objects.filter(
book=book book=book
@ -508,10 +507,10 @@ def book_page(request, book_id):
'rating': rating['rating__avg'], 'rating': rating['rating__avg'],
'tags': tags, 'tags': tags,
'user_tags': user_tags, 'user_tags': user_tags,
'readthroughs': readthroughs,
'review_form': forms.ReviewForm(), 'review_form': forms.ReviewForm(),
'quotation_form': forms.QuotationForm(), 'quotation_form': forms.QuotationForm(),
'comment_form': forms.CommentForm(), 'comment_form': forms.CommentForm(),
'readthroughs': readthroughs,
'tag_form': forms.TagForm(), 'tag_form': forms.TagForm(),
'path': '/book/%s' % book_id, 'path': '/book/%s' % book_id,
'cover_form': forms.CoverForm(instance=book), 'cover_form': forms.CoverForm(instance=book),