Browse Source

Reduce usage of Profile.current_project cc: #336

pull/340/head
Pēteris Caune 5 years ago
parent
commit
157711bc95
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 14 additions and 35 deletions
  1. +1
    -10
      hc/accounts/tests/test_add_project.py
  2. +2
    -6
      hc/front/tests/test_my_checks.py
  3. +2
    -9
      hc/front/tests/test_transfer.py
  4. +8
    -9
      hc/front/views.py
  5. +1
    -1
      templates/front/projects.html

+ 1
- 10
hc/accounts/tests/test_add_project.py View File

@ -2,12 +2,7 @@ from hc.accounts.models import Project
from hc.test import BaseTestCase
class RemoveProjectTestCase(BaseTestCase):
def setUp(self):
super(RemoveProjectTestCase, self).setUp()
self.url = "/projects/%s/remove/" % self.project.code
class AddProjectTestCase(BaseTestCase):
def test_it_works(self):
self.client.login(username="[email protected]", password="password")
r = self.client.post("/projects/add/", {"name": "My Second Project"})
@ -16,10 +11,6 @@ class RemoveProjectTestCase(BaseTestCase):
self.assertRedirects(r, "/projects/%s/checks/" % p.code)
self.assertEqual(str(p.code), p.badge_key)
# Alice's current project should be the just created one
self.profile.refresh_from_db()
self.assertEqual(self.profile.current_project, p)
def test_it_rejects_get(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get("/projects/add/")


+ 2
- 6
hc/front/tests/test_my_checks.py View File

@ -34,16 +34,12 @@ class MyChecksTestCase(BaseTestCase):
delta = timezone.now() - self.profile.last_active_date
self.assertTrue(delta.total_seconds() < 1)
def test_it_updates_current_project(self):
self.profile.current_project = None
self.profile.save()
def test_it_updates_session(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.profile.refresh_from_db()
self.assertEqual(self.profile.current_project, self.project)
self.assertEqual(self.client.session["last_project_id"], self.project.id)
def test_it_checks_access(self):
self.client.login(username="[email protected]", password="password")


+ 2
- 9
hc/front/tests/test_transfer.py View File

@ -2,9 +2,9 @@ from hc.api.models import Channel, Check
from hc.test import BaseTestCase
class TrabsferTestCase(BaseTestCase):
class TransferTestCase(BaseTestCase):
def setUp(self):
super(TrabsferTestCase, self).setUp()
super(TransferTestCase, self).setUp()
self.check = Check.objects.create(project=self.bobs_project)
self.url = "/checks/%s/transfer/" % self.check.code
@ -15,9 +15,6 @@ class TrabsferTestCase(BaseTestCase):
self.assertContains(r, "Transfer to Another Project")
def test_it_works(self):
self.bobs_profile.current_project = self.bobs_project
self.bobs_profile.save()
self.client.login(username="[email protected]", password="password")
payload = {"project": self.project.code}
r = self.client.post(self.url, payload, follow=True)
@ -27,10 +24,6 @@ class TrabsferTestCase(BaseTestCase):
check = Check.objects.get()
self.assertEqual(check.project, self.project)
# Bob's current project should have been updated
self.bobs_profile.refresh_from_db()
self.assertEqual(self.bobs_profile.current_project, self.project)
def test_it_obeys_check_limit(self):
# Alice's projects cannot accept checks due to limits:
self.profile.check_limit = 0


+ 8
- 9
hc/front/views.py View File

@ -154,9 +154,8 @@ def my_checks(request, code):
request.profile.sort = request.GET["sort"]
request.profile.save()
if request.profile.current_project_id != project.id:
request.profile.current_project = project
request.profile.save()
if request.session.get("last_project_id") != project.id:
request.session["last_project_id"] = project.id
q = Check.objects.filter(project=project)
checks = list(q.prefetch_related("channel_set"))
@ -257,7 +256,12 @@ def index(request):
if request.user.is_authenticated:
projects = list(request.profile.projects())
ctx = {"page": "projects", "projects": projects}
ctx = {
"page": "projects",
"projects": projects,
"last_project_id": request.session.get("last_project_id"),
}
return render(request, "front/projects.html", ctx)
check = Check()
@ -555,14 +559,9 @@ def transfer(request, code):
check.project = target_project
check.save()
check.assign_all_channels()
request.profile.current_project = target_project
request.profile.save()
messages.success(request, "Check transferred successfully!")
return redirect("hc-details", code)
ctx = {"check": check}


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

@ -15,7 +15,7 @@
{% for project in projects %}
<a href="{% url 'hc-checks' project.code %}">
<div class="col-sm-6 col-md-4">
<div class="panel project {% if project == request.profile.current_project %}selected{% endif %}">
<div class="panel project {% if project.id == last_project_id %}selected{% endif %}">
<div class="status icon-{{ project.overall_status }}"></div>
<h4>{{ project }}</h4>


Loading…
Cancel
Save