forked from getpatchwork/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
66 lines (49 loc) · 2.15 KB
/
Copy pathuser.py
File metadata and controls
66 lines (49 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Patchwork - automated patch tracking system
# Copyright (C) 2016 Linaro Corporation
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from django.contrib.auth.models import User
from rest_framework.generics import ListAPIView
from rest_framework.generics import RetrieveUpdateAPIView
from rest_framework import permissions
from rest_framework.serializers import HyperlinkedModelSerializer
class IsOwnerOrReadOnly(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS:
return True
return obj == request.user
class UserSerializer(HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'url', 'username', 'first_name', 'last_name', 'email')
# we don't allow updating of emails via the API, as we need to
# validate that the User actually owns said email first
read_only_fields = ('username', 'email')
extra_kwargs = {
'url': {'view_name': 'api-user-detail'},
}
class UserMixin(object):
queryset = User.objects.all()
permission_classes = (permissions.IsAuthenticated, IsOwnerOrReadOnly)
serializer_class = UserSerializer
class UserList(UserMixin, ListAPIView):
"""List users."""
search_fields = ('username', 'first_name', 'last_name', 'email')
ordering_fields = ('id', 'username', 'email')
class UserDetail(UserMixin, RetrieveUpdateAPIView):
"""Show a user."""
pass