Browse Source

Small tweaks to make sure we don't query accounts_profile multiple times on every request.

pull/133/head
Pēteris Caune 7 years ago
parent
commit
367f5a595d
6 changed files with 26 additions and 17 deletions
  1. +1
    -1
      hc/accounts/backends.py
  2. +8
    -9
      hc/accounts/middleware.py
  3. +11
    -3
      hc/accounts/models.py
  4. +2
    -1
      hc/front/views.py
  5. +3
    -2
      hc/payments/context_processors.py
  6. +1
    -1
      templates/front/channels.html

+ 1
- 1
hc/accounts/backends.py View File

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


+ 8
- 9
hc/accounts/middleware.py View File

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

+ 11
- 3
hc/accounts/models.py View File

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


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

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


+ 3
- 2
hc/payments/context_processors.py View File

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

+ 1
- 1
templates/front/channels.html View File

@ -171,7 +171,7 @@
<img src="{% static 'img/integrations/sms.png' %}"
class="icon" alt="SMS icon" />
<h2>SMS {% if show_pricing %}<small>(paid plans)</small>{% endif %}</h2>
<h2>SMS {% if use_payments %}<small>(paid plans)</small>{% endif %}</h2>
<p>Get a text message to your phone when a check goes down.</p>
<a href="{% url 'hc-add-sms' %}" class="btn btn-primary">Add Integration</a>


Loading…
Cancel
Save