[extensions-web/wip/api/v1: 36/45] api: initial /hello node implementation with DRF
- From: Yuri Konotopov <ykonotopov src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web/wip/api/v1: 36/45] api: initial /hello node implementation with DRF
- Date: Sun, 16 Jan 2022 11:27:29 +0000 (UTC)
commit 423687f9fc799aefed862535a58bdbd1afbaeba8
Author: Yuri Konotopov <ykonotopov gnome org>
Date: Sun Nov 22 11:24:11 2020 +0400
api: initial /hello node implementation with DRF
requirements.txt | 3 +++
sweettooth/api/__init__.py | 0
sweettooth/api/v1/__init__.py | 0
sweettooth/api/v1/urls.py | 19 +++++++++++++++++++
sweettooth/api/v1/views.py | 32 ++++++++++++++++++++++++++++++++
sweettooth/auth/serializers.py | 20 ++++++++++++++++++++
sweettooth/settings.py | 12 ++++++++++++
sweettooth/urls.py | 3 +++
8 files changed, 89 insertions(+)
---
diff --git a/requirements.txt b/requirements.txt
index ddced17..0cfe3b3 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,9 @@
Django==3.2.11 \
--hash=sha256:0a0a37f0b93aef30c4bf3a839c187e1175bcdeb7e177341da0cb7b8194416891 \
--hash=sha256:69c94abe5d6b1b088bf475e09b7b74403f943e34da107e798465d2045da27e75
+djangorestframework==3.13.1 \
+ --hash=sha256:0c33407ce23acc68eca2a6e46424b008c9c02eceb8cf18581921d0092bc1f2ee \
+ --hash=sha256:24c4bf58ed7e85d1fe4ba250ab2da926d263cd57d64b03e8dcef0ac683f8b1aa
django-autoslug==1.9.8 \
--hash=sha256:26459eeddec207e307c55777a10fc25d17f4978753695340b16a17ed248a6f70 \
--hash=sha256:bae66c27d35615f472865b99c4d107f3b3add3d22ee337e84960fc07694abd45
diff --git a/sweettooth/api/__init__.py b/sweettooth/api/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sweettooth/api/v1/__init__.py b/sweettooth/api/v1/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sweettooth/api/v1/urls.py b/sweettooth/api/v1/urls.py
new file mode 100644
index 0000000..a1a721c
--- /dev/null
+++ b/sweettooth/api/v1/urls.py
@@ -0,0 +1,19 @@
+"""
+ GNOME Shell extensions repository
+ Copyright (C) 2020 Yuri Konotopov <ykonotopov gnome org>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+"""
+
+from django.urls import path
+
+from rest_framework.routers import SimpleRouter
+
+from sweettooth.api.v1.views import HelloView
+
+urlpatterns = [
+ path('v1/hello/', HelloView.as_view()),
+]
diff --git a/sweettooth/api/v1/views.py b/sweettooth/api/v1/views.py
new file mode 100644
index 0000000..3619419
--- /dev/null
+++ b/sweettooth/api/v1/views.py
@@ -0,0 +1,32 @@
+"""
+ GNOME Shell extensions repository
+ Copyright (C) 2020 Yuri Konotopov <ykonotopov gnome org>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+"""
+
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from sweettooth.auth import forms
+from sweettooth.auth import serializers
+from sweettooth.utils import gravatar_url
+
+class HelloView(APIView):
+ def get(self, request, format=None):
+ user = request.user
+ user.avatar = (
+ gravatar_url(None, user.email)
+ if hasattr(user, 'email') and user.email
+ else None
+ )
+
+ return Response({
+ 'user': serializers.UserSerializer(user).data,
+ 'forms': {
+ 'login_popup_form': forms.InlineAuthenticationForm().as_plain()
+ }
+ })
diff --git a/sweettooth/auth/serializers.py b/sweettooth/auth/serializers.py
new file mode 100644
index 0000000..89221aa
--- /dev/null
+++ b/sweettooth/auth/serializers.py
@@ -0,0 +1,20 @@
+"""
+ GNOME Shell extensions repository
+ Copyright (C) 2020 Yuri Konotopov <ykonotopov gnome org>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+"""
+
+from django.contrib.auth import get_user_model
+from rest_framework import serializers
+
+class UserSerializer(serializers.ModelSerializer):
+ avatar = serializers.CharField(read_only=True)
+
+ class Meta:
+ model = get_user_model()
+ fields = '__all__'
+ extra_kwargs = {'password': {'write_only': True}}
diff --git a/sweettooth/settings.py b/sweettooth/settings.py
index 2399425..c822a17 100644
--- a/sweettooth/settings.py
+++ b/sweettooth/settings.py
@@ -46,6 +46,9 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'django.contrib.messages',
+ 'rest_framework',
+
+ 'sweettooth.api',
'sweettooth.extensions',
'sweettooth.auth',
'sweettooth.core',
@@ -158,6 +161,15 @@ LOGIN_URL = '/accounts/login/'
COMMENTS_APP = 'sweettooth.ratings'
+REST_FRAMEWORK = {
+ 'DEFAULT_AUTHENTICATION_CLASSES': [
+ 'rest_framework.authentication.BasicAuthentication',
+ 'rest_framework.authentication.SessionAuthentication',
+ ],
+ 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
+ 'PAGE_SIZE': 10,
+}
+
# See http://docs.djangoproject.com/en/stable/topics/logging for
# more details on how to customize your logging configuration.
from django.utils.log import DEFAULT_LOGGING as LOGGING
diff --git a/sweettooth/urls.py b/sweettooth/urls.py
index f3ec200..2b6bb7d 100644
--- a/sweettooth/urls.py
+++ b/sweettooth/urls.py
@@ -4,6 +4,7 @@ import os.path
from django.conf.urls import include
from django.conf import settings
from django.http import HttpResponse
+from django.urls import path
from django.contrib import admin
from django.urls import re_path
@@ -14,6 +15,8 @@ from django.views.i18n import JavaScriptCatalog
admin.autodiscover()
urlpatterns = [
+ path('api/', include('sweettooth.api.v1.urls')),
+
# 'login' and 'register'
re_path(r'^accounts/', include('sweettooth.auth.urls')),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]