Skip to content

Commit

Permalink
feat: improve client admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ngurenyaga committed Nov 23, 2021
1 parent 17fcad6 commit 7618b53
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 12 deletions.
6 changes: 3 additions & 3 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@
"wagtail.search",
"wagtail.admin",
"wagtail.core",
"wagtail.contrib.modeladmin",
"wagtailvideos",
"wagtailmedia",
"taggit",
"modelcluster",
"wagtail.api.v2",
"wagtail.contrib.settings",
"wagtail.contrib.frontend_cache",
"wagtail.contrib.search_promotions",
"wagtail.contrib.modeladmin",
"django_extensions",
"wagtailvideos",
"wagtailmedia",
]

LOCAL_APPS = [
Expand Down
16 changes: 15 additions & 1 deletion mycarehub/clients/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from django.contrib.admin.decorators import display

from mycarehub.common.admin import BaseAdmin

Expand Down Expand Up @@ -34,7 +35,20 @@ class RelatedPersonAdmin(BaseAdmin):

@admin.register(Client)
class ClientAdmin(BaseAdmin):
pass
list_display = (
"get_user_name",
"client_type",
"enrollment_date",
)
date_hierarchy = "enrollment_date"
exclude = (
"fhir_patient_id",
"emr_health_record_id",
) # type: ignore

@display(ordering="user__name", description="User")
def get_user_name(self, obj):
return obj.user.name if obj.user else "-"


@admin.register(ClientFacility)
Expand Down
11 changes: 11 additions & 0 deletions mycarehub/clients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class IdentifierUse(models.TextChoices):
valid_to = models.DateTimeField(null=True, blank=True)
is_primary_identifier = models.BooleanField(default=False)

def __str__(self):
return f"{self.identifier_value} ({self.identifier_type}, {self.identifier_use})"


class SecurityQuestion(AbstractBase):
class ResponseType(models.TextChoices):
Expand Down Expand Up @@ -95,6 +98,9 @@ class RelationshipType(TextChoices):
related_name="related_person_contacts",
)

def __str__(self):
return f"{self.first_name} {self.other_name} {self.last_name} ({self.relationship_type})"


class Client(AbstractBase):
"""
Expand Down Expand Up @@ -233,6 +239,11 @@ class Languages(TextChoices):
blank=True,
)

def __str__(self):
return (
f"{self.user.name} ({self.client_type})" if self.user else f"{self.client_type} client"
)


class ClientFacility(AbstractBase):
"""
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions mycarehub/clients/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from django.contrib.admin.sites import AdminSite
from model_bakery import baker

from mycarehub.clients.admin import ClientAdmin
from mycarehub.clients.models import Client

pytestmark = pytest.mark.django_db


def test_client_admin_get_user_name(user_with_all_permissions):
admin = ClientAdmin(model=Client, admin_site=AdminSite())
client = baker.make(Client, user=user_with_all_permissions)
assert admin.get_user_name(client) == user_with_all_permissions.name
32 changes: 32 additions & 0 deletions mycarehub/clients/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from model_bakery import baker

from mycarehub.clients.models import Client, Identifier, RelatedPerson

pytestmark = pytest.mark.django_db


def test_identifier_str():
identifier = baker.make(
Identifier,
identifier_value="222",
identifier_type="NATIONAL_ID",
identifier_use="OFFICIAL",
)
assert str(identifier) == "222 (NATIONAL_ID, OFFICIAL)"


def test_related_person_str():
related_person = baker.make(
RelatedPerson,
first_name="Juha",
last_name="Mwenyewe",
other_name="Kalulu",
relationship_type="SPOUSE",
)
assert str(related_person) == "Juha Kalulu Mwenyewe (SPOUSE)"


def test_client_str(user_with_all_permissions):
client = baker.make(Client, user=user_with_all_permissions, client_type="PMTCT")
assert str(client) == f"{user_with_all_permissions.name} (PMTCT)"
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
from faker import Faker
from model_bakery import baker

from mycarehub.common.models.common_models import Facility
from mycarehub.common.tests.test_api import CRUDTestMixin

