-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93c12eb
commit 7d10089
Showing
8 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from rest_framework.permissions import BasePermission, SAFE_METHODS | ||
|
||
|
||
class IsAdminOrIfAuthenticatedReadOnly(BasePermission): | ||
def has_permission(self, request, view): | ||
return bool( | ||
( | ||
request.method in SAFE_METHODS | ||
and request.user | ||
and request.user.is_authenticated | ||
) | ||
or (request.user and request.user.is_staff) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,24 @@ | ||
# write your code here | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import serializers | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = get_user_model() | ||
fields = ("id", "username", "email", "password", "is_staff") | ||
read_only_fields = ("id", "is_staff") | ||
extra_kwargs = {"password": {"write_only": True, "min_length": 5}} | ||
|
||
def create(self, validated_data): | ||
return get_user_model().objects.create_user(**validated_data) | ||
|
||
def update(self, instance, validated_data): | ||
password = validated_data.pop("password", None) | ||
user = super().update(instance, validated_data) | ||
|
||
if password: | ||
user.set_password(password) | ||
user.save() | ||
|
||
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
# write your code here | ||
from django.urls import path | ||
|
||
from user.views import CreateUserView, CreateTokenView, ManageUserView | ||
|
||
urlpatterns = [ | ||
path("register/", CreateUserView.as_view(), name="create"), | ||
path("login/", CreateTokenView.as_view(), name="login"), | ||
path("me/", ManageUserView.as_view(), name="manage") | ||
] | ||
|
||
app_name = "user" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,24 @@ | ||
# write your code here | ||
from rest_framework import generics | ||
from rest_framework.authentication import TokenAuthentication | ||
from rest_framework.authtoken.views import ObtainAuthToken | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.settings import api_settings | ||
|
||
from user.serializers import UserSerializer | ||
|
||
|
||
class CreateUserView(generics.CreateAPIView): | ||
serializer_class = UserSerializer | ||
|
||
|
||
class CreateTokenView(ObtainAuthToken): | ||
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES | ||
|
||
|
||
class ManageUserView(generics.RetrieveUpdateAPIView): | ||
serializer_class = UserSerializer | ||
authentication_classes = (TokenAuthentication,) | ||
permission_classes = (IsAuthenticated,) | ||
|
||
def get_object(self): | ||
return self.request.user |