[extensions-web/wip/api/v1: 12/19] 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: 12/19] api: initial /hello node implementation with DRF
- Date: Sun, 20 Dec 2020 14:26:11 +0000 (UTC)
commit dd1efbc8ae5e71441f61d5ef582678ed2a35afb0
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 07265d24..4ff5f4b6 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,9 @@
Django==2.2.17 \
--hash=sha256:558cb27930defd9a6042133258caf797b2d1dee233959f537e3dc475cb49bd7c \
--hash=sha256:cf5370a4d7765a9dd6d42a7b96b53c74f9446cd38209211304b210fe0404b861
+djangorestframework==3.11.0 \
+ --hash=sha256:05809fc66e1c997fd9a32ea5730d9f4ba28b109b9da71fccfa5ff241201fd0a4 \
+ --hash=sha256:e782087823c47a26826ee5b6fa0c542968219263fb3976ec3c31edab23a4001f
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 00000000..e69de29b
diff --git a/sweettooth/api/v1/__init__.py b/sweettooth/api/v1/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/sweettooth/api/v1/urls.py b/sweettooth/api/v1/urls.py
new file mode 100644
index 00000000..a1a721cf
--- /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 00000000..36194193
--- /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 00000000..89221aa1
--- /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 d84cec02..65b19df6 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',
@@ -155,6 +158,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 d1d3b1e9..a53aab60 100644
--- a/sweettooth/urls.py
+++ b/sweettooth/urls.py
@@ -4,6 +4,7 @@ import os.path
from django.conf.urls import include, url, handler404, handler500
from django.conf import settings
from django.http import HttpResponse
+from django.urls import path
from django.contrib import admin
from django.views import static
@@ -13,6 +14,8 @@ from django.views.i18n import JavaScriptCatalog
admin.autodiscover()
urlpatterns = [
+ path('api/', include('sweettooth.api.v1.urls')),
+
# 'login' and 'register'
url(r'^accounts/', include('sweettooth.auth.urls')),
url(r'^', include('sweettooth.extensions.urls'), name='index'),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]