[extensions-web/wip/api/v1: 37/45] api: initial implementation of /profile node




commit 628839d9390088ae8a9b64b110d796e9d02f4053
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Sun Nov 22 11:57:47 2020 +0400

    api: initial implementation of /profile node

 sweettooth/api/v1/urls.py                          |  4 ++
 .../migrations/0010_auto_20200511_1019.py          | 20 +++++++++
 sweettooth/extensions/models.py                    |  2 +-
 sweettooth/extensions/serializers.py               | 49 ++++++++++++++++++++++
 sweettooth/settings.py                             |  1 +
 sweettooth/users/__init__.py                       |  0
 sweettooth/users/migrations/__init__.py            |  0
 sweettooth/users/serializers.py                    | 31 ++++++++++++++
 sweettooth/users/views.py                          | 29 +++++++++++++
 9 files changed, 135 insertions(+), 1 deletion(-)
---
diff --git a/sweettooth/api/v1/urls.py b/sweettooth/api/v1/urls.py
index a1a721c..aa9849a 100644
--- a/sweettooth/api/v1/urls.py
+++ b/sweettooth/api/v1/urls.py
@@ -13,7 +13,11 @@ from django.urls import path
 from rest_framework.routers import SimpleRouter
 
 from sweettooth.api.v1.views import HelloView
+from sweettooth.users.views import UserProfileDetailView
 
 urlpatterns = [
     path('v1/hello/', HelloView.as_view()),
+    path('v1/profile/<int:pk>/',
+         UserProfileDetailView.as_view(),
+         name='userprofile-detail'),
 ]
diff --git a/sweettooth/extensions/migrations/0010_auto_20200511_1019.py 
b/sweettooth/extensions/migrations/0010_auto_20200511_1019.py
new file mode 100644
index 0000000..0fde67a
--- /dev/null
+++ b/sweettooth/extensions/migrations/0010_auto_20200511_1019.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.2.12 on 2020-05-11 10:19
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extensions', '0009_shell40_minor_version'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='extension',
+            name='creator',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='extensions', 
to=settings.AUTH_USER_MODEL),
+        ),
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 86b425d..a5a2e00 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -109,7 +109,7 @@ class Extension(models.Model):
     name = models.CharField(max_length=200)
     uuid = models.CharField(max_length=200, unique=True, db_index=True)
     slug = autoslug.AutoSlugField(populate_from="name")
-    creator = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, on_delete=models.PROTECT)
+    creator = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="extensions", db_index=True, 
on_delete=models.PROTECT)
     description = models.TextField(blank=True)
     url = HttpURLField(blank=True)
     created = models.DateTimeField(auto_now_add=True)
diff --git a/sweettooth/extensions/serializers.py b/sweettooth/extensions/serializers.py
new file mode 100644
index 0000000..6adf4e0
--- /dev/null
+++ b/sweettooth/extensions/serializers.py
@@ -0,0 +1,49 @@
+"""
+    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 import serializers
+
+from sweettooth.extensions.models import Extension, ExtensionVersion, ShellVersion
+from sweettooth.users.serializers import BaseUserProfileSerializer
+
+class ShellVersionSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = ShellVersion
+        fields = [
+            'major',
+            'minor',
+            'point',
+        ]
+
+class ExtensionVersionSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = ExtensionVersion
+        fields = [
+            'version',
+            'status',
+        ]
+
+class ExtensionSerializer(serializers.ModelSerializer):
+    creator = BaseUserProfileSerializer(many=False, read_only=True)
+
+    class Meta:
+        model = Extension
+        fields = [
+            'id',
+            'uuid',
+            'name',
+            'creator',
+            'description',
+            'created',
+            'downloads',
+            'popularity',
+            'screenshot',
+            'icon',
+        ]
diff --git a/sweettooth/settings.py b/sweettooth/settings.py
index c822a17..aeb87e0 100644
--- a/sweettooth/settings.py
+++ b/sweettooth/settings.py
@@ -55,6 +55,7 @@ INSTALLED_APPS = (
     'sweettooth.review',
     'sweettooth.errorreports',
     'sweettooth.templates',
+    'sweettooth.users',
 
     'django.contrib.admin',
 )
diff --git a/sweettooth/users/__init__.py b/sweettooth/users/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sweettooth/users/migrations/__init__.py b/sweettooth/users/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sweettooth/users/serializers.py b/sweettooth/users/serializers.py
new file mode 100644
index 0000000..13d69ab
--- /dev/null
+++ b/sweettooth/users/serializers.py
@@ -0,0 +1,31 @@
+"""
+    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 BaseUserProfileSerializer(serializers.ModelSerializer):
+    avatar = serializers.CharField(read_only=True)
+
+    class Meta:
+        model = get_user_model()
+        fields = ['id', 'username', 'avatar']
+
+
+class UserProfileSerializer(BaseUserProfileSerializer):
+    extensions = serializers.PrimaryKeyRelatedField(
+        many=True,
+        read_only=True
+    )
+
+    class Meta(BaseUserProfileSerializer.Meta):
+        fields = BaseUserProfileSerializer.Meta.fields + ['extensions']
diff --git a/sweettooth/users/views.py b/sweettooth/users/views.py
new file mode 100644
index 0000000..3e2dcf1
--- /dev/null
+++ b/sweettooth/users/views.py
@@ -0,0 +1,29 @@
+"""
+    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 generics
+from rest_framework.response import Response
+
+from sweettooth.users.serializers import UserProfileSerializer
+from sweettooth.utils import gravatar_url
+
+User = get_user_model()
+
+class UserProfileDetailView(generics.RetrieveAPIView):
+    queryset = User.objects.all()
+    serializer_class = UserProfileSerializer
+
+    def retrieve(self, request, *args, **kwargs):
+        instance = self.get_object()
+        instance.avatar = gravatar_url(None, instance.email)
+        serializer = self.get_serializer(instance)
+        return Response(serializer.data)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]