Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow manually adding hardware to user #450

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions back/admin/people/templates/_toggle_button_hardware.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% load i18n %}
<div hx-post="{% url 'people:toggle_hardware' object.id template.id %}" hx-swap="outerHTML" classes="click btn-loading" class="btn {% if template in object.hardware.all %}btn-primary{% else %}btn-white{% endif %}" hx-indicator="#spinner-{{template.id}}">
<span class="requesting alsodisplay spinner-border spinner-border-sm me-2" id="spinner-{{template.id}}" role="status"></span>
{% if template in object.hardware.all %}
{% translate "Added" %}
{% else %}
{% translate "Add" %}
{% endif %}
</div>
38 changes: 38 additions & 0 deletions back/admin/people/templates/add_hardware.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends 'admin_base.html' %}
{% load i18n %}

{% block content %}
<div class="col-12">

<div class="alert alert-info">
<p>{% translate "Changing hardware here will not send any messages to colleagues to send out or collect hardware. Use the sequences for that instead!"%}</p>
</div>

<div class="card">
<div class="table-responsive">
<table
class="table table-vcenter table-nowrap">
<thead>
<tr>
<th>{% translate "Name" %}</th>
<th>{% translate "Tags" %}</th>
<th></th>
</tr>
</thead>
<tbody>
{% for template in object_list %}
<tr>
<td>{{ template.name }}</td>
<td>{% for tag in template.tags %}<span class="badge bg-blue" style="margin-right: 10px">{{ tag }}</span>{% endfor %}</td>
<td class="text-end">
{% include "_toggle_button_hardware.html" %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% include "_paginator.html" %}
</div>
</div>
</div>
{% endblock %}
5 changes: 5 additions & 0 deletions back/admin/people/templates/colleague_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ <h3 class="card-title">{% translate "Hardware" %}</h3>
</div>
{% endfor %}
</div>
<div class="card-footer">
<a href="{% url 'people:add_hardware' object.id %}" class="btn btn-primary">
{% translate "Change" %}
</a>
</div>
</div>
</div>
</div>
Expand Down
54 changes: 54 additions & 0 deletions back/admin/people/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,60 @@ def test_employee_toggle_resources(
assert not employee1.resources.filter(id=resource1.id).exists()


@pytest.mark.django_db
def test_employee_hardware(
client, django_user_model, employee_factory, hardware_factory
):
client.force_login(
django_user_model.objects.create(role=get_user_model().Role.ADMIN)
)

employee1 = employee_factory()
hardware1 = hardware_factory()
hardware2 = hardware_factory(template=False)

url = reverse("people:add_hardware", args=[employee1.id])
response = client.get(url, follow=True)

assert hardware1.name in response.content.decode()
# Only show templates
assert hardware2.name not in response.content.decode()
assert "Added" not in response.content.decode()

# Add resource to user
employee1.hardware.add(hardware1)

response = client.get(url, follow=True)

# Has been added, so change button name
assert hardware2.name not in response.content.decode()
assert "Added" in response.content.decode()


@pytest.mark.django_db
def test_employee_toggle_hardware(
client, django_user_model, employee_factory, hardware_factory
):
client.force_login(
django_user_model.objects.create(role=get_user_model().Role.ADMIN)
)

hardware1 = hardware_factory()
employee1 = employee_factory()

url = reverse("people:toggle_hardware", args=[employee1.id, hardware1.id])
response = client.post(url, follow=True)

assert "Added" in response.content.decode()
assert employee1.hardware.filter(id=hardware1.id).exists()

# Now remove the item
response = client.post(url, follow=True)

assert "Add" in response.content.decode()
assert not employee1.resources.filter(id=hardware1.id).exists()


@pytest.mark.django_db
def test_visibility_import_employees_button(
client,
Expand Down
10 changes: 10 additions & 0 deletions back/admin/people/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@
views.ColleagueResourceView.as_view(),
name="add_resource",
),
path(
"colleagues/<int:pk>/hardware/",
views.ColleagueHardwareView.as_view(),
name="add_hardware",
),
path(
"colleagues/<int:pk>/hardware/<int:template_id>/",
views.ColleagueToggleHardwareView.as_view(),
name="toggle_hardware",
),
path(
"colleagues/<int:pk>/resource/<int:template_id>/",
views.ColleagueToggleResourceView.as_view(),
Expand Down
31 changes: 31 additions & 0 deletions back/admin/people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from rest_framework.authentication import SessionAuthentication

from admin.admin_tasks.models import AdminTask
from admin.hardware.models import Hardware
from admin.integrations.exceptions import (
DataIsNotJSONError,
FailedPaginatedResponseError,
Expand Down Expand Up @@ -117,6 +118,36 @@ def get_context_data(self, **kwargs):
return context


class ColleagueHardwareView(LoginRequiredMixin, ManagerPermMixin, DetailView):
template_name = "add_hardware.html"
model = get_user_model()

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user = context["object"]
context["title"] = _("Add new hardware for %(name)s") % {"name": user.full_name}
context["subtitle"] = _("Employee")
context["object_list"] = Hardware.templates.all()
return context


class ColleagueToggleHardwareView(LoginRequiredMixin, ManagerPermMixin, View):
template_name = "_toggle_button_hardware.html"

def post(self, request, pk, template_id, *args, **kwargs):
context = {}
user = get_object_or_404(get_user_model(), id=pk)
hardware = get_object_or_404(Hardware, id=template_id, template=True)
if user.hardware.filter(id=hardware.id).exists():
user.hardware.remove(hardware)
else:
user.hardware.add(hardware)
context["id"] = id
context["template"] = hardware
context["object"] = user
return render(request, self.template_name, context)


class ColleagueResourceView(LoginRequiredMixin, ManagerPermMixin, DetailView):
template_name = "add_resources.html"
model = get_user_model()
Expand Down