From d59a692884efc8a8f19da128023ec8b1e6b53f02 Mon Sep 17 00:00:00 2001 From: Daniel Xenes Date: Fri, 10 Feb 2023 14:29:36 -0500 Subject: [PATCH] adds peak exp changes --- neuvue_project/dashboard/views.py | 51 +++++++++++++++++-------- neuvue_project/neuvue/settings.py | 5 +++ neuvue_project/workspace/views/tools.py | 10 +++++ 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/neuvue_project/dashboard/views.py b/neuvue_project/dashboard/views.py index ab430fd8..78673885 100644 --- a/neuvue_project/dashboard/views.py +++ b/neuvue_project/dashboard/views.py @@ -40,21 +40,26 @@ def _format_time(x): def _get_status_count(task_df, status): return task_df["status"].value_counts().get(status, 0) - +# DEV NOTE: Hacky way to restrict dashboard to other group. class DashboardView(View, LoginRequiredMixin): def get(self, request, *args, **kwargs): - if not request.user.is_staff: + peak_admin = request.user.groups.filter(name=settings.PEAK_ADMIN).exists() + if not request.user.is_staff and not peak_admin: return redirect(reverse("index")) Namespaces = apps.get_model("workspace", "Namespace") - context = {} - context["all_groups"] = sorted([x.name for x in Group.objects.all()]) - context["all_groups"].append("See All Users") - context["all_namespaces"] = sorted( - [x.display_name for x in Namespaces.objects.all()] - ) - context["all_users"] = sorted([x.username for x in User.objects.all()]) + if peak_admin: + context["all_groups"] = [settings.PEAK_COHORT] + context["all_namespaces"] = [settings.PEAK_NAMESPACE] + context["all_users"] = _get_users_from_group(settings.PEAK_COHORT) + else: + context["all_groups"] = sorted([x.name for x in Group.objects.all()]) + context["all_groups"].append("See All Users") + context["all_namespaces"] = sorted( + [x.display_name for x in Namespaces.objects.all()] + ) + context["all_users"] = sorted([x.username for x in User.objects.all()]) return render(request, "admin_dashboard/dashboard.html", context) @@ -78,11 +83,16 @@ def post(self, request, *args, **kwargs): class DashboardNamespaceView(View, LoginRequiredMixin): def get(self, request, group=None, namespace=None, *args, **kwargs): - if not request.user.is_staff: + peak_admin = request.user.groups.filter(name=settings.PEAK_ADMIN).exists() + if not request.user.is_staff and not peak_admin: + return redirect(reverse("index")) + + if namespace != settings.PEAK_NAMESPACE: return redirect(reverse("index")) Namespaces = apps.get_model("workspace", "Namespace") + context = {} users = _get_users_from_group(group) table, counts = self._generate_table_and_counts(namespace, users) @@ -172,11 +182,17 @@ def _generate_table_and_counts(self, namespace: str, users: List): class DashboardUserView(View, LoginRequiredMixin): def get(self, request, username=None, filter=None, *args, **kwargs): - if not request.user.is_staff: + peak_admin = request.user.groups.filter(name=settings.PEAK_ADMIN).exists() + if not request.user.is_staff and not peak_admin: return redirect(reverse("index")) + if username not in _get_users_from_group(settings.PEAK_COHORT): + return redirect(reverse("index")) context = {} - table, counts = self._generate_table_and_counts(username) + if peak_admin: + table, counts = self._generate_table_and_counts(username, settings.PEAK_NAMESPACE) + else: + table, counts = self._generate_table_and_counts(username, settings.PEAK_NAMESPACE) context["username"] = username context["table"] = table @@ -188,14 +204,17 @@ def get(self, request, username=None, filter=None, *args, **kwargs): return render(request, "admin_dashboard/dashboard-user-view.html", context) - def _generate_table_and_counts(self, user: str): + def _generate_table_and_counts(self, user: str, namespace=None): table_rows = [] # Counts tc = tp = to = te = 0 + if namespace: + sieve = {"assignee": user,"namespace": namespace} + else: + sieve = {"assignee": user} + user_df = client.get_tasks( - sieve={ - "assignee": user, - }, + sieve=sieve, select=[ "opened", "closed", diff --git a/neuvue_project/neuvue/settings.py b/neuvue_project/neuvue/settings.py index aa5f0a8e..cc60bb28 100644 --- a/neuvue_project/neuvue/settings.py +++ b/neuvue_project/neuvue/settings.py @@ -238,3 +238,8 @@ mimetypes.add_type("application/javascript", ".js", True) STATIC_NG_FILES = os.listdir(os.path.join(BASE_DIR, "workspace", "static", "workspace")) + +# PEAK EXPERIMENT +PEAK_ADMIN = "AGTExpAdmins" +PEAK_COHORT = "PeakCohort" +PEAK_NAMESPACE = "peakExtension" \ No newline at end of file diff --git a/neuvue_project/workspace/views/tools.py b/neuvue_project/workspace/views/tools.py index 1aac229d..355821b3 100644 --- a/neuvue_project/workspace/views/tools.py +++ b/neuvue_project/workspace/views/tools.py @@ -1,6 +1,7 @@ import logging from django.shortcuts import render, redirect, reverse +from django.contrib.auth.models import Group, User from django.views.generic.base import View from django.conf import settings from neuvue.client import client @@ -22,6 +23,12 @@ # Get an instance of a logger logger = logging.getLogger(__name__) +def _get_users_from_group(group: str): + if group == "See All Users": # case if all users are queried + return [] + else: # case if group is provided + users = Group.objects.get(name=group).user_set.all() + return [x.username for x in users] class InspectTaskView(View): def get(self, request, task_id=None, *args, **kwargs): @@ -36,6 +43,9 @@ def get(self, request, task_id=None, *args, **kwargs): logging.warning(f"Unauthorized requests from {request.user}.") return redirect(reverse("index")) + if request.user.username in _get_users_from_group(settings.PEAK_NAMESPACE): + return redirect(reverse("index")) + if task_id is None: return render(request, "inspect.html", context)