Fix liniting errors

This commit is contained in:
Neil Martinsen-Burrell 2023-03-21 12:45:15 -05:00
parent 7c852de743
commit dd5d30a7f1
No known key found for this signature in database
GPG key ID: 6A3C818CC10D0184
8 changed files with 106 additions and 4 deletions

View file

@ -63,6 +63,11 @@ urlpatterns = [
),
path("domain/<int:pk>", views.DomainView.as_view(), name="domain"),
path("domain/<int:pk>/users", views.DomainUsersView.as_view(), name="domain-users"),
path(
"domain/<int:pk>/users/add",
views.DomainAddUserView.as_view(),
name="domain-users-add",
),
]

View file

@ -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()

View file

@ -0,0 +1,24 @@
{% extends "domain_base.html" %}
{% load static field_helpers %}
{% block title %}Add another user{% endblock %}
{% block domain_content %}
<h1>Add another user</h1>
<p>You can add another user to help manage your domain. They will need to sign
into the .gov registrar with their Login.gov account.
</p>
<form class="usa-form usa-form--large" method="post" novalidate>
{% csrf_token %}
{% input_with_errors form.email %}
<button
type="submit"
class="usa-button"
>Add user</button>
</form>
{% endblock %} {# domain_content #}

View file

@ -51,7 +51,7 @@
<li class="usa-sidenav__item">
{% url 'domain-users' pk=domain.id as url %}
<a href="{{ url }}"
{% if request.path == url %}class="usa-current"{% endif %}
{% if request.path|startswith:url %}class="usa-current"{% endif %}
>
User management
</a>

View file

@ -1,4 +1,5 @@
{% extends "domain_base.html" %}
{% load static %}
{% block title %}User management{% endblock %}
@ -30,4 +31,11 @@
aria-live="polite"
></div>
{% endif %}
<a class="usa-button usa-button--unstyled" href="{% url 'domain-users-add' pk=domain.id %}">
<svg class="usa-icon" aria-hidden="true" focusable="false" role="img" width="24" height="24">
<use xlink:href="{%static 'img/sprite.svg'%}#add_circle"></use>
</svg><span class="margin-left-05">Add another user</span>
</a>
{% endblock %} {# domain_content #}

View file

@ -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

View file

@ -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 *

View file

@ -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())