forked from GithubBackups/healthchecks
Small tweaks to make sure we don't query accounts_profile multiple times on every request.
This commit is contained in:
parent
ef591b03ae
commit
367f5a595d
@ -6,7 +6,7 @@ class BasicBackend(object):
|
|||||||
|
|
||||||
def get_user(self, user_id):
|
def get_user(self, user_id):
|
||||||
try:
|
try:
|
||||||
return User.objects.get(pk=user_id)
|
return User.objects.select_related("profile").get(pk=user_id)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -6,15 +6,14 @@ class TeamAccessMiddleware(object):
|
|||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
||||||
def __call__(self, request):
|
def __call__(self, request):
|
||||||
if request.user.is_authenticated:
|
if not request.user.is_authenticated:
|
||||||
teams_q = Profile.objects.filter(member__user_id=request.user.id)
|
return self.get_response(request)
|
||||||
teams_q = teams_q.select_related("user")
|
|
||||||
request.teams = list(teams_q)
|
|
||||||
|
|
||||||
profile = Profile.objects.for_user(request.user)
|
teams_q = Profile.objects.filter(member__user_id=request.user.id)
|
||||||
if profile.current_team:
|
teams_q = teams_q.select_related("user")
|
||||||
request.team = profile.current_team
|
request.teams = list(teams_q)
|
||||||
else:
|
|
||||||
request.team = profile
|
request.profile = Profile.objects.for_user(request.user)
|
||||||
|
request.team = request.profile.team()
|
||||||
|
|
||||||
return self.get_response(request)
|
return self.get_response(request)
|
||||||
|
@ -20,8 +20,9 @@ def month(dt):
|
|||||||
|
|
||||||
class ProfileManager(models.Manager):
|
class ProfileManager(models.Manager):
|
||||||
def for_user(self, user):
|
def for_user(self, user):
|
||||||
profile = self.filter(user=user).first()
|
try:
|
||||||
if profile is None:
|
return user.profile
|
||||||
|
except Profile.DoesNotExist:
|
||||||
profile = Profile(user=user, team_access_allowed=user.is_superuser)
|
profile = Profile(user=user, team_access_allowed=user.is_superuser)
|
||||||
if not settings.USE_PAYMENTS:
|
if not settings.USE_PAYMENTS:
|
||||||
# If not using payments, set high limits
|
# If not using payments, set high limits
|
||||||
@ -29,7 +30,7 @@ class ProfileManager(models.Manager):
|
|||||||
profile.sms_limit = 500
|
profile.sms_limit = 500
|
||||||
|
|
||||||
profile.save()
|
profile.save()
|
||||||
return profile
|
return profile
|
||||||
|
|
||||||
|
|
||||||
class Profile(models.Model):
|
class Profile(models.Model):
|
||||||
@ -54,6 +55,13 @@ class Profile(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.team_name or self.user.email
|
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):
|
def prepare_token(self, salt):
|
||||||
token = str(uuid.uuid4())
|
token = str(uuid.uuid4())
|
||||||
self.token = make_password(token, salt)
|
self.token = make_password(token, salt)
|
||||||
|
@ -353,7 +353,8 @@ def channels(request):
|
|||||||
"enable_telegram": settings.TELEGRAM_TOKEN is not None,
|
"enable_telegram": settings.TELEGRAM_TOKEN is not None,
|
||||||
"enable_sms": settings.TWILIO_AUTH is not None,
|
"enable_sms": settings.TWILIO_AUTH is not None,
|
||||||
"enable_pd": settings.PD_VENDOR_KEY 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)
|
return render(request, "front/channels.html", ctx)
|
||||||
|
@ -5,8 +5,9 @@ def payments(request):
|
|||||||
|
|
||||||
show_pricing = settings.USE_PAYMENTS
|
show_pricing = settings.USE_PAYMENTS
|
||||||
if show_pricing and request.user.is_authenticated:
|
if show_pricing and request.user.is_authenticated:
|
||||||
profile = request.user.profile
|
if request.profile != request.team:
|
||||||
if profile.current_team_id and profile.current_team_id != profile.id:
|
# Hide "Pricing" tab when user is not working on their
|
||||||
|
# own team
|
||||||
show_pricing = False
|
show_pricing = False
|
||||||
|
|
||||||
return {'show_pricing': show_pricing}
|
return {'show_pricing': show_pricing}
|
||||||
|
@ -171,7 +171,7 @@
|
|||||||
<img src="{% static 'img/integrations/sms.png' %}"
|
<img src="{% static 'img/integrations/sms.png' %}"
|
||||||
class="icon" alt="SMS icon" />
|
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>
|
<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>
|
<a href="{% url 'hc-add-sms' %}" class="btn btn-primary">Add Integration</a>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user