Skip to content

Commit

Permalink
Don't switch projects when viewing the "Account Settings" page
Browse files Browse the repository at this point in the history
  • Loading branch information
cuu508 committed Jan 22, 2019
1 parent b12eb1e commit eaf49f2
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 61 deletions.
9 changes: 1 addition & 8 deletions hc/accounts/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db.models import Q
from hc.accounts.models import Profile, Project
from hc.accounts.models import Profile


class TeamAccessMiddleware(object):
Expand All @@ -10,12 +9,6 @@ def __call__(self, request):
if not request.user.is_authenticated:
return self.get_response(request)

is_owner = Q(owner=request.user)
is_member = Q(member__user_id=request.user.id)
projects_q = Project.objects.filter(is_owner | is_member).distinct()
projects_q = projects_q.select_related("owner")
request.get_projects = lambda: list(projects_q)

profile = Profile.objects.for_user(request.user)
if profile.current_project is None:
profile.current_project = profile.get_own_project()
Expand Down
12 changes: 8 additions & 4 deletions hc/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ def send_change_email_link(self):
}
emails.change_email(self.user.email, ctx)

def checks_from_all_projects(self):
""" Return a queryset of checks from projects we have access to. """
def projects(self):
""" Return a queryset of all projects we have access to. """

is_owner = models.Q(owner=self.user)
is_member = models.Q(member__user=self.user)
q = Project.objects.filter(is_owner | is_member)
project_ids = q.values("id")
return Project.objects.filter(is_owner | is_member)

def checks_from_all_projects(self):
""" Return a queryset of checks from projects we have access to. """

project_ids = self.projects().values("id")

from hc.api.models import Check
return Check.objects.filter(project_id__in=project_ids)
Expand Down
11 changes: 0 additions & 11 deletions hc/accounts/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.conf import settings
from django.utils.timezone import now
from hc.test import BaseTestCase
from hc.accounts.models import Member
from hc.api.models import Check


Expand Down Expand Up @@ -93,16 +92,6 @@ def test_it_skips_nag_if_none_down(self):

self.assertEqual(len(mail.outbox), 0)

def test_it_switches_to_own_team(self):
self.client.login(username="[email protected]", password="password")

self.client.get("/accounts/profile/")

# After visiting the profile page, team should be switched back
# to user's default team.
self.bobs_profile.refresh_from_db()
self.assertEqual(self.bobs_profile.current_project, self.bobs_project)

def test_it_sends_change_email_link(self):
self.client.login(username="[email protected]", password="password")

Expand Down
24 changes: 1 addition & 23 deletions hc/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ def _make_user(email):
return user


def _ensure_own_team(request):
""" Make sure user is switched to their own team. """

if request.project.owner != request.user:
request.project = request.profile.get_own_project()

request.profile.current_project = request.project
request.profile.save()


def _redirect_after_login(request):
""" Redirect to the URL indicated in ?next= query parameter. """

Expand Down Expand Up @@ -188,14 +178,11 @@ def check_token(request, username, token):

@login_required
def profile(request):
_ensure_own_team(request)
profile = request.profile
project = profile.get_own_project()

ctx = {
"page": "profile",
"profile": profile,
"project": project
}

if request.method == "POST":
Expand All @@ -212,12 +199,10 @@ def profile(request):
@login_required
def project(request, code):
project = Project.objects.get(code=code, owner_id=request.user.id)
profile = project.owner_profile

ctx = {
"page": "profile",
"project": project,
"profile": profile,
"show_api_keys": False,
"project_name_status": "default",
"api_status": "default",
Expand Down Expand Up @@ -292,7 +277,6 @@ def project(request, code):

@login_required
def notifications(request):
_ensure_own_team(request)
profile = request.profile

ctx = {
Expand Down Expand Up @@ -328,14 +312,8 @@ def notifications(request):

@login_required
def badges(request):
_ensure_own_team(request)

projects = [request.project]
for membership in request.user.memberships.all():
projects.append(membership.project)

badge_sets = []
for project in projects:
for project in request.profile.projects():
tags = set()
for check in Check.objects.filter(project=project):
tags.update(check.tags_list())
Expand Down
4 changes: 1 addition & 3 deletions hc/front/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ def _has_access(request, project_code):
if request.user.is_superuser:
return True

is_owner = Q(owner_id=request.user.id)
is_member = Q(member__user_id=request.user.id)
projects = Project.objects.filter(is_owner | is_member)
projects = request.profile.projects()
return projects.filter(code=project_code).exists()


Expand Down
6 changes: 0 additions & 6 deletions hc/payments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ def pricing(request):

@login_required
def billing(request):
if request.project.owner != request.user:
request.project = request.profile.get_own_project()

request.profile.current_project = request.project
request.profile.save()

# Don't use Subscription.objects.for_user method here, so a
# subscription object is not created just by viewing a page.
sub = Subscription.objects.filter(user_id=request.user.id).first()
Expand Down
2 changes: 1 addition & 1 deletion templates/accounts/badges.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% block content %}
<div class="row">
<div class="col-sm-12">
<h1 class="settings-title">Settings</h1>
<h1 class="settings-title">Settings <small>{{ request.user.email }}</small></h1>
</div>
</div>

Expand Down
5 changes: 4 additions & 1 deletion templates/accounts/billing.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
{% block content %}
<div class="row">
<div class="col-sm-12">
<h1 class="settings-title">Settings</h1>
<h1 class="settings-title">
Settings
<small>{{ request.user.email}}</small>
</h1>
</div>
</div>

Expand Down
5 changes: 4 additions & 1 deletion templates/accounts/notifications.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
{% block content %}
<div class="row">
<div class="col-sm-12">
<h1 class="settings-title">Settings</h1>
<h1 class="settings-title">
Settings
<small>{{ request.user.email }}</small>
</h1>
</div>
</div>

Expand Down
4 changes: 1 addition & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
{% with projects=request.get_projects %}
{% for project in projects %}
{% for project in request.profile.projects.all %}
<li class="dropdown-header">{{ project }}</li>
<li>
<a href="{% url 'hc-switch-project' project.code %}">Checks</a>
Expand All @@ -136,7 +135,6 @@
{% endif %}
<li role="separator" class="divider"></li>
{% endfor %}
{% endwith %}

<li><a href="{% url 'hc-profile' %}">Account Settings</a></li>
<li><a href="{% url 'hc-logout' %}">Log Out</a></li>
Expand Down

0 comments on commit eaf49f2

Please sign in to comment.