Handle edit and delete readthroughs

This commit is contained in:
Mouse Reeve 2020-10-30 10:40:05 -07:00
parent 5641c36539
commit e39bf026cb
3 changed files with 55 additions and 7 deletions

View file

@ -73,7 +73,7 @@
</label> </label>
<form name="delete-readthrough-{{ readthrough.id }}" action="/delete-readthrough" method="POST"> <form name="delete-readthrough-{{ readthrough.id }}" action="/delete-readthrough" method="POST">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="readthrough" value="{{ readthrough.id }}"> <input type="hidden" name="id" value="{{ readthrough.id }}">
<button class="button is-small" type="submit"> <button class="button is-small" type="submit">
<span class="icon icon-x"> <span class="icon icon-x">
<span class="is-sr-only">Delete this readthrough</span> <span class="is-sr-only">Delete this readthrough</span>
@ -90,18 +90,16 @@
<form name="edit-readthrough" action="/edit-readthrough" method="post"> <form name="edit-readthrough" action="/edit-readthrough" method="post">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}"> <input type="hidden" name="id" value="{{ readthrough.id }}">
<input type="hidden" name="user" value="{{ request.user.id }}">
<input type="hidden" name="book" value="{{ readthrough.book.id }}">
<div class="field"> <div class="field">
<label class="label" for="start_date-{{ readthrough.id }}"> <label class="label" for="start_date">
Started reading Started reading
<input type="date" name="start_date-{{ readthrough.id }}" class="input" id="id_start_date-{{ readthrough.id }}" value="{{ readthrough.start_date | date:"Y-m-d" }}"> <input type="date" name="start_date" class="input" id="id_start_date-{{ readthrough.id }}" value="{{ readthrough.start_date | date:"Y-m-d" }}">
</label> </label>
</div> </div>
<div class="field"> <div class="field">
<label class="label" for="finish_date-{{ readthrough.id }}"> <label class="label" for="finish_date">
Finished reading Finished reading
<input type="date" name="finish_date-{{ readthrough.id }}" class="input" id="id_finish_date-{{ readthrough.id }}" value="{{ readthrough.finish_date | date:"Y-m-d" }}"> <input type="date" name="finish_date" class="input" id="id_finish_date-{{ readthrough.id }}" value="{{ readthrough.finish_date | date:"Y-m-d" }}">
</label> </label>
</div> </div>
<div class="field is-grouped"> <div class="field is-grouped">

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 '''