From 207b0f9048cb7c7b36e86d3dae764ec2b9e01e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sun, 28 May 2017 13:38:38 +0300 Subject: [PATCH] Adding on_delete keywords, more to come. --- hc/accounts/models.py | 4 ++-- hc/accounts/tests/test_close_account.py | 10 +++++----- hc/accounts/views.py | 19 ++++++++----------- hc/api/tests/test_check_model.py | 4 ++-- hc/front/views.py | 2 +- hc/test.py | 6 ++---- 6 files changed, 20 insertions(+), 25 deletions(-) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index 912f5177..369fab24 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -28,7 +28,7 @@ class ProfileManager(models.Manager): class Profile(models.Model): # Owner: - user = models.OneToOneField(User, blank=True, null=True) + user = models.OneToOneField(User, models.CASCADE, blank=True, null=True) team_name = models.CharField(max_length=200, blank=True) team_access_allowed = models.BooleanField(default=False) next_report_date = models.DateTimeField(null=True, blank=True) @@ -37,7 +37,7 @@ class Profile(models.Model): check_limit = models.IntegerField(default=20) token = models.CharField(max_length=128, blank=True) api_key = models.CharField(max_length=128, blank=True) - current_team = models.ForeignKey("self", null=True) + current_team = models.ForeignKey("self", models.SET_NULL, null=True) objects = ProfileManager() diff --git a/hc/accounts/tests/test_close_account.py b/hc/accounts/tests/test_close_account.py index 13dcc3db..63921d7e 100644 --- a/hc/accounts/tests/test_close_account.py +++ b/hc/accounts/tests/test_close_account.py @@ -1,9 +1,8 @@ from django.contrib.auth.models import User -from mock import patch - -from hc.test import BaseTestCase from hc.api.models import Check from hc.payments.models import Subscription +from hc.test import BaseTestCase +from mock import patch class CloseAccountTestCase(BaseTestCase): @@ -25,9 +24,9 @@ class CloseAccountTestCase(BaseTestCase): alices = User.objects.filter(username="alice") self.assertFalse(alices.exists()) - # Bob's current team should be updated to self + # Bob's current team should now be None self.bobs_profile.refresh_from_db() - self.assertEqual(self.bobs_profile.current_team, self.bobs_profile) + self.assertIsNone(self.bobs_profile.current_team) # Check should be gone self.assertFalse(Check.objects.exists()) @@ -46,6 +45,7 @@ class CloseAccountTestCase(BaseTestCase): # Alice should be still present self.alice.refresh_from_db() self.profile.refresh_from_db() + self.assertEqual(self.profile.current_team, None) # Bob should be gone bobs = User.objects.filter(username="bob") diff --git a/hc/accounts/views.py b/hc/accounts/views.py index f0765e95..11d9d10b 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -150,10 +150,10 @@ def check_token(request, username, token): @login_required def profile(request): profile = request.user.profile - # Switch user back to its default team - if profile.current_team_id != profile.id: + # Switch user back to its own team + if request.team != profile: request.team = profile - profile.current_team_id = profile.id + profile.current_team = profile profile.save() show_api_key = False @@ -245,10 +245,10 @@ def notifications(request): @login_required def badges(request): profile = request.user.profile - # Switch user back to its default team - if profile.current_team_id != profile.id: + # Switch user back to its own team + if request.team != profile: request.team = profile - profile.current_team_id = profile.id + profile.current_team = profile profile.save() tags = set() @@ -352,12 +352,9 @@ def close(request): if sub: sub.cancel() - # Any users currently using this team need to switch to their own team: - for partner in Profile.objects.filter(current_team=user.profile): - partner.current_team = partner - partner.save() - user.delete() + # Deleting user also deletes its profile, checks, channels etc. + request.session.flush() return redirect("hc-index") diff --git a/hc/api/tests/test_check_model.py b/hc/api/tests/test_check_model.py index ad347558..d34b8f55 100644 --- a/hc/api/tests/test_check_model.py +++ b/hc/api/tests/test_check_model.py @@ -11,10 +11,10 @@ class CheckModelTestCase(TestCase): check = Check() check.tags = " foo bar " - self.assertEquals(check.tags_list(), ["foo", "bar"]) + self.assertEqual(check.tags_list(), ["foo", "bar"]) check.tags = " " - self.assertEquals(check.tags_list(), []) + self.assertEqual(check.tags_list(), []) def test_in_grace_period_handles_new_check(self): check = Check() diff --git a/hc/front/views.py b/hc/front/views.py index 5b42cfeb..e38ccf0c 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -334,7 +334,7 @@ def channels(request): return HttpResponseForbidden() new_checks.append(check) - channel.checks = new_checks + channel.checks.set(new_checks) return redirect("hc-channels") channels = Channel.objects.filter(user=request.team.user) diff --git a/hc/test.py b/hc/test.py index ac2fbf9d..a13d57dc 100644 --- a/hc/test.py +++ b/hc/test.py @@ -27,13 +27,11 @@ class BaseTestCase(TestCase): self.bobs_profile.current_team = self.profile self.bobs_profile.save() - m = Member(team=self.profile, user=self.bob) - m.save() + Member.objects.create(team=self.profile, user=self.bob) # Charlie should have no access to Alice's stuff self.charlie = User(username="charlie", email="charlie@example.org") self.charlie.set_password("password") self.charlie.save() - charlies_profile = Profile(user=self.charlie) - charlies_profile.save() + Profile.objects.create(user=self.charlie)