Skip to content

Commit

Permalink
Merge pull request #370 from muccg/user-deactivation-pass-expiry-impr…
Browse files Browse the repository at this point in the history
…ovements

User deactivation pass expiry improvements
  • Loading branch information
sztamas authored Feb 1, 2017
2 parents 59f9005 + 38dbc5c commit 26afd76
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
23 changes: 22 additions & 1 deletion rdrf/rdrf/patients_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
from django.shortcuts import render
from django.db.models import Q
from django.core.paginator import Paginator, InvalidPage
from django.utils.safestring import mark_safe
from useraudit.password_expiry import should_warn_about_password_expiry, days_to_password_expiry
from rdrf.models import Registry
from rdrf.form_progress import FormProgress
from rdrf.contexts_api import RDRFContextManager
from rdrf.components import FormsButton
from registry.patients.models import Patient
from .utils import Message

import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,14 +75,32 @@ def get_template(self):
return template

def build_context(self):
messages = self.maybe_add_messages()

return {
"registries": self.registries,
"location": "Patient Listing",
"patient_id": self.patient_id,
"registry_code": self.registry_model.code if self.registry_model else None,
"columns": [col.to_dict(i) for (i, col) in enumerate(self.get_configure_columns())]
"columns": [col.to_dict(i) for (i, col) in enumerate(self.get_configure_columns())],
"messages": messages,
}

def maybe_add_messages(self):
messages = [
self._password_about_to_expire_msg(),
]

return [m for m in messages if m]

def _password_about_to_expire_msg(self):
if should_warn_about_password_expiry(self.user):
days_left = days_to_password_expiry(self.user)
return Message.warning(mark_safe(
'Your password will expiry in %d days.'
'Please use <a href="%s" class="alert-link">Password Reset</a> to change it.' %
(days_left, reverse('password_reset'))))

def get_columns(self):
return [
ColumnFullName("Patient", "patients.can_see_full_name"),
Expand Down
4 changes: 3 additions & 1 deletion rdrf/rdrf/templates/rdrf_cdes/base-2-cols.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ <h3>
<div class="row">
<div class="col-md-offset-3 col-md-6">
{% for message in messages %}
<div {% if message.tags %} class="alert alert-{{ message.tags }}"{% endif %}>{{ message }}</div>
<div {% if message.tags %} class="alert alert-{{ message.tags }}"{% endif %}>
{{ message }}
</div>
{% endfor %}
</div>
</div>
Expand Down
29 changes: 29 additions & 0 deletions rdrf/rdrf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,32 @@ def wrap(key, value):
return value

return { key: wrap(key, value) for key, value in list(post_files_data.items()) }


class Message():
def __init__(self, text, tags=None):
self.text = text
self.tags = tags

@staticmethod
def success(text):
return Message(text, tags='success')

@staticmethod
def info(text):
return Message(text, tags='info')

@staticmethod
def warning(text):
return Message(text, tags='warning')

@staticmethod
def danger(text):
return Message(text, tags='danger')

@staticmethod
def error(text):
return Message(text, tags='danger')

def __repr__(self):
return self.text
26 changes: 14 additions & 12 deletions rdrf/registry/groups/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .admin_forms import UserChangeForm, RDRFUserCreationForm
from .models import WorkingGroup
from useraudit.password_expiry import is_password_expired
from useraudit.models import UserDeactivation

import logging

Expand All @@ -30,7 +30,7 @@ class CustomUserAdmin(UserAdmin):
form = UserChangeForm
add_form = RDRFUserCreationForm

list_display = ('username', 'email', 'get_working_groups', 'get_registries', 'is_active_expired')
list_display = ('username', 'email', 'get_working_groups', 'get_registries', 'status')

def get_form(self, request, obj=None, **kwargs):
user = get_user_model().objects.get(username=request.user)
Expand Down Expand Up @@ -121,16 +121,18 @@ def get_registries(self, obj):
ordering = ('email',)
filter_horizontal = ()

def is_active_expired(self, obj):
if is_password_expired(obj):
return format_html(
'{}<a href="{}">Password Expired</a> (<a href="{}../password/">Change</a>)',
"" if obj.is_active else "Inactive, ",
reverse('password_reset'),
reverse('%s:groups_customuser_change' % self.admin_site.name,
args=(obj.pk,)))
return "Yes" if obj.is_active else "No"
is_active_expired.short_description = "Is active"
def status(self, user):
if user.is_active:
return 'Active'
choices = dict(UserDeactivation.DEACTIVATION_REASON_CHOICES)
last_deactivation = UserDeactivation.objects.filter(username=user.username).order_by('-timestamp').first()
if last_deactivation is None or last_deactivation.reason not in choices:
return 'Inactive'

reason = choices[last_deactivation.reason]

return 'Inactive (%s)' % reason


admin.site.register(get_user_model(), CustomUserAdmin)
admin.site.register(WorkingGroup, WorkingGroupAdmin)
2 changes: 1 addition & 1 deletion rdrf/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"django-storages==1.4.1",
"django-templatetag-handlebars==1.3.1",
"django-templatetag-sugar==1.0",
"django-useraudit==1.4.0",
"django-useraudit==1.5.1",
"geoip2==2.4.0",
"jsonschema==2.5.1",
"openpyxl==2.3.5",
Expand Down

0 comments on commit 26afd76

Please sign in to comment.