From 557659acfd97ddaacdb89192ff6a575691b39093 Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Wed, 25 Mar 2020 19:21:04 +0000 Subject: [PATCH] Add user + user list to API Change-Id: I15cf0c33b9d452fda47f97e3d9b43e00c1bf9bfd Signed-off-by: Sawyer Bergeron --- src/api/models.py | 22 +++++++++++++++++++++- src/api/urls.py | 4 ++++ src/api/views.py | 10 ++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/api/models.py b/src/api/models.py index df9bbf7..e41a44d 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -33,7 +33,7 @@ from resource_inventory.models import ( ) from resource_inventory.idf_templater import IDFTemplater from resource_inventory.pdf_templater import PDFTemplater -from account.models import Downtime +from account.models import Downtime, UserProfile from dashboard.utils import AbstractModelQuery @@ -174,6 +174,26 @@ class LabManager(object): for profile in ResourceProfile.objects.filter(labs=self.lab)] return prof + def format_user(self, userprofile): + return { + "id": userprofile.user.id, + "username": userprofile.user.username, + "email": userprofile.email_addr, + "first_name": userprofile.user.first_name, + "last_name": userprofile.user.last_name, + "company": userprofile.company + } + + def get_users(self): + userlist = [self.format_user(profile) for profile in UserProfile.objects.select_related("user").all()] + + return json.dumps({"users": userlist}) + + def get_user(self, user_id): + profile = get_object_or_404(UserProfile, pk=user_id) + + return json.dumps(self.format_user(profile)) + def get_inventory(self): inventory = {} resources = ResourceQuery.filter(lab=self.lab) diff --git a/src/api/urls.py b/src/api/urls.py index 39f07df..0005d34 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -42,6 +42,8 @@ from api.views import ( lab_host, get_pdf, get_idf, + lab_users, + lab_user, GenerateTokenView ) @@ -59,5 +61,7 @@ urlpatterns = [ path('labs//jobs/new', new_jobs), path('labs//jobs/current', current_jobs), path('labs//jobs/done', done_jobs), + path('labs//users', lab_users), + path('labs//users/', lab_user), url(r'^token$', GenerateTokenView.as_view(), name='generate_token'), ] diff --git a/src/api/views.py b/src/api/views.py index bc01562..4e0d058 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -97,6 +97,16 @@ def lab_status(request, lab_name=""): return JsonResponse(lab_manager.set_status(request.POST), safe=False) return JsonResponse(lab_manager.get_status(), safe=False) +def lab_users(request, lab_name=""): + lab_token = request.META.get('HTTP_AUTH_TOKEN') + lab_manager = LabManagerTracker.get(lab_name, lab_token) + return HttpResponse(lab_manager.get_users(), content_type="text/plain") + +def lab_user(request, lab_name="", user_id=-1): + lab_token = request.META.get('HTTP_AUTH_TOKEN') + lab_manager = LabManagerTracker.get(lab_name, lab_token) + return HttpResponse(lab_manager.get_user(user_id), content_type="text/plain") + @csrf_exempt def update_host_bmc(request, lab_name="", host_id=""): -- 2.16.6