-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collections #1810
Draft
quimmrc
wants to merge
19
commits into
master
Choose a base branch
from
collections
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Collections #1810
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
813bd48
collections app creation and first basic implementations
quimmrc 52543c4
migration: add collection attributes
quimmrc 5f137eb
attempt to implement collection modals
quimmrc 51adc61
models reformulation
quimmrc 13a90da
Merge branch 'master' into collections
quimmrc 2780959
add sound modal behavior tbd
quimmrc 4c7c190
add sound modal collection selector
quimmrc 0f47090
add sound to col from sound url
quimmrc fc55074
delete sound from collection func.
quimmrc b2e0277
deletion for CollectionSound + restrict add duplicates
quimmrc 5ba088d
minor details correction
quimmrc ae9950b
delete and create collection functionalities
quimmrc d679849
Edit collection permissions
quimmrc 58d845f
add collection parameter to settings.py
quimmrc 3d7a668
add maintainer modal (fails)
quimmrc 5ffea0b
add maintainers interface
quimmrc d5fed09
adequate variable namings for collection modals
quimmrc 04ba23d
maintainers display in edit collection url
quimmrc e2bc70a
Merge branch 'master' into collections
quimmrc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class FscollectionsConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'fscollections' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# | ||
# Freesound is (c) MUSIC TECHNOLOGY GROUP, UNIVERSITAT POMPEU FABRA | ||
# | ||
# Freesound is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# Freesound is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Authors: | ||
# See AUTHORS file. | ||
# | ||
|
||
from django import forms | ||
from fscollections.models import Collection | ||
|
||
#this class was aimed to perform similarly to BookmarkSound, however, at first the method to add a sound to a collection | ||
#will be opening the search engine in a modal, looking for a sound in there and adding it to the actual collection page | ||
#this can be found in edit pack -> add sounds | ||
#class CollectSoundForm(forms.ModelForm): | ||
|
||
class CollectionSoundForm(forms.Form): | ||
#list of existing collections | ||
#add sound to collection from sound page | ||
collection = forms.ChoiceField( | ||
label=False, | ||
choices=[], | ||
required=True) | ||
|
||
new_collection_name = forms.ChoiceField( | ||
label = False, | ||
help_text=None, | ||
required = False) | ||
|
||
use_last_collection = forms.BooleanField(widget=forms.HiddenInput(), required=False, initial=False) | ||
user_collections = None | ||
|
||
NO_COLLECTION_CHOICE_VALUE = '-1' | ||
NEW_COLLECTION_CHOICE_VALUE = '0' | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.user_collections = kwargs.pop('user_collections', False) | ||
self.user_saving_sound = kwargs.pop('user_saving_sound', False) | ||
self.sound_id = kwargs.pop('sound_id', False) | ||
super().__init__(*args, **kwargs) | ||
self.fields['collection'].choices = [(self.NO_COLLECTION_CHOICE_VALUE, '--- No collection ---'),#in this case this goes to bookmarks collection (might have to be created) | ||
(self.NEW_COLLECTION_CHOICE_VALUE, 'Create a new collection...')] + \ | ||
([(collection.id, collection.name) for collection in self.user_collections] | ||
if self.user_collections else[]) | ||
|
||
self.fields['new_collection_name'].widget.attrs['placeholder'] = "Fill in the name for the new collection" | ||
self.fields['category'].widget.attrs = { | ||
'data-grey-items': f'{self.NO_CATEGORY_CHOICE_VALUE},{self.NEW_CATEGORY_CHOICE_VALUE}'} | ||
#i don't fully understand what this last line of code does | ||
|
||
def save(self, *args, **kwargs): | ||
collection_to_use = None | ||
|
||
if not self.cleaned_data['use_last_collection']: | ||
if self.cleaned_data['collection'] == self.NO_COLLECTION_CHOICE_VALUE: | ||
pass | ||
elif self.cleaned_data['collection'] == self.NEW_COLLECTION_CHOICE_VALUE: | ||
if self.cleaned_data['new_collection_name'] != "": | ||
collection = \ | ||
Collection(author=self.user_saving_bookmark, name=self.cleaned_data['new_collection_name']) | ||
collection.save() | ||
collection_to_use = collection | ||
else: | ||
collection_to_use = Collection.objects.get(id=self.cleaned_data['collection']) | ||
else: #en aquest cas - SÍ estem fent servir l'última coleccio, NO estem creant una nova coleccio, NO estem agafant una coleccio existent i | ||
# per tant ens trobem en un cas de NO COLLECTION CHOICE VALUE (no s'ha triat cap coleccio) | ||
# si no es tria cap coleccio: l'usuari té alguna colecció? NO -> creem BookmarksCollection pels seus sons privats | ||
# SI -> per defecte es posa a BookmarksCollection | ||
try: | ||
last_user_collection = \ | ||
Collection.objects.filter(user=self.user_saving_bookmark).order_by('-created')[0] | ||
# If user has a previous bookmark, use the same category (or use none if no category used in last | ||
# bookmark) | ||
collection_to_use = last_user_collection | ||
except IndexError: | ||
# This is first bookmark of the user | ||
pass | ||
|
||
# If collection already exists, don't save it and return the existing one | ||
collection, _ = Collection.objects.get_or_create( | ||
name = collection_to_use.name, author=self.user_saving_bookmark) | ||
return collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 3.2.23 on 2025-01-07 12:44 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('sounds', '0052_alter_sound_type'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Collection', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(default='', max_length=128)), | ||
('created', models.DateTimeField(auto_now_add=True, db_index=True)), | ||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
('sound', models.ManyToManyField(to='sounds.Sound')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 3.2.23 on 2025-01-09 10:35 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('sounds', '0052_alter_sound_type'), | ||
('fscollections', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='collection', | ||
name='sound', | ||
), | ||
migrations.AddField( | ||
model_name='collection', | ||
name='description', | ||
field=models.TextField(default='', max_length=500), | ||
), | ||
migrations.AddField( | ||
model_name='collection', | ||
name='sounds', | ||
field=models.ManyToManyField(related_name='collections', to='sounds.Sound'), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# | ||
# Freesound is (c) MUSIC TECHNOLOGY GROUP, UNIVERSITAT POMPEU FABRA | ||
# | ||
# Freesound is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# Freesound is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Authors: | ||
# See AUTHORS file. | ||
# | ||
|
||
from django.contrib.auth.models import User | ||
from django.db import models | ||
|
||
from sounds.models import Sound, License | ||
|
||
class Collection(models.Model): | ||
|
||
author = models.ForeignKey(User, on_delete=models.CASCADE) | ||
name = models.CharField(max_length=128, default="BookmarkCollection") #add restrictions | ||
sounds = models.ManyToManyField(Sound, related_name="collections") | ||
created = models.DateTimeField(db_index=True, auto_now_add=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
description = models.TextField(max_length=500, default="") | ||
#NOTE: before next migration add a num_sounds attribute | ||
#bookmarks = True or False depending on it being the BookmarkCollection for a user (the one created for the first sound without collection assigned) | ||
#contributors = delicate stuff | ||
#subcolletion_path = sth with tagsn and routing folders for downloads | ||
#follow relation for users and collections (intersted but not owner nor contributor) | ||
#sooner or later you'll need to start using forms for adding sounds to collections xd | ||
|
||
|
||
def __str__(self): | ||
return f"{self.name}" | ||
|
||
''' | ||
class CollectionSound(models.Model): | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path('',views.collections, name='collections'), | ||
path('<int:collection_id>/', views.collections, name='collections'), | ||
path('<int:collection_id>/<int:sound_id>/delete/', views.delete_sound_from_collection, name='delete-sound-from-collection'), | ||
path('<int:sound_id>/add/', views.add_sound_to_collection, name='add-sound-to-collection'), | ||
path('get_form_for_collection_sound/<int:sound_id>/', views.get_form_for_collecting_sound, name="collection-add-form-for-sound") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# | ||
# Freesound is (c) MUSIC TECHNOLOGY GROUP, UNIVERSITAT POMPEU FABRA | ||
# | ||
# Freesound is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# Freesound is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Authors: | ||
# See AUTHORS file. | ||
# | ||
|
||
from django.contrib.auth.decorators import login_required | ||
from django.db import transaction | ||
from django.http import Http404, HttpResponse, HttpResponseRedirect, JsonResponse | ||
from django.shortcuts import get_object_or_404, render | ||
from django.urls import reverse | ||
|
||
from fscollections.models import Collection | ||
from fscollections.forms import CollectionSoundForm | ||
from sounds.models import Sound | ||
from sounds.views import add_sounds_modal_helper | ||
|
||
@login_required | ||
def collections(request, collection_id=None): | ||
user = request.user | ||
user_collections = Collection.objects.filter(author=user).order_by('-created') #first user collection should be on top - this would affect the if block | ||
is_owner = False | ||
#if no collection id is provided for this URL, render the oldest collection in the webpage | ||
#be careful when loading this url without having any collection for a user | ||
#rn the relation user-collection only exists when you own the collection | ||
|
||
if not collection_id: | ||
collection = user_collections.last() | ||
else: | ||
collection = get_object_or_404(Collection, id=collection_id) | ||
|
||
if user == collection.author: | ||
is_owner = True | ||
|
||
tvars = {'collection': collection, | ||
'collections_for_user': user_collections, | ||
'is_owner': is_owner} | ||
|
||
return render(request, 'collections/collections.html', tvars) | ||
|
||
#NOTE: tbd - when a user wants to save a sound without having any collection, create a personal bookmarks collection | ||
|
||
def add_sound_to_collection(request, sound_id, collection_id=None): | ||
sound = get_object_or_404(Sound, id=sound_id) | ||
if collection_id is None: | ||
collection = Collection.objects.filter(author=request.user).order_by("created")[0] | ||
else: | ||
collection = get_object_or_404(Collection, id=collection_id, author=request.user) | ||
|
||
if sound.moderation_state=='OK': | ||
collection.sounds.add(sound) | ||
collection.save() | ||
return HttpResponseRedirect(reverse("collections", args=[collection.id])) | ||
else: | ||
return "sound not moderated or not collection owner" | ||
|
||
|
||
def delete_sound_from_collection(request, collection_id, sound_id): | ||
#this should work as in Packs - select several sounds and remove them all at once from the collection | ||
#by now it works as in Bookmarks in terms of UI | ||
sound = get_object_or_404(Sound, id=sound_id) | ||
collection = get_object_or_404(Collection, id=collection_id, author=request.user) | ||
collection.sounds.remove(sound) | ||
collection.save() | ||
return HttpResponseRedirect(reverse("collections", args=[collection.id])) | ||
|
||
@login_required | ||
def get_form_for_collecting_sound(request, sound_id): | ||
user = request.user | ||
sound = Sound.objects.get(id=sound_id) | ||
|
||
try: | ||
last_collection = \ | ||
Collection.objects.filter(author=request.user).order_by('-created')[0] | ||
# If user has a previous bookmark, use the same category by default (or use none if no category used in last | ||
# bookmark) | ||
except IndexError: | ||
last_collection = None | ||
|
||
user_collections = Collection.objects.filter(author=user).order_by('-created') | ||
form = CollectionSoundForm(initial={'collection': last_collection.id if last_collection else CollectionSoundForm.NO_COLLECTION_CHOICE_VALUE}, | ||
prefix=sound.id, | ||
user_collections=user_collections) | ||
|
||
collections_already_containing_sound = Collection.objects.filter(author=user, collection__sounds=sound).distinct() | ||
tvars = {'user': user, | ||
'sound': sound, | ||
'last_collection': last_collection, | ||
'collections': user_collections, | ||
'form': form, | ||
'collections_with_sound': collections_already_containing_sound} | ||
print("NICE CHECKPOINT") | ||
print(tvars) | ||
|
||
return render(request, 'modal_collect_sound.html', tvars) | ||
|
||
|
||
#NOTE: there should be two methods to add a sound into a collection | ||
#1: adding from the sound.html page through a "bookmark-like" button and opening a Collections modal | ||
#2: from the collection.html page through a search-engine modal as done in Packs | ||
""" | ||
@login_required | ||
def add_sounds_modal_for_collection(request, collection_id): | ||
collection = get_object_or_404(Collection, id=collection_id) | ||
tvars = add_sounds_modal_helper(request, username=collection.author.username) | ||
tvars.update({ | ||
'modal_title': 'Add sounds to Collection', | ||
'help_text': 'Collections are great!', | ||
}) | ||
return render(request, 'sounds/modal_add_sounds.html', tvars) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to specify a default