Skip to content

Commit cf1683a

Browse files
committed
wip: edit bookmark categories on the modal
1 parent 0c939ac commit cf1683a

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

bookmarks/forms.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,21 @@ def __init__(self, *args, **kwargs):
6060
self.user_bookmark_categories = kwargs.pop('user_bookmark_categories', False)
6161
self.user_saving_bookmark = kwargs.pop('user_saving_bookmark', False)
6262
self.sound_id = kwargs.pop('sound_id', False)
63+
# Get categories that already contain this sound
64+
self.categories_already_containing_sound = kwargs.pop('categories_already_containing_sound', [])
6365
super().__init__(*args, **kwargs)
64-
self.fields['category'].choices = [(self.NO_CATEGORY_CHOICE_VALUE, '--- No category ---'),
65-
(self.NEW_CATEGORY_CHOICE_VALUE, 'Create a new category...')] + \
66-
([(category.id, category.name) for category in self.user_bookmark_categories]
67-
if self.user_bookmark_categories else [])
66+
67+
# Filter out categories that already contain the sound
68+
available_categories = []
69+
if self.user_bookmark_categories:
70+
for category in self.user_bookmark_categories:
71+
if category not in self.categories_already_containing_sound:
72+
available_categories.append((category.id, category.name))
73+
74+
self.fields['category'].choices = [
75+
(self.NO_CATEGORY_CHOICE_VALUE, '--- No category ---'),
76+
(self.NEW_CATEGORY_CHOICE_VALUE, 'Create a new category...')
77+
] + available_categories
6878

6979
self.fields['new_category_name'].widget.attrs['placeholder'] = "Fill in the name for the new category"
7080
self.fields['category'].widget.attrs = {

bookmarks/views.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,25 @@ def get_form_for_sound(request, sound_id):
190190
except IndexError:
191191
last_category = None
192192
user_bookmark_categories = BookmarkCategory.objects.filter(user=request.user)
193-
form = BookmarkForm(initial={'category': last_category.id if last_category else BookmarkForm.NO_CATEGORY_CHOICE_VALUE},
194-
prefix=sound.id,
195-
user_bookmark_categories=user_bookmark_categories)
196193
categories_already_containing_sound = BookmarkCategory.objects.filter(user=request.user,
197-
bookmarks__sound=sound).distinct()
198-
sound_has_bookmark_without_category = Bookmark.objects.filter(user=request.user, sound=sound, category=None).exists()
194+
bookmarks__sound=sound).distinct()
195+
form = BookmarkForm(
196+
initial={'category': last_category.id if last_category else BookmarkForm.NO_CATEGORY_CHOICE_VALUE},
197+
prefix=sound.id,
198+
user_bookmark_categories=user_bookmark_categories,
199+
categories_already_containing_sound=categories_already_containing_sound)
200+
bookmark_without_category = Bookmark.objects.filter(user=request.user, sound=sound, category=None).first()
199201
add_bookmark_url = '/'.join(
200202
request.build_absolute_uri(reverse('add-bookmark', args=[sound_id])).split('/')[:-2]) + '/'
203+
print("bookmark form")
204+
201205
tvars = {
202-
'bookmarks': Bookmark.objects.filter(user=request.user, sound=sound).exists(),
206+
'bookmarks': Bookmark.objects.filter(user=request.user, sound=sound),
203207
'sound_id': sound.id,
204208
'sound_is_moderated_and_processed_ok': sound.moderated_and_processed_ok,
205209
'form': form,
206-
'sound_has_bookmark_without_category': sound_has_bookmark_without_category,
207-
'categories_aready_containing_sound': categories_already_containing_sound,
210+
'bookmark_without_category': bookmark_without_category,
208211
'add_bookmark_url': add_bookmark_url
209212
}
213+
print(tvars['bookmarks'])
210214
return render(request, 'bookmarks/modal_bookmark_sound.html', tvars)

freesound/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,9 @@
847847
# Important: place settings which depend on other settings potentially modified in local_settings.py BELOW the import
848848
from .local_settings import *
849849

850+
CACHES['default'] = {
851+
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
852+
}
850853

