API: Upload new version of an extension #138
@ -197,44 +197,36 @@ class UploadExtensionVersionView(APIView):
|
|||||||
status=status.HTTP_403_FORBIDDEN,
|
status=status.HTTP_403_FORBIDDEN,
|
||||||
)
|
)
|
||||||
|
|
||||||
Oleg-Komarov marked this conversation as resolved
Outdated
|
|||||||
try:
|
# Create a NewVersionView instance to handle file creation
|
||||||
# Create a NewVersionView instance to handle file creation
|
new_version_view = NewVersionView(request=request, extension=extension)
|
||||||
new_version_view = NewVersionView(request=request, extension=extension)
|
|
||||||
|
|
||||||
# Pass the version_file to the form
|
# Pass the version_file to the form
|
||||||
form = new_version_view.get_form(FileFormSkipAgreed)
|
form = new_version_view.get_form(FileFormSkipAgreed)
|
||||||
form.fields['source'].initial = version_file
|
form.fields['source'].initial = version_file
|
||||||
|
|
||||||
if not form.is_valid():
|
if not form.is_valid():
|
||||||
return Response({'message': form.errors}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({'message': form.errors}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
# Create the file instance
|
# Create the file instance
|
||||||
file_instance = form.save(commit=False)
|
file_instance = form.save(commit=False)
|
||||||
file_instance.user = user
|
file_instance.user = user
|
||||||
dfelinto marked this conversation as resolved
Outdated
Oleg-Komarov
commented
this save and the update_or_create below should ideally be together in the same transaction, we should wrap all db writes in one this save and the update_or_create below should ideally be together in the same transaction, we should wrap all db writes in one `with transaction.atomic():`
|
|||||||
file_instance.save()
|
file_instance.save()
|
||||||
|
|
||||||
Oleg-Komarov
commented
btw, what's the use case for update here? why we are not just creating the object? btw, what's the use case for update here? why we are not just creating the object?
Dalai Felinto
commented
I guess I was just copying the same code we used on UploadFileView.form_valid. Which probably should be changed as well. Anyways, changing the PR to use create(), and will merge after testing. I guess I was just copying the same code we used on UploadFileView.form_valid. Which probably should be changed as well.
Anyways, changing the PR to use create(), and will merge after testing.
|
|||||||
# Create the version from the file
|
# Create the version from the file
|
||||||
version = Version.objects.update_or_create(
|
version = Version.objects.update_or_create(
|
||||||
extension=extension,
|
extension=extension,
|
||||||
file=file_instance,
|
file=file_instance,
|
||||||
release_notes=release_notes,
|
release_notes=release_notes,
|
||||||
**file_instance.parsed_version_fields,
|
**file_instance.parsed_version_fields,
|
||||||
)[0]
|
)[0]
|
||||||
|
|
||||||
return Response(
|
return Response(
|
||||||
{
|
{
|
||||||
'message': 'Extension version uploaded successfully!',
|
'message': 'Extension version uploaded successfully!',
|
||||||
'extension_id': extension_id,
|
'extension_id': extension_id,
|
||||||
'version_file': version_file.name,
|
'version_file': version_file.name,
|
||||||
'release_notes': version.release_notes,
|
'release_notes': version.release_notes,
|
||||||
dfelinto marked this conversation as resolved
Outdated
Oleg-Komarov
commented
in UI we don't expose our internal ids, do we need to return this one here? in UI we don't expose our internal ids, do we need to return this one here?
|
|||||||
},
|
},
|
||||||
status=status.HTTP_201_CREATED,
|
status=status.HTTP_201_CREATED,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
|
||||||
return Response(
|
|
||||||
{
|
|
||||||
'message': str(e),
|
|
||||||
},
|
|
||||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user
could we avoid this big try-except? what is the default drf error behavior?
we need to make sure that we are not leaking any error details in production - we will have the exception logged via sentry, and api users can't do anything useful with those details