Skip to content

Commit

Permalink
migration: add collection attributes
Browse files Browse the repository at this point in the history
improve url handling and visualization
  • Loading branch information
quimmrc committed Jan 9, 2025
1 parent 813bd48 commit 52543c4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 15 deletions.
2 changes: 1 addition & 1 deletion freesound/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
path('tickets/', include('tickets.urls')),
path('monitor/', include('monitor.urls')),
path('follow/', include('follow.urls')),
path('fscollections/', include('fscollections.urls')),
path('collections/', include('fscollections.urls')),

path('blog/', RedirectView.as_view(url='https://blog.freesound.org/'), name="blog"),

Expand Down
28 changes: 28 additions & 0 deletions fscollections/migrations/0002_auto_20250109_1035.py
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'),
),
]
13 changes: 9 additions & 4 deletions fscollections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@
class Collection(models.Model):

author = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=128, default="") #prob not default to ""
sounds = models.ManyToManyField(Sound, related_name="collections") #this will affect the following migration [related_name, sound(s)]
name = models.CharField(max_length=128, default="") #add restrictions
sounds = models.ManyToManyField(Sound, related_name="collections") #NOTE: before next migration pluralize sound(s) - check consequences in views
created = models.DateTimeField(db_index=True, auto_now_add=True)
description = models.TextField(max_length=500, default="")
#NOTE: before next migration add a num_sounds attribute
#contributors = delicate stuff
#subcolletion_path =
#subcolletion_path = sth with tagsn and routing folders for downloads

def __str__(self):
return f"{self.name}"


'''
class CollectionSound(models.Model):
'''
3 changes: 2 additions & 1 deletion fscollections/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from . import views

urlpatterns = [
path('',views.collections, name='collections')
path('',views.collections, name='collections'),
path('<int:collection_id>/', views.collections, name='collections')
]
22 changes: 16 additions & 6 deletions fscollections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,28 @@
from sounds.models import Sound

@login_required
def collections(request):
def collections(request, collection_id=None):
user = request.user
#collection = Collection.__init__(author=user, )
#tvars = {collection = collection}
return render(request, 'collections/collections.html')
user_collections = Collection.objects.filter(author=user).order_by('-created') #first user collection should be on top - this would affect the if block
#if no collection id is provided for this URL, render the oldest collection in the webpage
if not collection_id:
collection = user_collections.last()
else:
collection = get_object_or_404(Collection, id=collection_id)

tvars = {'collection': collection,
'collections_for_user': user_collections}

return render(request, 'collections/collections.html', tvars)

#upon creating a new user, create alongside a personal PRIVATE collection to store sounds

def add_sound_to_collection(request, sound_id, collection_id):
sound = get_object_or_404(Sound, id=sound_id)
collection = get_object_or_404(Collection, id=collection_id, author=request.user)

if sound.moderation_state=='OK':
collection.sound.add(sound) #TBC after next migration
collection.sounds.add(sound)
collection.save()
return True
else:
Expand All @@ -50,7 +60,7 @@ def delete_sound_from_collection(request, sound_id, collection_id):
collection = get_object_or_404(Collection, id=collection_id, author=request.user)

if sound in collection.sound.all():
collection.sound.remove(sound) #TBC after next migration
collection.sounds.remove(sound)
collection.save()
return True
else:
Expand Down
32 changes: 29 additions & 3 deletions templates/collections/collections.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@

{% block title%}Collections{%endblock title%}
{% block page-title%}Collections{%endblock page-title%}
{%block page-content%}
Collection Name: {{collection.name}}
{%endblock page-content%}
{% block page-content %}
<div class="v-spacing-top-5">
<div class="row no-gutters">
<div class="col-lg-3 offset-lg-1 order-lg-last v-spacing-3">
<h4>Your collections</h4>
<div class="v-spacing-top-3">
<ul class="list-style-type-none">
{% for col in collections_for_user %}
<li><a href="{% url "collections" col.id %}" {% if collection.id == col.id %}style="font-weight:bold"{% endif %}>{{col.name}}</a>
<span class=text-light-grey> · {{col.sounds.count}} sound{{col.sounds.count|pluralize}}</span></li>
{% endfor %}
</ul>
</div>
</div>
<div class="col-lg-8">
<h3>{{collection.name}}</h3>
<div class = "row">
{% if collection.sounds.count > 0 %}
{% for sound in collection.sounds.all %}
<div class="col-6 col-md-4">
{% display_sound_small sound %}
</div>
{% endfor %}
{% else %} There aren't any sounds in this collection yet {% endif %}
</div>
</div>
</div>
</div>
{% endblock page-content %}

0 comments on commit 52543c4

Please sign in to comment.