-
Notifications
You must be signed in to change notification settings - Fork 1
Add a new instance action: Resize #29
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ class InstanceSerializer(serializers.ModelSerializer): | |
activity = serializers.CharField(read_only=True, source='esh_activity') | ||
fault = serializers.ReadOnlyField(source='esh_fault') | ||
size_alias = serializers.CharField(read_only=True, source='esh_size') | ||
size = serializers.SerializerMethodField() | ||
machine_alias = serializers.CharField(read_only=True, source='esh_source') | ||
machine_name = serializers.CharField(read_only=True, | ||
source='esh_source_name') | ||
|
@@ -51,6 +52,14 @@ def __init__(self, *args, **kwargs): | |
self.request_user = user | ||
super(InstanceSerializer, self).__init__(*args, **kwargs) | ||
|
||
def get_size(self, obj): | ||
from api.v2.serializers.summaries import ( | ||
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. Should this be imported at the top? 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. @kamfonik hmm, I will ask you on this then. Since this is only used by this function. Sometimes I see people import them within only the functions. But we could also put them on top. What do you think, Laura? 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. PEP8 recommends imports only at the top of the file, but I can see that there are already function-level imports elsewhere in this code. From what I have read there may be a slight performance cost to importing inside the function, but probably only on the first call to the function (after that the module should be cached). In discussions about this on StackOverflow there were a few notes that some people do import within the function while developing, until they have some stable code and know what they need and where it is used, then they move all the imports to the top to comply with PEP8. Mostly my thought is that if we are going to violate PEP8 there should be a reason, although the reason could be simple like "consistency with existing code" etc. 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. @kamfonik Thanks for researching on this. I could clean up the functions that I have modified/created. As for other elsewhere in code base. We either need to have a new PR for cleaning all the code, or keep ignoring them, but make sure we do it properly for the furture PRs. 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. @xuhang57 sure, cleaning up is probably not urgent, but deciding now what you want for new code will save a lot of work later. 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. Yes it should be moved to the top |
||
SizeSummarySerializer | ||
) | ||
size = obj.get_size() | ||
serializer = SizeSummarySerializer(size, context=self.context) | ||
return serializer.data | ||
|
||
class Meta: | ||
model = Instance | ||
exclude = ('source', 'provider_alias', | ||
|
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.
I can't find where this new function comes into play. Does it get called in some way by your frontend code?
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.
@kamfonik Yes, so
get_size
gets called when we havesize = SerializerMethodField
(Line 26) it will calldef get_<field-name>