Skip to content

Commit

Permalink
Merge 437bf08 into f3e8cd0
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Feb 28, 2019
2 parents f3e8cd0 + 437bf08 commit 266863b
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 25 deletions.
2 changes: 2 additions & 0 deletions changelog.md
@@ -1,5 +1,7 @@
### 0.14.0 (Major Release)

A User's UserProfile is now automatically created when you create a user in a post save signal.

### 0.13.1 (Minor Release)

Upgrades the setup.py Django version from 2.0.9 to 2.0.13. Removes the six library dependency from setup.py.
Expand Down
4 changes: 4 additions & 0 deletions doc/docs/reference/upgrading.md
Expand Up @@ -3,6 +3,10 @@
This document provides instructions for specific steps required to upgrading your Opal
application to a later version where there are extra steps required.

### 0.13.1 -> 0.14.0
If you were creating a UserProfile, this will now fail as a UserProfile is created automatically after creating a user (by a signal).


### 0.13.0 -> 0.13.1

Upgrades django (minor security upgrade).
Expand Down
2 changes: 1 addition & 1 deletion opal/core/episodes.py
Expand Up @@ -41,7 +41,7 @@ def episode_visible_to(kls, episode, user):
"""
from opal.models import UserProfile # Avoid circular import

profile, _ = UserProfile.objects.get_or_create(user=user)
profile = UserProfile.objects.get(user=user)
if profile.restricted_only:
return False

Expand Down
2 changes: 1 addition & 1 deletion opal/core/patient_lists.py
Expand Up @@ -141,7 +141,7 @@ def list(klass):
def visible_to(klass, user):
from opal.models import UserProfile # Avoid circular import

profile, _ = UserProfile.objects.get_or_create(user=user)
profile = UserProfile.objects.get(user=user)
if profile.restricted_only:
return False

Expand Down
6 changes: 4 additions & 2 deletions opal/core/test.py
Expand Up @@ -34,10 +34,12 @@ def user(self):
is_staff=True,
is_superuser=True
)
profile, _ = UserProfile.objects.get_or_create(
profile = UserProfile.objects.get(
user=user,
can_extract=True
)
if not profile.can_extract:
profile.can_extract = True
profile.save()
return user

def post_json(self, path, data):
Expand Down
9 changes: 9 additions & 0 deletions opal/models.py
Expand Up @@ -19,6 +19,7 @@
from django.urls import reverse
from django.core.exceptions import FieldDoesNotExist
from django.utils.encoding import force_str
from django.db.models.signals import post_save

from opal.core import (
application, exceptions, lookuplists, plugins, patient_lists, tagging
Expand Down Expand Up @@ -1656,6 +1657,14 @@ def explicit_access_only(self):
return any(r for r in all_roles if r == "scientist")


def save_profile(sender, instance, **kwargs):
if not UserProfile.objects.filter(user=instance).exists():
UserProfile.objects.create(user=instance)


post_save.connect(save_profile, sender=User)


class InpatientAdmission(PatientSubrecord, ExternallySourcedModel):
_icon = 'fa fa-map-marker'
_sort = "-admitted"
Expand Down
2 changes: 0 additions & 2 deletions opal/tests/test_api.py
Expand Up @@ -576,7 +576,6 @@ class UserProfileTestCase(TestCase):

def setUp(self):
self.user = User.objects.create(username='testuser')
models.UserProfile.objects.create(user=self.user)
self.mock_request = MagicMock(name='request')
self.mock_request.user = self.user

Expand Down Expand Up @@ -605,7 +604,6 @@ class UserTestCase(TestCase):

def setUp(self):
self.user = User.objects.create(username='testuser')
models.UserProfile.objects.create(user=self.user)
self.mock_request = MagicMock(name='request')
self.mock_request.user = self.user

Expand Down
6 changes: 4 additions & 2 deletions opal/tests/test_core_episodes.py
Expand Up @@ -14,8 +14,10 @@ class EpisodeCategoryTestCase(test.OpalTestCase):

def setUp(self):
self.restricted_user = User.objects.create(username='restrictedonly')
self.profile, _ = UserProfile.objects.get_or_create(
user=self.restricted_user, restricted_only=True
UserProfile.objects.filter(
user=self.restricted_user
).update(
restricted_only=True
)
self.patient = Patient.objects.create()
self.inpatient_episode = self.patient.create_episode(
Expand Down
9 changes: 7 additions & 2 deletions opal/tests/test_episode.py
Expand Up @@ -230,14 +230,19 @@ def setUp(self):

def test_episode_visible_false(self):
user = User.objects.create()
UserProfile.objects.create(user=user, restricted_only=True)
UserProfile.objects.filter(user=user).update(
restricted_only=True
)
self.assertFalse(
self.episode.category.episode_visible_to(self.episode, user)
)

def test_episode_visible_true(self):
user = User.objects.create()
UserProfile.objects.create(user=user, restricted_only=False)
UserProfile.objects.filter()
UserProfile.objects.filter(user=user).update(
restricted_only=False
)
self.assertTrue(
self.episode.category.episode_visible_to(self.episode, user)
)
Expand Down
7 changes: 7 additions & 0 deletions opal/tests/test_models.py
Expand Up @@ -7,6 +7,7 @@

from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import User

from opal import models
from opal.core import application, exceptions, subrecords
Expand Down Expand Up @@ -854,3 +855,9 @@ class RoleTestCase(OpalTestCase):
def test_str(self):
r = models.Role(name='Doctor')
self.assertEqual('Doctor', r.__str__())


class UserProfileTestCase(OpalTestCase):
def test_create(self):
user = User.objects.create()
self.assertTrue(bool(user.profile))
6 changes: 4 additions & 2 deletions opal/tests/test_patient_lists.py
Expand Up @@ -230,8 +230,10 @@ class TestPatientList(OpalTestCase):

def setUp(self):
self.restricted_user = User.objects.create(username='restrictedonly')
self.profile, _ = UserProfile.objects.get_or_create(
user=self.restricted_user, restricted_only=True
UserProfile.objects.filter(
user=self.restricted_user
).update(
restricted_only=True
)

def test_unimplemented_schema(self):
Expand Down
2 changes: 1 addition & 1 deletion opal/tests/test_user_profile.py
Expand Up @@ -18,7 +18,7 @@ def setUp(self):
first_name='Test', last_name='User'
)
self.user.save()
self.profile, _ = UserProfile.objects.get_or_create(user=self.user)
self.profile = self.user.profile

def test_to_dict_has_full_name(self):
as_dict = self.profile.to_dict()
Expand Down
14 changes: 2 additions & 12 deletions opal/views.py
Expand Up @@ -123,18 +123,8 @@ def check_password_reset(request, *args, **kwargs):
"""
response = login(request, *args, **kwargs)
if response.status_code == 302:
try:
profile = request.user.profile
if profile and profile.force_password_change:
return redirect(
reverse('change-password')
)
except models.UserProfile.DoesNotExist:
# TODO: This probably doesn't do any harm, but
# we should really never reach this. Creation
# of profiles shouldn't happen in a random view.
models.UserProfile.objects.create(
user=request.user, force_password_change=True)
profile = request.user.profile
if profile and profile.force_password_change:
return redirect(
reverse('change-password')
)
Expand Down

0 comments on commit 266863b

Please sign in to comment.