Browse Source

Adding on_delete keywords, more to come.

pull/125/head
Pēteris Caune 8 years ago
parent
commit
207b0f9048
6 changed files with 20 additions and 25 deletions
  1. +2
    -2
      hc/accounts/models.py
  2. +5
    -5
      hc/accounts/tests/test_close_account.py
  3. +8
    -11
      hc/accounts/views.py
  4. +2
    -2
      hc/api/tests/test_check_model.py
  5. +1
    -1
      hc/front/views.py
  6. +2
    -4
      hc/test.py

+ 2
- 2
hc/accounts/models.py View File

@ -28,7 +28,7 @@ class ProfileManager(models.Manager):
class Profile(models.Model): class Profile(models.Model):
# Owner: # 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_name = models.CharField(max_length=200, blank=True)
team_access_allowed = models.BooleanField(default=False) team_access_allowed = models.BooleanField(default=False)
next_report_date = models.DateTimeField(null=True, blank=True) next_report_date = models.DateTimeField(null=True, blank=True)
@ -37,7 +37,7 @@ class Profile(models.Model):
check_limit = models.IntegerField(default=20) check_limit = models.IntegerField(default=20)
token = models.CharField(max_length=128, blank=True) token = models.CharField(max_length=128, blank=True)
api_key = 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() objects = ProfileManager()


+ 5
- 5
hc/accounts/tests/test_close_account.py View File

@ -1,9 +1,8 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from mock import patch
from hc.test import BaseTestCase
from hc.api.models import Check from hc.api.models import Check
from hc.payments.models import Subscription from hc.payments.models import Subscription
from hc.test import BaseTestCase
from mock import patch
class CloseAccountTestCase(BaseTestCase): class CloseAccountTestCase(BaseTestCase):
@ -25,9 +24,9 @@ class CloseAccountTestCase(BaseTestCase):
alices = User.objects.filter(username="alice") alices = User.objects.filter(username="alice")
self.assertFalse(alices.exists()) 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.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 # Check should be gone
self.assertFalse(Check.objects.exists()) self.assertFalse(Check.objects.exists())
@ -46,6 +45,7 @@ class CloseAccountTestCase(BaseTestCase):
# Alice should be still present # Alice should be still present
self.alice.refresh_from_db() self.alice.refresh_from_db()
self.profile.refresh_from_db() self.profile.refresh_from_db()
self.assertEqual(self.profile.current_team, None)
# Bob should be gone # Bob should be gone
bobs = User.objects.filter(username="bob") bobs = User.objects.filter(username="bob")


+ 8
- 11
hc/accounts/views.py View File

@ -150,10 +150,10 @@ def check_token(request, username, token):
@login_required @login_required
def profile(request): def profile(request):
profile = request.user.profile 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 request.team = profile
profile.current_team_id = profile.id
profile.current_team = profile
profile.save() profile.save()
show_api_key = False show_api_key = False
@ -245,10 +245,10 @@ def notifications(request):
@login_required @login_required
def badges(request): def badges(request):
profile = request.user.profile 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 request.team = profile
profile.current_team_id = profile.id
profile.current_team = profile
profile.save() profile.save()
tags = set() tags = set()
@ -352,12 +352,9 @@ def close(request):
if sub: if sub:
sub.cancel() 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() user.delete()
# Deleting user also deletes its profile, checks, channels etc.
request.session.flush() request.session.flush()
return redirect("hc-index") return redirect("hc-index")

+ 2
- 2
hc/api/tests/test_check_model.py View File

@ -11,10 +11,10 @@ class CheckModelTestCase(TestCase):
check = Check() check = Check()
check.tags = " foo bar " check.tags = " foo bar "
self.assertEquals(check.tags_list(), ["foo", "bar"])
self.assertEqual(check.tags_list(), ["foo", "bar"])
check.tags = " " check.tags = " "
self.assertEquals(check.tags_list(), [])
self.assertEqual(check.tags_list(), [])
def test_in_grace_period_handles_new_check(self): def test_in_grace_period_handles_new_check(self):
check = Check() check = Check()


+ 1
- 1
hc/front/views.py View File

@ -334,7 +334,7 @@ def channels(request):
return HttpResponseForbidden() return HttpResponseForbidden()
new_checks.append(check) new_checks.append(check)
channel.checks = new_checks
channel.checks.set(new_checks)
return redirect("hc-channels") return redirect("hc-channels")
channels = Channel.objects.filter(user=request.team.user) channels = Channel.objects.filter(user=request.team.user)


+ 2
- 4
hc/test.py View File

@ -27,13 +27,11 @@ class BaseTestCase(TestCase):
self.bobs_profile.current_team = self.profile self.bobs_profile.current_team = self.profile
self.bobs_profile.save() 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 # Charlie should have no access to Alice's stuff
self.charlie = User(username="charlie", email="[email protected]") self.charlie = User(username="charlie", email="[email protected]")
self.charlie.set_password("password") self.charlie.set_password("password")
self.charlie.save() self.charlie.save()
charlies_profile = Profile(user=self.charlie)
charlies_profile.save()
Profile.objects.create(user=self.charlie)

Loading…
Cancel
Save