-
Notifications
You must be signed in to change notification settings - Fork 693
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
Created a solution #668
base: master
Are you sure you want to change the base?
Created a solution #668
Changes from all commits
ab962a3
e6d53db
6ad869d
f82da1c
ac89995
a9fcdfa
c059fb2
1c6dd63
4989fa3
0453d63
11c6f51
1d331a2
321444d
f662b9a
3886edd
12fcaf6
c439554
45dd28c
ab1ace8
07568a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,7 +125,7 @@ | |
|
||
USE_I18N = True | ||
|
||
USE_TZ = False | ||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
urlpatterns = [ | ||
path("admin/", admin.site.urls), | ||
path("api/cinema/", include("cinema.urls", namespace="cinema")), | ||
path("api/user/", include("user.urls", namespace="user")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
path("__debug__/", include("debug_toolbar.urls")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Debug Toolbar should be used only in development. Ensure that it is disabled in production by checking the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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 | ||
) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
# 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 |
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, LoginUserView, ManageUserView | ||
|
||
app_name = "user" | ||
|
||
urlpatterns = [ | ||
path("register/", CreateUserView.as_view(), name="create"), | ||
path("login/", LoginUserView.as_view(), name="login"), | ||
path("me/", ManageUserView.as_view(), name="manage") | ||
] |
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.permissions import IsAuthenticated | ||
from rest_framework.settings import api_settings | ||
from rest_framework.authtoken.views import ObtainAuthToken | ||
|
||
from user.serializers import UserSerializer | ||
|
||
|
||
class CreateUserView(generics.CreateAPIView): | ||
serializer_class = UserSerializer | ||
|
||
|
||
class LoginUserView(ObtainAuthToken): | ||
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES | ||
|
||
|
||
class ManageUserView(generics.RetrieveUpdateAPIView): | ||
serializer_class = UserSerializer | ||
authentication_classes = (TokenAuthentication,) | ||
permission_classes = (IsAuthenticated,) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
def get_object(self): | ||
return self.request.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.
Ensure that the
namespace
attribute in theinclude
function matches the namespaces defined in the respectiveurls.py
files of thecinema
anduser
apps. This is important for URL reversing and namespacing.