Skip to content

Commit

Permalink
feat: customize user login redirection and index page
Browse files Browse the repository at this point in the history
  • Loading branch information
SharleneNdinda committed Oct 26, 2023
1 parent 7557f21 commit c0e7e2c
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 16 deletions.
2 changes: 1 addition & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
]
AUTH_USER_MODEL = "users.User"

LOGIN_REDIRECT_URL = "wagtailadmin_home"
LOGIN_REDIRECT_URL = "wagtailadmin_login_redirect"
LOGIN_URL = "wagtailadmin_login"

# PASSWORDS
Expand Down
3 changes: 3 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
SignedURLView,
media_index,
)
from mycarehub.users.views import CustomLoginView, login_redirect

urlpatterns = [
path("sysadmin/", HomeView.as_view(), name="home"),
Expand Down Expand Up @@ -124,6 +125,8 @@
path("admin/images/", CustomImageIndexView.as_view(), name="wagtailimages-index"),
path("admin/media/", media_index, name="wagtailmedia-index"),
path("admin/documents/", CustomDocumentIndexView.as_view(), name="wagtaildocs-index"),
path("admin/login/", CustomLoginView.as_view(), name="wagtailadmin_login"),
path("admin/redirect/", login_redirect, name="wagtailadmin_redirect"),
path("admin/", include(wagtailadmin_urls)),
path("documents/", include(wagtaildocs_urls)),
re_path(
Expand Down
4 changes: 2 additions & 2 deletions mycarehub/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def content_item_with_tag_and_category(content_item_index, program, facility):
def initial_sms_content_item(content_item_index, sms_category, sms_tag, request_with_user):
"""Initial SMS content item fixture."""
initial_sms_content_item = SMSContentItem(
content="This is some sample content for testing purposes",
body="This is some sample content for testing purposes",
category=sms_category,
tag=sms_tag,
)
Expand All @@ -211,7 +211,7 @@ def initial_sms_content_item(content_item_index, sms_category, sms_tag, request_
def sms_content_item(content_item_index, sms_category, sms_tag, request_with_user):
"""Subsequent SMS content item fixture."""
sms_content_item = SMSContentItem(
content="Hello is some sample content for testing purposes",
body="Hello is some sample content for testing purposes",
category=sms_category,
tag=sms_tag,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.20 on 2023-10-25 07:39

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("content", "0017_alter_contentitem_facilities"),
]

operations = [
migrations.RenameField(
model_name="smscontentitem",
old_name="content",
new_name="body",
),
]
10 changes: 5 additions & 5 deletions mycarehub/content/models/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class SMSContentItem(Page):
)
sequence_number = models.IntegerField(null=True, blank=True)
sequence = models.CharField(max_length=10, null=True, blank=True)
content = models.TextField(
body = models.TextField(
max_length=160, help_text="Write out the body of the content to be sent to subscribers"
)

Expand All @@ -175,13 +175,13 @@ class SMSContentItem(Page):
],
heading="About",
),
FieldPanel("content"),
FieldPanel("body"),
]

# these fields determine the content that is indexed for search purposes
search_fields = Page.search_fields + [
index.SearchField("category"),
index.SearchField("content"),
index.SearchField("body"),
]

# this configuration allows these custom fields to be available over the API
Expand All @@ -193,7 +193,7 @@ class SMSContentItem(Page):
APIField("tag"),
APIField("sequence"),
APIField("sequence_number"),
APIField("content"),
APIField("body"),
]

