-
Notifications
You must be signed in to change notification settings - Fork 0
Feature user login #6
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf81cfd
Split UserSerializer
MartynaAnnaGottschling bcebfdb
Add permissions class
MartynaAnnaGottschling 3381c5f
Add new views for users app
MartynaAnnaGottschling bca4069
Add automatic set user_type to Admin for django superuser
MartynaAnnaGottschling 7cc64fd
Change default BrowsableAPIRenderer to AdminRenderer in base.py
MartynaAnnaGottschling ceb503d
fixup! Add new views for users app
MartynaAnnaGottschling 5372713
fixup! Add new views for users app
MartynaAnnaGottschling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
| from rest_framework import permissions | ||
| from users.models import CustomUser | ||
|
|
||
|
|
||
| class IsAdminUser(permissions.BasePermission): | ||
| message = 'You are not allowed to enter - for admin only.' | ||
| def has_permission(self, request, view): | ||
| return request.user and request.user.is_authenticated and request.user.user_type == CustomUser.UserType.ADMIN.name | ||
|
|
||
|
|
||
| class IsOwnerOrAdmin(permissions.BasePermission): | ||
| message = "It's none of your business." | ||
| def has_permission(self, request, view): | ||
| return (view.get_object() == request.user) or (request.user and request.user.is_authenticated and request.user.user_type == CustomUser.UserType.ADMIN.name) | ||
| def has_object_permission(self, request, view, obj): | ||
| if request.user.is_authenticated: | ||
| return request.user.user_type == CustomUser.UserType.ADMIN.name or obj == request.user | ||
This file contains hidden or 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 hidden or 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 hidden or 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,10 +1,32 @@ | ||
| from django.conf.urls import url, include | ||
| from rest_framework.routers import DefaultRouter | ||
| from users import views | ||
| from django.conf.urls import url | ||
| from rest_framework import renderers | ||
| from rest_framework.urlpatterns import format_suffix_patterns | ||
| from users.views import UserViewSet | ||
| from users.views import UsersViewSet | ||
| from users.views import api_root | ||
|
TheCM marked this conversation as resolved.
|
||
|
|
||
| router = DefaultRouter() | ||
| router.register(r'users', views.UserViewSet) | ||
|
TheCM marked this conversation as resolved.
|
||
| users_list = UsersViewSet.as_view({ | ||
| 'get': 'list', | ||
| 'post': 'create' | ||
| }) | ||
|
|
||
| urlpatterns = [ | ||
| url(r'^', include(router.urls)) | ||
| ] | ||
| users_detail = UsersViewSet.as_view({ | ||
| 'get': 'retrieve', | ||
| 'put': 'update', | ||
| 'patch': 'partial_update', | ||
| 'delete': 'destroy' | ||
| }) | ||
|
|
||
| user_account_detail = UserViewSet.as_view({ | ||
| 'get': 'retrieve', | ||
| 'put': 'update', | ||
| 'patch': 'partial_update', | ||
| 'delete': 'destroy' | ||
| }) | ||
|
|
||
| urlpatterns = format_suffix_patterns([ | ||
| url(r'^$', api_root), | ||
| url(r'^users/$', users_list, name='users-list'), | ||
| url(r'^users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail'), | ||
| url(r'^account/(?P<pk>[0-9]+)/$', user_account_detail, name='user-account-detail'), | ||
| ]) | ||
This file contains hidden or 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,8 +1,73 @@ | ||
| from rest_framework import viewsets | ||
| from rest_framework import permissions | ||
| from rest_framework.decorators import api_view | ||
| from rest_framework.response import Response | ||
| from rest_framework.reverse import reverse | ||
|
|
||
| from users.common.fields import Action | ||
| from users.models import CustomUser | ||
| from users.permissions import IsAdminUser | ||
| from users.permissions import IsOwnerOrAdmin | ||
| from users.serializers import UserCreateSerializer | ||
| from users.serializers import UserDetailSerializer | ||
| from users.serializers import UserListSerializer | ||
| from users.serializers import UserSerializer | ||
| from rest_framework import viewsets | ||
| from users.serializers import UserUpdateSerializer | ||
|
TheCM marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @api_view() | ||
| def api_root(request, format=None): | ||
|
TheCM marked this conversation as resolved.
|
||
| if request.user.is_authenticated and request.user.user_type == CustomUser.UserType.ADMIN.name: | ||
| return Response({ | ||
|
TheCM marked this conversation as resolved.
|
||
| 'users': reverse( | ||
| 'users-list', | ||
| request=request, | ||
| format=format, | ||
| ), | ||
| 'account': reverse( | ||
| 'user-account-detail', | ||
| args=(request.user.pk,), | ||
| request=request, | ||
| format=format, | ||
| ), | ||
| }) | ||
| elif request.user.is_authenticated: | ||
| return Response({ | ||
| 'account': reverse( | ||
| 'user-account-detail', | ||
| args=(request.user.pk,), | ||
| request=request, | ||
| format=format, | ||
| ), | ||
| }) | ||
|
TheCM marked this conversation as resolved.
|
||
| else: | ||
| return Response({ | ||
| }) | ||
|
|
||
|
|
||
| class UsersViewSet(viewsets.ModelViewSet): | ||
|
TheCM marked this conversation as resolved.
|
||
| queryset = CustomUser.objects.all() | ||
| permission_classes = (IsAdminUser,) | ||
|
|
||
| def get_serializer_class(self): | ||
| if self.action == Action.LIST.value: | ||
| return UserListSerializer | ||
| if self.action == Action.RETRIEVE.value: | ||
| return UserDetailSerializer | ||
| if self.action == Action.CREATE.value: | ||
| return UserCreateSerializer | ||
| if self.action == Action.UPDATE.value: | ||
| return UserCreateSerializer | ||
| return UserSerializer | ||
|
|
||
|
|
||
| class UserViewSet(viewsets.ModelViewSet): | ||
| queryset = CustomUser.objects.all() | ||
| serializer_class = UserSerializer | ||
| permission_classes = (IsOwnerOrAdmin,) | ||
|
|
||
| def get_serializer_class(self): | ||
| if self.action == Action.RETRIEVE.value: | ||
| return UserDetailSerializer | ||
| if self.action == Action.UPDATE.value: | ||
| return UserUpdateSerializer | ||
| return UserSerializer | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.