| @@ -15,7 +15,6 @@ from pathlib import Path | |||
| # Build paths inside the project like this: BASE_DIR / 'subdir'. | |||
| BASE_DIR = Path(__file__).resolve().parent.parent | |||
| # Quick-start development settings - unsuitable for production | |||
| # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ | |||
| @@ -27,7 +26,6 @@ DEBUG = True | |||
| ALLOWED_HOSTS = [] | |||
| # Application definition | |||
| INSTALLED_APPS = [ | |||
| @@ -37,7 +35,8 @@ INSTALLED_APPS = [ | |||
| 'django.contrib.sessions', | |||
| 'django.contrib.messages', | |||
| 'django.contrib.staticfiles', | |||
| 'rest_framework' | |||
| 'rest_framework', | |||
| 'rest_framework.authtoken', | |||
| ] | |||
| MIDDLEWARE = [ | |||
| @@ -57,6 +56,9 @@ REST_FRAMEWORK = { | |||
| # or allow read-only access for unauthenticated users. | |||
| 'DEFAULT_PERMISSION_CLASSES': [ | |||
| 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' | |||
| ], | |||
| 'DEFAULT_AUTHENTICATION_CLASSES': [ | |||
| 'rest_framework.authentication.BasicAuthentication' | |||
| ] | |||
| } | |||
| @@ -78,7 +80,6 @@ TEMPLATES = [ | |||
| WSGI_APPLICATION = 'workx_backend.wsgi.application' | |||
| # Database | |||
| # https://docs.djangoproject.com/en/4.0/ref/settings/#databases | |||
| @@ -89,7 +90,6 @@ DATABASES = { | |||
| } | |||
| } | |||
| # Password validation | |||
| # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators | |||
| @@ -108,7 +108,6 @@ AUTH_PASSWORD_VALIDATORS = [ | |||
| }, | |||
| ] | |||
| # Internationalization | |||
| # https://docs.djangoproject.com/en/4.0/topics/i18n/ | |||
| @@ -120,7 +119,6 @@ USE_I18N = True | |||
| USE_TZ = True | |||
| # Static files (CSS, JavaScript, Images) | |||
| # https://docs.djangoproject.com/en/4.0/howto/static-files/ | |||
| @@ -1,21 +1,15 @@ | |||
| from django.contrib import admin | |||
| from django.urls import path, include | |||
| # Serializers define the API representation. | |||
| from rest_framework import routers | |||
| # Routers provide an easy way of automatically determining the URL conf. | |||
| from workx_backend.views import UserViewSet | |||
| from workx_backend.views import UserViewSet, CustomAuthToken | |||
| from rest_framework.authtoken import views | |||
| router = routers.DefaultRouter() | |||
| router.register(r'users', UserViewSet) | |||
| # Wire up our API using automatic URL routing. | |||
| # Additionally, we include login URLs for the browsable API. | |||
| urlpatterns = [ | |||
| path('admin/', admin.site.urls), | |||
| path('api/', include(router.urls)), | |||
| path('api-auth/', include('rest_framework.urls')) | |||
| path('authenticate/', CustomAuthToken.as_view()) | |||
| ] | |||
| @@ -1,9 +1,26 @@ | |||
| from django.contrib.auth.models import User | |||
| from rest_framework import viewsets | |||
| from rest_framework.authtoken.models import Token | |||
| from rest_framework.authtoken.views import ObtainAuthToken | |||
| from workx_backend.serializers import UserSerializer | |||
| from rest_framework.response import Response | |||
| class UserViewSet(viewsets.ModelViewSet): | |||
| queryset = User.objects.all() | |||
| serializer_class = UserSerializer | |||
| class CustomAuthToken(ObtainAuthToken): | |||
| def post(self, request, *args, **kwargs): | |||
| serializer = self.serializer_class(data=request.data, | |||
| context={'request': request}) | |||
| serializer.is_valid(raise_exception=True) | |||
| user = serializer.validated_data['user'] | |||
| # token, created = Token.objects.get_or_create(user=user) | |||
| return Response({ | |||
| # 'token': token.key, | |||
| # 'user_id': user.pk, | |||
| 'email': user.email | |||
| }) | |||