From dd5d30a7f1b9a7943e2a22b97e50ea181a2e3100 Mon Sep 17 00:00:00 2001 From: Neil Martinsen-Burrell Date: Tue, 21 Mar 2023 12:45:15 -0500 Subject: [PATCH] Fix liniting errors --- src/registrar/config/urls.py | 5 ++ src/registrar/fixtures.py | 5 +- src/registrar/templates/domain_add_user.html | 24 +++++++++ src/registrar/templates/domain_sidebar.html | 2 +- src/registrar/templates/domain_users.html | 8 +++ src/registrar/templatetags/url_helpers.py | 7 +++ src/registrar/views/__init__.py | 2 +- src/registrar/views/domain.py | 57 +++++++++++++++++++- 8 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/registrar/templates/domain_add_user.html diff --git a/src/registrar/config/urls.py b/src/registrar/config/urls.py index dcf592570..0d0ec89f5 100644 --- a/src/registrar/config/urls.py +++ b/src/registrar/config/urls.py @@ -63,6 +63,11 @@ urlpatterns = [ ), path("domain/", views.DomainView.as_view(), name="domain"), path("domain//users", views.DomainUsersView.as_view(), name="domain-users"), + path( + "domain//users/add", + views.DomainAddUserView.as_view(), + name="domain-users-add", + ), ] diff --git a/src/registrar/fixtures.py b/src/registrar/fixtures.py index 9f971ce18..4edca7cf6 100644 --- a/src/registrar/fixtures.py +++ b/src/registrar/fixtures.py @@ -254,6 +254,7 @@ class DomainApplicationFixture: except Exception as e: logger.warning(e) + class DomainFixture(DomainApplicationFixture): """Create one domain and permissions on it for each user.""" @@ -268,7 +269,9 @@ class DomainFixture(DomainApplicationFixture): for user in users: # approve one of each users investigating status domains - application = DomainApplication.objects.filter(creator=user, status=DomainApplication.INVESTIGATING).last() + application = DomainApplication.objects.filter( + creator=user, status=DomainApplication.INVESTIGATING + ).last() logger.debug(f"Approving {application} for {user}") application.approve() application.save() diff --git a/src/registrar/templates/domain_add_user.html b/src/registrar/templates/domain_add_user.html new file mode 100644 index 000000000..87b141570 --- /dev/null +++ b/src/registrar/templates/domain_add_user.html @@ -0,0 +1,24 @@ +{% extends "domain_base.html" %} +{% load static field_helpers %} + +{% block title %}Add another user{% endblock %} + +{% block domain_content %} +

Add another user

+ +

You can add another user to help manage your domain. They will need to sign + into the .gov registrar with their Login.gov account. +

+ +
+ {% csrf_token %} + + {% input_with_errors form.email %} + + +
+ +{% endblock %} {# domain_content #} diff --git a/src/registrar/templates/domain_sidebar.html b/src/registrar/templates/domain_sidebar.html index ae8444bea..7f3a66be6 100644 --- a/src/registrar/templates/domain_sidebar.html +++ b/src/registrar/templates/domain_sidebar.html @@ -51,7 +51,7 @@
  • {% url 'domain-users' pk=domain.id as url %} User management diff --git a/src/registrar/templates/domain_users.html b/src/registrar/templates/domain_users.html index 6f2f83ff8..d4ee4e361 100644 --- a/src/registrar/templates/domain_users.html +++ b/src/registrar/templates/domain_users.html @@ -1,4 +1,5 @@ {% extends "domain_base.html" %} +{% load static %} {% block title %}User management{% endblock %} @@ -30,4 +31,11 @@ aria-live="polite" > {% endif %} + + + Add another user + + {% endblock %} {# domain_content #} diff --git a/src/registrar/templatetags/url_helpers.py b/src/registrar/templatetags/url_helpers.py index 63ff9db6c..6201e61eb 100644 --- a/src/registrar/templatetags/url_helpers.py +++ b/src/registrar/templatetags/url_helpers.py @@ -8,3 +8,10 @@ register = template.Library() def namespaced_url(namespace, name="", **kwargs): """Get a URL, given its Django namespace and name.""" return reverse(f"{namespace}:{name}", kwargs=kwargs) + + +@register.filter("startswith") +def startswith(text, starts): + if isinstance(text, str): + return text.startswith(starts) + return False diff --git a/src/registrar/views/__init__.py b/src/registrar/views/__init__.py index 696c4640d..e1ae2cc32 100644 --- a/src/registrar/views/__init__.py +++ b/src/registrar/views/__init__.py @@ -1,5 +1,5 @@ from .application import * -from .domain import * +from .domain import DomainView, DomainUsersView, DomainAddUserView from .health import * from .index import * from .whoami import * diff --git a/src/registrar/views/domain.py b/src/registrar/views/domain.py index 600709dc4..48cd9443b 100644 --- a/src/registrar/views/domain.py +++ b/src/registrar/views/domain.py @@ -1,8 +1,13 @@ """View for a single Domain.""" +from django import forms +from django.db import IntegrityError +from django.shortcuts import redirect +from django.urls import reverse from django.views.generic import DetailView +from django.views.generic.edit import FormMixin -from registrar.models import Domain +from registrar.models import Domain, User, UserDomainRole from .utility import DomainPermission @@ -17,3 +22,53 @@ class DomainUsersView(DomainPermission, DetailView): model = Domain template_name = "domain_users.html" context_object_name = "domain" + + +class DomainAddUserForm(DomainPermission, forms.Form): + + """Form for adding a user to a domain.""" + + email = forms.EmailField(label="Email") + + def clean_email(self): + requested_email = self.cleaned_data["email"] + try: + User.objects.get(email=requested_email) + except User.DoesNotExist: + # TODO: send an invitation email to a non-existent user + raise forms.ValidationError("That user does not exist in this system.") + return requested_email + + +class DomainAddUserView(DomainPermission, FormMixin, DetailView): + template_name = "domain_add_user.html" + model = Domain + form_class = DomainAddUserForm + + def get_success_url(self): + return reverse("domain-users", kwargs={"pk": self.object.pk}) + + def post(self, request, *args, **kwargs): + self.object = self.get_object() + form = self.get_form() + if form.is_valid(): + return self.form_valid(form) + else: + return self.form_invalid(form) + + def form_valid(self, form): + """Add the specified user on this domain.""" + requested_email = form.cleaned_data["email"] + # look up a user with that email + # they should exist because we checked in clean_email + requested_user = User.objects.get(email=requested_email) + + try: + UserDomainRole.objects.create( + user=requested_user, domain=self.object, role=UserDomainRole.Roles.ADMIN + ) + except IntegrityError: + # User already has the desired role! Do nothing?? + pass + + return redirect(self.get_success_url())