Browse Source

Project.num_checks_available() method.

pull/214/head
Pēteris Caune 6 years ago
parent
commit
82b644ae0a
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 27 additions and 7 deletions
  1. +7
    -0
      hc/accounts/models.py
  2. +16
    -0
      hc/accounts/tests/test_project_model.py
  3. +1
    -3
      hc/api/views.py
  4. +2
    -3
      hc/front/views.py
  5. +1
    -1
      hc/payments/views.py

+ 7
- 0
hc/accounts/models.py View File

@ -242,6 +242,13 @@ class Project(models.Model):
api_key_readonly = models.CharField(max_length=128, blank=True)
badge_key = models.CharField(max_length=150, unique=True)
def num_checks_available(self):
owner_profile = Profile.objects.for_user(self.owner)
from hc.api.models import Check
num_used = Check.objects.filter(project__owner=self.owner).count()
return owner_profile.check_limit - num_used
class Member(models.Model):
team = models.ForeignKey(Profile, models.CASCADE)


+ 16
- 0
hc/accounts/tests/test_project_model.py View File

@ -0,0 +1,16 @@
from hc.test import BaseTestCase
from hc.accounts.models import Project
from hc.api.models import Check
class ProjectModelTestCase(BaseTestCase):
def test_num_checks_available_handles_multiple_projects(self):
# One check in Alice's primary project:
Check.objects.create(user=self.alice, project=self.project)
# One check in Alice's secondary project:
p2 = Project.objects.create(owner=self.alice)
Check.objects.create(user=self.alice, project=p2)
self.assertEqual(self.project.num_checks_available(), 18)

+ 1
- 3
hc/api/views.py View File

@ -128,9 +128,7 @@ def create_check(request):
created = False
check = _lookup(request.project, request.json)
if check is None:
user = request.project.owner
num_checks = Check.objects.filter(project__owner=user).count()
if num_checks >= user.profile.check_limit:
if request.project.num_checks_available() <= 0:
return HttpResponseForbidden()
check = Check(user=request.project.owner, project=request.project)


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

@ -132,7 +132,7 @@ def my_checks(request):
"tags": pairs,
"ping_endpoint": settings.PING_ENDPOINT,
"timezones": pytz.all_timezones,
"num_available": request.team.check_limit - len(checks),
"num_available": request.project.num_checks_available(),
"sort": request.profile.sort,
"selected_tags": selected_tags,
"show_search": True,
@ -245,8 +245,7 @@ def docs_resources(request):
@require_POST
@login_required
def add_check(request):
num_checks = Check.objects.filter(user=request.team.user).count()
if num_checks >= request.team.check_limit:
if request.project.num_checks_available() <= 0:
return HttpResponseBadRequest()
check = Check(user=request.team.user, project=request.project)


+ 1
- 1
hc/payments/views.py View File

@ -58,7 +58,7 @@ def billing(request):
"page": "billing",
"profile": request.profile,
"sub": sub,
"num_checks": Check.objects.filter(user=request.user).count(),
"num_checks": Check.objects.filter(project__owner=request.user).count(),
"team_size": request.profile.member_set.count() + 1,
"team_max": request.profile.team_limit + 1,
"send_invoices_status": send_invoices_status,


Loading…
Cancel
Save