Skip to content

Commit

Permalink
Merge pull request #87 from GeriLife/add-user-to-home-form
Browse files Browse the repository at this point in the history
Add user to home form
  • Loading branch information
brylie authored Jan 7, 2024
2 parents 314c086 + 0a5cfbe commit e5bf148
Show file tree
Hide file tree
Showing 9 changed files with 544 additions and 5 deletions.
9 changes: 9 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
from pathlib import Path

from django.contrib.messages import constants as message_constants
from django.utils.translation import gettext_lazy as _

import environ
Expand Down Expand Up @@ -178,3 +179,11 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

MESSAGE_TAGS = {
message_constants.DEBUG: "alert-info",
message_constants.INFO: "alert-info",
message_constants.SUCCESS: "alert-success",
message_constants.WARNING: "alert-warning",
message_constants.ERROR: "alert-danger",
}
5 changes: 5 additions & 0 deletions homes/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django import forms


class AddCaregiverForm(forms.Form):
email = forms.EmailField()
7 changes: 7 additions & 0 deletions homes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ def has_access(self, user: user_model) -> bool:
"""
return user.is_superuser or user in self.members.all()

def user_can_manage(self, user: user_model) -> bool:
"""Returns True if the user can manage this home.
- Superusers can manage all homes.
"""
return user.is_superuser

@property
def current_residents(self) -> models.QuerySet["Resident"]:
"""Returns a QuerySet of all current residents for this home."""
Expand Down
13 changes: 12 additions & 1 deletion homes/templates/homes/home_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@

{% block content %}
<div class="container">
<h1>{{ home.name }}</h1>
<div class="d-flex justify-content-between align-items-center">
<h1>{{ home.name }}</h1>
{% if user_can_manage %}
<a
href="{% url 'home-user-relation-list-view' home.url_uuid %}"
class="btn btn-outline-primary">
<i class="bi bi-people"></i>
{% translate "Manage Caregivers" %}
</a>
{% endif %}
</div>


<p>{% translate "Percent of residents by activity level" %}</p>

Expand Down
107 changes: 107 additions & 0 deletions homes/templates/homes/home_user_relation_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{% extends "base.html" %}

{% load i18n %}
{% load crispy_forms_tags %}

{% block content %}

<div class="d-flex justify-content-between align-items-center">
<h1>
<a
href="{{ home.get_absolute_url }}"
class="btn btn-outline-primary">
<i class="bi bi-house"></i>
{{ home }}
</a>

{% translate "Manage Caregivers" %}
</h1>

<button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#addCaregiverModal">
<i class="bi bi-person-plus"></i>
{% translate "Add Caregiver" %}
</button>
</div>

{% if home_user_relations %}
<ul class="list-group">
{% for user_relation in home_user_relations %}
<li class="list-group-item d-flex justify-content-between align-items-center">
<span>
{{ user_relation.user.username }}
</span>

<div>
{% if user_relation.user.is_superuser %}
<span class="badge bg-primary rounded-pill">
{% translate "Superuser" %}
</span>
{% endif %}

{% if user_relation.is_manager %}
<span class="badge bg-primary rounded-pill">
{% translate "Manager" %}
</span>
{% endif %}
</div>
</li>
{% endfor %}
</ul>
{% else %}
<div class="alert alert-info">
{% translate "No caregivers found." %}
</div>
{% endif %}

<div
class="modal fade"
id="addCaregiverModal"
tabindex="-1"
aria-labelledby="addCaregiverModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="" method="post">
{% csrf_token %}
<div class="modal-header">
<h2 class="modal-title" id="addCaregiverModalLabel">
{% translate "Add Caregiver" %}
</h2>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="{% translate 'Close' %}">
</button>
</div>

<div class="modal-body">
<p>{% translate "Enter the email of the caregiver you want to add." %}</p>

{{ form|crispy }}
</div>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
{% translate "Close" %}
</button>
<button type="submit" class="btn btn-primary">
{% translate "Add Caregiver" %}
</button>
</div>
</form>
</div>
</div>
</div>
{% endblock content %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', (event) => {
{% if form.errors %}
var myModal = new bootstrap.Modal(document.getElementById('addCaregiverModal'));
myModal.show();
{% endif %}
});
</script>
{% endblock extra_js %}
Loading

0 comments on commit e5bf148

Please sign in to comment.