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
5 changed files with 31 additions and 28 deletions
Showing only changes of commit e680ac610f - Show all commits

View File

@ -17,7 +17,7 @@ urlpatterns = [
# API
path('api/v1/extensions/', api.ExtensionsAPIView.as_view(), name='api'),
path(
'api/v1/extensions/<str:extension_id>/versions/new/',
'api/v1/extensions/<str:extension_id>/versions/upload/',
api.UploadExtensionVersionView.as_view(),
name='upload-extension-version',
),

View File

@ -12,7 +12,6 @@ from django.db import transaction
from common.compare import is_in_version_range, version
from extensions.models import Extension, Platform
from extensions.utils import clean_json_dictionary_from_optional_fields
from extensions.views.manage import NewVersionView
from files.forms import FileFormSkipAgreed
@ -201,11 +200,12 @@ class UploadExtensionVersionView(APIView):
status=status.HTTP_403_FORBIDDEN,
)
# Create a NewVersionView instance to handle file creation
new_version_view = NewVersionView(request=request, extension=extension)
# Pass the version_file to the form
form = new_version_view.get_form(FileFormSkipAgreed)
form = FileFormSkipAgreed(
allow_existing_version=True,
data={},
extension=extension,
request=request,
)
form.fields['source'].initial = version_file
if not form.is_valid():

View File

@ -301,7 +301,7 @@ class UploadVersionFileView(
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
kwargs['extension'] = self.extension
kwargs['version'] = self.version
kwargs['allow_existing_version'] = True
return kwargs
@transaction.atomic

View File

@ -74,9 +74,9 @@ class FileForm(forms.ModelForm):
)
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
self.allow_existing_version = kwargs.pop('allow_existing_version', None)
self.extension = kwargs.pop('extension', None)
self.version = kwargs.pop('version', None)
self.request = kwargs.pop('request')
for field in self.base_fields:
if field not in {'source', 'agreed_with_terms'}:
self.base_fields[field].required = False
@ -166,7 +166,11 @@ class FileForm(forms.ModelForm):
ManifestValidator(manifest)
ExtensionIDManifestValidator(manifest, self.extension)
ExtensionNameManifestValidator(manifest, self.extension)
ExtensionVersionManifestValidator(manifest, self.extension, self.version)
ExtensionVersionManifestValidator(
manifest,
self.extension,
self.allow_existing_version,
)
self.cleaned_data['metadata'] = manifest
self.cleaned_data['type'] = EXTENSION_SLUG_TYPES[manifest['type']]

View File

@ -167,36 +167,35 @@ class ExtensionNameManifestValidator:
class ExtensionVersionManifestValidator:
"""Validates version."""
def __init__(self, manifest, extension_to_be_updated, version_to_be_updated):
def __init__(self, manifest, extension_to_be_updated, allow_existing_version):
# If the extension wasn't created yet, any version is valid
if not extension_to_be_updated:
return
# check for duplicates in database
if not version_to_be_updated:
version = manifest.get('version')
if extension_to_be_updated.versions.filter(version=version).first():
manifest_version = manifest.get('version')
version = extension_to_be_updated.versions.filter(version=manifest_version).first()
if not version:
return
if not allow_existing_version:
raise ValidationError(
{
'source': [
f'The version {escape(version)} was already uploaded for this '
f'The version {escape(manifest_version)} was already uploaded for this '
f'extension ({extension_to_be_updated.name})'
],
},
code='invalid',
)
return
# check for platforms
if platforms := manifest.get('platforms', None):
available_platforms = version_to_be_updated.get_available_platforms()
available_platforms = version.get_available_platforms()
if diff := set(platforms) - available_platforms:
raise ValidationError(
{
'source': [
f'{version_to_be_updated} already has files for {", ".join(diff)}'
],
'source': [f'{version} already has files for {", ".join(diff)}'],
},
code='invalid',
)