Expose the version used at creation/update in add-on API (#20367)

* Expose the version used at creation/update in add-on API
This commit is contained in:
Mathieu Pillard 2023-02-20 13:54:34 +01:00 коммит произвёл GitHub
Родитель 404f468a4b
Коммит 918288b6c9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 42 добавлений и 11 удалений

Просмотреть файл

@ -208,6 +208,7 @@ This endpoint allows you to fetch a specific add-on by id, slug or guid.
:>json array tags: List containing the tag names set on the add-on.
:>json string type: The :ref:`add-on type <addon-detail-type>`.
:>json string url: The (absolute) add-on detail URL.
:>json object version: For create or update requests that included a :ref:`version <version-create-request>` only. Object holding the :ref:`version <version-detail-object>` that was submitted.
:>json string versions_url: The URL to the version history page for the add-on.
:>json int weekly_downloads: The number of downloads for the add-on in the last week. Not present for lightweight themes.
@ -324,6 +325,10 @@ is compatible with.
:<json array tags: List containing the tag names to set on the add-on - see :ref:`available tags <tag-list>`.
:<json object version: Object containing the :ref:`version <version-create-request>` to create this addon with.
**Response:**
See :ref:`add-on <addon-detail-object>`
----
Edit
----

Просмотреть файл

@ -942,7 +942,7 @@ class AddonSerializer(AMOModelSerializer):
)
url = serializers.SerializerMethodField()
version = DeveloperVersionSerializer(
write_only=True,
write_only=True, # Overridden in create/update to expose the version submitted.
validators=(
PreventPartialUpdateValidator(),
MatchingGuidValidator(),
@ -1196,11 +1196,12 @@ class AddonSerializer(AMOModelSerializer):
# Add categories
addon.set_categories(validated_data.get('all_categories', []))
addon.set_tag_list(validated_data.get('tag_list', []))
self.fields['version'].create(
addon.version = self.fields['version'].create(
{**validated_data.get('version', {}), 'addon': addon}
)
# When creating, always return the version we just created in the
# representation. It uses <instance>.version.
self.fields['version'].write_only = False
addon.update_status()
return addon
@ -1228,9 +1229,12 @@ class AddonSerializer(AMOModelSerializer):
del instance.tag_list # super.update will have set it.
instance.set_tag_list(validated_data['tag_list'])
if 'version' in validated_data:
self.fields['version'].create(
instance.version = self.fields['version'].create(
{**validated_data.get('version', {}), 'addon': instance}
)
# When updating, always return the version we just created in the
# representation if there was one.
self.fields['version'].write_only = False
self.log(instance, validated_data)
return instance

Просмотреть файл

@ -7,7 +7,7 @@ import tarfile
import tempfile
import zipfile
from collections import Counter
from collections import Counter, OrderedDict
from datetime import datetime
from unittest import mock
from unittest.mock import patch
@ -887,10 +887,17 @@ class TestAddonViewSetCreate(UploadMixin, AddonViewSetCreateUpdateMixin, TestCas
request = APIRequestFactory().get('/')
request.version = 'v5'
request.user = self.user
assert data == DeveloperAddonSerializer(
expected_version = addon.find_latest_version(channel=None)
assert expected_version.channel == amo.CHANNEL_UNLISTED
expected_data = DeveloperAddonSerializer(
context={'request': request}
).to_representation(addon)
assert addon.find_latest_version(channel=None).channel == amo.CHANNEL_UNLISTED
# The additional `version` property contains the version we just
# uploaded.
expected_data['version'] = DeveloperVersionSerializer(
context={'request': request}
).to_representation(expected_version)
assert dict(data) == dict(expected_data)
assert (
ActivityLog.objects.for_addons(addon)
.filter(action=amo.LOG.CREATE_ADDON.id)
@ -919,10 +926,18 @@ class TestAddonViewSetCreate(UploadMixin, AddonViewSetCreateUpdateMixin, TestCas
request = APIRequestFactory().get('/')
request.version = 'v5'
request.user = self.user
assert data == DeveloperAddonSerializer(
expected_version = addon.find_latest_version(channel=None)
assert expected_version.channel == amo.CHANNEL_LISTED
expected_data = DeveloperAddonSerializer(
context={'request': request}
).to_representation(addon)
assert addon.current_version.channel == amo.CHANNEL_LISTED
expected_data = OrderedDict(sorted(expected_data.items()))
# The additional `version` property contains the version we just
# uploaded.
expected_data['version'] = DeveloperVersionSerializer(
context={'request': request}
).to_representation(expected_version)
assert dict(data) == dict(expected_data)
assert (
ActivityLog.objects.for_addons(addon)
.filter(action=amo.LOG.CREATE_ADDON.id)
@ -1665,9 +1680,16 @@ class TestAddonViewSetUpdate(AddonViewSetCreateUpdateMixin, TestCase):
request = APIRequestFactory().get('/')
request.version = 'v5'
request.user = self.user
assert data == DeveloperAddonSerializer(
expected_data = DeveloperAddonSerializer(
context={'request': request}
).to_representation(self.addon)
if 'version' in getattr(self, 'minimal_data', {}):
expected_version = self.addon.find_latest_version(channel=None)
expected_data['version'] = DeveloperVersionSerializer(
context={'request': request}
).to_representation(expected_version)
assert dict(data) == dict(expected_data)
assert self.addon.summary == 'summary update!'
alog = ActivityLog.objects.exclude(
action__in=(