-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: swagger show more args #2438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,9 @@ class FileSerializer(serializers.Serializer): | |
| def upload(self, with_valid=True): | ||
| if with_valid: | ||
| self.is_valid(raise_exception=True) | ||
| meta = self.data.get('meta', {'debug': True}) | ||
| meta = self.data.get('meta', None) | ||
| if not meta: | ||
| meta = {'debug': True} | ||
| file_id = meta.get('file_id', uuid.uuid1()) | ||
| file = File(id=file_id, file_name=self.data.get('file').name, meta=meta) | ||
| file.save(self.data.get('file').read()) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code looks generally correct, but there are a couple of improvements that can be made:
Here's an optimized version of the method: from django.core.files.storage import FileSystemStorage
class FileSerializer(serializers.Serializer):
def upload(self, with_valid=True):
if with_valid:
self.is_valid(raise_exception=True)
# Get metadata
meta = {}
if 'meta' in self.data:
meta.update(self.data['meta'])
# Set default debug setting
meta.setdefault('debug', True)
file_id = meta.get('file_id', uuid.uuid4().hex)
# Create a temporary file object
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(self.data.get('file').read())
try:
# Save the file using Django's FileField API
file_object = FileSystemStorage(location=str(settings.MEDIA_ROOT)).save(file_name=temp_file.name.split('/')[-1], content=temp_file)
# Create or update the existing File entry
File.objects.update_or_create(
id=file_id,
defaults={
'file_name': os.path.basename(temp_file.name),
'meta': meta,
}
)
finally:
# Clean up the temporary file after saving it
temp_file.close()
os.unlink(temp_file.name) # Remove the physical file
Key Changes:
This approach avoids direct database interaction with binary data, making it more efficient both from performance and resource management perspectives. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code has several optimizations and corrections suggested to enhance its structure, performance, and adherence to best practices:
Optimizations:
Imports:
from django.utils.translation import gettext_lazy as _.Method Separation:
UploadFileview outside of the main API class.Docstring Corrections:
class DeleteFilesView(ListAPIView)should beDeleteFilesView).Parameter Formatting:
Comments:
Code Readability:
Potential Issues Suggested for Improvement:
Permissions Check:
@has_permissionsdecorator is correctly linked to the appropriate endpoint or if it's being used in the correct place.Error Handling:
uploadmethod for missing or invalid data.Logging / Debugging:
Validation:
Revised Version with Suggestions Implemented:
This revised version separates concerns logically and adds basic input validation to handle file uploads appropriately. Make sure to adjust function arguments based on your specific model structures and requirements.