aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-07-21 16:52:25 -0400
committerBobby <[email protected]>2023-07-21 16:52:25 -0400
commit27164c3de9e1d43940c047ae6be963e1df90f513 (patch)
tree333ab65adacdb8b3d85d091391fcc22498182884
parent0ed61d5af8afa07dc43ec0285c7cc97955334b50 (diff)
downloadthatcomputerscientist-27164c3de9e1d43940c047ae6be963e1df90f513.tar.xz
thatcomputerscientist-27164c3de9e1d43940c047ae6be963e1df90f513.zip
Added `API` app and `whoami` route
-rw-r--r--solitude/api/__init__.py0
-rw-r--r--solitude/api/admin.py3
-rw-r--r--solitude/api/apps.py6
-rw-r--r--solitude/api/migrations/__init__.py0
-rw-r--r--solitude/api/models.py3
-rw-r--r--solitude/api/serializers.py8
-rw-r--r--solitude/api/tests.py3
-rw-r--r--solitude/api/urls.py9
-rw-r--r--solitude/api/views.py29
-rw-r--r--thatcomputerscientist/hosts.py1
-rw-r--r--thatcomputerscientist/settings.py8
11 files changed, 70 insertions, 0 deletions
diff --git a/solitude/api/__init__.py b/solitude/api/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/solitude/api/__init__.py
diff --git a/solitude/api/admin.py b/solitude/api/admin.py
new file mode 100644
index 00000000..8c38f3f3
--- /dev/null
+++ b/solitude/api/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/solitude/api/apps.py b/solitude/api/apps.py
new file mode 100644
index 00000000..878e7d54
--- /dev/null
+++ b/solitude/api/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class ApiConfig(AppConfig):
+ default_auto_field = "django.db.models.BigAutoField"
+ name = "api"
diff --git a/solitude/api/migrations/__init__.py b/solitude/api/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/solitude/api/migrations/__init__.py
diff --git a/solitude/api/models.py b/solitude/api/models.py
new file mode 100644
index 00000000..71a83623
--- /dev/null
+++ b/solitude/api/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/solitude/api/serializers.py b/solitude/api/serializers.py
new file mode 100644
index 00000000..a667de99
--- /dev/null
+++ b/solitude/api/serializers.py
@@ -0,0 +1,8 @@
+from django.contrib.auth.models import User
+from rest_framework import serializers
+
+class UserSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = User
+ fields = ('id', 'username', 'email', 'is_staff')
+ \ No newline at end of file
diff --git a/solitude/api/tests.py b/solitude/api/tests.py
new file mode 100644
index 00000000..7ce503c2
--- /dev/null
+++ b/solitude/api/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/solitude/api/urls.py b/solitude/api/urls.py
new file mode 100644
index 00000000..6406b0fa
--- /dev/null
+++ b/solitude/api/urls.py
@@ -0,0 +1,9 @@
+from django.urls import path
+
+from . import views
+
+app_name = 'api'
+
+urlpatterns = [
+ path('whoami', views.CurrentUser.as_view(), name='whoami'),
+]
diff --git a/solitude/api/views.py b/solitude/api/views.py
new file mode 100644
index 00000000..23c73a55
--- /dev/null
+++ b/solitude/api/views.py
@@ -0,0 +1,29 @@
+from django.contrib.auth.models import User
+from rest_framework.authentication import SessionAuthentication, BasicAuthentication
+from rest_framework.permissions import IsAuthenticated
+from rest_framework.response import Response
+from rest_framework.views import APIView
+from rest_framework.decorators import authentication_classes, permission_classes
+
+from solitude.api.serializers import UserSerializer
+
+
+# Create your views here.
+
+class CurrentUser(APIView):
+ authentication_classes = [SessionAuthentication, BasicAuthentication]
+ permission_classes = [IsAuthenticated]
+
+ def get(self, request, format=None):
+ current_user = request.user
+ serializer = UserSerializer(current_user)
+ return Response(serializer.data)
+
+
+# @APIView(['GET'])
+# @authentication_classes([SessionAuthentication, BasicAuthentication])
+# @permission_classes([IsAuthenticated])
+# def current_user(request, format=None):
+# current_user = request.user
+# serializer = UserSerializer(current_user)
+# return Response(serializer.data) \ No newline at end of file
diff --git a/thatcomputerscientist/hosts.py b/thatcomputerscientist/hosts.py
index 790decc8..b1289898 100644
--- a/thatcomputerscientist/hosts.py
+++ b/thatcomputerscientist/hosts.py
@@ -6,4 +6,5 @@ host_patterns = patterns(
host(r'', settings.ROOT_URLCONF, name='default'),
host(r'www', settings.ROOT_URLCONF, name='www'),
host(r'solitude', 'solitude.backend.urls', name='solitude'),
+ host(r'api', 'solitude.api.urls', name='api'),
)
diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py
index 793ff41e..4636ee0b 100644
--- a/thatcomputerscientist/settings.py
+++ b/thatcomputerscientist/settings.py
@@ -59,6 +59,7 @@ INSTALLED_APPS = [
'django.contrib.sites',
'django.contrib.sitemaps',
'sslserver',
+ 'rest_framework',
'thatcomputerscientist',
'haystack',
'django_hosts',
@@ -80,6 +81,13 @@ HAYSTACK_CONNECTIONS = {
},
}
+REST_FRAMEWORK = {
+ 'DEFAULT_AUTHENTICATION_CLASSES': [
+ 'rest_framework.authentication.BasicAuthentication',
+ 'rest_framework.authentication.SessionAuthentication',
+ ]
+}
+
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',