# limit the parent page types
Expand Down Expand Up @@ -241,7 +241,7 @@ def save(self, *args, **kwargs):
We replace the title to make it easier for users to
keep track of their sms's.
"""
new_title = Truncator(self.content).chars(30)
new_title = Truncator(self.body).chars(30)
self.slug = slugify(new_title)
self.title = new_title
super().save(*args, **kwargs)
4 changes: 2 additions & 2 deletions mycarehub/content/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ def test_bypass_generate_sequence_after_save():
sms_content_item = SMSContentItem(
path="test",
depth=3,
content="This is some sample content for testing purposes",
body="This is some sample content for testing purposes",
category=category,
tag=tag,
)

sms_content_item.save()

sms_content_item.content = "This is a new title to test the save method"
sms_content_item.body = "This is a new title to test the save method"
sms_content_item.save()
assert sms_content_item.title == "This is a new title to test t…"

Expand Down
2 changes: 1 addition & 1 deletion mycarehub/content/tests/test_wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_set_sequence_number_after_page_create(
sms_tag,
):
sms_content_item = SMSContentItem(
content="Hello is some sample content for testing purposes",
body="Hello is some sample content for testing purposes",
category=sms_category,
tag=sms_tag,
)
Expand Down
84 changes: 84 additions & 0 deletions mycarehub/templates/wagtailadmin/pages/listing/_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{% load i18n %}
{% load l10n %}
{% load wagtailadmin_tags %}
<table class="listing {% if full_width %}full-width{% endif %} {% block table_classname %}{% endblock %}" {% if show_ordering_column %}aria-descrption="{% trans 'Press enter to select an item, use up and down arrows to move the item, press enter to complete the move or escape to cancel the current move.' %}"{% endif %}>
{% if show_ordering_column or show_bulk_actions %}
<col width="10px" />
{% endif %}
<col />
{% if show_parent %}
<col />
{% endif %}
<col width="12%" />
<col width="12%" />
<col width="12%" />
<col width="10%" />
<thead>
{% block post_parent_page_headers %}
{% endblock %}
</thead>
<tbody>
{% if pages %}
{% trans "Select page" as checkbox_aria_label %}
{% for page in pages %}
{% page_permissions page as page_perms %}
<tr {% if ordering == "ord" %}id="page_{{ page.id|unlocalize }}" data-page-title="{{ page.get_admin_display_title }}"{% endif %} class="{% if not page.live %}unpublished{% endif %} {% block page_row_classname %}{% endblock %}">
{% if show_ordering_column %}
<td class="ord">
{% if orderable and ordering == "ord" %}
<div class="handle icon icon-grip text-replace" tabindex="0" aria-live="polite" data-order-handle>
{% trans 'Drag' %}
<span data-order-label>Item {{ forloop.counter }} of {{ pages|length }}</span>
</div>
{% endif %}
</td>
{% elif show_bulk_actions %}
{% include "wagtailadmin/bulk_actions/listing_checkbox_cell.html" with obj_type="page" obj=page aria_labelledby_prefix="page_" aria_labelledby=page.pk|unlocalize aria_labelledby_suffix="_title" %}
{% endif %}
<td id="page_{{ page.pk|unlocalize }}_title" class="title" valign="top" data-listing-page-title>
{% block page_title %}
{% endblock %}
</td>
<td class="category" valign="top">
{% if page.category %}
{{ page.category }}
{% endif %}

{% for category in page.categories.all %}
{{ category.name }}
</a>{% if not forloop.last %}, {% endif %}
{% endfor %}
</td>
<td class="message" valign="top">{{ page.body|safe|truncatechars:70 }}</td>
{% if show_parent %}
<td class="parent" valign="top">
{% block page_parent_page_title %}
{% with page.get_parent as parent %}
{% if parent %}
<a href="{% url 'wagtailadmin_explore' parent.id %}">{{ parent.specific_deferred.get_admin_display_title }}</a>
{% endif %}
{% endwith %}
{% endblock %}
</td>
{% endif %}
<td class="updated" valign="top">
{% if page.latest_revision_created_at %}
{% human_readable_date page.latest_revision_created_at %}
{% endif %}
</td>
<td class="type" valign="top">{{ page.page_type_display_name }}</td>
<td class="status" valign="top">
{% include "wagtailadmin/shared/page_status_tag.html" with page=page %}
</td>
<td class="added-by" valign="top">
{{ page.owner }}
</td>
{% block page_navigation %}
{% endblock %}
</tr>
{% endfor %}
{% else %}
{% block no_results %}{% endblock %}
{% endif %}
</tbody>
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{% load i18n wagtailadmin_tags %}

{% comment %}

Table headers for the page listing, when in 'explore' mode. Expects the following variables:

sortable: if true, headings are links to wagtailadmin_explore with sort parameters applied.
sortable_by_type: must be true to allow sorting on the 'type' column
show_ordering_column: if true, an 'ordering' column is added.
orderable: if true, the 'ordering' column is populated (again with links to wagtailadmin_explore).

If either sortable or orderable is true, the following variables are also required:

parent_page: The page instance currently being browsed (used to generate the correct wagtailadmin_explore urls and title text)
ordering: the current sort parameter

{% endcomment %}

<tr class="table-headers">
{% if show_ordering_column %}
<th class="ord{% if orderable and ordering == 'ord' %} ord--active{% endif %}">
{% if orderable %}
{% if ordering == "ord" %}
<a href="{% url 'wagtailadmin_explore' parent_page.id %}" title="{% trans 'Disable ordering of child pages' %}">
{% icon name="order" %}{% trans 'Sort' %}
</a>
{% else %}
<a href="{% url 'wagtailadmin_explore' parent_page.id %}?ordering=ord" title="{% trans 'Enable ordering of child pages' %}">
{% icon name="order" %}{% trans 'Sort' %}
</a>
{% endif %}
{% endif %}
</th>
{% elif show_bulk_actions %}
{% include 'wagtailadmin/bulk_actions/select_all_checkbox_cell.html' with parent=parent_page.id %}
{% endif %}
<th class="title">
{% trans 'Title' as title_label %}
{% page_table_header_label label=title_label sortable=sortable sort_field='title' parent_page_title=parent_page.title %}
</th>
<th class="category">
{% trans 'Category' as category_label %}
{% page_table_header_label label=category_label parent_page_title=parent_page.title %}
</th>
<th class="message">
{% trans 'Message' as message_label %}
{% page_table_header_label label=message_label parent_page_title=parent_page.title %}
</th>
{% if show_parent %}
<th class="parent">
{% trans 'Parent' as parent_label %}
{% page_table_header_label label=parent_label sortable=0 parent_page_title=parent_page.title %}
</th>
{% endif %}
<th class="updated">
{% trans 'Last Updated' as updated_label %}
{% page_table_header_label label=updated_label sortable=sortable sort_field='latest_revision_created_at' parent_page_title=parent_page.title %}
</th>
<th class="type">
{% trans 'Type' as type_label %}

{% if sortable and sortable_by_type %}
{% page_table_header_label label=type_label sortable=1 sort_field='content_type' parent_page_title=parent_page.title %}
{% else %}
{% page_table_header_label label=type_label sortable=0 parent_page_title=parent_page.title %}
{% endif %}
</th>
<th class="status">
{% trans 'Status' as status_label %}
{% page_table_header_label label=status_label sortable=sortable sort_field='live' parent_page_title=parent_page.title %}
</th>
<th class="added-by">
{% trans 'Added By' as added_by_label %}
{% page_table_header_label label=added_by_label parent_page_title=parent_page.title %}
</th>
<th></th>
</tr>
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
{% block header_content %}
{{ block.super }}

<div class="container" style="margin-left: auto; margin-right: 160px;">
<div style="width: 300px; float: left; margin-right: 15px;">
<div class="container" style="margin-left: auto;">
<div style="width: 50%; float: left;">
<form class="search-form" action="{% url 'wagtailadmin_pages:search' %}" method="get">
<input type="text" style="font-size: 14px;" placeholder="Search for content ..." name="q"{% if query_string %} value="{{ query_string }}"{% endif %}>
</form>
Expand All @@ -22,9 +22,9 @@
{% endif %}
</div>

<div style="float: left;">
<div style="width: 50%; float: left;">
<a class="button bicolor button--icon" style="float:right;" href="{% url 'wagtailadmin_pages:add_subpage' parent_page.id %}">
{% icon name="plus" wrapped=1 %} {% trans 'Add Content' %}
{% icon name="plus" wrapped=1 %} {% trans 'Add New Content' %}
</a>
</div>
</div>
Expand Down
30 changes: 29 additions & 1 deletion mycarehub/users/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
from django.http import HttpRequest
from django.test import RequestFactory

from mycarehub.content.models.models import ContentItemIndexPage
from mycarehub.users.forms import UserChangeForm
from mycarehub.users.models import User
from mycarehub.users.tests.factories import UserFactory
from mycarehub.users.views import UserRedirectView, UserUpdateView, user_detail_view
from mycarehub.users.views import (
CustomLoginView,
UserRedirectView,
UserUpdateView,
login_redirect,
user_detail_view,
)

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -82,3 +89,24 @@ def test_not_authenticated(self, user: User, rf: RequestFactory):

response = user_detail_view(request, username=user.username)
assert response.status_code == 302


class TestCustomLoginView:
def test_get_succes_url(
self, content_item_index: ContentItemIndexPage, user: User, rf: RequestFactory
):
view = CustomLoginView()
request = rf.post("/fake-url")
request.user = user

view.request = request

assert view.get_success_url() == f"/admin/pages/{content_item_index.id}/"


def test_login_redirect(content_item_index: ContentItemIndexPage, user: User, rf: RequestFactory):
view = login_redirect
request = rf.post("/fake-url")
request.user = user

assert view(request) == f"/admin/pages/{content_item_index.id}/"
25 changes: 25 additions & 0 deletions mycarehub/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, RedirectView, UpdateView
from wagtail.admin.views.account import LoginView

from mycarehub.common.views import ApprovedMixin
from mycarehub.content.models.models import ContentItemIndexPage

User = get_user_model()

Expand Down Expand Up @@ -43,3 +45,26 @@ def get_redirect_url(self):


user_redirect_view = UserRedirectView.as_view()


class CustomLoginView(LoginView):
"""
Modifes `get_success_url` to redirect users to the program index page.
"""

def get_success_url(self):
user = self.request.user

# we show the first index page created for the current user's program
index_page = (
ContentItemIndexPage.objects.filter(program=user.program).order_by("id").first()
)
return reverse("wagtailadmin_explore", kwargs={"parent_page_id": index_page.id})


def login_redirect(request):
user = request.user

# we show the first index page created for the current user's program
index_page = ContentItemIndexPage.objects.filter(program=user.program).order_by("id").first()
return reverse("wagtailadmin_explore", kwargs={"parent_page_id": index_page.id})

0 comments on commit c0e7e2c

Please sign in to comment.