Skip to content
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

bookmark category download implementation #1800

Merged
merged 9 commits into from
Dec 10, 2024
Merged

bookmark category download implementation #1800

merged 9 commits into from
Dec 10, 2024

Conversation

quimmrc
Copy link
Contributor

@quimmrc quimmrc commented Dec 2, 2024

Issue(s)
#1290

Description
Implementation of new view for bookmarkcategory downloads, new method to get the sound attributions for a bookmark category and download_sounds adaptation to make it viable for both packs and bookmark categories

Errors: download_sounds redirects to a url with the text content of the download instead of downloading the desired zip file

Captura de pantalla de 2024-12-02 12-05-14

Copy link
Member

@ffont ffont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple of comments to make code a bit cleaner and better documented, but other than that the PR looks great. The "error" you mention (that clicking the download link redirects to a file list), is also happening with pack downloads right? This is because to turn this into a zip file we use some specific server tricks which are not available in the development server. If the behaviour is the same as for the pack downloads, then it should be working when deployed.

def download_bookmark_category(request, category_id):
category = get_object_or_404(BookmarkCategory, id=category_id)
licenses_url = (reverse('bookmark-category-licenses', args=[category_id]))
#missing: cache checking done in packdownload
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to improve this comment, something like: # NOTE: unlike pack downloads, here we are not doing any cache check to...

Make it clear, so if we look at it in the future we understand it easily.

from utils.nginxsendfile import prepare_sendfile_arguments_for_sound_download


def download_sounds(licenses_url, pack):
def download_sounds(licenses_url, batch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of don't like the name batch. Currently it should be some type of object that implements a method called get_attribution which should return the same thing that is returned in the licenses_url argument. But this is not well explained. It wasn't well explained before either. But a good practice we try to follow is to take the opportunity to improve code documentation when we refactor things, so maybe it could be documented now.

I might consider having 3 arguments licneses_file_url, licenses_file_content and sound_list, and then add proper documentation for these arguments. Copilot should probably help you. We follow google style for python documentation, see for example this function as an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending the sound_list into the function was an option I had also considered and implemented previously, I will look back into it as well as the licenses data. I will work on proper documentation for the further push. Thanks.

@@ -0,0 +1,21 @@
{% load absurl %}Bookmark Category downloaded from Freesound
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking if it would make sense to reuse some template/method code between packs and bookmark categories. Did you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am reusing the pack_attributions.txt file for bookmark categories as well, shall I modify the "pack" naming of the variable in the txt file to find something suitable for both kind of classes? I have made it work to return a proper .txt file in either case using "pack" naming but I don't know if it might be confusing in the future:

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not realize you were reusing the same file. Then I might rename the file to multiple_sounds_download_attribution.txt, and pass pack or bookmark_category as needed, then check type to see if you should use pack.name or bookmark_category.name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not reusing the same file in fact, I just tried after you suggested it xD

@ffont
Copy link
Member

ffont commented Dec 2, 2024

Oh, and please edit the title of the PR, don't say this is an "attempt". I guess everything that we do is just initially an attempt :)

@quimmrc quimmrc changed the title bookmark category download implementation attempt bookmark category download implementation Dec 2, 2024
Copy link
Member

@ffont ffont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @quimmrc! I left some other comments, this is almost there!

apiv2/views.py Outdated
@@ -702,7 +702,9 @@ def get(self, request, *args, **kwargs):
msg='Sounds in pack %i have not yet been described or moderated' % int(pack_id), resource=self)

licenses_url = (reverse('pack-licenses', args=[pack.user.username, pack.id]))
return download_sounds(licenses_url, pack)
licenses_content = pack.get_attribution()
sound_list = pack.sounds.filter(processing_state="OK", moderation_state="OK").select_related('user', 'license')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine that inside get_attribution we already do this query pack.sounds.filter(processing_state="OK", moderation_state="OK").select_related('user', 'license') so we are doing it twice. We should improve that. Maybe get_attribution could get an optional argument sound_qs (qsstands for queryset), and when the argument is provided, it does not do the query inside get_attribution and it uses the provided queryset. Simlar logic should be applied for the bookmarks case. Do you think it makes sense?

@@ -84,20 +84,25 @@ def delete_bookmark_category(request, category_id):
return HttpResponseRedirect(next)
else:
return HttpResponseRedirect(reverse("bookmarks-for-user", args=[request.user.username]))


@login_required
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

licenses_url = (reverse('category-licenses', args=[category_id]))

bookmarked_sounds = Bookmark.objects.filter(category_id=category.id).values("sound_id")
sounds_list = Sound.objects.filter(id__in=bookmarked_sounds, processing_state="OK", moderation_state="OK").select_related('user','license')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before, try to do it in a way in which this query is only performed once.

sounds/models.py Outdated
dict(users=users,
pack=self,
attribution = render_to_string("sounds/multiple_sounds_attribution.txt",
dict(type=self.__class__.__name__,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, this is ugly :S
just hardcode the name, e.g. type="pack", it is clearer.


Args:
licenses_file_url (str): url to the sound Pack or BookmarkCategory licenses
licenses_file_content (django SafeString): attributions for the different sounds in the Pack or BookmarkCategory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These django things were suggested by copilot or some other AI, or did you find about that elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't provided by neither copilot nor an AI tool, I can't remember exactly how I got that but probably by looking at Django official documentation or by looking into the type of the variable. I'll check it again and see what it really is.

Copy link
Contributor Author

@quimmrc quimmrc Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definetly took it from the "type" of the variable "licenses_file_content", as it returns:

<class 'django.utils.safestring.SafeString'>

However I would agree that it's not a very friendly name. Attributions come from the "render_to_string" from a .txt file. Any proposals on other name type this could take? Maybe just string would be enough.

dict(users=users,
category=self,
attribution = render_to_string(("sounds/multiple_sounds_attribution.txt"),
dict(type=self.__class__.__name__,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type="bookmark_category" (or whatever you use to compare in the template). It will be clearer for our future selves this way.

@ffont ffont merged commit 762b267 into master Dec 10, 2024
1 check passed
@ffont ffont deleted the issue1290 branch December 10, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants