Spaces:
Configuration error
Configuration error
Setup Accounts and add Authentication on this
Browse files- .gitignore +0 -1
- accounts/__init__.py +0 -0
- accounts/admin.py +3 -0
- accounts/apps.py +6 -0
- accounts/migrations/__init__.py +0 -0
- accounts/models.py +3 -0
- accounts/serializers.py +43 -0
- accounts/tests.py +3 -0
- accounts/urls.py +9 -0
- accounts/views.py +16 -0
- config/settings.py +13 -11
- config/urls.py +2 -1
- db.sqlite3 +0 -0
- texttovoice/views.py +4 -0
.gitignore
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
venv
|
2 |
env
|
3 |
-
accounts
|
4 |
__pycache__
|
|
|
1 |
venv
|
2 |
env
|
|
|
3 |
__pycache__
|
accounts/__init__.py
ADDED
File without changes
|
accounts/admin.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from django.contrib import admin
|
2 |
+
|
3 |
+
# Register your models here.
|
accounts/apps.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from django.apps import AppConfig
|
2 |
+
|
3 |
+
|
4 |
+
class AccountsConfig(AppConfig):
|
5 |
+
default_auto_field = 'django.db.models.BigAutoField'
|
6 |
+
name = 'accounts'
|
accounts/migrations/__init__.py
ADDED
File without changes
|
accounts/models.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from django.db import models
|
2 |
+
|
3 |
+
# Create your models here.
|
accounts/serializers.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from django.contrib.auth.models import User
|
2 |
+
from rest_framework import serializers
|
3 |
+
from django.contrib.auth import authenticate
|
4 |
+
|
5 |
+
class UserSerializer(serializers.ModelSerializer):
|
6 |
+
password = serializers.CharField(write_only=True)
|
7 |
+
|
8 |
+
def create(self, validated_data):
|
9 |
+
user = User.objects.create_user(
|
10 |
+
username=validated_data['username'],
|
11 |
+
password=validated_data['password']
|
12 |
+
)
|
13 |
+
return user
|
14 |
+
|
15 |
+
class Meta:
|
16 |
+
model = User
|
17 |
+
fields = ('id', 'username', 'password')
|
18 |
+
|
19 |
+
|
20 |
+
class LoginSerializer(serializers.Serializer):
|
21 |
+
username = serializers.CharField(max_length=150)
|
22 |
+
password = serializers.CharField(max_length=128, write_only=True)
|
23 |
+
|
24 |
+
def validate(self, data):
|
25 |
+
username = data.get('username')
|
26 |
+
password = data.get('password')
|
27 |
+
print(username,password)
|
28 |
+
|
29 |
+
if username and password:
|
30 |
+
user = authenticate(username=username, password=password)
|
31 |
+
print("user",user)
|
32 |
+
|
33 |
+
if user:
|
34 |
+
if user.is_active:
|
35 |
+
data['user'] = user
|
36 |
+
else:
|
37 |
+
raise serializers.ValidationError("User is not active.")
|
38 |
+
else:
|
39 |
+
raise serializers.ValidationError("Unable to log in with the provided credentials.")
|
40 |
+
else:
|
41 |
+
raise serializers.ValidationError("Must include 'username' and 'password'.")
|
42 |
+
|
43 |
+
return data
|
accounts/tests.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from django.test import TestCase
|
2 |
+
|
3 |
+
# Create your tests here.
|
accounts/urls.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from django.urls import path
|
2 |
+
from . import views
|
3 |
+
from rest_framework.authtoken.views import obtain_auth_token
|
4 |
+
|
5 |
+
urlpatterns = [
|
6 |
+
path('register/', views.RegisterView.as_view(), name='register'),
|
7 |
+
path('login', obtain_auth_token, name="Get Token"),
|
8 |
+
]
|
9 |
+
|
accounts/views.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .serializers import LoginSerializer, UserSerializer
|
2 |
+
from rest_framework import status
|
3 |
+
from rest_framework.response import Response
|
4 |
+
from rest_framework.generics import CreateAPIView
|
5 |
+
from rest_framework.authtoken.models import Token
|
6 |
+
from django.contrib.auth.models import User
|
7 |
+
|
8 |
+
class RegisterView(CreateAPIView):
|
9 |
+
queryset = User.objects.all()
|
10 |
+
serializer_class = UserSerializer # Create a UserSerializer to handle user registration.
|
11 |
+
|
12 |
+
def perform_create(self, serializer):
|
13 |
+
user = serializer.save()
|
14 |
+
token, _ = Token.objects.get_or_create(user=user)
|
15 |
+
return Response({'token': token.key}, status=status.HTTP_201_CREATED)
|
16 |
+
|
config/settings.py
CHANGED
@@ -10,16 +10,6 @@ For the full list of settings and their values, see
|
|
10 |
https://docs.djangoproject.com/en/5.0/ref/settings/
|
11 |
"""
|
12 |
|
13 |
-
import os
|
14 |
-
import pprint
|
15 |
-
pprint.pprint(os.environ)
|
16 |
-
|
17 |
-
os.environ[ 'NUMBA_CACHE_DIR' ] = '/tmp/'
|
18 |
-
|
19 |
-
# from numba.caching import _UserProvidedCacheLocator
|
20 |
-
# print("@@@@@@@@@@@@@",_UserProvidedCacheLocator(lambda x:x, 'string').get_cache_path())
|
21 |
-
|
22 |
-
|
23 |
|
24 |
from pathlib import Path
|
25 |
|
@@ -50,7 +40,19 @@ INSTALLED_APPS = [
|
|
50 |
'django.contrib.staticfiles',
|
51 |
'rest_framework',
|
52 |
'drf_yasg',
|
53 |
-
'texttovoice'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
]
|
55 |
|
56 |
X_FRAME_OPTIONS = 'ALLOW-FROM https://huggingface.co/'
|
|
|
10 |
https://docs.djangoproject.com/en/5.0/ref/settings/
|
11 |
"""
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
from pathlib import Path
|
15 |
|
|
|
40 |
'django.contrib.staticfiles',
|
41 |
'rest_framework',
|
42 |
'drf_yasg',
|
43 |
+
'texttovoice',
|
44 |
+
'accounts',
|
45 |
+
'rest_framework.authtoken'
|
46 |
+
]
|
47 |
+
|
48 |
+
REST_FRAMEWORK = {
|
49 |
+
'DEFAULT_AUTHENTICATION_CLASSES': [
|
50 |
+
'rest_framework.authentication.TokenAuthentication',
|
51 |
+
],
|
52 |
+
}
|
53 |
+
|
54 |
+
AUTHENTICATION_BACKENDS = [
|
55 |
+
'django.contrib.auth.backends.ModelBackend',
|
56 |
]
|
57 |
|
58 |
X_FRAME_OPTIONS = 'ALLOW-FROM https://huggingface.co/'
|
config/urls.py
CHANGED
@@ -22,8 +22,9 @@ from texttovoice.views import TextToSpeechCreateView
|
|
22 |
|
23 |
urlpatterns = [
|
24 |
path('admin/', admin.site.urls),
|
|
|
25 |
path('generate-speech/', TextToSpeechCreateView.as_view(), name='generate-speech-create'),
|
26 |
-
|
27 |
re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
|
28 |
|
29 |
]
|
|
|
22 |
|
23 |
urlpatterns = [
|
24 |
path('admin/', admin.site.urls),
|
25 |
+
path('auth/', include('accounts.urls')),
|
26 |
path('generate-speech/', TextToSpeechCreateView.as_view(), name='generate-speech-create'),
|
27 |
+
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
|
28 |
re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
|
29 |
|
30 |
]
|
db.sqlite3
CHANGED
Binary files a/db.sqlite3 and b/db.sqlite3 differ
|
|
texttovoice/views.py
CHANGED
@@ -5,10 +5,14 @@ from rest_framework import status
|
|
5 |
from rest_framework.response import Response
|
6 |
from rest_framework.generics import CreateAPIView
|
7 |
from TTS.api import TTS
|
|
|
|
|
8 |
from .serializers import TextToSpeechSerializer
|
9 |
|
10 |
class TextToSpeechCreateView(CreateAPIView):
|
11 |
serializer_class = TextToSpeechSerializer
|
|
|
|
|
12 |
|
13 |
def create(self, request, *args, **kwargs):
|
14 |
serializer = self.get_serializer(data=request.data)
|
|
|
5 |
from rest_framework.response import Response
|
6 |
from rest_framework.generics import CreateAPIView
|
7 |
from TTS.api import TTS
|
8 |
+
from rest_framework.authentication import TokenAuthentication
|
9 |
+
from rest_framework.permissions import IsAuthenticated
|
10 |
from .serializers import TextToSpeechSerializer
|
11 |
|
12 |
class TextToSpeechCreateView(CreateAPIView):
|
13 |
serializer_class = TextToSpeechSerializer
|
14 |
+
authentication_classes = [TokenAuthentication] # Apply token authentication
|
15 |
+
permission_classes = [IsAuthenticated] # Require authentication for this view
|
16 |
|
17 |
def create(self, request, *args, **kwargs):
|
18 |
serializer = self.get_serializer(data=request.data)
|