From 79b7327c9ea3bbd9f41ecabdf388740998860ac3 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Mon, 4 Jan 2016 23:38:50 -0500 Subject: [PATCH] fix tests and optimize authentication and fix some unclosed markup --- hc/accounts/backends.py | 10 ++++------ hc/api/management/commands/sendalerts.py | 2 +- hc/front/tests/test_add_channel.py | 1 + hc/front/tests/test_add_check.py | 1 + hc/front/tests/test_channel_checks.py | 1 + hc/front/tests/test_log.py | 1 + hc/front/tests/test_my_checks.py | 1 + hc/front/tests/test_remove_channel.py | 1 + hc/front/tests/test_remove_check.py | 1 + hc/front/tests/test_update_channel.py | 1 + hc/front/tests/test_update_name.py | 1 + hc/front/tests/test_update_timeout.py | 1 + hc/front/tests/test_verify_email.py | 1 + hc/front/views.py | 4 ++-- hc/payments/tests/test_billing.py | 1 + hc/payments/tests/test_cancel_plan.py | 1 + hc/payments/tests/test_create_plan.py | 1 + hc/payments/tests/test_get_client_token.py | 1 + hc/payments/tests/test_invoice.py | 1 + hc/payments/tests/test_pricing.py | 1 + templates/front/channel_checks.html | 2 +- templates/front/channels.html | 6 +++--- templates/front/my_checks.html | 4 ++-- templates/payments/pricing.html | 2 +- 24 files changed, 31 insertions(+), 16 deletions(-) diff --git a/hc/accounts/backends.py b/hc/accounts/backends.py index f18ca975..d21780b1 100644 --- a/hc/accounts/backends.py +++ b/hc/accounts/backends.py @@ -3,7 +3,7 @@ from django.contrib.auth.models import User from hc.accounts.models import Profile -class BasicBackend: +class BasicBackend(object): def get_user(self, user_id): try: @@ -17,7 +17,8 @@ class ProfileBackend(BasicBackend): def authenticate(self, username=None, token=None): try: - profile = Profile.objects.get(user__username=username) + profile = (Profile.objects + .select_related("user").get(user__username=username)) except Profile.DoesNotExist: return None @@ -27,10 +28,7 @@ class ProfileBackend(BasicBackend): return profile.user def get_user(self, user_id): - try: - return User.objects.get(pk=user_id) - except User.DoesNotExist: - return None + return User.objects.filter(pk=user_id).first() class EmailBackend(BasicBackend): diff --git a/hc/api/management/commands/sendalerts.py b/hc/api/management/commands/sendalerts.py index 333d1b3e..68666a8b 100644 --- a/hc/api/management/commands/sendalerts.py +++ b/hc/api/management/commands/sendalerts.py @@ -16,7 +16,7 @@ class Command(BaseCommand): def handle_many(self): """ Send alerts for many checks simultaneously. """ - query = Check.objects.filter(user__isnull=False) + query = Check.objects.filter(user__isnull=False).select_related("user") now = timezone.now() going_down = query.filter(alert_after__lt=now, status="up") diff --git a/hc/front/tests/test_add_channel.py b/hc/front/tests/test_add_channel.py index 50b2a201..dd381e4e 100644 --- a/hc/front/tests/test_add_channel.py +++ b/hc/front/tests/test_add_channel.py @@ -7,6 +7,7 @@ from hc.api.models import Channel class AddChannelTestCase(TestCase): def setUp(self): + super(AddChannelTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_add_check.py b/hc/front/tests/test_add_check.py index d4f6d06f..343a3bfb 100644 --- a/hc/front/tests/test_add_check.py +++ b/hc/front/tests/test_add_check.py @@ -6,6 +6,7 @@ from hc.api.models import Check class AddCheckTestCase(TestCase): def setUp(self): + super(AddCheckTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_channel_checks.py b/hc/front/tests/test_channel_checks.py index 7ab2ac8f..05730d6e 100644 --- a/hc/front/tests/test_channel_checks.py +++ b/hc/front/tests/test_channel_checks.py @@ -6,6 +6,7 @@ from hc.api.models import Channel class ChannelChecksTestCase(TestCase): def setUp(self): + super(ChannelChecksTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_log.py b/hc/front/tests/test_log.py index 0fb59933..49952762 100644 --- a/hc/front/tests/test_log.py +++ b/hc/front/tests/test_log.py @@ -6,6 +6,7 @@ from hc.api.models import Check, Ping class LogTestCase(TestCase): def setUp(self): + super(LogTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_my_checks.py b/hc/front/tests/test_my_checks.py index 37a28f00..36a91f31 100644 --- a/hc/front/tests/test_my_checks.py +++ b/hc/front/tests/test_my_checks.py @@ -6,6 +6,7 @@ from hc.api.models import Check class MyChecksTestCase(TestCase): def setUp(self): + super(MyChecksTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_remove_channel.py b/hc/front/tests/test_remove_channel.py index b1c25e41..22253d26 100644 --- a/hc/front/tests/test_remove_channel.py +++ b/hc/front/tests/test_remove_channel.py @@ -6,6 +6,7 @@ from hc.api.models import Channel class RemoveChannelTestCase(TestCase): def setUp(self): + super(RemoveChannelTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_remove_check.py b/hc/front/tests/test_remove_check.py index 9f5cba3f..41d165c4 100644 --- a/hc/front/tests/test_remove_check.py +++ b/hc/front/tests/test_remove_check.py @@ -6,6 +6,7 @@ from hc.api.models import Check class RemoveCheckTestCase(TestCase): def setUp(self): + super(RemoveCheckTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_update_channel.py b/hc/front/tests/test_update_channel.py index b3ecc205..3e35d90b 100644 --- a/hc/front/tests/test_update_channel.py +++ b/hc/front/tests/test_update_channel.py @@ -6,6 +6,7 @@ from hc.api.models import Channel, Check class UpdateChannelTestCase(TestCase): def setUp(self): + super(UpdateChannelTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_update_name.py b/hc/front/tests/test_update_name.py index 520cc8a0..bbf37865 100644 --- a/hc/front/tests/test_update_name.py +++ b/hc/front/tests/test_update_name.py @@ -6,6 +6,7 @@ from hc.api.models import Check class UpdateNameTestCase(TestCase): def setUp(self): + super(UpdateNameTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_update_timeout.py b/hc/front/tests/test_update_timeout.py index 1ee55ddc..0b06cdd8 100644 --- a/hc/front/tests/test_update_timeout.py +++ b/hc/front/tests/test_update_timeout.py @@ -6,6 +6,7 @@ from hc.api.models import Check class UpdateTimeoutTestCase(TestCase): def setUp(self): + super(UpdateTimeoutTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/tests/test_verify_email.py b/hc/front/tests/test_verify_email.py index b38797ab..0bfb6069 100644 --- a/hc/front/tests/test_verify_email.py +++ b/hc/front/tests/test_verify_email.py @@ -6,6 +6,7 @@ from hc.api.models import Channel class VerifyEmailTestCase(TestCase): def setUp(self): + super(VerifyEmailTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/front/views.py b/hc/front/views.py index 322868ee..44c3efa4 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -30,7 +30,7 @@ def my_checks(request): counter = Counter() down_tags, grace_tags = set(), set() - for check in checks: + for check in checks.iterator(): status = check.get_status() for tag in check.tags_list(): if tag == "": @@ -196,7 +196,7 @@ def log(request, code): limit = profile.ping_log_limit pings = Ping.objects.filter(owner=check).order_by("-id")[:limit] - pings = list(pings) + pings = list(pings.iterator()) # oldest-to-newest order will be more convenient for adding # "not received" placeholders: pings.reverse() diff --git a/hc/payments/tests/test_billing.py b/hc/payments/tests/test_billing.py index f2dcf43a..ce2aba13 100644 --- a/hc/payments/tests/test_billing.py +++ b/hc/payments/tests/test_billing.py @@ -7,6 +7,7 @@ from mock import Mock, patch class BillingTestCase(TestCase): def setUp(self): + super(BillingTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/payments/tests/test_cancel_plan.py b/hc/payments/tests/test_cancel_plan.py index 40ba3fd0..adac22f8 100644 --- a/hc/payments/tests/test_cancel_plan.py +++ b/hc/payments/tests/test_cancel_plan.py @@ -7,6 +7,7 @@ from mock import patch class CancelPlanTestCase(TestCase): def setUp(self): + super(CancelPlanTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/payments/tests/test_create_plan.py b/hc/payments/tests/test_create_plan.py index a476b167..1139fc1f 100644 --- a/hc/payments/tests/test_create_plan.py +++ b/hc/payments/tests/test_create_plan.py @@ -8,6 +8,7 @@ from mock import patch class CreatePlanTestCase(TestCase): def setUp(self): + super(CreatePlanTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/payments/tests/test_get_client_token.py b/hc/payments/tests/test_get_client_token.py index 01ba3b62..d79aad1d 100644 --- a/hc/payments/tests/test_get_client_token.py +++ b/hc/payments/tests/test_get_client_token.py @@ -7,6 +7,7 @@ from mock import patch class GetClientTokenTestCase(TestCase): def setUp(self): + super(GetClientTokenTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/payments/tests/test_invoice.py b/hc/payments/tests/test_invoice.py index 8ae735cd..c0737a49 100644 --- a/hc/payments/tests/test_invoice.py +++ b/hc/payments/tests/test_invoice.py @@ -7,6 +7,7 @@ from mock import Mock, patch class InvoiceTestCase(TestCase): def setUp(self): + super(InvoiceTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/hc/payments/tests/test_pricing.py b/hc/payments/tests/test_pricing.py index 1ae04855..c617740e 100644 --- a/hc/payments/tests/test_pricing.py +++ b/hc/payments/tests/test_pricing.py @@ -6,6 +6,7 @@ from hc.payments.models import Subscription class PricingTestCase(TestCase): def setUp(self): + super(PricingTestCase, self).setUp() self.alice = User(username="alice", email="alice@example.org") self.alice.set_password("password") self.alice.save() diff --git a/templates/front/channel_checks.html b/templates/front/channel_checks.html index 005e8925..48127f2d 100644 --- a/templates/front/channel_checks.html +++ b/templates/front/channel_checks.html @@ -4,7 +4,7 @@ {% csrf_token %} diff --git a/templates/front/channels.html b/templates/front/channels.html index 59e3fb80..10f4bd25 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -149,8 +149,8 @@ {% csrf_token %}