-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: application save #3151
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
Merged
Merged
feat: application save #3151
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: maxkb | ||
@Author:虎 | ||
@file: common.py | ||
@date:2024/1/11 18:44 | ||
@desc: | ||
""" | ||
from rest_framework import serializers | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class ObjectField(serializers.Field): | ||
def __init__(self, model_type_list, **kwargs): | ||
self.model_type_list = model_type_list | ||
super().__init__(**kwargs) | ||
|
||
def to_internal_value(self, data): | ||
for model_type in self.model_type_list: | ||
if isinstance(data, model_type): | ||
return data | ||
self.fail(_('Message type error'), value=data) | ||
|
||
def to_representation(self, value): | ||
return value | ||
|
||
|
||
class InstanceField(serializers.Field): | ||
def __init__(self, model_type, **kwargs): | ||
self.model_type = model_type | ||
super().__init__(**kwargs) | ||
|
||
def to_internal_value(self, data): | ||
if not isinstance(data, self.model_type): | ||
self.fail(_('Message type error'), value=data) | ||
return data | ||
|
||
def to_representation(self, value): | ||
return value | ||
|
||
|
||
class FunctionField(serializers.Field): | ||
|
||
def to_internal_value(self, data): | ||
if not callable(data): | ||
self.fail(_('not a function'), value=data) | ||
return data | ||
|
||
def to_representation(self, value): | ||
return value | ||
|
||
|
||
class UploadedImageField(serializers.ImageField): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def to_representation(self, value): | ||
return value | ||
|
||
|
||
class UploadedFileField(serializers.FileField): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def to_representation(self, value): | ||
return value | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Based on the provided Python code (which appears to be part of Django REST Framework), here are some comments and areas for improvement:
Code Comments and Issues
File Encoding: The file starts with
@@
, which suggests it might be coming from a diff tool like Git or GitHub Actions. Ensure that this is intentional and does not affect the original code.Docstrings: All classes have doc strings but lack clear descriptions of what each method does. This can make the code harder to understand without additional documentation.
Field Classes:
image
forUploadedImageField
. They default to uploading files, so explicitly mentioning these fields helps clarity.Consistent Naming Convention: While naming conventions such as
self.model_type_list
are consistent,model_type
in certain contexts could benefit from being more descriptive (e.g.,allowed_model_types
).Optimizations
Error Messages: Enhancing error messages can improve user feedback when an invalid type is encountered.
Code Duplication: If multiple field types share similar functionality (like calling
.fail()
within.to_internal_value()
) consider extracting this logic into a base class if possible.Documentation: Adding more detailed documentation alongside inline comments can help maintainability and understanding, especially for developers who may encounter this codebase later.
Type Checking Beyond Conformity: Consider adding checks for attribute existence or other behaviors specific to your domain model if necessary beyond just checking the presence of a single
id
.By addressing these points, the code will become more robust, easier to maintain, and better documented, enhancing its reliability and usability across different development teams.