drf-appkit 0.5.3__tar.gz → 0.9.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {drf-appkit-0.5.3/drf_appkit.egg-info → drf-appkit-0.9.1}/PKG-INFO +1 -5
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/filters.py +16 -1
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/mixins.py +35 -14
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/serializers.py +61 -17
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/tests.py +67 -47
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/views.py +19 -24
- drf-appkit-0.9.1/appkit/auth/email.py +46 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/debug.py +12 -4
- drf-appkit-0.9.1/appkit/drf/authentication.py +29 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/fields.py +6 -12
- drf-appkit-0.9.1/appkit/email.py +45 -0
- drf-appkit-0.9.1/appkit/migrations/0006_sitealias.py +26 -0
- drf-appkit-0.9.1/appkit/migrations/0007_auto_20211021_1901.py +18 -0
- drf-appkit-0.9.1/appkit/migrations/0008_update_autofields.py +46 -0
- drf-appkit-0.9.1/appkit/migrations/0009_alter_arrangement_unique_together_and_more.py +41 -0
- drf-appkit-0.9.1/appkit/migrations/0010_arrangement_name.py +19 -0
- drf-appkit-0.9.1/appkit/migrations/0011_alter_sitealias_options.py +17 -0
- drf-appkit-0.9.1/appkit/migrations/0012_alter_sitealias_site.py +20 -0
- drf-appkit-0.9.1/appkit/migrations/0013_usergroup.py +35 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/models.py +70 -46
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/settings.py +2 -1
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/shortcuts.py +21 -4
- {drf-appkit-0.5.3 → drf-appkit-0.9.1/drf_appkit.egg-info}/PKG-INFO +1 -5
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/drf_appkit.egg-info/SOURCES.txt +9 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/setup.cfg +1 -1
- drf-appkit-0.5.3/appkit/auth/email.py +0 -36
- drf-appkit-0.5.3/appkit/email.py +0 -50
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/LICENSE +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/README.md +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/__init__.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/__init__.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/metadata.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/pagination.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/relations.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/api/renderers.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/auth/__init__.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/auth/backends.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/auth/decorators.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/auth/permissions.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/__init__.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/backends.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/exceptions.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/filters.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/metadata.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/mixins.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/permissions.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/drf/serializers.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/managers.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/migrations/0001_initial.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/migrations/0002_arrangement_tags.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/migrations/0003_auto_20210221_0852.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/migrations/0004_auto_20210309_0655.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/migrations/0005_auto_20210324_1046.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/migrations/__init__.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/util.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/appkit/views.py +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/drf_appkit.egg-info/dependency_links.txt +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/drf_appkit.egg-info/top_level.txt +0 -0
- {drf-appkit-0.5.3 → drf-appkit-0.9.1}/setup.py +0 -0
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: drf-appkit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.9.1
|
|
4
4
|
Summary: A Django app providing generic CRUD functionality
|
|
5
|
-
Home-page: UNKNOWN
|
|
6
5
|
Author: Allan Hart
|
|
7
6
|
License: BSD
|
|
8
|
-
Platform: UNKNOWN
|
|
9
7
|
Classifier: Environment :: Web Environment
|
|
10
8
|
Classifier: Framework :: Django
|
|
11
9
|
Classifier: Framework :: Django :: 3.1
|
|
@@ -23,5 +21,3 @@ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
|
23
21
|
License-File: LICENSE
|
|
24
22
|
|
|
25
23
|
# drf-appkit
|
|
26
|
-
|
|
27
|
-
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from django.contrib.auth import get_user_model
|
|
2
2
|
from django.contrib.auth.models import Group
|
|
3
|
+
from django.contrib.sites.models import Site
|
|
3
4
|
|
|
4
5
|
import rest_framework_filters as filters
|
|
5
6
|
|
|
@@ -30,13 +31,15 @@ def choices_for_region():
|
|
|
30
31
|
|
|
31
32
|
class DocumentFilter(FilterSet):
|
|
32
33
|
date_created = filters.DateFromToRangeFilter()
|
|
34
|
+
date_modified = filters.DateFromToRangeFilter()
|
|
33
35
|
|
|
34
36
|
|
|
35
37
|
class GroupFilter(FilterSet):
|
|
36
38
|
class Meta:
|
|
37
39
|
model = Group
|
|
38
40
|
fields = {
|
|
39
|
-
'
|
|
41
|
+
'id': ('exact', 'in',),
|
|
42
|
+
'name': ('exact', 'in',),
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
|
|
@@ -49,6 +52,7 @@ class NoteFilter(FilterSet):
|
|
|
49
52
|
'text': ('icontains',),
|
|
50
53
|
}
|
|
51
54
|
|
|
55
|
+
|
|
52
56
|
class RegionFilter(FilterSet):
|
|
53
57
|
class Meta:
|
|
54
58
|
model = Region
|
|
@@ -59,6 +63,16 @@ class RegionFilter(FilterSet):
|
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
|
|
66
|
+
class SiteFilter(FilterSet):
|
|
67
|
+
class Meta:
|
|
68
|
+
model = Site
|
|
69
|
+
fields = {
|
|
70
|
+
'id': ('exact', 'in',),
|
|
71
|
+
'name': ('exact',),
|
|
72
|
+
'domain': ('exact',),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
62
76
|
class TagFilter(FilterSet):
|
|
63
77
|
class Meta:
|
|
64
78
|
model = Tag
|
|
@@ -82,6 +96,7 @@ class UserFilter(FilterSet):
|
|
|
82
96
|
fields = {
|
|
83
97
|
'id': ('exact', 'in',),
|
|
84
98
|
'is_active': ('exact',),
|
|
99
|
+
'is_staff': ('exact',),
|
|
85
100
|
'first_name': ('exact', 'startswith', 'icontains'),
|
|
86
101
|
'last_name': ('exact', 'startswith', 'icontains'),
|
|
87
102
|
'username': ('exact', 'startswith', 'icontains'),
|
|
@@ -8,38 +8,56 @@ from django.conf import settings
|
|
|
8
8
|
from django.contrib.auth import get_user_model
|
|
9
9
|
from django.contrib.contenttypes.models import ContentType
|
|
10
10
|
from django.core.files.uploadedfile import InMemoryUploadedFile
|
|
11
|
+
from django.db import transaction
|
|
11
12
|
|
|
12
13
|
from rest_framework import status
|
|
13
14
|
from rest_framework.decorators import action
|
|
14
15
|
from rest_framework.response import Response
|
|
15
16
|
from rest_framework.serializers import ValidationError
|
|
16
17
|
|
|
18
|
+
from filedepot.mixins import FlowFileUploadMixin
|
|
19
|
+
|
|
17
20
|
from ..models import (
|
|
18
21
|
Attachment,
|
|
19
22
|
ImageAttachment,
|
|
20
23
|
Tag,
|
|
21
24
|
)
|
|
22
25
|
from ..util import hashed_filename
|
|
23
|
-
from ..
|
|
24
|
-
|
|
25
|
-
from filedepot.mixins import FlowFileUploadMixin
|
|
26
|
+
from ..settings import appkit_settings
|
|
27
|
+
from .serializers import ImageAttachmentBulkRepositionSerializer
|
|
26
28
|
|
|
27
29
|
User = get_user_model()
|
|
30
|
+
get_current_site = appkit_settings.CURRENT_SITE_ACCESSOR
|
|
28
31
|
|
|
29
32
|
|
|
30
33
|
# ------------------------------------------------------------------------------
|
|
31
34
|
# ViewSet Mixins
|
|
32
35
|
# ------------------------------------------------------------------------------
|
|
33
|
-
class DocumentViewSetMixin:
|
|
36
|
+
class DocumentViewSetMixin(object):
|
|
34
37
|
def get_queryset(self):
|
|
35
38
|
queryset = super().get_queryset()
|
|
36
39
|
current_site = get_current_site(self.request)
|
|
37
40
|
return queryset.filter(site=current_site)
|
|
38
41
|
|
|
39
42
|
|
|
43
|
+
class ImageAttachmentViewSetMixin(object):
|
|
44
|
+
@action(detail=False, methods=['patch'])
|
|
45
|
+
@transaction.atomic()
|
|
46
|
+
def bulk_reposition(self, request, **kwargs):
|
|
47
|
+
serializer = ImageAttachmentBulkRepositionSerializer(data=request.data)
|
|
48
|
+
serializer.is_valid(raise_exception=True)
|
|
49
|
+
validated_data = serializer.validated_data
|
|
50
|
+
for position_index, uuid in enumerate(validated_data['image_attachment_uuids']):
|
|
51
|
+
image_attachment = ImageAttachment.objects.get(uuid=uuid)
|
|
52
|
+
image_attachment.position = validated_data['positions'][position_index]
|
|
53
|
+
image_attachment.save()
|
|
54
|
+
|
|
55
|
+
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
56
|
+
|
|
57
|
+
|
|
40
58
|
class AttachmentMixin(FlowFileUploadMixin):
|
|
41
59
|
@action(detail=True, methods=['delete'])
|
|
42
|
-
def delete_attachment(self, request,
|
|
60
|
+
def delete_attachment(self, request, uuid, **kwargs):
|
|
43
61
|
attachment = Attachment.objects.get(uuid=request.data['item_uuid'])
|
|
44
62
|
attachment.delete()
|
|
45
63
|
|
|
@@ -48,7 +66,7 @@ class AttachmentMixin(FlowFileUploadMixin):
|
|
|
48
66
|
|
|
49
67
|
|
|
50
68
|
@action(detail=True, methods=['get', 'post'])
|
|
51
|
-
def add_attachment(self, request,
|
|
69
|
+
def add_attachment(self, request, uuid, **kwargs):
|
|
52
70
|
result = self.handle_flow_request(request)
|
|
53
71
|
|
|
54
72
|
response_data = {}
|
|
@@ -75,16 +93,15 @@ class AttachmentMixin(FlowFileUploadMixin):
|
|
|
75
93
|
return Response(response_data, status=result['status'])
|
|
76
94
|
|
|
77
95
|
@action(detail=True, methods=['delete'])
|
|
78
|
-
def delete_image(self, request,
|
|
96
|
+
def delete_image(self, request, uuid, **kwargs):
|
|
79
97
|
image_attachment = ImageAttachment.objects.get(uuid=request.data['item_uuid'])
|
|
80
98
|
image_attachment.delete()
|
|
81
99
|
|
|
82
100
|
serializer = self.get_serializer(self.get_object())
|
|
83
101
|
return Response(serializer.data)
|
|
84
102
|
|
|
85
|
-
|
|
86
103
|
@action(detail=True, methods=['post'])
|
|
87
|
-
def add_image(self, request,
|
|
104
|
+
def add_image(self, request, uuid, **kwargs):
|
|
88
105
|
obj = self.get_object()
|
|
89
106
|
request_params = self.get_request_params(request)
|
|
90
107
|
|
|
@@ -142,9 +159,10 @@ class AttachmentMixin(FlowFileUploadMixin):
|
|
|
142
159
|
|
|
143
160
|
# Position the newly uploaded image at the end of the collection
|
|
144
161
|
image_position = 0
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
162
|
+
if hasattr(obj, 'images'):
|
|
163
|
+
images = obj.images.all()
|
|
164
|
+
if images.count():
|
|
165
|
+
image_position = images.last().position + 1
|
|
148
166
|
|
|
149
167
|
image_attachment = ImageAttachment.objects.create(
|
|
150
168
|
content_type=ContentType.objects.get_for_model(obj),
|
|
@@ -168,7 +186,10 @@ class AttachmentMixin(FlowFileUploadMixin):
|
|
|
168
186
|
flow_file.delete()
|
|
169
187
|
|
|
170
188
|
serializer = self.get_serializer(obj)
|
|
171
|
-
|
|
189
|
+
serialized_data = serializer.data
|
|
190
|
+
serialized_data['image_attachment_uuid'] = image_attachment.uuid
|
|
191
|
+
|
|
192
|
+
return Response(serialized_data, status=status.HTTP_201_CREATED)
|
|
172
193
|
|
|
173
194
|
|
|
174
195
|
# ------------------------------------------------------------------------------
|
|
@@ -204,7 +225,7 @@ class TaggableMixin(object):
|
|
|
204
225
|
return Response(serializer.data)
|
|
205
226
|
|
|
206
227
|
@action(detail=True, methods=['post'])
|
|
207
|
-
def update_tags(self, request,
|
|
228
|
+
def update_tags(self, request, uuid, **kwargs):
|
|
208
229
|
obj = self.get_object()
|
|
209
230
|
|
|
210
231
|
ids_to_add = request.data.get('ids_to_add')
|
|
@@ -14,6 +14,9 @@ from django.contrib.auth.password_validation import validate_password
|
|
|
14
14
|
from django.core import exceptions as django_exceptions
|
|
15
15
|
|
|
16
16
|
from rest_framework import serializers
|
|
17
|
+
from rest_framework import exceptions
|
|
18
|
+
|
|
19
|
+
from rest_framework_recursive.fields import RecursiveField
|
|
17
20
|
|
|
18
21
|
from djoser.conf import settings as djoser_settings
|
|
19
22
|
|
|
@@ -27,7 +30,6 @@ from phonenumber_field.serializerfields import PhoneNumberField
|
|
|
27
30
|
from versatileimagefield.serializers import VersatileImageFieldSerializer
|
|
28
31
|
|
|
29
32
|
from ..auth import get_userprofile_model
|
|
30
|
-
from ..shortcuts import get_current_site
|
|
31
33
|
|
|
32
34
|
from ..drf.serializers import (
|
|
33
35
|
IntegerListField,
|
|
@@ -37,7 +39,7 @@ from ..drf.serializers import (
|
|
|
37
39
|
from ..drf.fields import (
|
|
38
40
|
AttachmentFileField,
|
|
39
41
|
DocumentPathField,
|
|
40
|
-
|
|
42
|
+
ContentTypeField,
|
|
41
43
|
)
|
|
42
44
|
|
|
43
45
|
from ..models import (
|
|
@@ -46,14 +48,18 @@ from ..models import (
|
|
|
46
48
|
Note,
|
|
47
49
|
Place,
|
|
48
50
|
Region,
|
|
51
|
+
SiteAlias,
|
|
49
52
|
Tag,
|
|
50
53
|
)
|
|
51
54
|
|
|
55
|
+
from ..settings import appkit_settings
|
|
52
56
|
from ..shortcuts import image_variation_size
|
|
53
57
|
|
|
54
58
|
User = get_user_model()
|
|
55
59
|
UserProfile = get_userprofile_model()
|
|
56
60
|
|
|
61
|
+
get_current_site = appkit_settings.CURRENT_SITE_ACCESSOR
|
|
62
|
+
|
|
57
63
|
|
|
58
64
|
class PermissionsMixin(object):
|
|
59
65
|
def get_permissions(self, obj):
|
|
@@ -93,12 +99,31 @@ class PermissionSerializer(serializers.Serializer):
|
|
|
93
99
|
return attrs
|
|
94
100
|
|
|
95
101
|
|
|
102
|
+
class SiteAliasSerializer(serializers.ModelSerializer):
|
|
103
|
+
class Meta:
|
|
104
|
+
model = SiteAlias
|
|
105
|
+
fields = ('domain',)
|
|
106
|
+
|
|
107
|
+
|
|
96
108
|
class SiteSerializer(serializers.ModelSerializer):
|
|
109
|
+
aliases = SiteAliasSerializer(many=True)
|
|
110
|
+
public_url = serializers.SerializerMethodField('get_public_url')
|
|
111
|
+
|
|
97
112
|
class Meta:
|
|
98
113
|
model = Site
|
|
99
114
|
fields = (
|
|
100
|
-
'
|
|
115
|
+
'aliases', 'domain', 'name', 'public_url',
|
|
101
116
|
)
|
|
117
|
+
|
|
118
|
+
def get_public_url(self, instance):
|
|
119
|
+
request = self.context['request']
|
|
120
|
+
|
|
121
|
+
if instance.aliases.exists():
|
|
122
|
+
site_alias = instance.aliases.first()
|
|
123
|
+
domain = site_alias.domain
|
|
124
|
+
else:
|
|
125
|
+
domain = instance.domain
|
|
126
|
+
return f'{request.scheme}://{domain}'
|
|
102
127
|
|
|
103
128
|
|
|
104
129
|
class DocumentSerializer(serializers.HyperlinkedModelSerializer):
|
|
@@ -106,13 +131,13 @@ class DocumentSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
106
131
|
Override the create method to automatically set the created_by field to the
|
|
107
132
|
authenticated user who issued the request
|
|
108
133
|
"""
|
|
109
|
-
|
|
110
|
-
path = DocumentPathField(max_length=100)
|
|
134
|
+
content_type = ContentTypeField(read_only=True)
|
|
135
|
+
path = DocumentPathField(max_length=100, read_only=True)
|
|
111
136
|
|
|
112
137
|
class Meta:
|
|
113
138
|
fields = (
|
|
114
139
|
'id', 'uuid', 'url',
|
|
115
|
-
'date_created', '
|
|
140
|
+
'date_created', 'content_type', 'path',
|
|
116
141
|
)
|
|
117
142
|
|
|
118
143
|
def create(self, validated_data):
|
|
@@ -121,12 +146,11 @@ class DocumentSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
121
146
|
authenticated user who issued the request
|
|
122
147
|
"""
|
|
123
148
|
request = self.context.get('request')
|
|
124
|
-
if request:
|
|
125
|
-
|
|
126
|
-
if user and user.is_authenticated:
|
|
127
|
-
validated_data['created_by_id'] = user.pk
|
|
149
|
+
if not request.user.is_authenticated:
|
|
150
|
+
raise exceptions.NotAuthenticated('Documents can only be created by an authenticated user')
|
|
128
151
|
|
|
129
|
-
|
|
152
|
+
validated_data['created_by_id'] = request.user.pk
|
|
153
|
+
validated_data['site'] = get_current_site(request)
|
|
130
154
|
|
|
131
155
|
return super().create(validated_data)
|
|
132
156
|
|
|
@@ -177,13 +201,15 @@ class PlaceSerializer(serializers.ModelSerializer):
|
|
|
177
201
|
|
|
178
202
|
class Meta:
|
|
179
203
|
model = Place
|
|
180
|
-
fields = ('info', 'place_id',)
|
|
204
|
+
fields = ('city', 'info', 'place_id',)
|
|
181
205
|
|
|
182
206
|
|
|
183
207
|
class RegionSerializer(serializers.HyperlinkedModelSerializer):
|
|
208
|
+
children = serializers.ListSerializer(child=RecursiveField())
|
|
209
|
+
|
|
184
210
|
class Meta:
|
|
185
211
|
model = Region
|
|
186
|
-
fields = ('id', 'url', 'name', 'full_name', 'root_name',)
|
|
212
|
+
fields = ('id', 'url', 'name', 'full_name', 'root_name', 'children')
|
|
187
213
|
|
|
188
214
|
|
|
189
215
|
class TagSerializer(serializers.HyperlinkedModelSerializer):
|
|
@@ -235,12 +261,15 @@ class ImageAttachmentSerializer(DocumentSerializer):
|
|
|
235
261
|
class Meta(DocumentSerializer):
|
|
236
262
|
model = ImageAttachment
|
|
237
263
|
fields = DocumentSerializer.Meta.fields + (
|
|
238
|
-
'position', 'label', 'width', 'height',
|
|
264
|
+
'image', 'position', 'label', 'width', 'height',
|
|
239
265
|
)
|
|
240
266
|
|
|
241
267
|
def get_fields(self):
|
|
242
268
|
fields = super().get_fields()
|
|
243
|
-
|
|
269
|
+
|
|
270
|
+
if self.sizes:
|
|
271
|
+
fields['image'] = ImageAttachmentImageSerializer(sizes=self.sizes)
|
|
272
|
+
|
|
244
273
|
return fields
|
|
245
274
|
|
|
246
275
|
|
|
@@ -248,11 +277,13 @@ class ImageAttachmentSerializer(DocumentSerializer):
|
|
|
248
277
|
# List Serializers
|
|
249
278
|
# ------------------------------------------------------------------------------
|
|
250
279
|
class UserListSerializer(serializers.HyperlinkedModelSerializer):
|
|
280
|
+
content_type = ContentTypeField(read_only=True)
|
|
281
|
+
|
|
251
282
|
class Meta:
|
|
252
283
|
model = User
|
|
253
284
|
fields = (
|
|
254
|
-
'id', 'url', 'is_active',
|
|
255
|
-
'email', 'first_name', 'last_name', 'full_name',
|
|
285
|
+
'id', 'url', 'content_type', 'is_active', 'is_staff',
|
|
286
|
+
'email', 'first_name', 'last_name', 'full_name',
|
|
256
287
|
)
|
|
257
288
|
|
|
258
289
|
|
|
@@ -276,6 +307,16 @@ class NoteListSerializer(DocumentSerializer):
|
|
|
276
307
|
)
|
|
277
308
|
|
|
278
309
|
|
|
310
|
+
class UserSetSerializer(serializers.Serializer):
|
|
311
|
+
users = serializers.PrimaryKeyRelatedField(
|
|
312
|
+
queryset=User.objects.all(),
|
|
313
|
+
many=True,
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
class Meta:
|
|
317
|
+
fields = ('users',)
|
|
318
|
+
|
|
319
|
+
|
|
279
320
|
# ------------------------------------------------------------------------------
|
|
280
321
|
# Detail & Update Serializers
|
|
281
322
|
# ------------------------------------------------------------------------------
|
|
@@ -288,6 +329,9 @@ class GroupDetailSerializer(PermissionsMixin, GroupSerializer):
|
|
|
288
329
|
'permissions', 'user_set',
|
|
289
330
|
)
|
|
290
331
|
|
|
332
|
+
def managed_model_classes(self, group):
|
|
333
|
+
return []
|
|
334
|
+
|
|
291
335
|
|
|
292
336
|
class UserDetailSerializer(PermissionsMixin, UserListSerializer):
|
|
293
337
|
groups = GroupDetailSerializer(read_only=True, many=True)
|
|
@@ -4,15 +4,12 @@ import shutil
|
|
|
4
4
|
from django.conf import settings
|
|
5
5
|
from django.contrib.auth import get_user_model
|
|
6
6
|
from django.contrib.auth.models import Group
|
|
7
|
-
from django.contrib.contenttypes.models import ContentType
|
|
8
7
|
from django.contrib.sites.models import Site
|
|
9
|
-
|
|
8
|
+
|
|
9
|
+
from django.db import models
|
|
10
10
|
from django.urls import reverse
|
|
11
11
|
from django.utils.text import slugify
|
|
12
12
|
|
|
13
|
-
from rest_framework.status import (
|
|
14
|
-
HTTP_200_OK,
|
|
15
|
-
)
|
|
16
13
|
from rest_framework.test import (
|
|
17
14
|
APIRequestFactory,
|
|
18
15
|
APITestCase as DRFAPITestCase,
|
|
@@ -20,7 +17,7 @@ from rest_framework.test import (
|
|
|
20
17
|
from rest_framework_simplejwt.tokens import RefreshToken
|
|
21
18
|
|
|
22
19
|
from ..auth import get_userprofile_model
|
|
23
|
-
from ..
|
|
20
|
+
from ..models import Document, UserGroup
|
|
24
21
|
|
|
25
22
|
User = get_user_model()
|
|
26
23
|
UserProfile = get_userprofile_model()
|
|
@@ -37,6 +34,7 @@ of text is expected (such as a comment).
|
|
|
37
34
|
|
|
38
35
|
DEFAULT_USER_PASSWORD = 'asdf;#KLVCSUkal3dsfui#@FS@!'
|
|
39
36
|
|
|
37
|
+
|
|
40
38
|
class TestCaseAssertionMixin(object):
|
|
41
39
|
"""
|
|
42
40
|
A mixin to provide additional test assertions that specifically applicable
|
|
@@ -73,7 +71,7 @@ class TestCaseAssertionMixin(object):
|
|
|
73
71
|
|
|
74
72
|
|
|
75
73
|
class APITestCase(TestCaseAssertionMixin, DRFAPITestCase):
|
|
76
|
-
|
|
74
|
+
site = None
|
|
77
75
|
|
|
78
76
|
@classmethod
|
|
79
77
|
def setUpTestData(cls):
|
|
@@ -85,7 +83,7 @@ class APITestCase(TestCaseAssertionMixin, DRFAPITestCase):
|
|
|
85
83
|
'password': DEFAULT_USER_PASSWORD,
|
|
86
84
|
})
|
|
87
85
|
|
|
88
|
-
cls.
|
|
86
|
+
cls.site = Site.objects.create(
|
|
89
87
|
name='Test Site',
|
|
90
88
|
domain='testserver'
|
|
91
89
|
)
|
|
@@ -113,8 +111,8 @@ class APITestCase(TestCaseAssertionMixin, DRFAPITestCase):
|
|
|
113
111
|
super().setUp()
|
|
114
112
|
self._logout()
|
|
115
113
|
|
|
116
|
-
def create_user(self, first_name='
|
|
117
|
-
is_active=True, is_staff=False, site=None):
|
|
114
|
+
def create_user(self, first_name='TestUser', last_name='', email=None, phone=None,
|
|
115
|
+
is_active=True, is_staff=False, site=None, created_by=None):
|
|
118
116
|
full_name = '{} {}'.format(first_name, last_name)
|
|
119
117
|
username = slugify(full_name)
|
|
120
118
|
|
|
@@ -129,30 +127,40 @@ class APITestCase(TestCaseAssertionMixin, DRFAPITestCase):
|
|
|
129
127
|
user.set_password(DEFAULT_USER_PASSWORD)
|
|
130
128
|
user.save()
|
|
131
129
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
130
|
+
UserProfile.objects.create(
|
|
131
|
+
created_by=created_by if created_by else self.superuser,
|
|
132
|
+
site=site if site else self.site,
|
|
133
|
+
user=user,
|
|
134
|
+
phone=phone,
|
|
135
|
+
)
|
|
135
136
|
|
|
136
137
|
return user
|
|
137
138
|
|
|
138
139
|
def create_tpb_users(self):
|
|
139
140
|
TRAILER_PARK_BOYS = (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
141
|
+
{'first_name': 'Jim', 'last_name': 'Lahey', 'is_staff': True, 'phone': '+17825556969'},
|
|
142
|
+
{'first_name': 'Randy', 'last_name': 'BoBandy', 'is_staff': False},
|
|
143
|
+
{'first_name': 'Sam', 'last_name': 'Lasco', 'is_staff': False, 'phone': '+17825551234'},
|
|
144
|
+
{'first_name': 'Ricky', 'is_staff': False},
|
|
145
|
+
{'first_name': 'Julian', 'is_staff': False},
|
|
146
|
+
{'first_name': 'Bubbles', 'is_staff': False},
|
|
146
147
|
)
|
|
147
148
|
|
|
148
149
|
tpb_users = {}
|
|
149
150
|
for user_info in TRAILER_PARK_BOYS:
|
|
150
|
-
user = self.create_user(
|
|
151
|
-
first_name=user_info[0],
|
|
152
|
-
last_name=user_info[1]
|
|
153
|
-
)
|
|
151
|
+
user = self.create_user(**user_info)
|
|
154
152
|
tpb_users[user.username] = user
|
|
155
153
|
|
|
154
|
+
tpb_group = Group.objects.create(name='trailer-park-boys')
|
|
155
|
+
tpb_group.user_set.add(*tpb_users.values())
|
|
156
|
+
self.tpb_group = tpb_group
|
|
157
|
+
|
|
158
|
+
self.tpb_usergroup = UserGroup.objects.create(
|
|
159
|
+
created_by=self.superuser,
|
|
160
|
+
site=self.site,
|
|
161
|
+
group=tpb_group
|
|
162
|
+
)
|
|
163
|
+
|
|
156
164
|
return tpb_users
|
|
157
165
|
|
|
158
166
|
def url_namespace(self):
|
|
@@ -168,53 +176,65 @@ class APITestCase(TestCaseAssertionMixin, DRFAPITestCase):
|
|
|
168
176
|
|
|
169
177
|
return reverse(view_name, kwargs=url_kwargs)
|
|
170
178
|
|
|
171
|
-
def detail_url(self,
|
|
172
|
-
|
|
179
|
+
def detail_url(self, model, **kwargs):
|
|
180
|
+
url_kwargs = {**kwargs}
|
|
181
|
+
|
|
182
|
+
if isinstance(model, models.Model):
|
|
183
|
+
model_name = model._meta.model_name
|
|
184
|
+
if not url_kwargs:
|
|
185
|
+
if isinstance(model, Document):
|
|
186
|
+
url_kwargs['uuid'] = str(model.uuid)
|
|
187
|
+
else:
|
|
188
|
+
url_kwargs['pk'] = model.pk
|
|
189
|
+
else:
|
|
190
|
+
model_name = model
|
|
191
|
+
|
|
192
|
+
return self.url_reverse((f'{model_name}-detail', url_kwargs))
|
|
193
|
+
|
|
194
|
+
def api_request(self, request_method, url, params=None, **kwargs):
|
|
195
|
+
request_kwargs = {'format': 'json', **kwargs}
|
|
173
196
|
|
|
174
|
-
def api_request(self, request_method, url, params=None):
|
|
175
|
-
kwargs = {'format': 'json'}
|
|
176
197
|
if self.access_token:
|
|
177
|
-
|
|
198
|
+
request_kwargs['HTTP_AUTHORIZATION'] = 'Bearer {}'.format(self.access_token)
|
|
178
199
|
|
|
179
200
|
if request_method == 'GET':
|
|
180
|
-
return self.client.get(url, params, **
|
|
201
|
+
return self.client.get(url, params, **request_kwargs)
|
|
181
202
|
elif request_method == 'POST':
|
|
182
|
-
return self.client.post(url, params, **
|
|
203
|
+
return self.client.post(url, params, **request_kwargs)
|
|
183
204
|
elif request_method == 'PUT':
|
|
184
|
-
return self.client.put(url, params, **
|
|
205
|
+
return self.client.put(url, params, **request_kwargs)
|
|
185
206
|
elif request_method == 'PATCH':
|
|
186
|
-
return self.client.patch(url, params, **
|
|
207
|
+
return self.client.patch(url, params, **request_kwargs)
|
|
187
208
|
elif request_method == 'DELETE':
|
|
188
|
-
return self.client.delete(url, params, **
|
|
209
|
+
return self.client.delete(url, params, **request_kwargs)
|
|
189
210
|
elif request_method == 'OPTIONS':
|
|
190
|
-
return self.client.options(url, params, **
|
|
211
|
+
return self.client.options(url, params, **request_kwargs)
|
|
191
212
|
else:
|
|
192
213
|
raise RuntimeError('Request method not supported: {}'.format(request_method))
|
|
193
214
|
|
|
194
|
-
def api_get(self, url_info, params=None):
|
|
195
|
-
return self.api_request('GET', self.url_reverse(url_info), params)
|
|
215
|
+
def api_get(self, url_info, params=None, **kwargs):
|
|
216
|
+
return self.api_request('GET', self.url_reverse(url_info), params, **kwargs)
|
|
196
217
|
|
|
197
|
-
def api_post(self, url_info, params=None):
|
|
198
|
-
return self.api_request('POST', self.url_reverse(url_info), params)
|
|
218
|
+
def api_post(self, url_info, params=None, **kwargs):
|
|
219
|
+
return self.api_request('POST', self.url_reverse(url_info), params, **kwargs)
|
|
199
220
|
|
|
200
|
-
def api_put(self, url_info, params=None):
|
|
201
|
-
return self.api_request('PUT', self.url_reverse(url_info), params)
|
|
221
|
+
def api_put(self, url_info, params=None, **kwargs):
|
|
222
|
+
return self.api_request('PUT', self.url_reverse(url_info), params, **kwargs)
|
|
202
223
|
|
|
203
|
-
def api_patch(self, url_info, params=None):
|
|
204
|
-
return self.api_request('PATCH', self.url_reverse(url_info), params)
|
|
224
|
+
def api_patch(self, url_info, params=None, **kwargs):
|
|
225
|
+
return self.api_request('PATCH', self.url_reverse(url_info), params, **kwargs)
|
|
205
226
|
|
|
206
|
-
def api_delete(self, url_info, params=None):
|
|
207
|
-
return self.api_request('DELETE', self.url_reverse(url_info), params)
|
|
227
|
+
def api_delete(self, url_info, params=None, **kwargs):
|
|
228
|
+
return self.api_request('DELETE', self.url_reverse(url_info), params, **kwargs)
|
|
208
229
|
|
|
209
|
-
def api_options(self, url_info, params=None):
|
|
210
|
-
return self.api_request('OPTIONS', self.url_reverse(url_info), params)
|
|
230
|
+
def api_options(self, url_info, params=None, **kwargs):
|
|
231
|
+
return self.api_request('OPTIONS', self.url_reverse(url_info), params, **kwargs)
|
|
211
232
|
|
|
212
233
|
def authenticate(self, user):
|
|
213
234
|
refresh = RefreshToken.for_user(user)
|
|
214
235
|
self.refresh_token = str(refresh)
|
|
215
236
|
self.access_token = str(refresh.access_token)
|
|
216
237
|
|
|
217
|
-
|
|
218
238
|
def serialize(self, instance, type, serializer_class):
|
|
219
239
|
factory = APIRequestFactory()
|
|
220
240
|
request = factory.get(self.detail_url(type, pk=instance.pk))
|