diff --git a/hc/accounts/backends.py b/hc/accounts/backends.py index 2a3381f5..5485144e 100644 --- a/hc/accounts/backends.py +++ b/hc/accounts/backends.py @@ -6,7 +6,7 @@ class BasicBackend(object): def get_user(self, user_id): try: - return User.objects.get(pk=user_id) + return User.objects.select_related("profile").get(pk=user_id) except User.DoesNotExist: return None diff --git a/hc/accounts/middleware.py b/hc/accounts/middleware.py index 23570c05..857f6871 100644 --- a/hc/accounts/middleware.py +++ b/hc/accounts/middleware.py @@ -6,15 +6,14 @@ class TeamAccessMiddleware(object): self.get_response = get_response def __call__(self, request): - if request.user.is_authenticated: - teams_q = Profile.objects.filter(member__user_id=request.user.id) - teams_q = teams_q.select_related("user") - request.teams = list(teams_q) + if not request.user.is_authenticated: + return self.get_response(request) - profile = Profile.objects.for_user(request.user) - if profile.current_team: - request.team = profile.current_team - else: - request.team = profile + teams_q = Profile.objects.filter(member__user_id=request.user.id) + teams_q = teams_q.select_related("user") + request.teams = list(teams_q) + + request.profile = Profile.objects.for_user(request.user) + request.team = request.profile.team() return self.get_response(request) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index 56810e89..2a711153 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -20,8 +20,9 @@ def month(dt): class ProfileManager(models.Manager): def for_user(self, user): - profile = self.filter(user=user).first() - if profile is None: + try: + return user.profile + except Profile.DoesNotExist: profile = Profile(user=user, team_access_allowed=user.is_superuser) if not settings.USE_PAYMENTS: # If not using payments, set high limits @@ -29,7 +30,7 @@ class ProfileManager(models.Manager): profile.sms_limit = 500 profile.save() - return profile + return profile class Profile(models.Model): @@ -54,6 +55,13 @@ class Profile(models.Model): def __str__(self): return self.team_name or self.user.email + def team(self): + # compare ids to avoid SQL queries + if self.current_team_id and self.current_team_id != self.id: + return self.current_team + + return self + def prepare_token(self, salt): token = str(uuid.uuid4()) self.token = make_password(token, salt) diff --git a/hc/front/views.py b/hc/front/views.py index 5b7755c7..0fe65462 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -353,7 +353,8 @@ def channels(request): "enable_telegram": settings.TELEGRAM_TOKEN is not None, "enable_sms": settings.TWILIO_AUTH is not None, "enable_pd": settings.PD_VENDOR_KEY is not None, - "added": request.GET.get("added") + "added": request.GET.get("added"), + "use_payments": settings.USE_PAYMENTS } return render(request, "front/channels.html", ctx) diff --git a/hc/payments/context_processors.py b/hc/payments/context_processors.py index fb917081..6580e41b 100644 --- a/hc/payments/context_processors.py +++ b/hc/payments/context_processors.py @@ -5,8 +5,9 @@ def payments(request): show_pricing = settings.USE_PAYMENTS if show_pricing and request.user.is_authenticated: - profile = request.user.profile - if profile.current_team_id and profile.current_team_id != profile.id: + if request.profile != request.team: + # Hide "Pricing" tab when user is not working on their + # own team show_pricing = False return {'show_pricing': show_pricing} diff --git a/templates/front/channels.html b/templates/front/channels.html index 598e1709..0c25d193 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -171,7 +171,7 @@ SMS icon -

SMS {% if show_pricing %}(paid plans){% endif %}

+

SMS {% if use_payments %}(paid plans){% endif %}

Get a text message to your phone when a check goes down.

Add Integration