Task 6
Task
- Remove any apps/views that we created from before that have to do with authenticating users
- Create an app
users
- In the
users
app, extend Django's user model by inheriting fromAbstractUser
to include an optionalbio
CharField
with a max length of 256 characters- On django admin, this field should be displayed as a
TextArea
- You might want to delete all the migration files you have and leverage
reset_db
shell command to extend the user model. Why? - May I delete the migration files when working on a project? NEVER delete the migrations files without consulting your team first, migrations files serve as version control for your models, sometimes you might want to set a model's state to a snapshot from that past, by deleting migration files you lose access to that. In addition to that, not all migration files can be automatically generated, there are manually created migration files like data migration files, if you delete data migration files you're deleting code from the codebase.
- Why are we allowed to delete migration files now then? This purely for the purpose of this task.
- On django admin, this field should be displayed as a
- Create an app
authentication
- In the
authentication
app, support aPOST authentication/register/
endpoint that creates users.- Think about the suitable permission class(es) for this endpoint.
- This endpoint must accept the following fields formatted in JSON:
- username
- password1
- password2 (confirmation of
password1
)
- Perform proper validation on all fields including letting the user know if their password isn't strong enough or if password1 doesn't match password2.
- Make sure passwords are being hashed and email domains are stored in lowercase. (hint: use
create_user
)
- Create a
POST authentication/login/
that logs in users using their username and password and returns aKnoxToken
and the user's data in a nested object.
{
"token": <knox_token>
"user": {
"id": 1,
"username": "my_user"
"email": "email@email.com"
"bio": "my sample bio"
}
- Create a
POST authentication/logout/
endpoint that logs the user out from the app by invalidating the knox token - In the
users
app, create a user detail endpoint/users/<pk>
that supports the following requests:-
GET
returns the user data matching the givenpk
, namely, it should return the user'sid
,username
,email
, andbio
.- return 404 status code if the user with the given
pk
does not exist
- return 404 status code if the user with the given
- Support updating the
bio
,username
, andemail
fields via the following requests:-
PUT
This is exactly the same as when creating a user except that an ID of an existing user is provided in the URL, and that the request will overwrite the user's data with that given ID. -
PATCH
This is exactly the same as when updating a user except none of the fields are required, and that only fields given a value will be updated. (hint: seepartial_update
in serializers) - Allow update requests if the user making the request is the user in the
<pk>
of the url.
-
-
- Add
TokenAuthentication
to the default authentication classes