Skip to content
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

ad hoc refactor in utilities/responses.py #9

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

0xNima
Copy link

@0xNima 0xNima commented May 6, 2021

No description provided.



class SuccessResponse(BaseResponse):
def __init__(self, data=None, message=None, show_type=settings.MESSAGE_SHOW_TYPE['TOAST'], status=200, **kwargs):
self.data = data
def __init__(self, message, data=None, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message could be None

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I get it wrong with ErrorResponse which message should be positional.
Also status should be checked to it's not be None.

@staticmethod
def __make_response(status_code: int, extra: dict):
return {
'success': True if status_code // 100 == 2 else False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a clean code, other programmers my have difficulty to read this section

Copy link
Author

@0xNima 0xNima May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which part? __make_response method or only line 55?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have a condition like 200 <= status_code < 300 it would be easier to understand the point of condition

Copy link
Author

@0xNima 0xNima May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about this:

from rest_framework import status

...

@staticmethod
def __make_response(status_code: int, extra: dict):
        return {
            'success': status.is_success(status_code),
            'code': status_code,
            'current_time': round(time.time()),
            'show_type': settings.MESSAGE_SHOW_TYPE['NONE'],
            **extra
        }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now we have a good approach

super().__init__(**kwargs)
self.message = message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened to message?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it could be None, it's passed through kwargs and initialized in BaseResponse. Albeit maybe it's not good idea since the message field is initialized in each child class differently and inheritance is meaningless. When I moved the message initialization to BaseResponse, I thought that it could be None in each child class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't see that, I need to test it manually because we have no test case, I will reply to you asap.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not our expected result :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please test your code before any other commit or pull request

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I can't load test data to my DB. I'll try again.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants