add an API view to get username and teams, needed for POS system

This commit is contained in:
Thomas Steen Rasmussen 2020-08-03 22:36:09 +02:00
parent 51c95ddc9d
commit d9da1b3cd9
3 changed files with 20 additions and 1 deletions

View file

@ -1,9 +1,10 @@
from django.urls import path from django.urls import path
from .views import ProfileDetail, ProfileUpdate from .views import ProfileApiView, ProfileDetail, ProfileUpdate
app_name = "profiles" app_name = "profiles"
urlpatterns = [ urlpatterns = [
path("", ProfileDetail.as_view(), name="detail"), path("", ProfileDetail.as_view(), name="detail"),
path("edit/", ProfileUpdate.as_view(), name="update"), path("edit/", ProfileUpdate.as_view(), name="update"),
path("api/", ProfileApiView.as_view(), name="api"),
] ]

View file

@ -2,6 +2,8 @@ from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic import DetailView, UpdateView from django.views.generic import DetailView, UpdateView
from jsonview.views import JsonView
from oauth2_provider.views.generic import ProtectedResourceView
from . import models from . import models
@ -34,3 +36,18 @@ class ProfileUpdate(LoginRequiredMixin, UpdateView):
form.instance.save() form.instance.save()
messages.success(self.request, "Your profile has been updated.") messages.success(self.request, "Your profile has been updated.")
return super().form_valid(form, **kwargs) 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

View file

@ -14,6 +14,7 @@ django-cors-headers==3.4.0
django-extensions==3.0.3 django-extensions==3.0.3
django-filter==2.3.0 django-filter==2.3.0
django-leaflet==0.27.0 django-leaflet==0.27.0
django-jsonview==2.0.0
django-oauth-toolkit==1.3.2 django-oauth-toolkit==1.3.2
django-reversion==3.0.7 django-reversion==3.0.7
django-taggit==1.3.0 django-taggit==1.3.0