Browse Source

TeamAccessMiddleware: create Profile object if it is missing. This solves problems for users created with "manage.py createsuperuser". Fixes #77

pull/84/head
Pēteris Caune 8 years ago
parent
commit
ee3f49c89b
2 changed files with 23 additions and 1 deletions
  1. +6
    -1
      hc/accounts/middleware.py
  2. +17
    -0
      hc/accounts/tests/test_team_access_middleware.py

+ 6
- 1
hc/accounts/middleware.py View File

@ -10,7 +10,12 @@ class TeamAccessMiddleware(object):
teams_q = teams_q.select_related("user") teams_q = teams_q.select_related("user")
request.teams = list(teams_q) request.teams = list(teams_q)
profile = request.user.profile
try:
profile = request.user.profile
except Profile.DoesNotExist:
profile = Profile(user=request.user)
profile.save()
if profile.current_team: if profile.current_team:
request.team = profile.current_team request.team = profile.current_team
else: else:


+ 17
- 0
hc/accounts/tests/test_team_access_middleware.py View File

@ -0,0 +1,17 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.accounts.models import Profile
class TeamAccessMiddlewareTestCase(TestCase):
def test_it_handles_missing_profile(self):
user = User(username="ned", email="[email protected]")
user.set_password("password")
user.save()
self.client.login(username="[email protected]", password="password")
r = self.client.get("/about/")
self.assertEqual(r.status_code, 200)
self.assertEqual(Profile.objects.count(), 1)

Loading…
Cancel
Save