mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 11:31:08 +00:00
Let users create shelves
This commit is contained in:
parent
c3fe8e041a
commit
408ca6609c
6 changed files with 80 additions and 21 deletions
|
@ -159,3 +159,8 @@ class CreateInviteForm(CustomForm):
|
||||||
choices=[(i, "%d uses" % (i,)) for i in [1, 5, 10, 25, 50, 100]]
|
choices=[(i, "%d uses" % (i,)) for i in [1, 5, 10, 25, 50, 100]]
|
||||||
+ [(None, 'Unlimited')])
|
+ [(None, 'Unlimited')])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ShelfForm(CustomForm):
|
||||||
|
class Meta:
|
||||||
|
model = models.Shelf
|
||||||
|
fields = ['user', 'name', 'privacy']
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
''' puttin' books on shelves '''
|
''' puttin' books on shelves '''
|
||||||
|
import re
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
|
@ -23,6 +24,15 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
|
||||||
through_fields=('shelf', 'book')
|
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
|
@property
|
||||||
def collection_queryset(self):
|
def collection_queryset(self):
|
||||||
''' list of books for this shelf, overrides OrderedCollectionMixin '''
|
''' list of books for this shelf, overrides OrderedCollectionMixin '''
|
||||||
|
|
|
@ -12,20 +12,12 @@
|
||||||
shelves
|
shelves
|
||||||
</h1>
|
</h1>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
|
|
||||||
{% include 'snippets/user_header.html' with user=user %}
|
{% include 'snippets/user_header.html' with user=user %}
|
||||||
|
|
||||||
<div class="block">
|
<div class="block columns">
|
||||||
|
<div class="column">
|
||||||
<div class="tabs" aria-role="tablist">
|
<div class="tabs" aria-role="tablist">
|
||||||
<ul>
|
<ul>
|
||||||
{% for shelf_tab in shelves %}
|
{% for shelf_tab in shelves %}
|
||||||
|
@ -37,7 +29,44 @@
|
||||||
</div>
|
</div>
|
||||||
</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">
|
<div class="column">
|
||||||
<h2 class="title is-3">{{ shelf.name }}</h2>
|
<h2 class="title is-3">{{ shelf.name }}</h2>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -120,6 +120,7 @@ urlpatterns = [
|
||||||
|
|
||||||
re_path(r'^delete-status/?$', actions.delete_status),
|
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'^shelve/?$', actions.shelve),
|
||||||
re_path(r'^unshelve/?$', actions.unshelve),
|
re_path(r'^unshelve/?$', actions.unshelve),
|
||||||
re_path(r'^start-reading/?$', actions.start_reading),
|
re_path(r'^start-reading/?$', actions.start_reading),
|
||||||
|
|
|
@ -272,6 +272,17 @@ def upload_cover(request, book_id):
|
||||||
return redirect('/book/%s' % 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
|
@login_required
|
||||||
def shelve(request):
|
def shelve(request):
|
||||||
''' put a on a user's shelf '''
|
''' put a on a user's shelf '''
|
||||||
|
|
|
@ -626,19 +626,22 @@ def shelf_page(request, username, shelf_identifier):
|
||||||
except models.User.DoesNotExist:
|
except models.User.DoesNotExist:
|
||||||
return HttpResponseNotFound()
|
return HttpResponseNotFound()
|
||||||
|
|
||||||
|
if shelf_identifier:
|
||||||
|
shelf = user.shelf_set.get(identifier=shelf_identifier)
|
||||||
|
else:
|
||||||
|
shelf = user.shelf_set.first()
|
||||||
|
|
||||||
if is_api_request(request):
|
if is_api_request(request):
|
||||||
shelf = models.Shelf.objects.get(user=user, identifier=shelf_identifier)
|
|
||||||
return JsonResponse(shelf.to_activity(**request.GET))
|
return JsonResponse(shelf.to_activity(**request.GET))
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'title': user.name,
|
'title': user.name,
|
||||||
'user': user,
|
'user': user,
|
||||||
'is_self': request.user.id == user.id,
|
'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)
|
return TemplateResponse(request, 'shelf.html', data)
|
||||||
|
|
Loading…
Reference in a new issue