-
-
Notifications
You must be signed in to change notification settings - Fork 218
Add the possibility to configure a serializer for expiry in LoginView #179
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
Conversation
ping @johnraz what do you think? |
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.
Sorry for the delay @loicgasser and thanks for the submission, here are my thoughts.
context=self.get_context() | ||
).data) | ||
else: | ||
data['expiry'] = instance.expiry |
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.
First on a purely technical standpoint:
I would try to get rid of this if / else.
get_expiry_serializer_class()
should return the default serializer and in this context, ExpirySerializer
shouldn't be None
, ever.
Something like:
expiry_serializer = ExpirySerializer({'expiry': instance.expiry}, context=self.get_context())
data.update(expiry_serializer.data)
Now on a more architectural standpoint:
I'm wondering if the Serializer isn't a bit too much and introduce indirection and unforeseen use case that would make maintaining backward compatibility harder.
eg: due to the data.update
, one could use the ExpirySerializer to add more field to the payload.
Could we reach something simpler with:
data = {
'expiry': DateTimeField(format=knox_settings.EXPIRY_FORMAT).to_representation(instance.expiry)
'token': token,
}
and make knox_settings.EXPIRY_FORMAT
default to api_settings.DATE_FORMAT
?
No problem, thank you for taking the time to review.
Yes I think it's better not to either. I didn't know if you wanted to preserve the current behavior where the
Yes I was actually wondering the same, but defining only a field seemed a bit unnatural. I would propose On an additional note, why not take the same approach as for |
I'd argue that "expiry" should always be a datetime field, I don't quite see the need for having another field type - do you see a use case where you'd want something else than a datetime ? Edit:
I don't think the same thing applies, expiry is linked to the token, whereas the user object is really just needed for some specific flows. |
Also, @loicgasser give a look at #171 (comment). |
Yes, in my case I am returning an integer. I return all dates as UNIX timestamps. If you use
yes, both approaches are not mutually exclusive though. As a user of Let me know what is your decision on that and I'll adapt the code accordingly. |
I understand the need but I'm not so sure it's a common use case, it's really hard to tell.
I totally agree
This would indeed be very flexible but I'm not sure we could instantiate a field in the settings like that:
If the above works, let's go for it, if not let's fall back to the format string. I'd also like input from other collaborators of the project (@belugame , @sphrak , @James1345 ) |
@johnraz I like your idea of having it solved with the same concept as e.g. Django's SHORT_DATETIME_FORMAT. Keep it small and simple. 👍 PS: If you use moment.js on the frontend then you should already be reasonably flexible with the date format given by the backend, I would think. |
Here is my proposal. Per default the new setting Additionally, we implement @johnraz proposal where we separate the formatting of the response by adding |
@loicgasser let's do that and let's open a new PR for #171 (I'll add some more on the conversation there because I thought about some changes). edit: |
Fixes #178
This is what I had in mind.
Serializer
class for exipiry in knox with a simpleDateTimeField
, which will take the default datetime formatting of the current project.ExpirySerializer
in the settings.If you agree with these changes, I'll go ahead and adapt the doc.