-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Add Django manage command to create a DRF user Token #5188
Conversation
help = 'Create DRF Token for a given user' | ||
|
||
def create_user_token(self, username): | ||
user = User.objects.get(username=username) |
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 would like to handle the situation when username
doesn't exist. Should I let the code raise an exception here and try/except it in the handle() method (maybe displaying the error "ERROR: given username does not exists"
) or is there a better way to do this?
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.
Catching it in handle
seems reasonable enough.
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.
We should probably use get_user_model()
here. I'm also thinking this should make less assumptions regarding querying by username
. Perhaps we can leverage the user model's USERNAME_FIELD
together with get_by_natural_key()
. Similar to how django.contrib.auth.backends.ModelBackend
does it.
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.
@jpadilla I will take this advice into consideration, thanks! I'm learning a lot of things :)
|
||
def create_user_token(self, username): | ||
user = User.objects.get(username=username) | ||
token = Token.objects.get_or_create(user=user) |
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 also would like to handle the situation where the user want to regenerate the Token. Should this be handled as a separate manage.py command, as an additional argument or should the command be interactive and inform the user that the Token already exist and to confirm Y/N they want to create a new Token?
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.
Not sure. As a command line switch sounds okay on first pass. (?)
try: | ||
token = self.create_user_token(username) | ||
except User.DoesNotExist: | ||
print('Cannot create the Token: user {0} does not exist'.format( |
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.
Not entirely sure if it's relevant, but management commands typically use self.stdout.write()
instead of print()
. eg, https://github.com/django/django/blob/1.11/django/core/management/commands/migrate.py#L141
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.
@rpkilby yep! You are totally right and it's something I just spotted a couple of days ago and I was going to amend it. The officia documentation also suggest to use self.stdout because it's even easier to test. I just haven't had any time yet to complete it. I will try to finish during the weekend. Thanks
Is there any other change I need to do before it can be merged? Thank you so much |
Good stuff, thanks @andreagrandi! |
Woooooow thanks! :) I'm so happy to see this merged! |
p.s: maybe we should wait for this |
Documentation doesn't update live. |
Prepping the PR is a good plan. |
Sorry if it took me so long (I've been a little bit busy lately), here is the PR that adds documentation about this command #5328 |
Description
This pull request add the Django manage command called drf_create_token as documented in the issue #5173