Exemplo n.º 1
0
    def handle_upload(self, request, addon, version_string):
        if "upload" in request.FILES:
            filedata = request.FILES["upload"]
        else:
            raise forms.ValidationError(_(u'Missing "upload" key in multipart file data.'), status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(_(u"You cannot submit this type of add-on"), status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg["version"]

        if version_string and pkg["version"] != version_string:
            raise forms.ValidationError(_("Version does not match the manifest file."), status.HTTP_400_BAD_REQUEST)

        if addon is not None and addon.versions.filter(version=version_string).exists():
            raise forms.ValidationError(_("Version already exists."), status.HTTP_409_CONFLICT)

        dont_allow_no_guid = not addon and not pkg.get("guid", None) and not pkg.get("is_webextension", False)

        if dont_allow_no_guid:
            raise forms.ValidationError(
                _("Only WebExtensions are allowed to omit the GUID"), status.HTTP_400_BAD_REQUEST
            )

        if addon is None:
            addon = Addon.create_addon_from_upload_data(data=pkg, user=request.user, upload=filedata, is_listed=False)
            created = True
        else:
            created = False

        file_upload = handle_upload(filedata=filedata, user=request.user, addon=addon, submit=True)

        return file_upload, created
Exemplo n.º 2
0
    def create_installable_addon(self):
        activate('en-US')

        # using whatever add-on you already have should work imho, otherwise
        # fall back to a new one for test purposes
        addon = self.create_featured_addon_with_version_for_install()

        # the user the add-on gets created with
        user = UserProfile.objects.get(username='******')

        user, _ = UserProfile.objects.get_or_create(
            pk=settings.TASK_USER_ID,
            defaults={
                'email': '*****@*****.**',
                'username': '******'
            },
        )
        # Groups should have been created by loaddata initial.json at this
        # point, we need our user to be part of a group allowed to submit
        # extensions signed by Mozilla. Let's use Admins (pk=1) as a shortcut.
        GroupUser.objects.get_or_create(user=user,
                                        group=Group.objects.get(pk=1))

        # generate a proper uploaded file that simulates what django requires
        # as request.POST
        root = os.path.join(settings.ROOT, 'src/olympia/files/fixtures/files')
        file_to_upload = 'webextension_signed_already.xpi'
        file_path = os.path.join(root, file_to_upload)

        # make sure we are not using the file in the source-tree but a
        # temporary one to avoid the files get moved somewhere else and
        # deleted from source tree
        with copy_file_to_temp(file_path) as temporary_path:
            data = open(temporary_path, 'rb').read()
            filedata = SimpleUploadedFile(
                file_to_upload,
                data,
                content_type=mimetypes.guess_type(file_to_upload)[0],
            )

            # now, lets upload the file into the system
            from olympia.devhub.views import handle_upload

            request = RequestFactory().get('/')
            request.user = user

            upload = handle_upload(
                filedata=filedata,
                request=request,
                channel=amo.RELEASE_CHANNEL_LISTED,
                addon=addon,
            )
            upload.ip_address = '127.0.0.1'

            # And let's create a new version for that upload.
            create_version_for_upload(upload.addon, upload,
                                      amo.RELEASE_CHANNEL_LISTED)

            # Change status to public
            addon.update(status=amo.STATUS_APPROVED)
Exemplo n.º 3
0
    def handle_upload(self, request, addon, version_string):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            raise forms.ValidationError(
                _(u'Missing "upload" key in multipart file data.'),
                status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                _(u'You cannot submit this type of add-on'),
                status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg['version']

        if version_string and pkg['version'] != version_string:
            raise forms.ValidationError(
                _('Version does not match the manifest file.'),
                status.HTTP_400_BAD_REQUEST)

        if (addon is not None and
                addon.versions.filter(version=version_string).exists()):
            raise forms.ValidationError(
                _('Version already exists.'),
                status.HTTP_409_CONFLICT)

        dont_allow_no_guid = (
            not addon and not pkg.get('guid', None) and
            not pkg.get('is_webextension', False))

        if dont_allow_no_guid:
            raise forms.ValidationError(
                _('Only WebExtensions are allowed to omit the GUID'),
                status.HTTP_400_BAD_REQUEST)

        if addon is None:
            addon = Addon.create_addon_from_upload_data(
                data=pkg, user=request.user, upload=filedata, is_listed=False)
            created = True
            channel = amo.RELEASE_CHANNEL_UNLISTED
        else:
            created = False
            last_version = addon.find_latest_version_including_rejected()
            if last_version:
                channel = last_version.channel
            else:
                # TODO: we need to properly handle channels here and fail if
                # no previous version to guess with.  Also need to allow the
                # channel to be selected for versions.
                channel = (amo.RELEASE_CHANNEL_LISTED if addon.is_listed else
                           amo.RELEASE_CHANNEL_UNLISTED)

        file_upload = handle_upload(
            filedata=filedata, user=request.user, addon=addon, submit=True,
            channel=channel)

        return file_upload, created
Exemplo n.º 4
0
    def create_installable_addon(self):
        activate('en-US')

        # using whatever add-on you already have should work imho, otherwise
        # fall back to a new one for test purposes
        addon = self.create_featured_addon_with_version_for_install()

        # the user the add-on gets created with
        user = UserProfile.objects.get(username='******')

        user, _ = UserProfile.objects.get_or_create(pk=settings.TASK_USER_ID,
                                                    defaults={
                                                        'email':
                                                        '*****@*****.**',
                                                        'username': '******'
                                                    })

        # generate a proper uploaded file that simulates what django requires
        # as request.POST
        file_to_upload = 'webextension_signed_already.xpi'
        file_path = get_file(file_to_upload)

        # make sure we are not using the file in the source-tree but a
        # temporary one to avoid the files get moved somewhere else and
        # deleted from source tree
        with copy_file_to_temp(file_path) as temporary_path:
            data = open(temporary_path).read()
            filedata = SimpleUploadedFile(
                file_to_upload,
                data,
                content_type=mimetypes.guess_type(file_to_upload)[0])

            # now, lets upload the file into the system
            from olympia.devhub.views import handle_upload

            request = RequestFactory().get('/')
            request.user = user

            upload = handle_upload(
                filedata=filedata,
                request=request,
                channel=amo.RELEASE_CHANNEL_LISTED,
                addon=addon,
            )

            # And let's create a new version for that upload.
            create_version_for_upload(upload.addon, upload,
                                      amo.RELEASE_CHANNEL_LISTED)

            # Change status to public
            addon.update(status=amo.STATUS_PUBLIC)
Exemplo n.º 5
0
    def handle_upload(self, request, addon, version_string):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            raise forms.ValidationError(
                _(u'Missing "upload" key in multipart file data.'),
                status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                _(u'You cannot submit this type of add-on'),
                status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg['version']

        if version_string and pkg['version'] != version_string:
            raise forms.ValidationError(
                _('Version does not match the manifest file.'),
                status.HTTP_400_BAD_REQUEST)

        if (addon is not None
                and addon.versions.filter(version=version_string).exists()):
            raise forms.ValidationError(_('Version already exists.'),
                                        status.HTTP_409_CONFLICT)

        dont_allow_no_guid = (not addon and not pkg.get('guid', None)
                              and not pkg.get('is_webextension', False))

        if dont_allow_no_guid:
            raise forms.ValidationError(
                _('Only WebExtensions are allowed to omit the GUID'),
                status.HTTP_400_BAD_REQUEST)

        if addon is None:
            addon = Addon.create_addon_from_upload_data(data=pkg,
                                                        user=request.user,
                                                        upload=filedata,
                                                        is_listed=False)
            created = True
        else:
            created = False

        file_upload = handle_upload(filedata=filedata,
                                    user=request.user,
                                    addon=addon,
                                    submit=True)

        return file_upload, created
Exemplo n.º 6
0
    def handle_upload(self, request, addon, version_string):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            raise forms.ValidationError(
                _(u'Missing "upload" key in multipart file data.'),
                status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                _(u'You cannot submit this type of add-on'),
                status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg['version']

        if version_string and pkg['version'] != version_string:
            raise forms.ValidationError(
                _('Version does not match the manifest file.'),
                status.HTTP_400_BAD_REQUEST)

        if (addon is not None and
                addon.versions.filter(version=version_string).exists()):
            raise forms.ValidationError(
                _('Version already exists.'),
                status.HTTP_409_CONFLICT)

        dont_allow_no_guid = (
            not addon and not pkg.get('guid', None) and
            not pkg.get('is_webextension', False))

        if dont_allow_no_guid:
            raise forms.ValidationError(
                _('Only WebExtensions are allowed to omit the GUID'),
                status.HTTP_400_BAD_REQUEST)

        if addon is None:
            addon = Addon.create_addon_from_upload_data(
                data=pkg, user=request.user, upload=filedata, is_listed=False)
            created = True
        else:
            created = False

        no_prelim = waffle.flag_is_active(request, 'no-prelim-review')
        file_upload = handle_upload(
            filedata=filedata, user=request.user, addon=addon, submit=True,
            disallow_preliminary_review=no_prelim)

        return file_upload, created
Exemplo n.º 7
0
    def create_installable_addon(self):
        activate('en-US')

        # using whatever add-on you already have should work imho, otherwise
        # fall back to a new one for test purposes
        addon = self.create_featured_addon_with_version_for_install()

        # the user the add-on gets created with
        user = UserProfile.objects.get(username='******')

        user, _ = UserProfile.objects.get_or_create(
            pk=settings.TASK_USER_ID,
            defaults={'email': '*****@*****.**', 'username': '******'})

        # generate a proper uploaded file that simulates what django requires
        # as request.POST
        file_to_upload = 'webextension_signed_already.xpi'
        file_path = get_file(file_to_upload)

        # make sure we are not using the file in the source-tree but a
        # temporary one to avoid the files get moved somewhere else and
        # deleted from source tree
        with copy_file_to_temp(file_path) as temporary_path:
            data = open(temporary_path).read()
            filedata = SimpleUploadedFile(
                file_to_upload,
                data,
                content_type=mimetypes.guess_type(file_to_upload)[0])

            # now, lets upload the file into the system
            from olympia.devhub.views import handle_upload

            request = RequestFactory().get('/')
            request.user = user

            upload = handle_upload(
                filedata=filedata,
                request=request,
                channel=amo.RELEASE_CHANNEL_LISTED,
                addon=addon,
            )

            # And let's create a new version for that upload.
            create_version_for_upload(
                upload.addon, upload, amo.RELEASE_CHANNEL_LISTED)

            # Change status to public
            addon.update(status=amo.STATUS_PUBLIC)
Exemplo n.º 8
0
    def put(self, request, addon, version_string):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            return Response(
                {'error': _('Missing "upload" key in multipart file data.')},
                status=status.HTTP_400_BAD_REQUEST)

        try:
            # Parse the file to get and validate package data with the addon.
            pkg = parse_addon(filedata, addon)
            if not acl.submission_allowed(request.user, pkg):
                raise forms.ValidationError(
                    _(u'You cannot submit this type of add-on'))
        except forms.ValidationError as e:
            return Response({'error': e.message},
                            status=status.HTTP_400_BAD_REQUEST)
        if pkg['version'] != version_string:
            return Response(
                {'error': _('Version does not match the manifest file.')},
                status=status.HTTP_400_BAD_REQUEST)
        elif (addon is not None
              and addon.versions.filter(version=version_string).exists()):
            return Response({'error': _('Version already exists.')},
                            status=status.HTTP_409_CONFLICT)

        if addon is None:
            addon = Addon.create_addon_from_upload_data(data=pkg,
                                                        user=request.user,
                                                        is_listed=False)
            status_code = status.HTTP_201_CREATED
        else:
            status_code = status.HTTP_202_ACCEPTED

        file_upload = handle_upload(filedata=filedata,
                                    user=request.user,
                                    addon=addon,
                                    submit=True)

        return Response(FileUploadSerializer(file_upload).data,
                        status=status_code)
Exemplo n.º 9
0
    def put(self, request, addon, version_string):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            return Response(
                {'error': _('Missing "upload" key in multipart file data.')},
                status=status.HTTP_400_BAD_REQUEST)

        try:
            # Parse the file to get and validate package data with the addon.
            pkg = parse_addon(filedata, addon)
            if not acl.submission_allowed(request.user, pkg):
                raise forms.ValidationError(
                    _(u'You cannot submit this type of add-on'))
        except forms.ValidationError as e:
            return Response({'error': e.message},
                            status=status.HTTP_400_BAD_REQUEST)
        if pkg['version'] != version_string:
            return Response(
                {'error': _('Version does not match the manifest file.')},
                status=status.HTTP_400_BAD_REQUEST)
        elif (addon is not None and
                addon.versions.filter(version=version_string).exists()):
            return Response({'error': _('Version already exists.')},
                            status=status.HTTP_409_CONFLICT)

        if addon is None:
            addon = Addon.create_addon_from_upload_data(
                data=pkg, user=request.user, is_listed=False)
            status_code = status.HTTP_201_CREATED
        else:
            status_code = status.HTTP_202_ACCEPTED

        file_upload = handle_upload(
            filedata=filedata, user=request.user, addon=addon, submit=True)

        return Response(FileUploadSerializer(file_upload).data,
                        status=status_code)
Exemplo n.º 10
0
    def handle_upload(self, request, addon, version_string, guid=None):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            raise forms.ValidationError(
                ugettext(u'Missing "upload" key in multipart file data.'),
                status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                ugettext(u'You cannot submit this type of add-on'),
                status.HTTP_400_BAD_REQUEST)

        if not addon and not system_addon_submission_allowed(
                request.user, pkg):
            raise forms.ValidationError(
                ugettext(u'You cannot submit an add-on with a guid ending '
                         u'"@mozilla.org"'), status.HTTP_400_BAD_REQUEST)

        if not mozilla_signed_extension_submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                ugettext(u'You cannot submit a Mozilla Signed Extension'))

        if addon is not None and addon.status == amo.STATUS_DISABLED:
            msg = ugettext(
                'You cannot add versions to an addon that has status: %s.' %
                amo.STATUS_CHOICES_ADDON[amo.STATUS_DISABLED])
            raise forms.ValidationError(msg, status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg['version']

        if version_string and pkg['version'] != version_string:
            raise forms.ValidationError(
                ugettext('Version does not match the manifest file.'),
                status.HTTP_400_BAD_REQUEST)

        if (addon is not None
                and addon.versions.filter(version=version_string).exists()):
            raise forms.ValidationError(ugettext('Version already exists.'),
                                        status.HTTP_409_CONFLICT)

        package_guid = pkg.get('guid', None)

        dont_allow_no_guid = (not addon and not package_guid
                              and not pkg.get('is_webextension', False))

        if dont_allow_no_guid:
            raise forms.ValidationError(
                ugettext('Only WebExtensions are allowed to omit the GUID'),
                status.HTTP_400_BAD_REQUEST)

        if guid is not None and not addon and not package_guid:
            # No guid was present in the package, but one was provided in the
            # URL, so we take it instead of generating one ourselves. But
            # first, validate it properly.
            if len(guid) > 64:
                raise forms.ValidationError(
                    ugettext(
                        'Please specify your Add-on GUID in the manifest if it\'s '
                        'longer than 64 characters.'))

            if not amo.ADDON_GUID_PATTERN.match(guid):
                raise forms.ValidationError(ugettext('Invalid GUID in URL'),
                                            status.HTTP_400_BAD_REQUEST)
            pkg['guid'] = guid

        # channel will be ignored for new addons.
        if addon is None:
            channel = amo.RELEASE_CHANNEL_UNLISTED  # New is always unlisted.
            addon = Addon.create_addon_from_upload_data(data=pkg,
                                                        user=request.user,
                                                        upload=filedata,
                                                        channel=channel)
            created = True
        else:
            created = False
            channel_param = request.POST.get('channel')
            channel = amo.CHANNEL_CHOICES_LOOKUP.get(channel_param)
            if not channel:
                last_version = (addon.find_latest_version(None, exclude=()))
                if last_version:
                    channel = last_version.channel
                else:
                    channel = amo.RELEASE_CHANNEL_UNLISTED  # Treat as new.

            will_have_listed = channel == amo.RELEASE_CHANNEL_LISTED
            if not addon.has_complete_metadata(
                    has_listed_versions=will_have_listed):
                raise forms.ValidationError(
                    ugettext('You cannot add a listed version to this addon '
                             'via the API due to missing metadata. '
                             'Please submit via the website'),
                    status.HTTP_400_BAD_REQUEST)

        file_upload = handle_upload(filedata=filedata,
                                    user=request.user,
                                    addon=addon,
                                    submit=True,
                                    channel=channel)

        return file_upload, created
Exemplo n.º 11
0
    def handle_upload(self, request, addon, version_string):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            raise forms.ValidationError(
                _(u'Missing "upload" key in multipart file data.'),
                status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                _(u'You cannot submit this type of add-on'),
                status.HTTP_400_BAD_REQUEST)

        if addon is not None and addon.status == amo.STATUS_DISABLED:
            raise forms.ValidationError(
                _('You cannot add versions to an addon that has status: %s.' %
                  amo.STATUS_CHOICES_ADDON[amo.STATUS_DISABLED]),
                status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg['version']

        if version_string and pkg['version'] != version_string:
            raise forms.ValidationError(
                _('Version does not match the manifest file.'),
                status.HTTP_400_BAD_REQUEST)

        if (addon is not None
                and addon.versions.filter(version=version_string).exists()):
            raise forms.ValidationError(_('Version already exists.'),
                                        status.HTTP_409_CONFLICT)

        dont_allow_no_guid = (not addon and not pkg.get('guid', None)
                              and not pkg.get('is_webextension', False))

        if dont_allow_no_guid:
            raise forms.ValidationError(
                _('Only WebExtensions are allowed to omit the GUID'),
                status.HTTP_400_BAD_REQUEST)

        # channel will be ignored for new addons and while waffle
        # 'mixed-listed-unlisted' is disabled.
        if addon is None:
            channel = amo.RELEASE_CHANNEL_UNLISTED  # New is always unlisted.
            addon = Addon.create_addon_from_upload_data(data=pkg,
                                                        user=request.user,
                                                        upload=filedata,
                                                        channel=channel)
            created = True
        else:
            created = False
            if waffle.switch_is_active('mixed-listed-unlisted'):
                channel_param = request.POST.get('channel')
                channel = amo.CHANNEL_CHOICES_LOOKUP.get(channel_param)
                if not channel:
                    last_version = (
                        addon.find_latest_version_including_rejected())
                    if last_version:
                        channel = last_version.channel
                    else:
                        channel = amo.RELEASE_CHANNEL_UNLISTED  # Treat as new.
            else:
                # Don't allow channel choice until rest of AMO supports it.
                channel = (amo.RELEASE_CHANNEL_LISTED if addon.is_listed else
                           amo.RELEASE_CHANNEL_UNLISTED)

            will_have_listed = channel == amo.RELEASE_CHANNEL_LISTED
            if not addon.has_complete_metadata(
                    has_listed_versions=will_have_listed):
                raise forms.ValidationError(
                    _('You cannot add a listed version to this addon '
                      'via the API due to missing metadata. '
                      'Please submit via the website'),
                    status.HTTP_400_BAD_REQUEST)

        file_upload = handle_upload(filedata=filedata,
                                    user=request.user,
                                    addon=addon,
                                    submit=True,
                                    channel=channel)

        return file_upload, created
Exemplo n.º 12
0
    def handle_upload(self, request, addon, version_string, guid=None):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            raise forms.ValidationError(
                ugettext(u'Missing "upload" key in multipart file data.'),
                status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                ugettext(u'You cannot submit this type of add-on'),
                status.HTTP_400_BAD_REQUEST)

        if not addon and not system_addon_submission_allowed(
                request.user, pkg):
            raise forms.ValidationError(
                ugettext(u'You cannot submit an add-on with a guid ending '
                         u'"@mozilla.org"'),
                status.HTTP_400_BAD_REQUEST)
        if not mozilla_signed_extension_submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                ugettext(u'You cannot submit a Mozilla Signed Extension'))

        if addon is not None and addon.status == amo.STATUS_DISABLED:
            msg = ugettext(
                'You cannot add versions to an addon that has status: %s.'
                % amo.STATUS_CHOICES_ADDON[amo.STATUS_DISABLED])
            raise forms.ValidationError(msg, status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg['version']

        if version_string and pkg['version'] != version_string:
            raise forms.ValidationError(
                ugettext('Version does not match the manifest file.'),
                status.HTTP_400_BAD_REQUEST)

        if (addon is not None and
                addon.versions.filter(version=version_string).exists()):
            raise forms.ValidationError(
                ugettext('Version already exists.'),
                status.HTTP_409_CONFLICT)

        package_guid = pkg.get('guid', None)

        dont_allow_no_guid = (
            not addon and not package_guid and
            not pkg.get('is_webextension', False))

        if dont_allow_no_guid:
            raise forms.ValidationError(
                ugettext('Only WebExtensions are allowed to omit the GUID'),
                status.HTTP_400_BAD_REQUEST)

        if guid is not None and not addon and not package_guid:
            # No guid was present in the package, but one was provided in the
            # URL, so we take it instead of generating one ourselves. But
            # first, validate it properly.
            if not amo.ADDON_GUID_PATTERN.match(guid):
                raise forms.ValidationError(
                    ugettext('Invalid GUID in URL'),
                    status.HTTP_400_BAD_REQUEST)
            pkg['guid'] = guid

        # channel will be ignored for new addons.
        if addon is None:
            channel = amo.RELEASE_CHANNEL_UNLISTED  # New is always unlisted.
            addon = Addon.create_addon_from_upload_data(
                data=pkg, user=request.user, upload=filedata, channel=channel)
            created = True
        else:
            created = False
            channel_param = request.POST.get('channel')
            channel = amo.CHANNEL_CHOICES_LOOKUP.get(channel_param)
            if not channel:
                last_version = (
                    addon.find_latest_version(None, exclude=()))
                if last_version:
                    channel = last_version.channel
                else:
                    channel = amo.RELEASE_CHANNEL_UNLISTED  # Treat as new.

            will_have_listed = channel == amo.RELEASE_CHANNEL_LISTED
            if not addon.has_complete_metadata(
                    has_listed_versions=will_have_listed):
                raise forms.ValidationError(
                    ugettext('You cannot add a listed version to this addon '
                             'via the API due to missing metadata. '
                             'Please submit via the website'),
                    status.HTTP_400_BAD_REQUEST)

        file_upload = handle_upload(
            filedata=filedata, user=request.user, addon=addon, submit=True,
            channel=channel)

        return file_upload, created
Exemplo n.º 13
0
    def handle_upload(self, request, addon, version_string):
        if 'upload' in request.FILES:
            filedata = request.FILES['upload']
        else:
            raise forms.ValidationError(
                _(u'Missing "upload" key in multipart file data.'),
                status.HTTP_400_BAD_REQUEST)

        # Parse the file to get and validate package data with the addon.
        pkg = parse_addon(filedata, addon)
        if not acl.submission_allowed(request.user, pkg):
            raise forms.ValidationError(
                _(u'You cannot submit this type of add-on'),
                status.HTTP_400_BAD_REQUEST)

        version_string = version_string or pkg['version']

        if version_string and pkg['version'] != version_string:
            raise forms.ValidationError(
                _('Version does not match the manifest file.'),
                status.HTTP_400_BAD_REQUEST)

        if (addon is not None
                and addon.versions.filter(version=version_string).exists()):
            raise forms.ValidationError(_('Version already exists.'),
                                        status.HTTP_409_CONFLICT)

        dont_allow_no_guid = (not addon and not pkg.get('guid', None)
                              and not pkg.get('is_webextension', False))

        if dont_allow_no_guid:
            raise forms.ValidationError(
                _('Only WebExtensions are allowed to omit the GUID'),
                status.HTTP_400_BAD_REQUEST)

        if addon is None:
            addon = Addon.create_addon_from_upload_data(data=pkg,
                                                        user=request.user,
                                                        upload=filedata,
                                                        is_listed=False)
            created = True
            channel = amo.RELEASE_CHANNEL_UNLISTED
        else:
            created = False
            last_version = addon.find_latest_version_including_rejected()
            if last_version:
                channel = last_version.channel
            else:
                # TODO: we need to properly handle channels here and fail if
                # no previous version to guess with.  Also need to allow the
                # channel to be selected for versions.
                channel = (amo.RELEASE_CHANNEL_LISTED if addon.is_listed else
                           amo.RELEASE_CHANNEL_UNLISTED)

        file_upload = handle_upload(filedata=filedata,
                                    user=request.user,
                                    addon=addon,
                                    submit=True,
                                    channel=channel)

        return file_upload, created