Adding on_delete keywords, more to come.

This commit is contained in:
Pēteris Caune 2017-05-28 13:38:38 +03:00
parent 49eb8b6b23
commit 207b0f9048
6 changed files with 20 additions and 25 deletions

View File

@ -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()

View File

@ -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")

View File

@ -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")

View File

@ -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()

View File

@ -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)

View File

@ -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)