From 86a2efb339e879aeafd48c4115eca6d9ff1c0e75 Mon Sep 17 00:00:00 2001
From: rlacko <rlacko@sch.bme.hu>
Date: Mon, 17 Feb 2020 20:10:56 +0100
Subject: [PATCH] monitor endpoint with api key

---
 requirements/production.in     |  1 +
 requirements/production.txt    |  1 +
 src/account/serializers.py     | 26 ++++++++++++++++++++++++++
 src/account/urls.py            |  2 ++
 src/account/views.py           |  9 +++++++++
 src/kszkepzes/settings/base.py |  1 +
 6 files changed, 40 insertions(+)

diff --git a/requirements/production.in b/requirements/production.in
index 71c1587..2ef446c 100644
--- a/requirements/production.in
+++ b/requirements/production.in
@@ -8,3 +8,4 @@ python-language-server==0.28.2
 drf-yasg==1.16.1
 packaging==19.1
 Pillow==7.0.0
+djangorestframework-api-key==1.4.1
\ No newline at end of file
diff --git a/requirements/production.txt b/requirements/production.txt
index c0742ac..7cdc80b 100644
--- a/requirements/production.txt
+++ b/requirements/production.txt
@@ -17,6 +17,7 @@ django-import-export==1.2.0
 django-social-authsch==0.1
 django-solo==1.1.3
 django==2.2.4
+djangorestframework-api-key==1.4.1
 djangorestframework==3.10.2
 drf-yasg==1.16.1
 entrypoints==0.3          # via flake8
diff --git a/src/account/serializers.py b/src/account/serializers.py
index e057bbd..ffd2a12 100644
--- a/src/account/serializers.py
+++ b/src/account/serializers.py
@@ -124,3 +124,29 @@ class ProfileSerializer_Staff(serializers.ModelSerializer):
 
     def get_full_name(self, obj):
         return obj.full_name
+
+
+class MonitoringSerializer(serializers.ModelSerializer):
+    full_name = serializers.SerializerMethodField()
+    email = serializers.SerializerMethodField()
+
+    class Meta:
+        model = models.Profile
+        read_only_fields = (
+            'full_name',
+            'email',
+            'events_visited',
+            'homework_bits',
+        )
+        fields = (
+            'full_name',
+            'email',
+            'events_visited',
+            'homework_bits',
+        )
+
+    def get_full_name(self, obj):
+        return obj.full_name
+
+    def get_email(self, obj):
+        return obj.user.email
diff --git a/src/account/urls.py b/src/account/urls.py
index ebf85f3..b46b198 100644
--- a/src/account/urls.py
+++ b/src/account/urls.py
@@ -4,4 +4,6 @@ from . import views
 router = routers.DefaultRouter()
 
 router.register(r'profiles', views.ProfileViewSet, base_name='profile')
+router.register(r'monitoring/profiles', views.MonitorinViewSet, base_name='monitoring')
+
 urlpatterns = router.urls
diff --git a/src/account/views.py b/src/account/views.py
index 4f88c51..b579b83 100644
--- a/src/account/views.py
+++ b/src/account/views.py
@@ -3,11 +3,20 @@ from rest_framework import permissions
 from rest_framework.response import Response
 from rest_framework.decorators import action
 from common.permissions import IsSafeOrPatch
+from rest_framework_api_key.permissions import HasAPIKey
 
 from . import models
 from . import serializers
 
 
+class MonitorinViewSet(viewsets.ModelViewSet):
+    serializer_class = serializers.MonitoringSerializer
+    permission_classes = (HasAPIKey,)
+
+    def get_queryset(self):
+        return models.Profile.objects.filter(role='Student')
+
+
 class ProfileViewSet(viewsets.ModelViewSet):
     serializer_class = serializers.ProfileSerializer_User
     permission_classes = (permissions.IsAuthenticated, IsSafeOrPatch)
diff --git a/src/kszkepzes/settings/base.py b/src/kszkepzes/settings/base.py
index a1ce1fb..fa4afc6 100644
--- a/src/kszkepzes/settings/base.py
+++ b/src/kszkepzes/settings/base.py
@@ -40,6 +40,7 @@ INSTALLED_APPS = [
     'django_extensions',
     'import_export',
     'rest_framework',
+    "rest_framework_api_key",
     'social_django',
     'solo',
     'authsch',
-- 
GitLab