Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v0.17.0' into dependabot/pip/fla…
Browse files Browse the repository at this point in the history
…ke8-3.7.8
  • Loading branch information
davidmiller committed Aug 9, 2019
2 parents aec2169 + ac4d19e commit ccc5629
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,4 +1,5 @@
language: python
dist: trusty
python:
- "3.5"
- "3.6"
Expand Down
23 changes: 18 additions & 5 deletions opal/admin.py
Expand Up @@ -27,10 +27,6 @@ class UserProfileInline(admin.StackedInline):
filter_horizontal = ('roles',)


class FilterInline(admin.StackedInline):
model = models.Filter


class UserProfileAdmin(UserAdmin):
add_fieldsets = (
(None, {
Expand All @@ -42,7 +38,7 @@ class UserProfileAdmin(UserAdmin):
)
}),
)
inlines = [UserProfileInline, FilterInline, ]
inlines = [UserProfileInline]

def get_actions(self, request):
actions = super(UserProfileAdmin, self).get_actions(request)
Expand All @@ -63,6 +59,23 @@ def has_delete_permission(self, request, obj=None):
return False
return True

def save_formset(self, request, form, formset, change):
"""
The user profile is already created by a signal so
attatch the existing user profile if after the user
has been saved.
"""
if change:
super().save_formset(request, form, formset, change)
else:
instances = formset.save(commit=False)
profile = UserProfile.objects.get(user_id=form.instance.id)
for i in instances:
i.id = profile.id
i.user = profile.user
i.save()
formset.save_m2m()


class MyAdmin(VersionAdmin):
pass
Expand Down
88 changes: 87 additions & 1 deletion opal/tests/test_admin.py
Expand Up @@ -3,10 +3,12 @@
"""
from django.contrib.contenttypes.models import ContentType
from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User
from django.urls import reverse

from opal.core.test import OpalTestCase
from opal.tests.models import Hat
from opal.models import Synonym, Patient, Episode
from opal.models import Synonym, Patient, Episode, Role

from opal.admin import (LookupListForm, PatientAdmin, EpisodeAdmin,
UserProfileAdmin)
Expand Down Expand Up @@ -105,6 +107,90 @@ def test_delete_permission_has_been_lazy_and_done_nothing(self):
has_perm = admin.has_delete_permission(request, obj=self.user)
self.assertTrue(has_perm)

def test_create(self):
role = Role.objects.create(name="can_doctor")
post_dict = {
# profile fields
'_save': ['Save'],
'profile-MIN_NUM_FORMS': ['0'],
'profile-TOTAL_FORMS': ['1'],
'profile-MAX_NUM_FORMS': ['1'],
'profile-__prefix__-id': [''],
'profile-0-id': [''],
'profile-0-roles': [role.id],
'profile-0-user': [''],
'profile-INITIAL_FORMS': ['0'],
'profile-__prefix__-force_password_change': ['on'],

# user fields
'username': ['test_user'],
'email': [''],
'password1': ['test1'],
'password2': ['test1'],
'first_name': [''],
'last_name': [''],
}
url = reverse('admin:auth_user_add')
self.assertTrue(
self.client.login(
username=self.user.username, password=self.PASSWORD
)
)
response = self.client.post(url, post_dict)
new_user = User.objects.get(username="test_user")
self.assertEqual(
list(new_user.profile.roles.values_list('name', flat=True)),
["can_doctor"]
)

def test_edit(self):
self.assertTrue(
self.client.login(
username=self.user.username, password=self.PASSWORD
)
)
new_user = User.objects.create(username="test_user")
new_user.set_password("test1")
new_user.save()
profile = new_user.profile
profile.force_password_change = False
profile.save()
role = Role.objects.create(name="can_doctor")
url = reverse('admin:auth_user_change', args=(new_user.pk,))
post_dict = {
'_save': ['Save'],
'first_name': [''],
'username': ['test_user'],
'last_name': [''],
'email': [''],
'last_login_0': [''],
'last_login_1': [''],
'date_joined_0': ['02/08/2019'],
'date_joined_1': ['14:21:00'],
'initial-date_joined_0': ['02/08/2019'],
'initial-date_joined_1': ['14:21:00'],
'is_active': ['on'],


'profile-TOTAL_FORMS': ['1'],
'profile-MAX_NUM_FORMS': ['1'],
'profile-INITIAL_FORMS': ['1'],
'profile-__prefix__-id': [''],
'profile-__prefix__-user': [new_user.pk],
'profile-0-force_password_change': ['on'],
'profile-0-id': [new_user.profile.id],
'profile-0-user': [new_user.id],
'profile-0-roles': [role.id],
'profile-__prefix__-force_password_change': ['on'],
'profile-MIN_NUM_FORMS': ['0'],
}
response = self.client.post(url, post_dict)
reloaded_user = User.objects.get(username="test_user")
self.assertEqual(
list(reloaded_user.profile.roles.values_list('name', flat=True)),
["can_doctor"]
)


class LookupListFormTestCase(OpalTestCase):

Expand Down

0 comments on commit ccc5629

Please sign in to comment.