From d9da1b3cd95cdbe20344561b962dccf5912d3761 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Mon, 3 Aug 2020 22:36:09 +0200 Subject: [PATCH] add an API view to get username and teams, needed for POS system --- src/profiles/urls.py | 3 ++- src/profiles/views.py | 17 +++++++++++++++++ src/requirements/production.txt | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/profiles/urls.py b/src/profiles/urls.py index f658d629..a3dbe538 100644 --- a/src/profiles/urls.py +++ b/src/profiles/urls.py @@ -1,9 +1,10 @@ from django.urls import path -from .views import ProfileDetail, ProfileUpdate +from .views import ProfileApiView, ProfileDetail, ProfileUpdate app_name = "profiles" urlpatterns = [ path("", ProfileDetail.as_view(), name="detail"), path("edit/", ProfileUpdate.as_view(), name="update"), + path("api/", ProfileApiView.as_view(), name="api"), ] diff --git a/src/profiles/views.py b/src/profiles/views.py index 6477e38f..055ea532 100644 --- a/src/profiles/views.py +++ b/src/profiles/views.py @@ -2,6 +2,8 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.urls import reverse_lazy from django.views.generic import DetailView, UpdateView +from jsonview.views import JsonView +from oauth2_provider.views.generic import ProtectedResourceView from . import models @@ -34,3 +36,18 @@ class ProfileUpdate(LoginRequiredMixin, UpdateView): form.instance.save() messages.success(self.request, "Your profile has been updated.") return super().form_valid(form, **kwargs) + + +class ProfileApiView(JsonView, ProtectedResourceView): + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["user"] = {"username": self.request.user.username} + context["profile"] = { + "public_credit_name": self.request.user.profile.get_public_credit_name, + "description": self.request.user.profile.description, + } + context["teams"] = [ + {"team": team.name, "camp": team.camp.title} + for team in self.request.user.teams.all() + ] + return context diff --git a/src/requirements/production.txt b/src/requirements/production.txt index c4b15d5c..a2867033 100644 --- a/src/requirements/production.txt +++ b/src/requirements/production.txt @@ -14,6 +14,7 @@ django-cors-headers==3.4.0 django-extensions==3.0.3 django-filter==2.3.0 django-leaflet==0.27.0 +django-jsonview==2.0.0 django-oauth-toolkit==1.3.2 django-reversion==3.0.7 django-taggit==1.3.0