851854
# -------------------------------------------------------------------------------
852855
# Celery

sounds/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from django_ratelimit.decorators import ratelimit
5050

5151
from accounts.models import Profile
52+
from bookmarks.models import Bookmark
5253
from comments.forms import CommentForm
5354
from comments.models import Comment
5455
from donations.models import DonationsModalSettings
@@ -258,6 +259,7 @@ def sound(request, username, sound_id):
258259
display_random_link = request.GET.get('random_browsing', False)
259260
is_following = request.user.is_authenticated and follow_utils.is_user_following_user(request.user, sound.user)
260261
is_explicit = sound.is_explicit and (not request.user.is_authenticated or not request.user.profile.is_adult)
262+
sound_is_bookmarked = request.user.is_authenticated and Bookmark.objects.filter(user=request.user, sound=sound).exists()
261263

262264
tvars = {
263265
'sound': sound,
@@ -267,7 +269,8 @@ def sound(request, username, sound_id):
267269
'is_following': is_following,
268270
'is_explicit': is_explicit, # if the sound should be shown blurred, already checks for adult profile
269271
'sizes': settings.IFRAME_PLAYER_SIZE,
270-
'min_num_ratings': settings.MIN_NUMBER_RATINGS
272+
'min_num_ratings': settings.MIN_NUMBER_RATINGS,
273+
'sound_is_bookmarked': sound_is_bookmarked,
271274
}
272275
tvars.update(paginate(request, qs, settings.SOUND_COMMENTS_PER_PAGE))
273276
return render(request, 'sounds/sound.html', tvars)

templates/bookmarks/modal_bookmark_sound.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ <h4 class="v-spacing-5">Bookmark this sound</h4>
3131
<div class="v-spacing-4">
3232
{% if bookmarks %}
3333
<div class="v-spacing-4 text-grey">
34-
{% bw_icon 'bookmark-filled' %}This sound is already in your bookmarks{% if not categories_aready_containing_sound %} under no category{% endif %}
35-
{% if categories_aready_containing_sound %}
36-
under the categories:
37-
{% if sound_has_bookmark_without_category %}
38-
<span class="text-black">Uncategorized</span>,
39-
{% endif %}
40-
{% for cat in categories_aready_containing_sound %}
41-
<span class="text-black">{{cat.name}}</span>{% if not forloop.last %}, {% endif %}
34+
{% bw_icon 'bookmark-filled' %}This sound is already in your bookmarks
35+
{% if bookmark_without_category %}
36+
under:
37+
{% for bookmark in bookmarks %}
38+
<div class="bookmark-category">
39+
<span class="text-black">{{bookmark.category_name_or_uncategorized}}</span>
40+
<form action="{% url 'delete-bookmark' bookmark.id %}" method="post" style="display: inline;">
41+
{% csrf_token %}
42+
<button type="submit" class="btn-small btn-secondary">Remove</button>
43+
</form>
44+
</div>
4245
{% endfor %}
4346
{% endif %}
4447
</div>

templates/sounds/sound.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ <h1><a class="bw-link--black" href="{% url 'sound' sound.user.username sound.id
7272
</button>
7373
{% endif %}
7474
<span class="h-spacing-left-1"></span>
75+
{% if sound_is_bookmarked %}
76+
<button title="Sound is bookmarked" aria-label="Sound is bookmarked - open dialog to edit or remove" class="btn-neutral" data-toggle="bookmark-modal" data-modal-url="{% url 'bookmarks-add-form-for-sound' sound.id %}" data-add-bookmark-url="{% url 'add-bookmark' sound.id %}">
77+
{% bw_icon 'bookmark-filled' %}
78+
</button>
79+
{% else %}
7580
<button title="Bookmark this sound" aria-label="Bookmark this sound" class="btn-neutral" data-toggle="bookmark-modal" data-modal-url="{% url 'bookmarks-add-form-for-sound' sound.id %}" data-add-bookmark-url="{% url 'add-bookmark' sound.id %}">
7681
{% bw_icon 'bookmark' %}
7782
</button>
83+
{% endif %}
7884
</div>
7985
</div>
8086
</div>

0 commit comments

Comments
 (0)