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

This commit is contained in:
Pēteris Caune 2016-08-29 17:02:24 +03:00
parent c8bcb23fd6
commit ee3f49c89b
2 changed files with 23 additions and 1 deletions

View File

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

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="ned@example.org")
user.set_password("password")
user.save()
self.client.login(username="ned@example.org", password="password")
r = self.client.get("/about/")
self.assertEqual(r.status_code, 200)
self.assertEqual(Profile.objects.count(), 1)