Skip to content

Commit 0603d91

Browse files
authored
Merge pull request #7 from LedruRollin/add-authentication
Add authentication
2 parents 0199fbd + 864b852 commit 0603d91

16 files changed

+90
-24
lines changed

requirements.txt

76 Bytes
Binary file not shown.
File renamed without changes.

src/auth/serializers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
from .token_serializer import AugmentedTokenObtainPairSerializer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
3+
4+
5+
class AugmentedTokenObtainPairSerializer(TokenObtainPairSerializer):
6+
7+
@classmethod
8+
def get_token(cls, user):
9+
token_repr = super().get_token(user)
10+
token_repr['username'] = user.username
11+
return token_repr

src/roback/settings.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import os
14+
from datetime import timedelta
1415
from pathlib import Path
1516

1617
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -42,9 +43,10 @@
4243
'django.contrib.messages',
4344
'django.contrib.staticfiles',
4445
'rest_framework',
46+
'rest_framework_simplejwt',
4547
'corsheaders',
46-
'drf_spectacular',
4748
'search_targets',
49+
'drf_spectacular',
4850
]
4951

5052
MIDDLEWARE = [
@@ -139,7 +141,13 @@
139141
'DEFAULT_PAGINATION_CLASS': 'search_targets.paginations.PaginationWithTotalCount',
140142
'PAGE_SIZE': 20,
141143
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
142-
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
144+
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
145+
'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',),
146+
}
147+
148+
SIMPLE_JWT = {
149+
"ACCESS_TOKEN_LIFETIME": timedelta(days=1),
150+
"TOKEN_OBTAIN_SERIALIZER": "auth.serializers.AugmentedTokenObtainPairSerializer",
143151
}
144152

145153
# Custom media URL

src/roback/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from django.contrib import admin
2020
from django.urls import path, include
2121
from rest_framework import routers
22+
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
2223

2324
from search_targets import views as search_target_views
2425
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
@@ -33,6 +34,8 @@
3334
path('admin/', admin.site.urls),
3435
path('api-auth/', include('rest_framework.urls')),
3536
path('api/', include(router.urls)),
37+
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
38+
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
3639
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
3740
path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
3841
]

src/search_targets/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class SearchTargetAdmin(admin.ModelAdmin):
77
list_display = ("search_text", "insertion_date", "insertion_time")
88

99
class MediaAdmin(admin.ModelAdmin):
10-
list_display = ("search_target", "type", "file", "name")
10+
list_display = ("search_target", "type", "file_path", "name")
1111

1212
class URLAdmin(admin.ModelAdmin):
1313
list_display = ("url",)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.1.1 on 2024-10-10 13:04
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('search_targets', '0018_alter_searchtarget_insertion_time'),
10+
]
11+
12+
operations = [
13+
migrations.RenameField(
14+
model_name='media',
15+
old_name='file',
16+
new_name='file_path',
17+
),
18+
]

src/search_targets/models/media_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ class Media(Model):
3636
""" Model for a media (image, video, gif, etc)"""
3737
search_target = ForeignKey(SearchTarget, on_delete=CASCADE, related_name="media")
3838
name = TextField()
39-
file = FileField(upload_to=get_media_path)
39+
file_path = FileField(upload_to=get_media_path)
4040
type = TextField(choices=MediaType.choices, default='')
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11

2-
from rest_framework.serializers import ModelSerializer
2+
from rest_framework.serializers import ModelSerializer, FilePathField, SerializerMethodField
33

44
from search_targets.models import Media
55

66

77
class MediaSerializer(ModelSerializer):
88
class Meta:
99
model = Media
10-
fields = '__all__'
10+
fields = (
11+
"id",
12+
"name",
13+
"file_path",
14+
"type",
15+
"search_target",
16+
)

0 commit comments

Comments
 (0)