def post(self, request: HttpRequest, account_pk: int, team_pk: typing.Optional[int] = None) -> HttpResponse: self.perform_account_fetch(request.user, account_pk) if not self.has_permission(AccountPermissionType.ADMINISTER): raise PermissionDenied team = fetch_team_for_account(self.account, team_pk) form = TeamForm(request.POST, instance=team) if form.is_valid(): form.save() if team_pk is None: update_verb = "created" else: update_verb = "updated" messages.success( request, "Team '{}' was {} successfully".format(team.name, update_verb)) return redirect( reverse('account_team_list', args=(self.account.id, ))) return render( request, "accounts/team_detail.html", self.get_render_context({ "team": team, "account": self.account, "form": form }))
def get( self, request: HttpRequest, account_name: str, team_pk: typing.Optional[int] = None, ) -> HttpResponse: self.perform_account_fetch(request.user, account_name) assert self.account_fetch_result is not None max_teams_redirect = self.test_team_create_quota(request, team_pk) if max_teams_redirect: return max_teams_redirect team = fetch_team_for_account(self.account_fetch_result.account, team_pk) form = TeamForm(instance=team) if not self.has_permission(AccountPermissionType.ADMINISTER): for field in form.fields.values(): field.disabled = True return render( request, "accounts/team_detail.html", self.get_render_context({ "tab": "teams", "team_tab": "metadata", "team": team, "account": self.account, "form": form, }), )
def post(self, request: HttpRequest, account_pk: int, team_pk: int) -> HttpResponse: self.perform_account_fetch(request.user, account_pk) if not self.has_permission(AccountPermissionType.ADMINISTER): raise PermissionDenied team = fetch_team_for_account(self.account, team_pk) action = request.POST.get('action') if action in ('add_member', 'remove_member'): username = request.POST['name'] if username: user = User.objects.get(username=username) if action == 'add_member': if user not in team.members.all(): team.members.add(user) messages.success( request, "{} was added to team {}.".format( user.username, team)) elif action == 'remove_member': if user in team.members.all(): team.members.remove(user) messages.success( request, "{} was removed from team {}.".format( user.username, team)) return redirect( reverse('account_team_members', args=(account_pk, team_pk)))
def get(self, request: HttpRequest, account_pk: int, team_pk: int) -> HttpResponse: self.perform_account_fetch(request.user, account_pk) team = fetch_team_for_account(self.account, team_pk) return render( request, "accounts/team_members.html", self.get_render_context({ "account": self.account, "team": team }))
def post( self, request: HttpRequest, account_name: str, team_pk: typing.Optional[int] = None, ) -> HttpResponse: self.request_permissions_guard( request, account_name, permission=AccountPermissionType.ADMINISTER) team = fetch_team_for_account(self.account, team_pk) if request.POST.get("action") == "remove_team" and team_pk: team.delete() messages.success( request, "Team <em>{}</em> was deleted successfully.".format( escape(team.name)), extra_tags="safe", ) return redirect("account_team_list", self.account.name) max_teams_redirect = self.test_team_create_quota(request, team_pk) if max_teams_redirect: return max_teams_redirect form = TeamForm(request.POST, instance=team) if form.is_valid(): form.save() if team_pk is None: update_verb = "created" else: update_verb = "updated" messages.success( request, "Team <em>{}</em> was {} successfully.".format( escape(team.name), update_verb), extra_tags="safe", ) return redirect("account_team_list", self.account.name) return render( request, "accounts/team_detail.html", self.get_render_context({ "tab": "teams", "team_tab": "metadata", "team": team, "account": self.account, "form": form, }), )
def get(self, request: HttpRequest, account_name: str, team_pk: int) -> HttpResponse: self.request_permissions_guard(request, account_name) team = fetch_team_for_account(self.account, team_pk) project_roles = ProjectRole.objects.all() all_projects = self.account.projects.all() existing_project_roles = ProjectAgentRole.objects.filter( project__in=all_projects, agent_id=team.pk, content_type=ContentType.objects.get_for_model(Team), ) assigned_projects = list( map(attrgetter("project"), existing_project_roles)) unassigned_projects = filter(lambda p: p not in assigned_projects, all_projects) # this filtering can't be done by the ORM due to using GenericForeignKey # Shouldn't be dealing with millions of projects per account so should be OK return render( request, "accounts/team_projects.html", self.get_render_context({ "tab": "teams", "team_tab": "projects", "account": self.account, "team": team, "total_project_count": all_projects.count(), "existing_project_roles": existing_project_roles, "unassigned_projects": list(unassigned_projects), "project_roles": project_roles, "project_roles_map": json.dumps({ existing_project_role.pk: existing_project_role.role_id for existing_project_role in existing_project_roles }), "AGENT_ROLE_ID_PREFIX": AGENT_ROLE_ID_PREFIX, }), )
def get(self, request: HttpRequest, account_pk: int, team_pk: typing.Optional[int] = None) -> HttpResponse: self.perform_account_fetch(request.user, account_pk) team = fetch_team_for_account(self.account_fetch_result.account, team_pk) form = TeamForm(instance=team) if not self.has_permission(AccountPermissionType.ADMINISTER): for field in form.fields.values(): field.disabled = True return render( request, "accounts/team_detail.html", self.get_render_context({ "team": team, "account": self.account, "form": form }))
def post(self, request: HttpRequest, account_name: str, team_pk: int) -> HttpResponse: self.request_permissions_guard( request, account_name, permission=AccountPermissionType.ADMINISTER) team = fetch_team_for_account(self.account, team_pk) action = request.POST.get("action") if action in ("add_member", "remove_member"): username = request.POST["name"] if username: try: user = User.objects.get(username=username) except User.DoesNotExist: messages.error( request, 'User "{}" does not exist.'.format(username)) else: if action == "add_member": if user not in team.members.all(): team.members.add(user) messages.success( request, "{} was added to team {}.".format( user.username, team), ) elif action == "remove_member": if user in team.members.all(): team.members.remove(user) messages.success( request, "{} was removed from team {}.".format( user.username, team), ) return redirect("account_team_members", self.account.name, team_pk)
def get(self, request: HttpRequest, account_name: str, team_pk: int) -> HttpResponse: self.request_permissions_guard(request, account_name) team = fetch_team_for_account(self.account, team_pk) current_members = list(map(lambda u: u.username, team.members.all())) return render( request, "accounts/team_members.html", self.get_render_context({ "tab": "teams", "team_tab": "members", "account": self.account, "team": team, "current_members": json.dumps(current_members), }), )
def post(self, request: HttpRequest, account_pk: int, team_pk: int) -> HttpResponse: self.perform_account_fetch(request.user, account_pk) if not self.has_permission(AccountPermissionType.ADMINISTER): raise PermissionDenied team = fetch_team_for_account(self.account, team_pk) all_roles = ProjectRole.objects.all() role_lookup = {role.pk: role for role in all_roles} if request.POST.get('action') == 'add_project': role = role_lookup[int(request.POST['role_id'])] project = Project.objects.get(pk=request.POST['project_id']) ProjectAgentRole.objects.update_or_create( {'role': role}, agent_id=team.pk, content_type=ContentType.objects.get_for_model(Team), project=project) messages.success( request, "Project access to {} for {} was saved.".format( project.name, team.name)) elif request.POST.get('action') == 'remove_project': project_agent_role = ProjectAgentRole.objects.get( pk=request.POST['agent_role_id']) project = project_agent_role.project project_agent_role.delete() messages.success( request, "Project access to {} for {} was removed.".format( project.name, team.name)) elif request.POST.get('action') == 'set_role': role = role_lookup[int(request.POST['role_id'])] project_agent_role = ProjectAgentRole.objects.get( pk=request.POST['project_agent_role_id']) if project_agent_role.agent_type != AgentType.TEAM: return JsonResponse({ 'success': False, 'message': 'The role you trying to update is not for a team.' }) if project_agent_role.team != team: return JsonResponse({ 'success': False, 'message': 'The role you trying to update is not for this team.' }) project_agent_role.role = role project_agent_role.save() return JsonResponse({ 'success': True, 'message': "Access to the project '{}' was updated for this team.".format( project_agent_role.project.get_name()) }) return redirect( reverse("account_team_projects", args=(self.account.pk, team.pk)))
def post(self, request: HttpRequest, account_name: str, team_pk: int) -> HttpResponse: self.request_permissions_guard( request, account_name, permission=AccountPermissionType.ADMINISTER) team = fetch_team_for_account(self.account, team_pk) all_roles = ProjectRole.objects.all() role_lookup = {role.pk: role for role in all_roles} if request.POST.get("action") == "add_project": role = role_lookup[int(request.POST["role_id"])] project = Project.objects.get(pk=request.POST["project_id"]) ProjectAgentRole.objects.update_or_create( {"role": role}, agent_id=team.pk, content_type=ContentType.objects.get_for_model(Team), project=project, ) messages.success( request, "Project access to {} for {} was saved.".format( project.name, team.name), ) elif request.POST.get("action") == "remove_project": project_agent_role = ProjectAgentRole.objects.get( pk=request.POST["agent_role_id"]) project = project_agent_role.project project_agent_role.delete() messages.success( request, "Project access to {} for {} was removed.".format( project.name, team.name), ) elif request.POST.get("action") == "set_role": role = role_lookup[int(request.POST["role_id"])] project_agent_role = ProjectAgentRole.objects.get( pk=request.POST["project_agent_role_id"]) if project_agent_role.agent_type != AgentType.TEAM: return JsonResponse({ "success": False, "message": "The role you trying to update is not for a team.", }) if project_agent_role.team != team: return JsonResponse({ "success": False, "message": "The role you trying to update is not for this team.", }) project_agent_role.role = role project_agent_role.save() return JsonResponse({ "success": True, "message": "Access to the project '{}' was updated for this team.".format( project_agent_role.project.get_name()), }) return redirect("account_team_projects", self.account.name, team.pk)