Let users create shelves

This commit is contained in:
Mouse Reeve 2020-11-10 14:52:04 -08:00
parent c3fe8e041a
commit 408ca6609c
6 changed files with 80 additions and 21 deletions

View file

@ -159,3 +159,8 @@ class CreateInviteForm(CustomForm):
choices=[(i, "%d uses" % (i,)) for i in [1, 5, 10, 25, 50, 100]]
+ [(None, 'Unlimited')])
}
class ShelfForm(CustomForm):
class Meta:
model = models.Shelf
fields = ['user', 'name', 'privacy']

View file

@ -1,4 +1,5 @@
''' puttin' books on shelves '''
import re
from django.db import models
from bookwyrm import activitypub
@ -23,6 +24,15 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
through_fields=('shelf', 'book')
)
def save(self, *args, **kwargs):
''' set the identifier '''
saved = super().save(*args, **kwargs)
if not self.identifier:
slug = re.sub(r'[^\w]', '', self.name)
self.identifier = '%s-%d' % (slug, self.id)
return super().save(*args, **kwargs)
return saved
@property
def collection_queryset(self):
''' list of books for this shelf, overrides OrderedCollectionMixin '''

View file

@ -12,20 +12,12 @@
shelves
</h1>
</div>
{% if is_self %}
<div class="column is-narrow">
<a href="/create-shelf/">
<span class="icon icon-plus">
<span class="is-sr-only">Create new shelf</span>
</span>
</a>
</div>
{% endif %}
</div>
{% include 'snippets/user_header.html' with user=user %}
<div class="block">
<div class="block columns">
<div class="column">
<div class="tabs" aria-role="tablist">
<ul>
{% for shelf_tab in shelves %}
@ -37,7 +29,44 @@
</div>
</div>
<div class="columns">
{% if is_self %}
<div class="column is-narrow">
<label for="create-shelf-form">
<div role="button" tabindex="0">
<span class="icon icon-plus">
<span class="is-sr-only">Create new shelf</span>
</span>
</div>
</a>
</div>
{% endif %}
</div>
<input type="checkbox" id="create-shelf-form" class="toggle-control">
<div class="toggle-content hidden">
<div class="box mb-5">
<h2 class="title is-4">Create new shelf</h2>
<form name="create-shelf" action="/create-shelf/" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ request.user.id }}">
<div class="field">
<label class="label" for="id_name">Name:</label>
<input type="text" name="name" maxlength="100" class="input" required="" id="id_name">
</div>
<label class="label">
<p>Shelf privacy:</p>
{% include 'snippets/privacy_select.html' with no_label=True%}
</label>
<div class="field is-grouped">
<button class="button is-primary" type="submit">Create shelf</button>
<label role="button" class="button" for="create-shelf-form" tabindex="0">Cancel<label>
</div>
</form>
</div>
</div>
<div class="block columns">
<div class="column">
<h2 class="title is-3">{{ shelf.name }}</h2>
</div>

View file

@ -120,6 +120,7 @@ urlpatterns = [
re_path(r'^delete-status/?$', actions.delete_status),
re_path(r'^create-shelf/?$', actions.create_shelf),
re_path(r'^shelve/?$', actions.shelve),
re_path(r'^unshelve/?$', actions.unshelve),
re_path(r'^start-reading/?$', actions.start_reading),

View file

@ -272,6 +272,17 @@ def upload_cover(request, book_id):
return redirect('/book/%s' % book.id)
@login_required
def create_shelf(request):
''' user generated shelves '''
form = forms.ShelfForm(request.POST)
if not form.is_valid():
return redirect(request.headers.get('Referer', '/'))
shelf = form.save()
return redirect('/user/%s/shelves/%s' % \
(request.user.localname, shelf.identifier))
@login_required
def shelve(request):
''' put a on a user's shelf '''

View file

@ -626,19 +626,22 @@ def shelf_page(request, username, shelf_identifier):
except models.User.DoesNotExist:
return HttpResponseNotFound()
if shelf_identifier:
shelf = user.shelf_set.get(identifier=shelf_identifier)
else:
shelf = user.shelf_set.first()
if is_api_request(request):
shelf = models.Shelf.objects.get(user=user, identifier=shelf_identifier)
return JsonResponse(shelf.to_activity(**request.GET))
data = {
'title': user.name,
'user': user,
'is_self': request.user.id == user.id,
'shelves': user.shelf_set.all(),
'shelf': shelf,
'create_form': forms.ShelfForm(),
'edit_form': forms.ShelfForm(shelf),
}
data['shelves'] = user.shelf_set.all()
if shelf:
data['shelf'] = user.shelf_set.get(identifier=shelf)
else:
data['shelf'] = user.shelf_set.first()
return TemplateResponse(request, 'shelf.html', data)