Merge lp:~newell-jensen/maas/fix-1573219 into lp:~maas-committers/maas/trunk

Proposed by Newell Jensen
Status: Merged
Approved by: Newell Jensen
Approved revision: no longer in the source branch.
Merged at revision: 4965
Proposed branch: lp:~newell-jensen/maas/fix-1573219
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 50 lines (+21/-1)
2 files modified
src/maasserver/models/tests/test_userprofile.py (+19/-1)
src/maasserver/models/userprofile.py (+2/-0)
To merge this branch: bzr merge lp:~newell-jensen/maas/fix-1573219
Reviewer Review Type Date Requested Status
Gavin Panella (community) Approve
Review via email: mp+293018@code.launchpad.net

Commit message

Delete User's FileStorage objects before deleting user. This will be backported to MAAS 1.9 as well.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/models/tests/test_userprofile.py'
2--- src/maasserver/models/tests/test_userprofile.py 2015-12-01 18:12:59 +0000
3+++ src/maasserver/models/tests/test_userprofile.py 2016-04-26 21:31:42 +0000
4@@ -7,7 +7,10 @@
5
6 from django.contrib.auth.models import User
7 from maasserver.exceptions import CannotDeleteUserException
8-from maasserver.models import UserProfile
9+from maasserver.models import (
10+ FileStorage,
11+ UserProfile,
12+)
13 from maasserver.models.user import (
14 GENERIC_CONSUMER,
15 get_auth_tokens,
16@@ -81,6 +84,21 @@
17 self.assertFalse(Consumer.objects.filter(id__in=consumer_ids).exists())
18 self.assertFalse(Token.objects.filter(id__in=token_ids).exists())
19
20+ def test_delete_deletes_related_filestorage_objects(self):
21+ # Deleting a profile deletes the related filestorage objects.
22+ profile = factory.make_User().userprofile
23+ profile_id = profile.id
24+ filestorage = factory.make_FileStorage(owner=profile.user)
25+ filestorage_id = filestorage.id
26+ self.assertTrue(FileStorage.objects.filter(id=filestorage_id).exists())
27+ self.assertTrue(
28+ UserProfile.objects.filter(id=profile_id).exists())
29+ profile.delete()
30+ self.assertFalse(
31+ FileStorage.objects.filter(id=filestorage_id).exists())
32+ self.assertFalse(
33+ UserProfile.objects.filter(id=profile_id).exists())
34+
35 def test_delete_attached_nodes(self):
36 # Cannot delete a user with nodes attached to it.
37 profile = factory.make_User().userprofile
38
39=== modified file 'src/maasserver/models/userprofile.py'
40--- src/maasserver/models/userprofile.py 2015-12-02 20:43:30 +0000
41+++ src/maasserver/models/userprofile.py 2016-04-26 21:31:42 +0000
42@@ -67,6 +67,8 @@
43 "allocated to this user." %
44 (self.user.username, nb_nodes))
45 raise CannotDeleteUserException(msg)
46+ if self.user.filestorage_set.exists():
47+ self.user.filestorage_set.all().delete()
48 self.user.consumers.all().delete()
49 self.user.delete()
50 super(UserProfile, self).delete()