from .models import (
from mycarehub.clients.models import (
Client,
ClientFacility,
Identifier,
RelatedPerson,
SecurityQuestion,
SecurityQuestionResponse,
)
from mycarehub.common.models.common_models import Facility
from mycarehub.common.tests.test_api import CRUDTestMixin

fake = Faker()

Expand Down
7 changes: 6 additions & 1 deletion mycarehub/common/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
class BaseAdmin(admin.ModelAdmin):
readonly_fields = (
"created",
"created_by",
"updated",
"created_by",
"updated_by",
"deleted_at",
)
exclude = (
"created_by",
"updated_by",
"deleted_at",
)
Expand Down
21 changes: 21 additions & 0 deletions mycarehub/common/migrations/0011_alter_contact_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.9 on 2021-11-23 03:27

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('common', '0010_auto_20211119_0628'),
]

operations = [
migrations.AlterField(
model_name='contact',
name='user',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
26 changes: 26 additions & 0 deletions mycarehub/common/migrations/0012_auto_20211123_0636.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.9 on 2021-11-23 03:36

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('common', '0011_alter_contact_user'),
]

operations = [
migrations.AlterField(
model_name='contact',
name='flavour',
field=models.CharField(blank=True, choices=[('PRO', 'PRO'), ('CONSUMER', 'CONSUMER')], max_length=32, null=True),
),
migrations.AlterField(
model_name='contact',
name='user',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
12 changes: 10 additions & 2 deletions mycarehub/common/models/common_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ class AddressType(models.TextChoices):
postal_code = models.TextField()
country = models.CharField(max_length=255, choices=COUNTRY_CODES, default="KEN")

def __str__(self):
return f"{self.text} ({self.address_type})"


class Contact(AbstractBase):
class ContactType(models.TextChoices):
Expand All @@ -323,8 +326,13 @@ class FlavourChoices(models.TextChoices):
contact_type = models.CharField(choices=ContactType.choices, max_length=16)
contact_value = models.TextField(unique=True)
opted_in = models.BooleanField(default=False)
flavour = models.CharField(choices=FlavourChoices.choices, max_length=32, null=True)
user = models.ForeignKey(User, on_delete=models.PROTECT, null=True)
flavour = models.CharField(
choices=FlavourChoices.choices, max_length=32, null=True, blank=True
)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)

def __str__(self):
return f"{self.contact_value} ({self.contact_type})"


class AuditLog(AbstractBase):
Expand Down
11 changes: 11 additions & 0 deletions mycarehub/common/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
is_image_type,
unique_list,
)
from mycarehub.common.models.common_models import Address, Contact

fake = Faker()

Expand Down Expand Up @@ -669,3 +670,13 @@ def test_user_facility_allotment_by_both_facility_and_region(self):
allotment.save()

assert UserFacilityAllotment.get_facilities_for_allotment(allotment).count() == 5


def test_address_str():
addr = baker.make(Address, text="Wapi", address_type="BOTH")
assert str(addr) == "Wapi (BOTH)"


def test_contact_str():
contact = baker.make(Contact, contact_value="0722000000", contact_type="PHONE")
assert str(contact) == "0722000000 (PHONE)"
20 changes: 20 additions & 0 deletions mycarehub/users/migrations/0016_alter_userotp_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.9 on 2021-11-23 03:27

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('users', '0015_user_pin_change_required'),
]

operations = [
migrations.AlterField(
model_name='userotp',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
3 changes: 2 additions & 1 deletion mycarehub/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
UUIDField,
)
from django.db.models.base import Model
from django.db.models.deletion import CASCADE
from django.db.models.fields import DateField, DateTimeField, IntegerField
from django.db.utils import ProgrammingError
from django.urls import reverse
Expand Down Expand Up @@ -218,7 +219,7 @@ class UserOTP(Model):
UserOTP stores a user's OTP
"""

user = ForeignKey(User, on_delete=PROTECT)
user = ForeignKey(User, on_delete=CASCADE)
is_valid = BooleanField()
generated_at = DateTimeField(default=timezone.now)
valid_until = DateTimeField(null=True, blank=True)
Expand Down

0 comments on commit 7618b53

Please sign in to comment.