Multi-platform: support multiple files per version #201

Merged
Oleg-Komarov merged 43 commits from multi-os into main 2024-07-09 16:27:46 +02:00
3 changed files with 22 additions and 9 deletions
Showing only changes of commit a0f0c61825 - Show all commits

View File

@ -755,18 +755,12 @@ class Version(CreatedModifiedMixin, TrackChangesMixin, models.Model):
permissions.append({'slug': slug, 'reason': reason, 'name': all_permission_names[slug]}) permissions.append({'slug': slug, 'reason': reason, 'name': all_permission_names[slug]})
return permissions return permissions
# FIXME make dependent on File or platform # FIXME? make dependent on File or platform
@property @property
def download_name(self) -> str: def download_name(self) -> str:
"""Return a file name for downloads.""" """Return a file name for downloads."""
replace_char = f'{self}'.replace('.', '-') replace_char = f'{self}'.replace('.', '-')
return f'{utils.slugify(replace_char)}{self.files.first().suffix}' return f'{utils.slugify(replace_char)}.zip'
# FIXME make dependent on File or platform
@property
def downloadable_signed_url(self) -> str:
# TODO: actual signed URLs?
return self.files.first().source.url
# FIXME make dependent on File or platform # FIXME make dependent on File or platform
def download_url(self, append_repository_and_compatibility=True) -> str: def download_url(self, append_repository_and_compatibility=True) -> str:

View File

@ -91,6 +91,11 @@ urlpatterns = [
public.extension_version_download, public.extension_version_download,
name='version-download', name='version-download',
), ),
path(
'<slug:slug>/<version>/<platform>/download/<filename>',
public.extension_version_platform_download,
name='version-platform-download',
),
path( path(
'<slug:slug>/manage/versions/<int:pk>/upload/', '<slug:slug>/manage/versions/<int:pk>/upload/',
manage.UploadVersionFileView.as_view(), manage.UploadVersionFileView.as_view(),

View File

@ -4,6 +4,7 @@ import logging
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import connection from django.db import connection
from django.db.models import Count, Q from django.db.models import Count, Q
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
@ -52,6 +53,14 @@ class HomeView(ListedExtensionsView):
def extension_version_download(request, type_slug, slug, version, filename): def extension_version_download(request, type_slug, slug, version, filename):
"""A backward-compatible shortcut for the method below.
No platform is specified, assuming only a single file for a given version.
"""
return extension_version_platform_download(request, type_slug, slug, version, None, filename)
def extension_version_platform_download(request, type_slug, slug, version, platform, filename):
"""Download an extension version and count downloads. """Download an extension version and count downloads.
The `filename` parameter is used to pass a file name ending with `.zip`. The `filename` parameter is used to pass a file name ending with `.zip`.
@ -61,7 +70,12 @@ def extension_version_download(request, type_slug, slug, version, filename):
extension_version = get_object_or_404(Version, extension__slug=slug, version=version) extension_version = get_object_or_404(Version, extension__slug=slug, version=version)
ExtensionDownload.create_from_request(request, object_id=extension_version.extension_id) ExtensionDownload.create_from_request(request, object_id=extension_version.extension_id)
VersionDownload.create_from_request(request, object_id=extension_version.pk) VersionDownload.create_from_request(request, object_id=extension_version.pk)
return redirect(extension_version.downloadable_signed_url + f'?filename={filename}') for file in extension_version.files.all():
platforms = file.platforms() or []
if platform is None or platform in platforms:
url = file.source.url
return redirect(f'{url}?filename={filename}')
raise Http404()
class SearchView(ListedExtensionsView): class SearchView(ListedExtensionsView):